Skip to content

Commit f02bc34

Browse files
committed
Define heterogeneous apply using type families
1 parent 9e2f990 commit f02bc34

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

examples.hs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE NamedFieldPuns, RankNTypes, ExistentialQuantification, MultiParamTypeClasses, FlexibleInstances, TypeFamilies #-}
1+
{-# LANGUAGE GADTs, StandaloneDeriving, FlexibleContexts, NamedFieldPuns, RankNTypes, ExistentialQuantification, MultiParamTypeClasses, FlexibleInstances, TypeFamilies #-}
22

33
module Examples where
44

@@ -54,3 +54,26 @@ applyTupPairMethod (next, cont) f (arg, rest) = next cont (f arg) rest
5454
testMethod = (applyTupPairMethod, (applyTupPairMethod, (applyTupPairMethod, (applyTupUnitMethod, ()))))
5555
testArgs = (True, ((), (4, ())))
5656
testApplyTupEx = applyTupEx testMethod (\x () y -> (x, y)) testArgs
57+
58+
-- gadt translation
59+
data LL r where
60+
LLNil :: LL ()
61+
LLCons :: a -> LL bs -> LL (a, bs)
62+
-- can these be derived?
63+
instance Show (LL ()) where
64+
show LLNil = "LLNil"
65+
instance (Show a, Show (LL bs)) => Show (LL (a, bs)) where
66+
show (LLCons a bs) = "(LLCons " ++ show a ++ " " ++ show bs ++ ")"
67+
-- this does not work
68+
{-deriving instance Show (LL ())-}
69+
{-deriving instance (Show a, Show (LL b)) => Show (LL (a, b))-}
70+
71+
-- cleaner than the class-based version?
72+
type family ApplyFuncLL tup result where
73+
ApplyFuncLL () result = result
74+
ApplyFuncLL (a, b) result = a -> ApplyFuncLL b result
75+
applyLL :: ApplyFuncLL ab result -> LL ab -> result
76+
applyLL f LLNil = f
77+
applyLL f (LLCons a bs) = applyLL (f a) bs
78+
-- applyLL ((,) . (+ 1)) (LLCons 3 (LLCons 'c' LLNil))
79+
-- ===> (4, 'c')

0 commit comments

Comments
 (0)