Skip to content

Commit

Permalink
Merge pull request #42 from SoftwareFoundationGroupAtKyotoU/ldti_orig…
Browse files Browse the repository at this point in the history
…inal

tyenv_bug
  • Loading branch information
ymyzk authored Nov 18, 2024
2 parents e6bcf61 + e0bee77 commit e0bb51e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
6 changes: 3 additions & 3 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let rec read_eval_print lexbuf env tyenv =

(* Type inference *)
print_debug "***** Typing *****\n";
let tyenv, e, u = Typing.ITGL.type_of_program tyenv e in
let e, u = Typing.ITGL.type_of_program tyenv e in
print_debug "e: %a\n" Pp.ITGL.pp_program e;
print_debug "U: %a\n" Pp.pp_ty u;

Expand All @@ -29,7 +29,7 @@ let rec read_eval_print lexbuf env tyenv =

(* Translation *)
print_debug "***** Cast-insertion *****\n";
let tyenv, f, u' = Typing.ITGL.translate tyenv e in
let new_tyenv, f, u' = Typing.ITGL.translate tyenv e in
print_debug "f: %a\n" Pp.CC.pp_program f;
print_debug "U: %a\n" Pp.pp_ty u';
assert (Typing.is_equal u u');
Expand All @@ -43,7 +43,7 @@ let rec read_eval_print lexbuf env tyenv =
pp_print_string x
Pp.pp_ty2 u
Pp.CC.pp_value v;
read_eval_print lexbuf env tyenv
read_eval_print lexbuf env new_tyenv
with
| Failure message ->
print "Failure: %s\n" message;
Expand Down
6 changes: 3 additions & 3 deletions lib/stdlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ let env, tyenv =
List.fold_left
(fun (env, tyenv) str ->
let e = Parser.toplevel Lexer.main @@ Lexing.from_string str in
let tyenv, e, u = Typing.ITGL.type_of_program tyenv e in
let e, u = Typing.ITGL.type_of_program tyenv e in
let tyenv, e, _ = Typing.ITGL.normalize tyenv e u in
let tyenv, f, _ = Typing.ITGL.translate tyenv e in
let new_tyenv, f, _ = Typing.ITGL.translate tyenv e in
let _ = Typing.CC.type_of_program tyenv f in
let env, _, _ = Eval.eval_program env f in
env, tyenv)
env, new_tyenv)
(env, tyenv)
implementations

Expand Down
1 change: 1 addition & 0 deletions lib/syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ module CC = struct
| LetExp (r, _, _, _, _) -> r

let rec is_value = function
| Var _
| IConst _
| BConst _
| UConst _
Expand Down
7 changes: 3 additions & 4 deletions lib/typing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,10 @@ module ITGL = struct

let type_of_program env = function
| Exp e ->
env, Exp e, type_of_exp env e
Exp e, type_of_exp env e
| LetDecl (x, e) ->
let u = type_of_exp env e in
let xs = if is_value env e then closure_tyvars_let_decl e u env else [] in
let env = Environment.add x (TyScheme (xs, u)) env in
env, LetDecl (x, e), u
LetDecl (x, e), u

(* Normalize type variables *)

Expand Down Expand Up @@ -517,6 +515,7 @@ module ITGL = struct
env, CC.LetDecl (x, xs @ ys, f), u
| LetDecl (x, e) ->
let f, u = translate_exp env e in
let env = Environment.add x (tysc_of_ty u) env in
env, CC.LetDecl (x, [], f), u
end

Expand Down
2 changes: 1 addition & 1 deletion lib/typing.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ val tyarg_to_ty : Syntax.CC.tyarg -> ty
module ITGL : sig
open Syntax.ITGL

val type_of_program : tysc Environment.t -> program -> (tysc Environment.t * program * ty)
val type_of_program : tysc Environment.t -> program -> (program * ty)

val normalize : tysc Environment.t -> program -> ty -> (tysc Environment.t * program * ty)
val normalize_type : ty -> ty
Expand Down
12 changes: 9 additions & 3 deletions test/test_examples.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ let test_cases = [
"let dyn x = ((fun (y: 'b) -> y): ? -> ?) x", "'a -> ?", "<fun>";
"f (dyn 2) (dyn true)", "int", "0";
];
[
"let f = fun x -> x", "'a -> 'a", "<fun>";
"let f = fun x -> x f", "(('a -> 'a) -> 'b) -> 'b", "<fun>";
"f (fun x -> x) 4", "int", "4";
"f", "(('a -> 'a) -> 'b) -> 'b", "<fun>";
];
(* let-poly & recursion *)
["let rec fact n = if n <= 1 then 1 else n * fact (n - 1) in fact 5", "int", "120"];
["let rec fact (n:?) = if n <= 1 then 1 else n * fact (n - 1) in fact 5", "int", "120"];
Expand All @@ -176,15 +182,15 @@ let id x = x
let run env tyenv program =
let parse str = Parser.toplevel Lexer.main @@ Lexing.from_string str in
let e = parse @@ program ^ ";;" in
let tyenv, e, u = Typing.ITGL.type_of_program tyenv e in
let e, u = Typing.ITGL.type_of_program tyenv e in
let tyenv, e, u = Typing.ITGL.normalize tyenv e u in
let tyenv, f, u' = Typing.ITGL.translate tyenv e in
let new_tyenv, f, u' = Typing.ITGL.translate tyenv e in
assert (Typing.is_equal u u');
let u'' = Typing.CC.type_of_program tyenv f in
assert (Typing.is_equal u u'');
try
let env, _, v = Eval.eval_program env f in
env, tyenv, asprintf "%a" Pp.pp_ty2 u, asprintf "%a" Pp.CC.pp_value v
env, new_tyenv, asprintf "%a" Pp.pp_ty2 u, asprintf "%a" Pp.CC.pp_value v
with
| Eval.Blame (_, CC.Pos) -> env, tyenv, asprintf "%a" Pp.pp_ty2 u, "blame+"
| Eval.Blame (_, CC.Neg) -> env, tyenv, asprintf "%a" Pp.pp_ty2 u, "blame-"
Expand Down
2 changes: 1 addition & 1 deletion test/test_typing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module ITGL = struct
let test (program, expected) =
program >:: fun ctxt ->
let e = parse @@ program ^ ";;" in
let _, _, u = Typing.ITGL.type_of_program tyenv e in
let _, u = Typing.ITGL.type_of_program tyenv e in
let u = Typing.ITGL.normalize_type u in
assert_equal ~ctxt:ctxt ~printer:id expected @@ asprintf "%a" Pp.pp_ty2 u
in
Expand Down

0 comments on commit e0bb51e

Please sign in to comment.