diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-02-25 12:35:13 +0100 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-02-25 12:35:24 +0100 |
commit | b99e2171f7a63da4261fdafeeb527d61e4a5f85a (patch) | |
tree | ec7228aaf8aa9ef5b8534aa57558e7035bd5c4e8 /testdata/DepPrelude.wip.lc | |
parent | fd7ef2e607ec31c225dd01cdd1d05a3122fa823c (diff) |
cleanup test cases
Diffstat (limited to 'testdata/DepPrelude.wip.lc')
-rw-r--r-- | testdata/DepPrelude.wip.lc | 568 |
1 files changed, 568 insertions, 0 deletions
diff --git a/testdata/DepPrelude.wip.lc b/testdata/DepPrelude.wip.lc new file mode 100644 index 00000000..facb4b7b --- /dev/null +++ b/testdata/DepPrelude.wip.lc | |||
@@ -0,0 +1,568 @@ | |||
1 | {-# LANGUAGE NoImplicitPrelude #-} | ||
2 | {-# LANGUAGE NoTypeNamespace #-} | ||
3 | {-# LANGUAGE NoConstructorNamespace #-} | ||
4 | -- contains the lambda-pi prelude (http://www.andres-loeh.de/LambdaPi/prelude.lp) adatpted to the lc compiler prototype | ||
5 | builtintycons | ||
6 | Int :: Type | ||
7 | |||
8 | data Unit = TT | ||
9 | |||
10 | builtins | ||
11 | cstr :: Type -> Type -> Type -- TODO | ||
12 | -- cstr :: forall (a :: Type) (b :: Type) -> a -> b -> Type | ||
13 | -- reflCstr :: forall (a :: Type) -> cstr a a | ||
14 | coe :: forall (a :: Type) (b :: Type) -> cstr a b -> a -> b | ||
15 | |||
16 | tyType (a :: Type) = a | ||
17 | id a = a | ||
18 | const x y = x | ||
19 | const' :: forall a b . a -> b -> a | ||
20 | const' x y = x | ||
21 | const'' :: forall b a . a -> b -> a | ||
22 | const'' x y = x | ||
23 | id' a = id a | ||
24 | id'' = id id | ||
25 | app f x = f x | ||
26 | comp f g x = f (g x) | ||
27 | app2 f x y = f x y | ||
28 | flip f x y = f y x | ||
29 | scomb a b c = a c (b c) | ||
30 | |||
31 | builtins fix :: forall a . (a -> a) -> a | ||
32 | builtins f_i_x :: forall a . (a -> a) -> a | ||
33 | |||
34 | data Sigma a (b :: a -> Type) = Pair (x :: a) (b x) | ||
35 | |||
36 | -- undefined = undefined | ||
37 | builtins | ||
38 | undefined :: forall (a :: Type) . a | ||
39 | |||
40 | data Empty | ||
41 | --data T2 a b = T2C a b | ||
42 | builtins | ||
43 | T2 :: Type -> Type -> Type | ||
44 | T2C :: Unit -> Unit -> Unit | ||
45 | |||
46 | -- identity function, used for type annotations internally | ||
47 | typeAnn = \(A :: Type) (a :: A) -> a | ||
48 | |||
49 | data Bool = False | True | ||
50 | |||
51 | otherwise = True | ||
52 | |||
53 | if' b t f = case b of True -> t | ||
54 | False -> f | ||
55 | |||
56 | -- higher-order unification test | ||
57 | htest b = if' b id | ||
58 | htest' b = if' b id :: (forall a . a -> a) -> _ | ||
59 | htest'' b = if' b id :: _ -> (forall a . a -> a) | ||
60 | |||
61 | data List a = Nil' | Cons' a (List a) | ||
62 | |||
63 | builtins | ||
64 | primAdd, primSub, primMod | ||
65 | :: Int -> Int -> Int | ||
66 | primSqrt :: Int -> Int | ||
67 | primIntEq, primIntLess | ||
68 | :: Int -> Int -> Bool | ||
69 | |||
70 | dcomp | ||
71 | (X :: Type) | ||
72 | (Y :: X -> Type) | ||
73 | (Z :: forall (x :: X) -> Y x -> Type) | ||
74 | (f :: forall x (y :: Y x) -> Z x y) | ||
75 | (g :: forall (x :: X) -> Y x) | ||
76 | (x :: X) | ||
77 | = f x (g x) | ||
78 | dcomp' | ||
79 | @X | ||
80 | @(Y :: X -> _) | ||
81 | @(Z :: forall x -> Y x -> _) | ||
82 | (f :: forall x . forall y -> Z x y) | ||
83 | (g :: forall x -> Y x) | ||
84 | x | ||
85 | = f (g x) | ||
86 | dcomp'' | ||
87 | :: forall | ||
88 | X | ||
89 | (Y :: X -> _) | ||
90 | (Z :: forall x -> Y x -> _) | ||
91 | . forall | ||
92 | (f :: forall x . forall y -> Z x y) | ||
93 | (g :: forall x -> Y x) x | ||
94 | -> Z x (g x) | ||
95 | dcomp'' f g x = f (g x) | ||
96 | |||
97 | listMap f xs = | ||
98 | | Nil' <- xs -> Nil' | ||
99 | | Cons' x xs <- xs -> Cons' (f x) (listMap f xs) | ||
100 | foldr c n xs = case xs of | ||
101 | Nil' -> n | ||
102 | Cons' x xs -> c x (foldr c n xs) | ||
103 | |||
104 | concat xs ys = foldr Cons' ys xs | ||
105 | |||
106 | from n = Cons' n (from (primAdd #1 n)) | ||
107 | head x = case x of | ||
108 | Nil' -> undefined | ||
109 | Cons' x _ -> x | ||
110 | tail = listCase (\_ -> _) undefined (\_ xs -> xs) | ||
111 | nth n xs = | ||
112 | | primIntEq #0 n -> head xs | ||
113 | | otherwise -> nth (primSub n #1) (tail xs) | ||
114 | filter p xs = | ||
115 | | Nil' <- xs -> Nil' | ||
116 | | Cons' x xs <- xs -> | ||
117 | | p x -> Cons' x (filter p xs) | ||
118 | | otherwise -> filter p xs | ||
119 | takeWhile p xs = | ||
120 | | Nil' <- xs -> Nil' | ||
121 | | Cons' x xs <- xs -> | ||
122 | | p x -> Cons' x (takeWhile p xs) | ||
123 | | otherwise -> Nil' | ||
124 | and' a b = boolCase (\_ -> _) False b a | ||
125 | or' a b = boolCase (\_ -> _) b True a | ||
126 | not' = boolCase (\_ -> _) True False | ||
127 | all p = listCase (\_ -> _) True (\x xs -> and' (p x) (all p xs)) | ||
128 | intLEq n m = or' (primIntLess n m) (primIntEq n m) | ||
129 | {- todo | ||
130 | primes = | ||
131 | Cons' #2 (Cons' #3 (filter (\x -> all (\i -> not' (primIntEq #0 (primMod x i))) ( | ||
132 | takeWhile (\p -> (\m -> or' (primIntLess p m) (primIntEq p m)) (primSqrt x)) primes | ||
133 | )) (from #5))) | ||
134 | |||
135 | nthPrimes n = nth n primes | ||
136 | main = nthPrimes #10 | ||
137 | -} | ||
138 | data Nat = Zero | Succ Nat | ||
139 | data Fin :: Nat -> Type where | ||
140 | FZero :: Fin (Succ n) | ||
141 | FSucc :: Fin n -> Fin (Succ n) | ||
142 | data Vec a :: Nat -> Type where | ||
143 | Nil :: Vec a Zero | ||
144 | Cons :: a -> Vec a n -> Vec a (Succ n) | ||
145 | data Eq @a :: a -> a -> Type where | ||
146 | Refl :: Eq x x | ||
147 | |||
148 | wrong | ||
149 | x = 1 1 | ||
150 | |||
151 | vec = Vec _ | ||
152 | |||
153 | builtins | ||
154 | natElim :: forall m -> m Zero -> (forall n -> m n -> m (Succ n)) -> forall b -> m b | ||
155 | |||
156 | --natElim (m :: Nat -> Type) (z :: m Zero) (s :: forall n -> m n -> m (Succ n)) = fix (\natElim -> natCase m z (\n -> s n (natElim n))) | ||
157 | |||
158 | builtins | ||
159 | finElim :: | ||
160 | forall (m :: forall n -> Fin n -> _) | ||
161 | -> (forall n . m (Succ n) FZero) | ||
162 | -> (forall n . forall f -> m n f -> m (Succ n) (FSucc f)) | ||
163 | -> forall n f -> m n f | ||
164 | |||
165 | -- addition of natural numbers | ||
166 | plus = | ||
167 | natElim | ||
168 | (\_ -> _) -- motive | ||
169 | (\n -> n) -- case for Zero | ||
170 | (\p rec n -> Succ (rec n)) -- case for Succ | ||
171 | |||
172 | -- predecessor, mapping 0 to 0 | ||
173 | pred = | ||
174 | natElim | ||
175 | (\_ -> _) | ||
176 | Zero | ||
177 | (\n' _ -> n') | ||
178 | |||
179 | -- a simpler elimination scheme for natural numbers | ||
180 | natFold = | ||
181 | \mz ms -> natElim | ||
182 | (\_ -> _) | ||
183 | mz | ||
184 | (\_ rec -> ms rec) | ||
185 | |||
186 | -- an eliminator for natural numbers that has special | ||
187 | -- cases for 0 and 1 | ||
188 | nat1Elim = \m m0 m1 ms -> natElim m m0 | ||
189 | (\p rec -> natElim (\n -> m (Succ n)) m1 ms p) | ||
190 | |||
191 | -- an eliminator for natural numbers that has special | ||
192 | -- cases for 0, 1 and 2 | ||
193 | nat2Elim = \m m0 m1 m2 ms -> nat1Elim m m0 m1 | ||
194 | (\p rec -> natElim (\n -> m (Succ (Succ n))) m2 ms p) | ||
195 | |||
196 | -- increment by one | ||
197 | inc = natFold (Succ Zero) Succ | ||
198 | |||
199 | -- embed Fin into Nat | ||
200 | finNat = finElim (\_ _ -> Nat) | ||
201 | Zero | ||
202 | (\_ rec -> Succ rec) | ||
203 | |||
204 | -- unit type | ||
205 | Unit' = Fin 1 | ||
206 | -- constructor | ||
207 | U = FZero :: Unit' | ||
208 | -- eliminator | ||
209 | |||
210 | unitElim = \m mu -> finElim (nat1Elim (\n -> Fin n -> Type) | ||
211 | (\_ -> Unit') | ||
212 | (\x -> m x) | ||
213 | (\_ _ _ -> Unit')) | ||
214 | (\ @n -> natElim (\n -> natElim (\n -> Fin (Succ n) -> Type) | ||
215 | (\x -> m x) | ||
216 | (\_ _ _ -> Unit') | ||
217 | n FZero) | ||
218 | mu | ||
219 | (\_ _ -> U) n) | ||
220 | (\ @n f _ -> finElim (\n f -> natElim (\n -> Fin (Succ n) -> Type) | ||
221 | (\x -> m x) | ||
222 | (\_ _ _ -> Unit') | ||
223 | n (FSucc f)) | ||
224 | U | ||
225 | (\_ _ -> U) | ||
226 | n f) | ||
227 | 1 | ||
228 | |||
229 | -- empty type | ||
230 | Void = Fin 0 | ||
231 | -- eliminator | ||
232 | voidElim m = finElim (natElim (\n -> Fin n -> Type) | ||
233 | (\x -> m x) | ||
234 | (\_ _ _ -> _)) | ||
235 | U | ||
236 | (\_ _ -> U) | ||
237 | 0 | ||
238 | |||
239 | -- type of booleans | ||
240 | Bool' = Fin 2 | ||
241 | -- constructors | ||
242 | False' = FZero :: Bool' | ||
243 | True' = FSucc FZero :: Bool' | ||
244 | -- eliminator | ||
245 | |||
246 | boolElim = \m mf mt -> finElim ( nat2Elim (\n -> Fin n -> Type) | ||
247 | (\_ -> Unit') (\_ -> Unit') | ||
248 | (\x -> m x) | ||
249 | (\_ _ _ -> Unit')) | ||
250 | (\ @n -> nat1Elim (\n -> nat1Elim (\n -> Fin (Succ n) -> Type) | ||
251 | (\_ -> Unit') | ||
252 | (\x -> m x) | ||
253 | (\_ _ _ -> Unit') | ||
254 | n FZero) | ||
255 | U mf (\_ _ -> U) n) | ||
256 | (\ @n f _ -> finElim (\n f -> nat1Elim (\n -> Fin (Succ n) -> Type) | ||
257 | (\_ -> Unit') | ||
258 | (\x -> m x) | ||
259 | (\_ _ _ -> Unit') | ||
260 | n (FSucc f)) | ||
261 | (\ @n -> natElim | ||
262 | (\n -> natElim | ||
263 | (\n -> Fin (Succ (Succ n)) -> Type) | ||
264 | (\x -> m x) | ||
265 | (\_ _ _ -> Unit') | ||
266 | n (FSucc FZero)) | ||
267 | mt (\_ _ -> U) n) | ||
268 | (\ @n f _ -> finElim | ||
269 | (\n f -> natElim | ||
270 | (\n -> Fin (Succ (Succ n)) -> Type) | ||
271 | (\x -> m x) | ||
272 | (\_ _ _ -> Unit') | ||
273 | n (FSucc (FSucc f))) | ||
274 | U | ||
275 | (\_ _ -> U) | ||
276 | n f) | ||
277 | n f) | ||
278 | 2 | ||
279 | |||
280 | |||
281 | -- boolean not, and, or, equivalence, xor | ||
282 | not = boolElim (\_ -> _) True' False' | ||
283 | and = boolElim (\_ -> _) (const False') id | ||
284 | or = boolElim (\_ -> _) id (const True') | ||
285 | iff = boolElim (\_ -> _) not id | ||
286 | xor = boolElim (\_ -> _) id not | ||
287 | |||
288 | -- even, odd, isZero, isSucc | ||
289 | even = natFold True' not | ||
290 | odd = natFold False' not | ||
291 | isZero = natFold True' (const False') | ||
292 | isSucc = natFold False' (const True') | ||
293 | |||
294 | -- equality on natural numbers | ||
295 | natEq = | ||
296 | natElim | ||
297 | (\_ -> _) | ||
298 | (natElim | ||
299 | (\_ -> _) | ||
300 | True' | ||
301 | (\n' _ -> False')) | ||
302 | (\m' rec_m' -> natElim | ||
303 | (\_ -> _) | ||
304 | False' | ||
305 | (\n' _ -> rec_m' n')) | ||
306 | |||
307 | -- "oh so true" | ||
308 | Prop = boolElim (\_ -> _) Void Unit' | ||
309 | |||
310 | -- reflexivity of equality on natural numbers | ||
311 | pNatEqRefl = | ||
312 | natElim | ||
313 | (\n -> Prop (natEq n n)) | ||
314 | U | ||
315 | (\_ rec -> rec) | ||
316 | -- :: forall (n :: Nat) -> Prop (natEq n n) | ||
317 | |||
318 | -- alias for type-level negation | ||
319 | Not a = a -> Void | ||
320 | |||
321 | -- Leibniz prinicple: forall a b . (a -> b) -> forall (x :: a) (y :: a) -> Eq x y -> Eq (f x) (f y) | ||
322 | leibniz f x y = eqCase | ||
323 | (\x y _ -> Eq (f x) (f y)) | ||
324 | Refl @x @y | ||
325 | |||
326 | -- symmetry of (general) equality | ||
327 | symm x y = eqCase (\x y _ -> Eq y x) Refl @x @y | ||
328 | |||
329 | -- transitivity of (general) equality | ||
330 | tran eq_x_y = eqCase | ||
331 | (\x y _ -> forall z -> Eq y z -> Eq x z) | ||
332 | (\_ x -> x) | ||
333 | eq_x_y _ | ||
334 | |||
335 | -- apply an equality proof on two types | ||
336 | apply = eqCase (\a b _ -> a -> b) id | ||
337 | |||
338 | p1IsNot0 p = apply | ||
339 | (leibniz | ||
340 | (natElim (\_ -> _) Void (\_ _ -> Unit')) | ||
341 | 1 0 p) | ||
342 | U | ||
343 | |||
344 | -- proof that 0 is not 1 | ||
345 | p0IsNot1 p = p1IsNot0 (symm 0 1 p) | ||
346 | |||
347 | -- proof that zero is not a successor | ||
348 | p0IsNoSucc = | ||
349 | natElim | ||
350 | (\n -> Not (Eq 0 (Succ n))) | ||
351 | p0IsNot1 | ||
352 | (\n' rec_n' eq_0_SSn' -> | ||
353 | rec_n' (leibniz pred Zero (Succ (Succ n')) eq_0_SSn')) | ||
354 | |||
355 | -- generate a vector of given length from a specified element (replicate) | ||
356 | |||
357 | replicate = | ||
358 | natElim | ||
359 | (\n -> forall a -> a -> Vec a n) | ||
360 | (\_ _ -> Nil) | ||
361 | (\n' rec_n' a x -> Cons x (rec_n' a x)) | ||
362 | |||
363 | -- alternative definition of replicate | ||
364 | replicate' = | ||
365 | natElim | ||
366 | (\n -> _ -> vec n) | ||
367 | (\_ -> Nil) | ||
368 | (\n' rec_n' x -> Cons x (rec_n' x)) | ||
369 | |||
370 | replicate'' x = natElim vec Nil (\n' rec_n' -> Cons x rec_n') | ||
371 | |||
372 | -- generate a vector of given length n, containing the natural numbers smaller than n | ||
373 | fromto = natElim vec Nil (\n' rec_n' -> Cons n' rec_n') | ||
374 | |||
375 | builtins | ||
376 | vecElim :: | ||
377 | forall (m :: forall k -> vec k -> _) -> | ||
378 | m Zero Nil | ||
379 | -> (forall l . forall x xs -> m l xs -> m (Succ l) (Cons x xs)) | ||
380 | -> forall k xs -> m k xs | ||
381 | |||
382 | -- append two vectors | ||
383 | append = vecElim | ||
384 | (\m _ -> forall n -> vec n -> vec (plus m n)) | ||
385 | (\_ v -> v) | ||
386 | (\v vs rec n w -> Cons v (rec n w)) | ||
387 | |||
388 | -- helper function for tail, see below | ||
389 | tail' a = vecElim (\m v -> forall n -> Eq m (Succ n) -> Vec a n) | ||
390 | (\n eq_0_SuccN -> voidElim (\_ -> _) | ||
391 | (p0IsNoSucc n eq_0_SuccN)) | ||
392 | (\ @m' v vs rec_m' n eq_SuccM'_SuccN -> | ||
393 | eqCase | ||
394 | (\m' n e -> Vec a m' -> Vec a n) | ||
395 | id | ||
396 | @m' @n | ||
397 | (leibniz pred (Succ m') (Succ n) eq_SuccM'_SuccN) vs) | ||
398 | |||
399 | -- compute the tail of a vector | ||
400 | tailVec = \n v -> tail' _ (Succ n) v n Refl | ||
401 | |||
402 | -- projection out of a vector | ||
403 | at = | ||
404 | vecElim (\n v -> Fin n -> _) | ||
405 | (\f -> voidElim (\_ -> _) f) | ||
406 | (\ @n' v vs rec_n' f_SuccN' -> | ||
407 | finElim (\n _ -> Eq n (Succ n') -> _) | ||
408 | (\e -> v) | ||
409 | (\ @n f_N _ eq_SuccN_SuccN' -> | ||
410 | rec_n' (eqCase | ||
411 | (\x y e -> Fin x -> Fin y) | ||
412 | id | ||
413 | @n @n' | ||
414 | (leibniz pred | ||
415 | (Succ n) (Succ n') eq_SuccN_SuccN') | ||
416 | f_N)) | ||
417 | (Succ n') | ||
418 | f_SuccN' | ||
419 | Refl) | ||
420 | |||
421 | -- head of a vector | ||
422 | headVec n v = at (Succ n) v FZero | ||
423 | |||
424 | -- vector map | ||
425 | map f = vecElim (\n _ -> vec n) Nil (\x _ rec -> Cons (f x) rec) | ||
426 | |||
427 | -- proofs that 0 is the neutral element of addition | ||
428 | -- one direction is trivial by definition of plus: | ||
429 | p0PlusNisN = Refl :: forall n . Eq (plus 0 n) n | ||
430 | |||
431 | -- the other direction requires induction on N: | ||
432 | pNPlus0isN = | ||
433 | natElim (\n -> Eq (plus n 0) n) | ||
434 | Refl | ||
435 | (\n' rec -> leibniz Succ (plus n' 0) n' rec) | ||
436 | |||
437 | testNoNorm = Refl :: Eq (primIntEq #3 #3) True | ||
438 | |||
439 | True'' = FSucc FZero :: Bool' | ||
440 | |||
441 | |||
442 | data EqD a = EqDC (a -> a -> Bool) | ||
443 | |||
444 | eqInt = EqDC primIntEq | ||
445 | |||
446 | builtins | ||
447 | matchInt :: Type -> (Type -> Type) -> Type -> Type | ||
448 | matchList :: (Type -> Type) -> (Type -> Type) -> Type -> Type | ||
449 | |||
450 | Eq_ :: Type -> Type | ||
451 | Eq_ a = matchInt Unit (matchList Eq_ (\_ -> Empty)) a | ||
452 | |||
453 | builtins eqD :: Eq_ a => EqD a | ||
454 | |||
455 | eq = eqDCase (\_ -> _) id eqD | ||
456 | |||
457 | eq' = eqDCase (\_ -> _) id | ||
458 | |||
459 | eqList = EqDC (\as bs -> listCase (\_ -> _) (listCase (\_ -> _) True (\_ _ -> False) bs) (\a as -> listCase (\_ -> _) False (\b bs -> and' (eq a b) (eq as bs)) bs) as) | ||
460 | |||
461 | main_ = eq' eqList (Cons' #3 Nil') Nil' | ||
462 | main'' = eq (Cons' #3 Nil') Nil' | ||
463 | |||
464 | data MonadD (m :: Type -> Type) = MonadDC (forall a . a -> m a) (forall a b . m a -> (a -> m b) -> m b) | ||
465 | |||
466 | data Identity a = IdentityC a | ||
467 | |||
468 | identityMonad = MonadDC IdentityC (\m f -> identityCase (\_ -> _) f m) | ||
469 | |||
470 | Char = Int | ||
471 | |||
472 | data IO a where | ||
473 | IORet :: a -> IO a | ||
474 | PutChar :: Char -> IO a -> IO a | ||
475 | GetChar :: (Char -> IO a) -> IO a | ||
476 | |||
477 | data ReaderT r (m :: Type -> Type) a = ReaderTC (r -> m a) | ||
478 | |||
479 | ----------------------------- | ||
480 | |||
481 | builtins | ||
482 | ifIdentity1 :: forall a . a -> ((Type -> Type) -> a) -> (Type -> Type) -> a | ||
483 | ifReaderT1 :: forall a . (Type -> (Type -> Type) -> a) -> ((Type -> Type) -> a) -> (Type -> Type) -> a | ||
484 | |||
485 | builtins | ||
486 | Monad :: (Type -> Type) -> Type | ||
487 | --let Monad = fix (\Monad -> ifIdentity1 Unit (ifReaderT1 (\r m -> Monad m) (\_ -> Empty))) | ||
488 | |||
489 | ----------------- recursive definition | ||
490 | builtins monadD :: forall m . Monad m => MonadD m | ||
491 | -- let monadD = fix (\monadD @t -> ifIdentity_1 identityMonad (ifReaderT_1 (\r m -> monadReaderT) undefined t)) | ||
492 | |||
493 | return = monadDCase (\_ -> _) (\r _ -> r) monadD | ||
494 | bind = monadDCase (\_ -> _) (\_ b -> b) monadD | ||
495 | |||
496 | monadReaderT = MonadDC (\a -> ReaderTC (\r -> return a)) | ||
497 | (\m f -> ReaderTC (\r -> | ||
498 | readerTCase (\_ -> _) | ||
499 | (\g -> bind | ||
500 | (g r) | ||
501 | (\a -> readerTCase (\_ -> _) (\h -> h r) (f a))) | ||
502 | m)) | ||
503 | -- :: forall r m . Monad m => MonadD (ReaderT r m) | ||
504 | |||
505 | IOBind ma f = case ma of | ||
506 | IORet a -> f a | ||
507 | PutChar i r -> PutChar i (bind r f) | ||
508 | GetChar g -> GetChar (\i -> bind (g i) f) | ||
509 | |||
510 | monadIO = MonadDC IORet IOBind | ||
511 | -------------------------- end of recursive definition | ||
512 | |||
513 | liftReaderT m = ReaderTC (\r -> m) | ||
514 | |||
515 | bind' m m' = bind m (const m') | ||
516 | fmap f m = bind m (comp return f) | ||
517 | |||
518 | sequence = listCase (\_ -> _) (return Nil') (\x xs -> bind x (\vx -> fmap (Cons' vx) (sequence xs))) | ||
519 | sequence_ = listCase (\_ -> _) (return TT) (\x xs -> bind' x (sequence_ xs)) | ||
520 | |||
521 | mapM f = comp sequence (listMap f) | ||
522 | mapM_ f = comp sequence_ (listMap f) | ||
523 | |||
524 | putChar i = PutChar i (return TT) | ||
525 | getChar = GetChar return | ||
526 | |||
527 | putStr = mapM_ putChar | ||
528 | putStrLn = comp putStr (flip concat (Cons' #0 Nil')) | ||
529 | |||
530 | -- todo -- getLine = bind getChar (\c -> boolCase (\_ -> _) (bind getLine (\cs -> return (Cons' c cs))) (return Nil') (eq c #0)) | ||
531 | |||
532 | --let mex_ = return' monadReaderT #3 | ||
533 | mex = return #3 :: IO Int | ||
534 | mex' = return #3 :: ReaderT Bool IO Int | ||
535 | |||
536 | -- todo -- main' = bind getLine putStrLn | ||
537 | |||
538 | |||
539 | data Exp :: Type -> Type where | ||
540 | EInt :: Int -> Exp Int | ||
541 | EApp :: Exp (a -> b) -> Exp a -> Exp b | ||
542 | ECond :: Exp Bool -> Exp a -> Exp a -> Exp a | ||
543 | |||
544 | expCase' :: forall | ||
545 | (c :: forall a -> Exp a -> Type) | ||
546 | -> (forall d -> c Int (EInt d)) | ||
547 | -> (forall f g . forall i j -> c g (EApp @f @g i j)) | ||
548 | -> (forall l . forall m n o -> c l (ECond @l m n o)) | ||
549 | -> forall q | ||
550 | (r :: Exp q) -> c q r | ||
551 | expCase' m i a c x e = expCase m i a c @x e | ||
552 | |||
553 | expCase'' :: forall | ||
554 | (c :: forall a -> Exp a -> Type) | ||
555 | -> (forall d -> c Int (EInt d)) | ||
556 | -> (forall f g . forall i j -> c g (EApp @f @g i j)) | ||
557 | -> (forall l . forall m n o -> c l (ECond @l m n o)) | ||
558 | -> forall q | ||
559 | . forall (r :: Exp q) -> c q r | ||
560 | expCase'' m i a c @x e = expCase' m i a c x e | ||
561 | |||
562 | -- todo -- eval :: forall a . Exp a -> a | ||
563 | eval = expCase (\b _ -> b) -- todo: how to guess the motive | ||
564 | (\i -> i) | ||
565 | (\f a -> eval f (eval a)) | ||
566 | (\b m n -> boolCase (\_ -> _) (eval n) (eval m) (eval b)) | ||
567 | :: forall a . Exp a -> a | ||
568 | |||