|
1 |
| -{-# LANGUAGE NamedFieldPuns, RankNTypes, ExistentialQuantification, MultiParamTypeClasses, FlexibleInstances, TypeFamilies #-} |
| 1 | +{-# LANGUAGE GADTs, StandaloneDeriving, FlexibleContexts, NamedFieldPuns, RankNTypes, ExistentialQuantification, MultiParamTypeClasses, FlexibleInstances, TypeFamilies #-} |
2 | 2 |
|
3 | 3 | module Examples where
|
4 | 4 |
|
@@ -54,3 +54,26 @@ applyTupPairMethod (next, cont) f (arg, rest) = next cont (f arg) rest
|
54 | 54 | testMethod = (applyTupPairMethod, (applyTupPairMethod, (applyTupPairMethod, (applyTupUnitMethod, ()))))
|
55 | 55 | testArgs = (True, ((), (4, ())))
|
56 | 56 | 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