-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.txt
More file actions
71 lines (49 loc) · 1.64 KB
/
core.txt
File metadata and controls
71 lines (49 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# functional combinators
let Id = λ x . x;
let Const = λ c . (λ x . c);
let Compose = λ f g . (λ x . f (g x));
# logical values and operators
let True = λ x y . x;
let False = λ x y . y;
let IfThenElse = λ p x y . p x y;
let And = λ x y . ITE x y F : {let T=True; let F=False; let ITE=IfThenElse;}
let Or = λ x y . ITE x T y : {let T=True; let F=False; let ITE=IfThenElse;}
let Not = λ x . ITE x F T : {let T=True; let F=False; let ITE=IfThenElse;}
let BoolEq = λ x y . Or (And x y) (And (Not x) (Not y));
# lists
let Nil = λ x . True;
let Pair = λ x y . (λ f . f x y);
let First = λ p . p True;
let Second = λ p . p False;
let Null = λ p . p (λ x y . False);
# convenience functions for creating small lists
let List2 = λ x y . Pair x (Pair y Nil);
let List3 = λ x y z . Pair x (Pair y (Pair z Nil));
let List4 = λ w x y z . Pair w (Pair x (Pair y (Pair z Nil)));
# folds, map & filter
letrec Foldr = λ f z xs . ITE (Null xs) z (f Head (Foldr f z Tail)) :
{
let ITE = IfThenElse;
let Head = First xs;
let Tail = Second xs;
}
let Map = λ f xs . Foldr G Nil xs :
{
let G = λ x ys . Pair (f x) ys;
}
let Filter = λ p xs . Foldr G Nil xs :
{
let G = λ x xs . IfThenElse (p x) (Pair x xs) xs;
}
# Church numerals
let Zero = λ f . (λ x . x);
let One = λ f . (λ x . f x);
let Plus = λ m n . λ f . Compose (m f) (n f);
let Succ = Plus One;
let Pred = λ n . First (n Inc (Pair Zero Zero)) :
{
let Inc = λ x . Pair (Second x) (Succ (Second x));
}
let Mult = λ x y . y (Plus x) Zero;
let Pow = λ x y . y (Mult x) One;
let IsZero = λ n . n (λ x . False) True;