summaryrefslogtreecommitdiff
path: root/testdata/DepPrelude.wip.lc
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-02-25 12:35:13 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-02-25 12:35:24 +0100
commitb99e2171f7a63da4261fdafeeb527d61e4a5f85a (patch)
treeec7228aaf8aa9ef5b8534aa57558e7035bd5c4e8 /testdata/DepPrelude.wip.lc
parentfd7ef2e607ec31c225dd01cdd1d05a3122fa823c (diff)
cleanup test cases
Diffstat (limited to 'testdata/DepPrelude.wip.lc')
-rw-r--r--testdata/DepPrelude.wip.lc568
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
5builtintycons
6 Int :: Type
7
8data Unit = TT
9
10builtins
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
16tyType (a :: Type) = a
17id a = a
18const x y = x
19const' :: forall a b . a -> b -> a
20const' x y = x
21const'' :: forall b a . a -> b -> a
22const'' x y = x
23id' a = id a
24id'' = id id
25app f x = f x
26comp f g x = f (g x)
27app2 f x y = f x y
28flip f x y = f y x
29scomb a b c = a c (b c)
30
31builtins fix :: forall a . (a -> a) -> a
32builtins f_i_x :: forall a . (a -> a) -> a
33
34data Sigma a (b :: a -> Type) = Pair (x :: a) (b x)
35
36-- undefined = undefined
37builtins
38 undefined :: forall (a :: Type) . a
39
40data Empty
41--data T2 a b = T2C a b
42builtins
43 T2 :: Type -> Type -> Type
44 T2C :: Unit -> Unit -> Unit
45
46-- identity function, used for type annotations internally
47typeAnn = \(A :: Type) (a :: A) -> a
48
49data Bool = False | True
50
51otherwise = True
52
53if' b t f = case b of True -> t
54 False -> f
55
56-- higher-order unification test
57htest b = if' b id
58htest' b = if' b id :: (forall a . a -> a) -> _
59htest'' b = if' b id :: _ -> (forall a . a -> a)
60
61data List a = Nil' | Cons' a (List a)
62
63builtins
64 primAdd, primSub, primMod
65 :: Int -> Int -> Int
66 primSqrt :: Int -> Int
67 primIntEq, primIntLess
68 :: Int -> Int -> Bool
69
70dcomp
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)
78dcomp'
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)
86dcomp''
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)
95dcomp'' f g x = f (g x)
96
97listMap f xs =
98 | Nil' <- xs -> Nil'
99 | Cons' x xs <- xs -> Cons' (f x) (listMap f xs)
100foldr c n xs = case xs of
101 Nil' -> n
102 Cons' x xs -> c x (foldr c n xs)
103
104concat xs ys = foldr Cons' ys xs
105
106from n = Cons' n (from (primAdd #1 n))
107head x = case x of
108 Nil' -> undefined
109 Cons' x _ -> x
110tail = listCase (\_ -> _) undefined (\_ xs -> xs)
111nth n xs =
112 | primIntEq #0 n -> head xs
113 | otherwise -> nth (primSub n #1) (tail xs)
114filter p xs =
115 | Nil' <- xs -> Nil'
116 | Cons' x xs <- xs ->
117 | p x -> Cons' x (filter p xs)
118 | otherwise -> filter p xs
119takeWhile p xs =
120 | Nil' <- xs -> Nil'
121 | Cons' x xs <- xs ->
122 | p x -> Cons' x (takeWhile p xs)
123 | otherwise -> Nil'
124and' a b = boolCase (\_ -> _) False b a
125or' a b = boolCase (\_ -> _) b True a
126not' = boolCase (\_ -> _) True False
127all p = listCase (\_ -> _) True (\x xs -> and' (p x) (all p xs))
128intLEq n m = or' (primIntLess n m) (primIntEq n m)
129{- todo
130primes =
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
135nthPrimes n = nth n primes
136main = nthPrimes #10
137-}
138data Nat = Zero | Succ Nat
139data Fin :: Nat -> Type where
140 FZero :: Fin (Succ n)
141 FSucc :: Fin n -> Fin (Succ n)
142data Vec a :: Nat -> Type where
143 Nil :: Vec a Zero
144 Cons :: a -> Vec a n -> Vec a (Succ n)
145data Eq @a :: a -> a -> Type where
146 Refl :: Eq x x
147
148wrong
149 x = 1 1
150
151vec = Vec _
152
153builtins
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
158builtins
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
166plus =
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
173pred =
174 natElim
175 (\_ -> _)
176 Zero
177 (\n' _ -> n')
178
179-- a simpler elimination scheme for natural numbers
180natFold =
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
188nat1Elim = \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
193nat2Elim = \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
197inc = natFold (Succ Zero) Succ
198
199-- embed Fin into Nat
200finNat = finElim (\_ _ -> Nat)
201 Zero
202 (\_ rec -> Succ rec)
203
204-- unit type
205Unit' = Fin 1
206-- constructor
207U = FZero :: Unit'
208-- eliminator
209
210unitElim = \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
230Void = Fin 0
231-- eliminator
232voidElim m = finElim (natElim (\n -> Fin n -> Type)
233 (\x -> m x)
234 (\_ _ _ -> _))
235 U
236 (\_ _ -> U)
237 0
238
239-- type of booleans
240Bool' = Fin 2
241-- constructors
242False' = FZero :: Bool'
243True' = FSucc FZero :: Bool'
244-- eliminator
245
246boolElim = \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
282not = boolElim (\_ -> _) True' False'
283and = boolElim (\_ -> _) (const False') id
284or = boolElim (\_ -> _) id (const True')
285iff = boolElim (\_ -> _) not id
286xor = boolElim (\_ -> _) id not
287
288-- even, odd, isZero, isSucc
289even = natFold True' not
290odd = natFold False' not
291isZero = natFold True' (const False')
292isSucc = natFold False' (const True')
293
294-- equality on natural numbers
295natEq =
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"
308Prop = boolElim (\_ -> _) Void Unit'
309
310-- reflexivity of equality on natural numbers
311pNatEqRefl =
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
319Not 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)
322leibniz f x y = eqCase
323 (\x y _ -> Eq (f x) (f y))
324 Refl @x @y
325
326-- symmetry of (general) equality
327symm x y = eqCase (\x y _ -> Eq y x) Refl @x @y
328
329-- transitivity of (general) equality
330tran 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
336apply = eqCase (\a b _ -> a -> b) id
337
338p1IsNot0 p = apply
339 (leibniz
340 (natElim (\_ -> _) Void (\_ _ -> Unit'))
341 1 0 p)
342 U
343
344-- proof that 0 is not 1
345p0IsNot1 p = p1IsNot0 (symm 0 1 p)
346
347-- proof that zero is not a successor
348p0IsNoSucc =
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
357replicate =
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
364replicate' =
365 natElim
366 (\n -> _ -> vec n)
367 (\_ -> Nil)
368 (\n' rec_n' x -> Cons x (rec_n' x))
369
370replicate'' 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
373fromto = natElim vec Nil (\n' rec_n' -> Cons n' rec_n')
374
375builtins
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
383append = 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
389tail' 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
400tailVec = \n v -> tail' _ (Succ n) v n Refl
401
402-- projection out of a vector
403at =
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
422headVec n v = at (Succ n) v FZero
423
424-- vector map
425map 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:
429p0PlusNisN = Refl :: forall n . Eq (plus 0 n) n
430
431-- the other direction requires induction on N:
432pNPlus0isN =
433 natElim (\n -> Eq (plus n 0) n)
434 Refl
435 (\n' rec -> leibniz Succ (plus n' 0) n' rec)
436
437testNoNorm = Refl :: Eq (primIntEq #3 #3) True
438
439True'' = FSucc FZero :: Bool'
440
441
442data EqD a = EqDC (a -> a -> Bool)
443
444eqInt = EqDC primIntEq
445
446builtins
447 matchInt :: Type -> (Type -> Type) -> Type -> Type
448 matchList :: (Type -> Type) -> (Type -> Type) -> Type -> Type
449
450Eq_ :: Type -> Type
451Eq_ a = matchInt Unit (matchList Eq_ (\_ -> Empty)) a
452
453builtins eqD :: Eq_ a => EqD a
454
455eq = eqDCase (\_ -> _) id eqD
456
457eq' = eqDCase (\_ -> _) id
458
459eqList = EqDC (\as bs -> listCase (\_ -> _) (listCase (\_ -> _) True (\_ _ -> False) bs) (\a as -> listCase (\_ -> _) False (\b bs -> and' (eq a b) (eq as bs)) bs) as)
460
461main_ = eq' eqList (Cons' #3 Nil') Nil'
462main'' = eq (Cons' #3 Nil') Nil'
463
464data MonadD (m :: Type -> Type) = MonadDC (forall a . a -> m a) (forall a b . m a -> (a -> m b) -> m b)
465
466data Identity a = IdentityC a
467
468identityMonad = MonadDC IdentityC (\m f -> identityCase (\_ -> _) f m)
469
470Char = Int
471
472data IO a where
473 IORet :: a -> IO a
474 PutChar :: Char -> IO a -> IO a
475 GetChar :: (Char -> IO a) -> IO a
476
477data ReaderT r (m :: Type -> Type) a = ReaderTC (r -> m a)
478
479-----------------------------
480
481builtins
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
485builtins
486 Monad :: (Type -> Type) -> Type
487--let Monad = fix (\Monad -> ifIdentity1 Unit (ifReaderT1 (\r m -> Monad m) (\_ -> Empty)))
488
489----------------- recursive definition
490builtins monadD :: forall m . Monad m => MonadD m
491-- let monadD = fix (\monadD @t -> ifIdentity_1 identityMonad (ifReaderT_1 (\r m -> monadReaderT) undefined t))
492
493return = monadDCase (\_ -> _) (\r _ -> r) monadD
494bind = monadDCase (\_ -> _) (\_ b -> b) monadD
495
496monadReaderT = 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
505IOBind 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
510monadIO = MonadDC IORet IOBind
511-------------------------- end of recursive definition
512
513liftReaderT m = ReaderTC (\r -> m)
514
515bind' m m' = bind m (const m')
516fmap f m = bind m (comp return f)
517
518sequence = listCase (\_ -> _) (return Nil') (\x xs -> bind x (\vx -> fmap (Cons' vx) (sequence xs)))
519sequence_ = listCase (\_ -> _) (return TT) (\x xs -> bind' x (sequence_ xs))
520
521mapM f = comp sequence (listMap f)
522mapM_ f = comp sequence_ (listMap f)
523
524putChar i = PutChar i (return TT)
525getChar = GetChar return
526
527putStr = mapM_ putChar
528putStrLn = 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
533mex = return #3 :: IO Int
534mex' = return #3 :: ReaderT Bool IO Int
535
536-- todo -- main' = bind getLine putStrLn
537
538
539data 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
544expCase' :: 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
551expCase' m i a c x e = expCase m i a c @x e
552
553expCase'' :: 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
560expCase'' m i a c @x e = expCase' m i a c x e
561
562-- todo -- eval :: forall a . Exp a -> a
563eval = 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