-
Notifications
You must be signed in to change notification settings - Fork 3
/
church.go
54 lines (49 loc) · 1.29 KB
/
church.go
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
// Copyright (C) 2017 JT Olds
// See LICENSE for copying information.
package sheepda
// ChurchNumeral returns a Church-encoded representation of the number val.
func ChurchNumeral(val uint) Value {
var base Expr = &VariableExpr{Name: "x"}
for i := uint(0); i < val; i++ {
base = &ApplicationExpr{
Func: &VariableExpr{Name: "f"},
Arg: base}
}
return NewClosure(NewScope(), &LambdaExpr{
Arg: "f",
Body: &LambdaExpr{
Arg: "x",
Body: base}})
}
// ChurchPair returns a Church-encoded pair of the two values first and second.
func ChurchPair(first, second Value) Value {
return NewClosure(NewScope().
Set("first", first).
Set("second", second),
&LambdaExpr{
Arg: "p",
Body: &ApplicationExpr{
Func: &ApplicationExpr{
Func: &VariableExpr{Name: "p"},
Arg: &VariableExpr{Name: "first"}},
Arg: &VariableExpr{Name: "second"}}})
}
var (
churchTrue = NewClosure(NewScope(), &LambdaExpr{
Arg: "t",
Body: &LambdaExpr{
Arg: "f",
Body: &VariableExpr{Name: "t"}}})
churchFalse = NewClosure(NewScope(), &LambdaExpr{
Arg: "t",
Body: &LambdaExpr{
Arg: "f",
Body: &VariableExpr{Name: "f"}}})
)
// ChurchBool returns a Church-encoded boolean representation of val.
func ChurchBool(val bool) Value {
if val {
return churchTrue
}
return churchFalse
}