Skip to content

Instantly share code, notes, and snippets.

@sinelaw sinelaw/tf.hs
Created Feb 19, 2015

What would you like to do?
{-# OPTIONS -Wall #-}
{-# LANGUAGE TypeFamilies, FlexibleInstances #-}
import Data.Map
class Indexable a where
type Index a
type Result a
index :: a -> Index a -> Result a
instance Indexable [a] where
type Index [a] = Int
type Result [a] = a
index = (!!)
newtype Str = Str { getStr :: String }
instance Indexable Str where
type Index Str = Int
type Result Str = Char
index = (!!) . getStr
instance Indexable (Map String a) where
type Index (Map String a) = String
type Result (Map String a) = a
index = (!)
f :: (Indexable a, Index a ~ Int) => a -> Result a
f x = index x (0::Int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.