diff --git a/lambda.ml b/lambda.ml index 3390806..943c29a 100644 --- a/lambda.ml +++ b/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 (ctx', r) = eval (ctx, t) in - print_endline (string_of_term r); flush stdout; - loop ctx' in - loop [] + 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' + | 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 diff --git a/parser.mly b/parser.mly index f6e9def..4c3299b 100644 --- a/parser.mly +++ b/parser.mly @@ -11,20 +11,21 @@ %token Lambda %start main -%type main +%type 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 } diff --git a/types.ml b/types.ml index 60ca23d..e48f838 100644 --- a/types.ml +++ b/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