Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sinelaw
Created February 19, 2015 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinelaw/a4a035a5bb6b0d6d6c1c to your computer and use it in GitHub Desktop.
Save sinelaw/a4a035a5bb6b0d6d6c1c to your computer and use it in GitHub Desktop.
{-# 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