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_ | `Bomb | `Pacman | '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 _ | `Bomb -> 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)
60 (* put t under n lambdas, lifting t accordingtly *)
64 | n when n > 0 -> `Lam (false,lift 1 (make_lams t (n-1)))
68 let rec aux n = function
70 | `Var x -> if x < n then [] else [x-n]
72 (if x < n then [] else [x-n]) @
73 List.concat (List.map (aux n) (Listx.to_list args))
74 | `Lam(_,t) -> aux (n+1) t
75 | `Match(t,liftno,bs,args) ->
77 List.concat (List.map (fun (_,t) -> aux (n-liftno) t) !bs) @
78 List.concat (List.map (aux n) args)
79 | `Bomb | `Pacman -> []
86 let rec t_of_i_num_var =
88 | `N n -> Scott.mk_n n
90 | `Match(t,liftno,bs,args) ->
91 let bs = List.map (fun (n,t) -> n, t_of_nf (lift liftno t)) !bs in
92 let t = t_of_i_num_var t in
93 let m = Scott.mk_match t bs in
94 List.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) m args
95 | `I(v, args) -> Listx.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) (Pure.V v) args
98 | #i_num_var as x -> t_of_i_num_var x
99 | `Lam(b,f) -> Pure.L (t_of_nf f)
100 | `Bomb -> let f x = Pure.A (x,x) in f (Pure.L (f (Pure.V 0)))
101 | `Pacman -> let f x = Pure.A (x,x) in f (Pure.L (Pure.L (f (Pure.V 0))))
106 (************ Pretty-printing ************************************)
108 (* let rec print ?(l=[]) =
110 `Var n -> print_name l n
111 | `N n -> string_of_int n
112 | `Match(t,bs_lift,bs,args) ->
113 "([" ^ print ~l (t :> nf) ^
114 " ? " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ print ~l (lift bs_lift t)) !bs) ^ "] " ^
115 String.concat " " (List.map (print ~l) args) ^ ")"
116 | `I(n,args) -> "(" ^ print_name l n ^ " " ^ String.concat " " (Listx.to_list (Listx.map (print ~l) args)) ^ ")"
118 let name = string_of_var (List.length l) in
119 "λ" ^ name ^ "." ^ print ~l:(name::l) (nf : nf)
122 let rec string_of_term l =
123 let rec string_of_term_w_pars l = function
124 | `Var n -> print_name l n
125 | `N n -> string_of_int n
126 | `I _ as t -> "(" ^ string_of_term_no_pars_app l (t :> nf) ^ ")"
127 | `Lam _ as t -> "(" ^ string_of_term_no_pars_lam l t ^ ")"
128 | `Match(t,bs_lift,bs,args) ->
129 "(match " ^ string_of_term_no_pars l (t :> nf) ^
130 " with " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ string_of_term l (lift bs_lift t)) !bs) ^ "] " ^
131 String.concat " " (List.map (string_of_term l) args) ^ ")"
134 and string_of_term_no_pars_app l = function
135 | `I(n, args) -> print_name l n ^ " " ^ String.concat " " (List.map (string_of_term_w_pars l) (Listx.to_list args))
136 | #nf as t -> string_of_term_w_pars l t
137 and string_of_term_no_pars_lam l = function
138 | `Lam(_,t) -> let name = string_of_var (List.length l) in
139 "λ" ^ name ^ ". " ^ (string_of_term_no_pars_lam (name::l) t)
140 | _ as t -> string_of_term_no_pars l t
141 and string_of_term_no_pars l : nf -> string = function
142 | `Lam _ as t -> string_of_term_no_pars_lam l t
143 | #nf as t -> string_of_term_no_pars_app l t
144 in string_of_term_no_pars l
147 let rec print ?(l=[]) = string_of_term l;;
149 (************ Hereditary substitutions ************************************)
153 #i_var as y -> (y : i_var)
155 prerr_endline (print (t :> nf));
156 assert false (* algorithm failed *)
158 let cast_to_i_n_var =
160 #i_n_var as y -> (y : i_n_var)
162 prerr_endline (print (t :> nf));
163 assert false (* algorithm failed *)
165 let cast_to_i_num_var =
167 #i_num_var as y -> (y : i_num_var)
169 prerr_endline (print (t :> nf));
170 assert false (* algorithm failed *)
172 let rec mk_app (h : nf) (arg : nf) =
175 `I(n,args) -> `I(n,Listx.append (Listx.Nil arg) args)
176 | `Var n -> `I(n, Listx.Nil arg)
177 | `Lam(_,nf) -> subst true 0 arg (nf : nf)
178 | `Match(t,lift,bs,args) -> `Match(t,lift,bs,List.append args [arg])
179 | `N _ -> assert false (* Numbers cannot be applied *)
180 | `Bomb | `Pacman -> failwith "mk_app su bomba o pacman"
181 (*in let l = ["v0";"v1";"v2"] in
182 prerr_endline ("mk_app h:" ^ print ~l h ^ " arg:" ^ print ~l:l arg ^ " res:" ^ print ~l:l res); res*)
185 (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
186 List.fold_left mk_app h args
188 and mk_appx h args = Listx.fold_left mk_app h args
190 and mk_match t bs_lift bs args =
191 (*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));*)
195 let h = List.assoc m !bs in
196 let h = lift bs_lift h in
199 `Match (t,bs_lift,bs,args))
200 | `I _ | `Var _ | `Match _ -> `Match(t,bs_lift,bs,args)
202 and subst delift_by_one what (with_what : nf) (where : nf) =
203 let rec aux_i_num_var l =
207 mk_appx (lift l with_what) (Listx.map (aux l) args)
209 `I ((if delift_by_one && n >= l then n-1 else n), Listx.map (aux l) args)
214 `Var (if delift_by_one && n >= l then n-1 else n)
216 | `Match(t,bs_lift,bs,args) ->
217 let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
218 let l' = l - bs_lift in
219 let with_what' = lift l' with_what in
220 (* The following line should be the identity when delift_by_one = true because we
221 are assuming the ts to not contain lambda-bound variables. *)
222 bs := List.map (fun (n,t) -> n,subst false what with_what' t) !bs ;
223 mk_match (cast_to_i_num_var (aux_i_num_var l t)) bs_lift bs (List.map (aux l) args)
225 (*function iii -> let res = match iii with*)
227 | #i_num_var as x -> aux_i_num_var l x
228 | `Lam(b,nf) -> `Lam(b,aux (l+1) nf)
231 (*in let ll = ["v0";"v1";"v2"] in
232 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*)
237 (************ Parsing ************************************)
240 let rec aux = function
241 | Parser.Lam t -> `Lam (true,aux t)
242 | Parser.App (t1, t2) -> mk_app (aux t1) (aux t2)
243 | Parser.Var v -> `Var v
244 in let (tms, free) = Parser.parse_many strs
245 in (List.map aux tms, free)
248 (************** Algorithm(s) ************************)
251 let rec aux = function
252 | `Var n , `Var m -> n = m
253 | `I(n1, l1), `I(n2, l2) ->
254 n1 = n2 && Listx.length l1 = Listx.length l2 &&
255 List.for_all aux (List.combine (Listx.to_list l1) (Listx.to_list l2))
256 | `Lam(_,t1), `Lam(_,t2) -> aux (t1,t2)
258 | t2, `Lam(_,t1) -> aux( t1, (mk_app (lift 1 t2) (`Var 0)) )
259 | `N n1, `N n2 -> n1 = n2
260 | `Match(u,bs_lift,bs,args), `Match(u',bs_lift',bs',args') ->
261 aux ((u :> nf), (u' :> nf)) && List.length !bs = List.length !bs' &&
262 let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
263 let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
264 List.for_all (fun ((n,t),(n',t')) -> n = n' && aux (t, t')) (List.combine bs bs') && List.length args = List.length args' &&
265 List.for_all aux (List.combine args args')
270 let eta_compare x y =
271 if eta_eq x y then 0 else compare x y
274 let eta_eq (#nf as x) (#nf as y) = eta_eq x y;;
276 let rec eta_subterm sub t =
277 if eta_eq sub t then true else
279 | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
280 | `Match(u,liftno,bs,args) ->
281 eta_subterm sub (u :> nf)
282 || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
283 || List.exists (eta_subterm sub) args
284 | `I(v, args) -> List.exists (eta_subterm sub) (Listx.to_list args) || (match sub with
286 | `I(v', args') -> v = v'
287 && Listx.length args' < Listx.length args
288 && 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'))
291 | `N _ | `Var _ | `Bomb | `Pacman -> false
294 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;