Created
August 15, 2013 08:57
-
-
Save autotaker/6239379 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE MultiParamTypeClasses,FlexibleInstances,FunctionalDependencies ,UndecidableInstances#-} | |
data Pair a b = Pair a b | |
data Zero = Zero | |
data Succ a = Succ a | |
class Tuple t n c | t n -> c where | |
nth :: t -> n -> c | |
instance Tuple (Pair a b) Zero a where | |
nth (Pair a _) _ = a | |
instance Tuple t n c => Tuple (Pair a t) (Succ n) c where | |
nth (Pair _ b) (Succ n) = nth b n | |
_0 = Zero | |
_1 = Succ _0 | |
_2 = Succ _1 | |
_3 = Succ _2 | |
_4 = Succ _3 | |
_5 = Succ _4 | |
_6 = Succ _5 | |
_7 = Succ _6 | |
_8 = Succ _7 | |
_9 = Succ _8 | |
class TupleLike t p | t -> p where | |
fromTuple :: t -> p | |
toTuple :: p -> t | |
instance TupleLike (a,b) (Pair a (Pair b ())) where | |
fromTuple (a,b) = Pair a (Pair b ()) | |
toTuple (Pair a (Pair b ())) = (a,b) | |
instance TupleLike (a,b,c) (Pair a (Pair b (Pair c ()))) where | |
fromTuple (a,b,c) = Pair a (Pair b (Pair c ())) | |
toTuple (Pair a (Pair b (Pair c ()))) = (a,b,c) | |
instance TupleLike (a,b,c,d) (Pair a (Pair b (Pair c (Pair d ())))) where | |
fromTuple (a,b,c,d) = Pair a (Pair b (Pair c (Pair d ()))) | |
toTuple (Pair a (Pair b (Pair c (Pair d ())))) = (a,b,c,d) | |
main = do | |
print $ fromTuple (0,"hoge") `nth` _0 | |
print $ fromTuple (0,"hoge") `nth` _1 | |
--print $ fromTuple (0,"hoge") `nth` _2 -- type error | |
print $ fromTuple (0,"hoge",True) `nth` _1 | |
print $ fromTuple (0,"hoge",True) `nth` _2 | |
print $ fromTuple (0,"hoge",True,"piyo") `nth` _3 | |
--print $ fromTuple (0,"hoge",True,"piyo") `nth` _4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment