5 (************ Syntax ************************************)
9 (* Var n = n-th De Bruijn index, 0-based *)
18 type 'nf i_var_ = [ `I of int * 'nf Listx.listx | `Var of int ]
19 type 'nf i_n_var_ = [ `N of int | 'nf i_var_ ]
20 type 'nf i_num_var_ = [
22 | `Match of 'nf i_num_var_ * (*lift*) int * (*branches*)(int * 'nf) list ref * (*args*)'nf list
24 type 'nf nf_ = [ `Lam of (* was_unpacked *) bool * 'nf nf_ | 'nf i_num_var_ ]
26 type i_var = nf i_var_;;
27 type i_n_var = nf i_n_var_;;
28 type i_num_var = nf i_num_var_;;
40 | `Match _ -> assert false
43 let rec aux_i_num_var l =
45 `I(n,args) -> (`I((if n < l then n else n+m),Listx.map (aux l) args) : i_num_var)
46 | `Var n -> `Var (if n < l then n else n+m)
48 | `Match(t,lift,bs,args) ->
49 `Match(aux_i_num_var l t, lift + m, bs, List.map (aux l) args)
52 #i_num_var as x -> (aux_i_num_var l x :> nf)
53 | `Lam(b,nf) -> `Lam (b,aux (l+1) nf)
58 (* put t under n lambdas, lifting t accordingtly *)
62 | n when n > 0 -> `Lam (false,lift 1 (make_lams t (n-1)))
66 let rec aux n = function
68 | `Var x -> if x < n then [] else [x-n]
70 (if x < n then [] else [x-n]) @
71 List.concat (List.map (aux n) (Listx.to_list args))
72 | `Lam(_,t) -> aux (n+1) t
73 | `Match(t,liftno,bs,args) ->
75 List.concat (List.map (fun (_,t) -> aux (n-liftno) t) !bs) @
76 List.concat (List.map (aux n) args)
83 let rec t_of_i_num_var =
85 | `N n -> Scott.mk_n n
87 | `Match(t,liftno,bs,args) ->
88 let bs = List.map (fun (n,t) -> n, t_of_nf (lift liftno t)) !bs in
89 let t = t_of_i_num_var t in
90 let m = Scott.mk_match t bs in
91 List.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) m args
92 | `I(v, args) -> Listx.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) (Pure.V v) args
95 | #i_num_var as x -> t_of_i_num_var x
96 | `Lam(b,f) -> Pure.L (t_of_nf f)
101 (************ Pretty-printing ************************************)
103 let rec print ?(l=[]) =
105 `Var n -> print_name l n
106 | `N n -> string_of_int n
107 | `Match(t,bs_lift,bs,args) ->
108 "([" ^ print ~l (t :> nf) ^
109 " ? " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ print ~l (lift bs_lift t)) !bs) ^ "] " ^
110 String.concat " " (List.map (print ~l) args) ^ ")"
111 | `I(n,args) -> "(" ^ print_name l n ^ " " ^ String.concat " " (Listx.to_list (Listx.map (print ~l) args)) ^ ")"
113 let name = string_of_var (List.length l) in
114 "λ" ^ name ^ "." ^ print ~l:(name::l) (nf : nf)
117 let rec string_of_term l =
118 let rec string_of_term_w_pars l = function
119 | `Var n -> print_name l n
120 | `N n -> string_of_int n
121 | `I _ as t -> "(" ^ string_of_term_no_pars_app l (t :> nf) ^ ")"
122 | `Lam _ as t -> "(" ^ string_of_term_no_pars_lam l t ^ ")"
123 | `Match(t,bs_lift,bs,args) ->
124 "(match " ^ string_of_term_no_pars l (t :> nf) ^
125 " with " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ string_of_term l (lift bs_lift t)) !bs) ^ "] " ^
126 String.concat " " (List.map (string_of_term l) args) ^ ")"
127 and string_of_term_no_pars_app l = function
128 | `I(n, args) -> print_name l n ^ " " ^ String.concat " " (List.map (string_of_term_w_pars l) (Listx.to_list args))
129 | #nf as t -> string_of_term_w_pars l t
130 and string_of_term_no_pars_lam l = function
131 | `Lam(_,t) -> let name = string_of_var (List.length l) in
132 "λ" ^ name ^ ". " ^ (string_of_term_no_pars_lam (name::l) t)
133 | _ as t -> string_of_term_no_pars l t
134 and string_of_term_no_pars l : nf -> string = function
135 | `Lam _ as t -> string_of_term_no_pars_lam l t
136 | #nf as t -> string_of_term_no_pars_app l t
137 in string_of_term_no_pars l
140 let print ?(l=[]) = string_of_term l;;
142 (************ Hereditary substitutions ************************************)
146 #i_var as y -> (y : i_var)
148 prerr_endline (print (t :> nf));
149 assert false (* algorithm failed *)
151 let cast_to_i_n_var =
153 #i_n_var as y -> (y : i_n_var)
155 prerr_endline (print (t :> nf));
156 assert false (* algorithm failed *)
158 let cast_to_i_num_var =
160 #i_num_var as y -> (y : i_num_var)
162 prerr_endline (print (t :> nf));
163 assert false (* algorithm failed *)
165 let rec mk_app (h : nf) (arg : nf) =
168 `I(n,args) -> `I(n,Listx.append (Listx.Nil arg) args)
169 | `Var n -> `I(n, Listx.Nil arg)
170 | `Lam(_,nf) -> subst true 0 arg (nf : nf)
171 | `Match(t,lift,bs,args) -> `Match(t,lift,bs,List.append args [arg])
172 | `N _ -> assert false (* Numbers cannot be applied *)
173 (*in let l = ["v0";"v1";"v2"] in
174 prerr_endline ("mk_app h:" ^ print ~l h ^ " arg:" ^ print ~l:l arg ^ " res:" ^ print ~l:l res); res*)
177 (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
178 List.fold_left mk_app h args
180 and mk_appx h args = Listx.fold_left mk_app h args
182 and mk_match t bs_lift bs args =
183 (*prerr_endline ("MK_MATCH: ([" ^ print t ^ "] " ^ String.concat " " (Listx.to_list (Listx.map (fun (n,t) -> string_of_int n ^ " => " ^ print t) bs)) ^ ") " ^ String.concat " " (List.map print args));*)
187 let h = List.assoc m !bs in
188 let h = lift bs_lift h in
191 `Match (t,bs_lift,bs,args))
192 | `I _ | `Var _ | `Match _ -> `Match(t,bs_lift,bs,args)
194 and subst delift_by_one what (with_what : nf) (where : nf) =
195 let rec aux_i_num_var l =
199 mk_appx (lift l with_what) (Listx.map (aux l) args)
201 `I ((if delift_by_one && n >= l then n-1 else n), Listx.map (aux l) args)
206 `Var (if delift_by_one && n >= l then n-1 else n)
208 | `Match(t,bs_lift,bs,args) ->
209 let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
210 let l' = l - bs_lift in
211 let with_what' = lift l' with_what in
212 (* The following line should be the identity when delift_by_one = true because we
213 are assuming the ts to not contain lambda-bound variables. *)
214 bs := List.map (fun (n,t) -> n,subst false what with_what' t) !bs ;
215 mk_match (cast_to_i_num_var (aux_i_num_var l t)) bs_lift bs (List.map (aux l) args)
217 (*function iii -> let res = match iii with*)
219 | #i_num_var as x -> aux_i_num_var l x
220 | `Lam(b,nf) -> `Lam(b,aux (l+1) nf)
221 (*in let ll = ["v0";"v1";"v2"] in
222 prerr_endline ("subst l:" ^ string_of_int l ^ " delift_by_one:" ^ string_of_bool delift_by_one ^ " what:" ^ (List.nth ll what) ^ " with_what:" ^ print ~l:ll with_what ^ " where:" ^ print ~l:ll iii ^ " res:" ^ print ~l:ll res); res*)
227 (************ Parsing ************************************)
230 let rec aux = function
231 | Parser.Lam t -> `Lam (true,aux t)
232 | Parser.App (t1, t2) -> mk_app (aux t1) (aux t2)
233 | Parser.Var v -> `Var v
234 in let (tms, free) = Parser.parse_many strs
235 in (List.map aux tms, free)
238 (************** Algorithm(s) ************************)
240 let eta_compare x y =
241 (* let clex a b = let diff = ? a b in if diff = 0 then cont () else 0 in *)
242 let clex aux1 aux2 (a1,a2) (b1,b2) =
243 let diff = aux1 a1 b1 in if diff = 0 then aux2 a2 b2 else diff in
244 let rec lex aux l1 l2 =
249 | x::xs, y::ys -> clex aux (lex aux) (x,xs) (y,ys) in
250 let rec aux t1 t2 = match t1, t2 with
251 | `Var n , `Var m -> compare n m
252 | `I(n1, l1), `I(n2, l2) ->
253 clex compare (lex aux) (n1, Listx.to_list l1) (n2, Listx.to_list l2)
256 | `Lam(_,t1), `Lam(_,t2) -> aux t1 t2
257 | `Lam(_,t1), t2 -> - aux t1 (mk_app (lift 1 t2) (`Var 0))
258 | t2, `Lam(_,t1) -> aux t1 (mk_app (lift 1 t2) (`Var 0))
259 | `N n1, `N n2 -> compare n1 n2
260 | `Match(u,bs_lift,bs,args), `Match(u',bs_lift',bs',args') ->
261 let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
262 let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
263 clex aux (clex (lex (clex compare aux)) (lex aux)) ((u :> nf), (bs, args)) ((u' :> nf), (bs', args'))
273 let eta_eq (#nf as x) (#nf as y) = 0 = eta_compare x y ;;
275 let rec eta_subterm sub t =
276 if eta_eq sub t then true else
278 | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
279 | `Match(u,liftno,bs,args) ->
280 eta_subterm sub (u :> nf)
281 || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
282 || List.exists (eta_subterm sub) args
283 | `I(v, args) -> List.exists (eta_subterm sub) (Listx.to_list args) || (match sub with
285 | `I(v', args') -> v = v'
286 && Listx.length args' < Listx.length args
287 && List.for_all (fun (x,y) -> eta_eq x y) (List.combine (Util.take (Listx.length args') (Listx.to_list args)) (Listx.to_list args'))
290 | `N _ | `Var _ -> false
293 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;
295 let compute_special_k tms =
296 let rec aux k (t: nf) = Pervasives.max k (match t with
297 | `Lam(b,t) -> aux (k + if b then 1 else 0) t
298 | `I(n, tms) -> Listx.max (Listx.map (aux 0) tms)
299 | `Match(t, liftno, bs, args) ->
300 List.fold_left max 0 (List.map (aux 0) ((t :> nf)::args@List.map snd !bs))
303 ) in Listx.max (Listx.map (aux 0) tms)