Skip to content

Commit

Permalink
"true", "false", "if" and "isZero".
Browse files Browse the repository at this point in the history
  • Loading branch information
Baris Aktemur committed Dec 3, 2016
1 parent cf9a48a commit 5ef5b3c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,20 @@ Multiplication and "predecessor" function:
- : string = "(\\f.(\\x.(f (f (f (f x))))))"
```

Booleans, "if", and "isZero" predicate:

```ocaml
# str(run "true");;
- : string = "(\\a.(\\b.a))"
# str(run "false");;
- : string = "(\\a.(\\b.b))"
# str(run "isZero zero");;
- : string = "(\\a.(\\b.a))"
# str(run "isZero one");;
- : string = "(\\a.(\\b.b))"
# str(run "if (isZero (pred one)) three four");;
- : string = "(\\f.(\\x.(f (f (f x)))))"
# str(run "if (isZero one) three four");;
- : string = "(\\f.(\\x.(f (f (f (f x))))))"
```

6 changes: 6 additions & 0 deletions Lambda/grammar.mly
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
%token ZERO ONE TWO THREE FOUR
%token SUCC ADD
%token MULT PRED
%token IF TRUE FALSE ISZERO

%start main
%type <expr> main
Expand Down Expand Up @@ -44,6 +45,11 @@ atomExpr:
App(App(App(Var "n", Lambda("g", Lambda("h", App(Var "h", App(Var "g", Var "f"))))),
Lambda("u", Var "x")),
Lambda("u", Var "u"))))) }
| TRUE { Lambda("a", Lambda("b", Var "a")) }
| FALSE { Lambda("a", Lambda("b", Var "b")) }
| IF { Lambda("cond", Lambda("then", Lambda("else", App(App(Var "cond", Var "then"), Var "else")))) }
| ISZERO { Lambda("n", App(App(Var "n", Lambda("x", Lambda("a", Lambda("b", Var "b")))),
Lambda("a", Lambda("b", Var "a")))) }
| LPAR expression RPAR { $2 }
;

Expand Down
4 changes: 4 additions & 0 deletions Lambda/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ let keyword s =
| "add" -> ADD
| "mult" -> MULT
| "pred" -> PRED
| "if" -> IF
| "true" -> TRUE
| "false" -> FALSE
| "isZero" -> ISZERO
| _ -> NAME s
}

Expand Down

0 comments on commit 5ef5b3c

Please sign in to comment.