| 1 |
open Location |
open Location |
| 2 |
|
open Ident |
| 3 |
|
|
| 4 |
|
let quiet = ref false |
| 5 |
|
|
| 6 |
let typing_env = State.ref "Cduce.typing_env" Typer.Env.empty |
let typing_env = State.ref "Cduce.typing_env" Typer.Env.empty |
| 7 |
let glb_env = State.ref "Cduce.glb_env" Typer.Env.empty |
let glb_env = State.ref "Cduce.glb_env" Typer.TypeEnv.empty |
| 8 |
let eval_env = Eval.global_env |
let eval_env = Eval.global_env |
| 9 |
|
|
| 10 |
let print_norm ppf d = |
let print_norm ppf d = |
| 16 |
|
|
| 17 |
let dump_env ppf = |
let dump_env ppf = |
| 18 |
Format.fprintf ppf "Global types:"; |
Format.fprintf ppf "Global types:"; |
| 19 |
Typer.Env.iter (fun x _ -> Format.fprintf ppf " %s" x) !glb_env; |
Typer.TypeEnv.iter (fun x _ -> Format.fprintf ppf " %s" x) !glb_env; |
| 20 |
Format.fprintf ppf ".@\n"; |
Format.fprintf ppf ".@\n"; |
| 21 |
Eval.Env.iter |
Eval.Env.iter |
| 22 |
(fun x v -> |
(fun x v -> |
| 23 |
let t = Typer.Env.find x !typing_env in |
let t = Typer.Env.find x !typing_env in |
| 24 |
Format.fprintf ppf "@[|- %s : %a@ => %a@]@\n" |
Format.fprintf ppf "@[|- %s : %a@ => %a@]@\n" |
| 25 |
x |
(Id.value x) |
| 26 |
print_norm t |
print_norm t |
| 27 |
print_value v |
print_value v |
| 28 |
) |
) |
| 39 |
print_value v |
print_value v |
| 40 |
| Typer.WrongLabel (t,l) -> |
| Typer.WrongLabel (t,l) -> |
| 41 |
Format.fprintf ppf "Wrong record selection: the label %s@\n" |
Format.fprintf ppf "Wrong record selection: the label %s@\n" |
| 42 |
(Types.LabelPool.value l); |
(LabelPool.value l); |
| 43 |
Format.fprintf ppf "applied to an expression of type %a@\n" |
Format.fprintf ppf "applied to an expression of type %a@\n" |
| 44 |
print_norm t |
print_norm t |
|
| Typer.MultipleLabel l -> |
|
|
Format.fprintf ppf "Multiple occurences for the record label %s@\n" |
|
|
(Types.LabelPool.value l); |
|
| 45 |
| Typer.ShouldHave (t,msg) -> |
| Typer.ShouldHave (t,msg) -> |
| 46 |
Format.fprintf ppf "This expression should have type %a@\n%s@\n" |
Format.fprintf ppf "This expression should have type %a@\n%s@\n" |
| 47 |
print_norm t |
print_norm t |
| 75 |
| Location.Generic s -> |
| Location.Generic s -> |
| 76 |
Format.fprintf ppf "%s@\n" s |
Format.fprintf ppf "%s@\n" s |
| 77 |
| exn -> |
| exn -> |
| 78 |
|
raise exn |
| 79 |
|
(* |
| 80 |
Format.fprintf ppf "%s@\n" (Printexc.to_string exn) |
Format.fprintf ppf "%s@\n" (Printexc.to_string exn) |
| 81 |
|
*) |
| 82 |
|
|
| 83 |
let debug ppf = function |
let debug ppf = function |
| 84 |
|
| `Subtype (t1,t2) -> |
| 85 |
|
Format.fprintf ppf "[DEBUG:subtype]@\n"; |
| 86 |
|
let t1 = Types.descr (Typer.typ !glb_env t1) |
| 87 |
|
and t2 = Types.descr (Typer.typ !glb_env t2) in |
| 88 |
|
Format.fprintf ppf "%a <= %a : %b@\n" print_norm t1 print_norm t2 |
| 89 |
|
(Types.subtype t1 t2) |
| 90 |
| `Filter (t,p) -> |
| `Filter (t,p) -> |
| 91 |
Format.fprintf ppf "[DEBUG:filter]@\n"; |
Format.fprintf ppf "[DEBUG:filter]@\n"; |
| 92 |
let t = Typer.typ !glb_env t |
let t = Typer.typ !glb_env t |
| 93 |
and p = Typer.pat !glb_env p in |
and p = Typer.pat !glb_env p in |
| 94 |
let f = Patterns.filter (Types.descr t) p in |
let f = Patterns.filter (Types.descr t) p in |
| 95 |
List.iter (fun (x,t) -> |
List.iter (fun (x,t) -> |
| 96 |
Format.fprintf ppf " %s:%a@\n" x |
Format.fprintf ppf " %s:%a@\n" (Id.value x) |
| 97 |
print_norm (Types.descr t)) f |
print_norm (Types.descr t)) f |
| 98 |
|
| `Compile2 (t,pl) -> |
| 99 |
|
Format.fprintf ppf "[DEBUG:compile2]@\n"; |
| 100 |
|
(* let t = Types.descr (Typer.typ !glb_env t) in |
| 101 |
|
let pl = List.map (fun p -> |
| 102 |
|
let p = Typer.pat !glb_env p in |
| 103 |
|
let a = Types.descr (Patterns.accept p) in |
| 104 |
|
(Some p, Types.cap a t)) pl in |
| 105 |
|
let d = Patterns.Compiler.make_dispatcher t pl in |
| 106 |
|
Patterns.Compiler.print_disp ppf d *) |
| 107 |
|
() |
| 108 |
|
|
| 109 |
| `Accept p -> |
| `Accept p -> |
| 110 |
Format.fprintf ppf "[DEBUG:accept]@\n"; |
Format.fprintf ppf "[DEBUG:accept]@\n"; |
| 111 |
let p = Typer.pat !glb_env p in |
let p = Typer.pat !glb_env p in |
| 115 |
Format.fprintf ppf "[DEBUG:compile]@\n"; |
Format.fprintf ppf "[DEBUG:compile]@\n"; |
| 116 |
let t = Typer.typ !glb_env t |
let t = Typer.typ !glb_env t |
| 117 |
and pl = List.map (Typer.pat !glb_env) pl in |
and pl = List.map (Typer.pat !glb_env) pl in |
| 118 |
let pl = Array.of_list |
Patterns.Compile.debug_compile ppf t pl |
| 119 |
(List.map (fun p -> Patterns.Compile.normal |
| `Normal_record p -> assert false |
|
(Patterns.descr p)) pl) in |
|
|
Patterns.Compile.show ppf (Types.descr t) pl |
|
|
| `Normal_record t -> |
|
|
Format.fprintf ppf "[DEBUG:normal_record]@\n"; |
|
|
let t = Types.descr (Typer.typ !glb_env t) in |
|
|
let count = ref 0 and seen = ref [] in |
|
|
match Types.Record.first_label t with |
|
|
| `Empty -> Format.fprintf ppf "Empty" |
|
|
| `Any -> Format.fprintf ppf "Any" |
|
|
| `Label l -> |
|
|
let (pr,ab) = Types.Record.normal' t l in |
|
|
Format.fprintf ppf "Label (%s,@[" (Types.LabelPool.value l); |
|
|
List.iter (fun (d,n) -> |
|
|
Format.fprintf ppf "%a => @[%a@];@\n" |
|
|
Types.Print.print_descr d |
|
|
Types.Print.print_descr n |
|
|
) pr; |
|
|
Format.fprintf ppf "@] Absent: @[%a@])@\n" |
|
|
Types.Print.print_descr |
|
|
(match ab with Some x -> x | None -> Types.empty) |
|
|
(* |
|
|
| `Normal_record t -> |
|
|
Format.fprintf ppf "[DEBUG:normal_record]@\n"; |
|
|
let t = Types.descr (Typer.typ !glb_env t) in |
|
|
let r = Types.Record.normal t in |
|
|
let count = ref 0 and seen = ref [] in |
|
|
let rec aux ppf x = |
|
|
try |
|
|
let no = List.assq x !seen in |
|
|
Format.fprintf ppf "[[%i]]" no |
|
|
with Not_found -> |
|
|
incr count; |
|
|
seen := (x, !count) :: !seen; |
|
|
Format.fprintf ppf "[[%i]]:" !count; |
|
|
match x with |
|
|
| `Success -> Format.fprintf ppf "Success" |
|
|
| `Fail -> Format.fprintf ppf "Fail" |
|
|
| `Label (l,pr,ab) -> |
|
|
Format.fprintf ppf "Label (%s,@[" (Types.label_name l); |
|
|
List.iter (fun (d,n) -> |
|
|
Format.fprintf ppf "%a => @[%a@];@\n" |
|
|
Types.Print.print_descr d |
|
|
aux n |
|
|
) pr; |
|
|
Format.fprintf ppf "@] Absent: @[%a@])" aux ab |
|
|
in |
|
|
Format.fprintf ppf "%a@\n" aux r |
|
|
*) |
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 132 |
let insert_type_bindings = |
let insert_type_bindings = |
| 133 |
List.iter (fun (x,t) -> |
List.iter (fun (x,t) -> |
| 134 |
typing_env := Typer.Env.add x t !typing_env; |
typing_env := Typer.Env.add x t !typing_env; |
| 135 |
Format.fprintf ppf "|- %s : %a@\n@." x print_norm t) |
if not !quiet then |
| 136 |
|
Format.fprintf ppf "|- %s : %a@\n@." (Id.value x) print_norm t) |
| 137 |
in |
in |
| 138 |
|
|
| 139 |
let type_decl decl = |
let type_decl decl = |
| 145 |
List.iter |
List.iter |
| 146 |
(fun (x,v) -> |
(fun (x,v) -> |
| 147 |
Eval.enter_global x v; |
Eval.enter_global x v; |
| 148 |
Format.fprintf ppf "=> %s : @[%a@]@\n@." x print_value v |
if not !quiet then |
| 149 |
|
Format.fprintf ppf "=> %s : @[%a@]@\n@." (Id.value x) print_value v |
| 150 |
) bindings |
) bindings |
| 151 |
in |
in |
| 152 |
|
|
| 156 |
let (fv,e) = Typer.expr !glb_env e in |
let (fv,e) = Typer.expr !glb_env e in |
| 157 |
let t = Typer.type_check !typing_env e Types.any true in |
let t = Typer.type_check !typing_env e Types.any true in |
| 158 |
Location.dump_loc ppf e.Typed.exp_loc; |
Location.dump_loc ppf e.Typed.exp_loc; |
| 159 |
|
if not !quiet then |
| 160 |
Format.fprintf ppf "|- %a@\n@." print_norm t; |
Format.fprintf ppf "|- %a@\n@." print_norm t; |
| 161 |
let v = Eval.eval Eval.Env.empty e in |
let v = Eval.eval Eval.Env.empty e in |
| 162 |
|
if not !quiet then |
| 163 |
Format.fprintf ppf "=> @[%a@]@\n@." print_value v |
Format.fprintf ppf "=> @[%a@]@\n@." print_value v |
| 164 |
| Ast.LetDecl (p,{descr=Ast.Abstraction _}) -> () |
| Ast.LetDecl (p,{descr=Ast.Abstraction _}) -> () |
| 165 |
| Ast.LetDecl (p,e) -> |
| Ast.LetDecl (p,e) -> |