fixed paren
This commit is contained in:
parent
97971a1d0c
commit
1055ba154d
3 changed files with 20 additions and 19 deletions
23
lambda.ml
23
lambda.ml
|
@ -44,16 +44,7 @@ let rec lookup ctx name : term =
|
|||
|
||||
let rec eval (ctx, t) =
|
||||
let rec helper (ctx, t) =
|
||||
match t with
|
||||
| Assign(name, t') ->
|
||||
let (ctx', r) = helper(ctx, t') in
|
||||
(assign ctx (name, r), r)
|
||||
| TmAbs(name, t') ->
|
||||
let (ctx', v) = helper (ctx, t') in
|
||||
(assign ctx (name, v), v)
|
||||
| TmVar(name) ->
|
||||
(ctx, lookup ctx name)
|
||||
| _ -> raise EvaluationComplete
|
||||
raise EvaluationComplete
|
||||
in try
|
||||
let (ctx', t') = helper (ctx, t) in
|
||||
eval (ctx', t')
|
||||
|
@ -64,11 +55,17 @@ let _ =
|
|||
let rec loop ctx =
|
||||
print_string "> "; flush stdout;
|
||||
let lexbuf = Lexing.from_channel stdin in
|
||||
let t = Parser.main Lexer.token lexbuf in
|
||||
let x = Parser.main Lexer.token lexbuf in
|
||||
match x with
|
||||
| Types.Term t ->
|
||||
let (ctx', r) = eval (ctx, t) in
|
||||
print_endline (string_of_term r); flush stdout;
|
||||
loop ctx' in
|
||||
loop []
|
||||
loop ctx'
|
||||
| Types.Assign (n, t) ->
|
||||
let (ctx', r) = eval (ctx, t) in
|
||||
(* bind the name *)
|
||||
loop ctx'
|
||||
in loop []
|
||||
with Lexer.Eof ->
|
||||
print_endline "^D";
|
||||
exit 0
|
||||
|
|
|
@ -11,20 +11,21 @@
|
|||
%token Lambda
|
||||
|
||||
%start main
|
||||
%type <Types.term> main
|
||||
%type <Types.input> main
|
||||
%%
|
||||
|
||||
main:
|
||||
| assign EOL { $1 }
|
||||
| expr EOL { $1 }
|
||||
| expr EOL { Types.Term($1) }
|
||||
;
|
||||
assign:
|
||||
| Ident Equal expr { Types.Assign($1, $3) }
|
||||
;
|
||||
expr:
|
||||
| var { Types.TmVar($1) }
|
||||
| LParen expr RParen { $2 }
|
||||
| Lambda var Dot expr { Types.TmAbs($2, $4) }
|
||||
| LParen expr expr RParen { Types.TmApp ($2, $3) }
|
||||
| expr expr { Types.TmApp($1, $2) }
|
||||
;
|
||||
var:
|
||||
Ident { $1 }
|
||||
|
|
5
types.ml
5
types.ml
|
@ -1,10 +1,13 @@
|
|||
type var = string
|
||||
|
||||
type term =
|
||||
| Assign of string * term
|
||||
| TmVar of var
|
||||
| TmAbs of var * term
|
||||
| TmApp of term * term
|
||||
|
||||
type input =
|
||||
| Term of term
|
||||
| Assign of string * term
|
||||
|
||||
type context = (string * term) list
|
||||
|
||||
|
|
Loading…
Reference in a new issue