12 let rec print ?(l=[]) =
15 | A(t1,t2) -> "(" ^ print ~l t1 ^ " " ^ print ~l t2 ^ ")"
17 let name = string_of_var (List.length l) in
18 "λ" ^ name ^ "." ^ print ~l:(name::l) t
24 | V n -> V (if n < l then n else n+m)
25 | A (t1,t2) -> A (aux l t1, aux l t2)
26 | L t -> L (aux (l+1) t)
31 (* Reference implementation.
32 Reduction machine used below
33 let subst delift_by_one what with_what =
36 | A(t1,t2) -> A(aux l t1, aux l t2)
41 V (if delift_by_one && n >= l then n-1 else n)
42 | L t -> L (aux (l+1) t)
52 | L f -> whd (subst true 0 t2 f)
59 let unwind ?(tbl = Hashtbl.create 317) m =
60 let rec unwind (e,t,s) =
69 let s = List.map cache_unwind s in
72 | A(t1,t2) -> A(aux l t1, aux l t2)
73 | V n as x when n < l -> x
76 lift l (cache_unwind (List.nth e (n - l)))
77 with Failure _ -> V (n - l))
78 | L t -> L (aux (l+1) t)
81 List.fold_left (fun f a -> A(f,a)) t s
89 let t2' = aux (e,t2,[]) in
93 else aux (e,t1,t2'::s)
94 | (e,L t,x::s) -> aux (x::e,t,s)
97 let e,t,s' = List.nth e n in
100 | (e, B, _) -> (e, B, [])
101 | (_,L _,[]) as m -> m
113 if n = 0 then L (L (A (V 1, L (V 0)))) else L (L (A (V 0, mk_n (n-1))))
115 let dummy = V (max_int / 2)
118 let bs = List.sort (fun (n1,_) (n2,_) -> compare n1 n2) bs in
124 A (A (t, L (lift (m+1) p)), L (aux (m+1) (V 0) tl))
126 A (A (t, dummy), L (aux (m+1) (V 0) l))