5 (************ Syntax ************************************)
9 (* Var n = n-th De Bruijn index, 0-based *)
18 type var = int * (* arity of variable*) int;;
19 type 'nf i_var_ = [ `I of var * 'nf Listx.listx | `Var of var ]
20 type 'nf i_n_var_ = [ `N of int | 'nf i_var_ ]
21 type 'nf i_num_var_ = [
23 | `Match of 'nf i_num_var_ * (* originating var *) var * (*lift*) int * (*branches*)(int * 'nf) list ref * (*args*)'nf list
25 type 'nf nf_ = [ `Lam of (* was_unpacked *) bool * 'nf nf_ | 'nf i_num_var_ ]
27 type i_var = nf i_var_;;
28 type i_n_var = nf i_n_var_;;
29 type i_num_var = nf i_num_var_;;
41 | `Match _ -> assert false
50 let aux_var l (n, ar) = (if n < l then n else n+m), ar in
51 let rec aux_i_num_var l =
53 `I(v,args) -> `I(aux_var l v, Listx.map (aux l) args)
54 | `Var v -> `Var(aux_var l v)
56 | `Match(t,v,lift,bs,args) ->
57 `Match(aux_i_num_var l t, v, lift + m, bs, List.map (aux l) args)
60 #i_num_var as x -> (aux_i_num_var l x :> nf)
61 | `Lam(b,nf) -> `Lam (b, aux (l+1) nf)
66 (* put t under n lambdas, lifting t accordingtly *)
70 | n when n > 0 -> `Lam (false, lift 1 (make_lams t (n-1)))
74 let rec aux n = function
76 | `Var(x,_) -> if x < n then [] else [x-n]
78 (if x < n then [] else [x-n]) @
79 List.concat (List.map (aux n) (Listx.to_list args))
80 | `Lam(_,t) -> aux (n+1) t
81 | `Match(t,_,liftno,bs,args) ->
83 List.concat (List.map (fun (_,t) -> aux (n-liftno) t) !bs) @
84 List.concat (List.map (aux n) args)
91 let rec t_of_i_num_var =
93 | `N n -> Scott.mk_n n
94 | `Var(v,_) -> Pure.V v
95 | `Match(t,_,liftno,bs,args) ->
96 let bs = List.map (fun (n,t) -> n, t_of_nf (lift liftno t)) !bs in
97 let t = t_of_i_num_var t in
98 let m = Scott.mk_match t bs in
99 List.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) m args
100 | `I((v,_), args) -> Listx.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) (Pure.V v) args
103 | #i_num_var as x -> t_of_i_num_var x
104 | `Lam(b,f) -> Pure.L (t_of_nf f)
109 (************ Pretty-printing ************************************)
111 let rec string_of_term l =
112 let rec string_of_term_w_pars l = function
113 | `Var(n,ar) -> print_name l n ^ ":" ^ string_of_int ar
114 | `N n -> string_of_int n
115 | `I _ as t -> "(" ^ string_of_term_no_pars_app l (t :> nf) ^ ")"
116 | `Lam _ as t -> "(" ^ string_of_term_no_pars_lam l t ^ ")"
117 | `Match(t,_,bs_lift,bs,args) ->
118 "(match " ^ string_of_term_no_pars l (t :> nf) ^
119 " with " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ string_of_term l (lift bs_lift t)) !bs) ^ "] " ^
120 String.concat " " (List.map (string_of_term l) args) ^ ")"
121 and string_of_term_no_pars_app l = function
122 | `I((n,ar), args) -> print_name l n ^ ":" ^ string_of_int ar ^ " " ^ String.concat " " (List.map (string_of_term_w_pars l) (Listx.to_list args))
123 | #nf as t -> string_of_term_w_pars l t
124 and string_of_term_no_pars_lam l = function
125 | `Lam(_,t) -> let name = string_of_var (List.length l) in
126 "λ" ^ name ^ ". " ^ (string_of_term_no_pars_lam (name::l) t)
127 | _ as t -> string_of_term_no_pars l t
128 and string_of_term_no_pars l : nf -> string = function
129 | `Lam _ as t -> string_of_term_no_pars_lam l t
130 | #nf as t -> string_of_term_no_pars_app l t
131 in string_of_term_no_pars l
134 let print ?(l=[]) = string_of_term l;;
135 let string_of_nf t = string_of_term [] (t:>nf);;
137 (************ Hereditary substitutions ************************************)
141 #i_var as y -> (y : i_var)
143 prerr_endline (print (t :> nf));
144 assert false (* algorithm failed *)
146 let cast_to_i_n_var =
148 #i_n_var as y -> (y : i_n_var)
150 prerr_endline (print (t :> nf));
151 assert false (* algorithm failed *)
153 let cast_to_i_num_var =
155 #i_num_var as y -> (y : i_num_var)
157 prerr_endline (print (t :> nf));
158 assert false (* algorithm failed *)
160 let rec mk_app (h : nf) (arg : nf) =
163 `I(v,args) -> `I(v,Listx.append (Listx.Nil arg) args)
164 | `Var v -> `I(v, Listx.Nil arg)
165 | `Lam(_,nf) -> subst true 0 arg (nf : nf) (* AC FIXME sanity check on arity *)
166 | `Match(t,v,lift,bs,args) -> `Match(t,v,lift,bs,List.append args [arg])
167 | `N _ -> assert false (* Numbers cannot be applied *)
168 (*in let l = ["v0";"v1";"v2"] in
169 prerr_endline ("mk_app h:" ^ print ~l h ^ " arg:" ^ print ~l:l arg ^ " res:" ^ print ~l:l res); res*)
172 (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
173 List.fold_left mk_app h args
175 and mk_appx h args = Listx.fold_left mk_app h args
177 and mk_match t ar bs_lift bs args =
178 (*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));*)
182 let h = List.assoc m !bs in
183 let h = lift bs_lift h in
186 `Match (t,ar,bs_lift,bs,args))
187 | `I _ | `Var _ | `Match _ -> `Match(t,ar,bs_lift,bs,args)
189 and subst delift_by_one what (with_what : nf) (where : nf) =
190 let rec aux_i_num_var l =
194 mk_appx (lift l with_what) (Listx.map (aux l) args)
196 `I (((if delift_by_one && n >= l then n-1 else n), ar), Listx.map (aux l) args)
201 `Var((if delift_by_one && n >= l then n-1 else n), ar)
203 | `Match(t,v,bs_lift,bs,args) ->
204 let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
205 let l' = l - bs_lift in
206 let with_what' = lift l' with_what in
207 (* The following line should be the identity when delift_by_one = true because we
208 are assuming the ts to not contain lambda-bound variables. *)
209 bs := List.map (fun (n,t) -> n,subst false what with_what' t) !bs ;
210 mk_match (cast_to_i_num_var (aux_i_num_var l t)) v bs_lift bs (List.map (aux l) args)
212 (*function iii -> let res = match iii with*)
214 | #i_num_var as x -> aux_i_num_var l x
215 | `Lam(b, nf) -> `Lam(b, aux (l+1) nf)
216 (*in let ll = ["v0";"v1";"v2"] in
217 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*)
222 (************ Parsing ************************************)
225 let fix_arity = function
226 | `I((n,_),args) -> `I((n,Listx.length args),args)
227 | _ -> assert false in
228 let rec aux = function
229 | Parser.Lam t -> `Lam (true, aux t)
230 | Parser.App (t1, t2) -> fix_arity (mk_app (aux t1) (aux t2))
231 | Parser.Var v -> `Var(v,0) in
232 let (tms, free) = Parser.parse_many strs in
233 List.map aux tms, free
236 (************** Algorithm(s) ************************)
238 let eta_compare x y =
239 (* let clex a b = let diff = ? a b in if diff = 0 then cont () else 0 in *)
240 let clex aux1 aux2 (a1,a2) (b1,b2) =
241 let diff = aux1 a1 b1 in if diff = 0 then aux2 a2 b2 else diff in
242 let rec lex aux l1 l2 =
247 | x::xs, y::ys -> clex aux (lex aux) (x,xs) (y,ys) in
248 let rec aux t1 t2 = match t1, t2 with
249 | `Var(n,_) , `Var(m,_) -> compare n m
250 | `I((n1,_), l1), `I((n2,_), l2) ->
251 clex compare (lex aux) (n1, Listx.to_list l1) (n2, Listx.to_list l2)
254 | `Lam(_,t1), `Lam(_,t2) -> aux t1 t2
255 | `Lam(_,t1), t2 -> - aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
256 | t2, `Lam(_,t1) -> aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
257 | `N n1, `N n2 -> compare n1 n2
258 | `Match(u,_,bs_lift,bs,args), `Match(u',_,bs_lift',bs',args') ->
259 let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
260 let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
261 clex aux (clex (lex (clex compare aux)) (lex aux)) ((u :> nf), (bs, args)) ((u' :> nf), (bs', args'))
271 let eta_eq (#nf as x) (#nf as y) = 0 = eta_compare x y ;;
273 let rec eta_subterm sub t =
274 if eta_eq sub t then true else
276 | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
277 | `Match(u,ar,liftno,bs,args) ->
278 eta_subterm sub (u :> nf)
279 || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
280 || List.exists (eta_subterm sub) args
281 | `I(v, args) -> List.exists (eta_subterm sub) (Listx.to_list args) || (match sub with
283 | `I(v', args') -> v = v'
284 && Listx.length args' < Listx.length args
285 && 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'))
288 | `N _ | `Var _ -> false
291 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;
294 let max_arity_tms n =
295 let aux_var l (m,a) = if n + l = m then a else -1 in
296 let rec aux l = function
297 | `Var v -> aux_var l v
298 | `I(v,tms) -> max (aux_var l v) (aux_tms l (Listx.to_list tms))
299 | `Lam(_,t) -> aux (l+1) t
300 | `Match(u,_,_,bs,args) -> max (max (aux l (u :> nf)) (aux_tms l args)) (aux_tms l (List.map snd !bs))
303 List.fold_left (fun acc t -> Pervasives.max acc (aux l t)) ~-1 in
304 fun tms -> aux_tms 0 (tms :> nf list)