6 let debug_display_arities = false;;
8 (************ Syntax ************************************)
12 (* Var n = n-th De Bruijn index, 0-based *)
21 type var = int * (* arity of variable*) int;;
22 type 'nf i_var_ = [ `I of var * 'nf Listx.listx | `Var of var ]
23 type 'nf i_n_var_ = [ `N of int | 'nf i_var_ ]
24 type 'nf i_num_var_ = [
26 | `Match of 'nf i_num_var_ * (* originating var *) var * (*lift*) int * (*branches*)(int * 'nf) list ref * (*args*)'nf list
28 type 'nf nf_ = [ `Lam of (* was_unpacked *) bool * 'nf nf_ | 'nf i_num_var_ ]
30 type i_var = nf i_var_;;
31 type i_n_var = nf i_n_var_;;
32 type i_num_var = nf i_num_var_;;
44 | `Match _ -> assert false
53 let aux_var l (n, ar) = (if n < l then n else n+m), ar in
54 let rec aux_i_num_var l =
56 `I(v,args) -> `I(aux_var l v, Listx.map (aux l) args)
57 | `Var v -> `Var(aux_var l v)
59 | `Match(t,v,lift,bs,args) ->
60 `Match(aux_i_num_var l t, v, lift + m, bs, List.map (aux l) args)
63 #i_num_var as x -> (aux_i_num_var l x :> nf)
64 | `Lam(b,nf) -> `Lam (b, aux (l+1) nf)
69 (* put t under n lambdas, lifting t accordingtly *)
73 | n when n > 0 -> `Lam (false, lift 1 (make_lams t (n-1)))
77 let rec aux n = function
79 | `Var(x,ar) -> if x < n then [] else [(x-n,ar)]
81 (if x < n then [] else [(x-n,ar)]) @
82 List.concat (List.map (aux n) (Listx.to_list args))
83 | `Lam(_,t) -> aux (n+1) t
84 | `Match(t,_,liftno,bs,args) ->
86 List.concat (List.map (fun (_,t) -> aux (n-liftno) t) !bs) @
87 List.concat (List.map (aux n) args)
90 let free_vars = (List.map fst) ++ free_vars';;
95 let rec t_of_i_num_var =
97 | `N n -> Scott.mk_n n
98 | `Var(v,_) -> Pure.V v
99 | `Match(t,_,liftno,bs,args) ->
100 let bs = List.map (fun (n,t) -> n, t_of_nf (lift liftno t)) !bs in
101 let t = t_of_i_num_var t in
102 let m = Scott.mk_match t bs in
103 List.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) m args
104 | `I((v,_), args) -> Listx.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) (Pure.V v) args
107 | #i_num_var as x -> t_of_i_num_var x
108 | `Lam(b,f) -> Pure.L (t_of_nf f)
113 (************ Pretty-printing ************************************)
115 let rec string_of_term l =
116 let rec string_of_term_w_pars l = function
117 | `Var(n,ar) -> print_name l n ^ ":" ^ string_of_int ar
118 | `N n -> string_of_int n
119 | `I _ as t -> "(" ^ string_of_term_no_pars_app l (t :> nf) ^ ")"
120 | `Lam _ as t -> "(" ^ string_of_term_no_pars_lam l t ^ ")"
121 | `Match(t,(v,ar),bs_lift,bs,args) ->
122 "[match("^ string_of_var v ^":"^ string_of_int ar ^") " ^ string_of_term_no_pars l (t :> nf) ^
123 " with " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ string_of_term l (lift bs_lift t)) !bs) ^ "] " ^
124 String.concat " " (List.map (string_of_term l) args) ^ ")"
125 and string_of_term_no_pars_app l = function
126 | `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))
127 | #nf as t -> string_of_term_w_pars l t
128 and string_of_term_no_pars_lam l = function
129 | `Lam(_,t) -> let name = string_of_var (List.length l) in
130 "λ" ^ name ^ ". " ^ (string_of_term_no_pars_lam (name::l) t)
131 | _ as t -> string_of_term_no_pars l t
132 and string_of_term_no_pars l : nf -> string = function
133 | `Lam _ as t -> string_of_term_no_pars_lam l t
134 | #nf as t -> string_of_term_no_pars_app l t
135 in string_of_term_no_pars l
138 let print ?(l=[]) = string_of_term l;;
139 let string_of_nf t = string_of_term [] (t:>nf);;
141 (************ Hereditary substitutions ************************************)
145 #i_var as y -> (y : i_var)
147 prerr_endline (print (t :> nf));
148 assert false (* algorithm failed *)
150 let cast_to_i_n_var =
152 #i_n_var as y -> (y : i_n_var)
154 prerr_endline (print (t :> nf));
155 assert false (* algorithm failed *)
157 let cast_to_i_num_var =
159 #i_num_var as y -> (y : i_num_var)
161 prerr_endline (print (t :> nf));
162 assert false (* algorithm failed *)
164 let rec set_arity arity = function
165 | `Var(n,_) -> `Var(n,arity)
167 | `Lam(false, t) -> `Lam(false, set_arity arity t)
168 | `Match(t,(n,_),bs_lift,bs,args) -> `Match(t,(n,arity),bs_lift,bs,args)
169 | `I _ | `Lam _ -> assert false
171 let minus1 n = if n = min_int then n else n - 1;;
173 let rec mk_app (h : nf) (arg : nf) =
176 `I(v,args) -> `I(v,Listx.append (Listx.Nil arg) args)
177 | `Var v -> `I(v, Listx.Nil arg)
178 | `Lam(truelam,nf) -> subst truelam true 0 arg (nf : nf) (* AC FIXME sanity check on arity *)
179 | `Match(t,v,lift,bs,args) -> `Match(t,v,lift,bs,List.append args [arg])
180 | `N _ -> assert false (* Numbers cannot be applied *)
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 (n,ar) 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 = set_arity (minus1 ar) h in
197 let h = lift bs_lift h in
200 `Match (t,(n,ar),bs_lift,bs,args))
201 | `I _ | `Var _ | `Match _ -> `Match(t,(n,ar),bs_lift,bs,args)
203 and subst truelam delift_by_one what (with_what : nf) (where : nf) =
204 let rec aux_propagate_arity ar = function
205 | `Lam(false, t) when not delift_by_one -> `Lam(false, aux_propagate_arity ar t)
206 | `Match(`I(v,args),(x,_),liftno,bs,args') when not delift_by_one ->
207 `Match(`I(v,args),(x,ar),liftno,bs,args')
208 | `Var(i,oldar) -> `Var(i, if truelam then (assert (oldar = min_int); ar) else oldar)
210 let rec aux_i_num_var l =
214 mk_appx (lift l (aux_propagate_arity ar with_what)) (Listx.map (aux l) args)
216 `I (((if delift_by_one && n >= l then n-1 else n), ar), Listx.map (aux l) args)
219 lift l (aux_propagate_arity ar with_what)
221 `Var((if delift_by_one && n >= l then n-1 else n), ar)
223 | `Match(t,v,bs_lift,bs,args) ->
224 let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
225 let l' = l - bs_lift in
226 let with_what' = lift l' with_what in
227 (* The following line should be the identity when delift_by_one = true because we
228 are assuming the ts to not contain lambda-bound variables. *)
229 bs := List.map (fun (n,t) -> n,subst truelam false what with_what' t) !bs ;
230 mk_match (cast_to_i_num_var (aux_i_num_var l t)) v bs_lift bs (List.map (aux l) args)
232 (*function iii -> let res = match iii with*)
234 | #i_num_var as x -> aux_i_num_var l x
235 | `Lam(b, nf) -> `Lam(b, aux (l+1) nf)
236 (*in let ll = ["v0";"v1";"v2"] in
237 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*)
242 (************ Parsing ************************************)
245 let fix_arity = function
246 | `I((n,_),args) -> `I((n,1+Listx.length args),args)
247 | _ -> assert false in
248 let rec aux = function
249 | Parser.Lam t -> `Lam (true, aux t)
250 | Parser.App (t1, t2) -> fix_arity (mk_app (aux t1) (aux t2))
251 | Parser.Var v -> `Var(v,1) in
252 let (tms, free) = Parser.parse_many strs in
253 List.map aux tms, free
256 (************** Algorithm(s) ************************)
258 let eta_compare x y =
259 (* let clex a b = let diff = ? a b in if diff = 0 then cont () else 0 in *)
260 let clex aux1 aux2 (a1,a2) (b1,b2) =
261 let diff = aux1 a1 b1 in if diff = 0 then aux2 a2 b2 else diff in
262 let rec lex aux l1 l2 =
267 | x::xs, y::ys -> clex aux (lex aux) (x,xs) (y,ys) in
268 let rec aux t1 t2 = match t1, t2 with
269 | `Var(n,_) , `Var(m,_) -> compare n m
270 | `I((n1,_), l1), `I((n2,_), l2) ->
271 clex compare (lex aux) (n1, Listx.to_list l1) (n2, Listx.to_list l2)
274 | `Lam(_,t1), `Lam(_,t2) -> aux t1 t2
275 | `Lam(_,t1), t2 -> - aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
276 | t2, `Lam(_,t1) -> aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
277 | `N n1, `N n2 -> compare n1 n2
278 | `Match(u,_,bs_lift,bs,args), `Match(u',_,bs_lift',bs',args') ->
279 let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
280 let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
281 clex aux (clex (lex (clex compare aux)) (lex aux)) ((u :> nf), (bs, args)) ((u' :> nf), (bs', args'))
291 let eta_eq (#nf as x) (#nf as y) = 0 = eta_compare x y ;;
293 let rec eta_subterm sub t =
294 if eta_eq sub t then true else
296 | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
297 | `Match(u,ar,liftno,bs,args) ->
298 eta_subterm sub (u :> nf)
299 || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
300 || List.exists (eta_subterm sub) args
301 | `I(v, args) -> List.exists (eta_subterm sub) (Listx.to_list args) || (match sub with
303 | `I(v', args') -> v = v'
304 && Listx.length args' < Listx.length args
305 && 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'))
308 | `N _ | `Var _ -> false
311 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;
314 let max_arity_tms n =
315 let max a b = match a, b with
318 | Some x, None -> Some x
319 | Some x, Some y -> Some (Pervasives.max x y) in
320 let aux_var l (m,a) = if n + l = m then Some a else None in
321 let rec aux l = function
322 | `Var v -> aux_var l v
323 | `I(v,tms) -> max (aux_var l v) (aux_tms l (Listx.to_list tms))
324 | `Lam(_,t) -> aux (l+1) t
325 | `Match(u,_,_,bs,args) -> max (max (aux l (u :> nf)) (aux_tms l args)) (aux_tms l (List.map snd !bs))
328 List.fold_left (fun acc t -> Pervasives.max acc (aux l t)) None in
329 fun tms -> aux_tms 0 (tms :> nf list)
332 let get_first_args var =
333 let rec aux l = function
334 | `Lam(_,t) -> aux (l+1) t
335 | `Match(u,orig,liftno,bs,args) -> Util.concat_map (aux l) args
336 | `I((n,_), args) -> if n = var + l then [Listx.last args] else []
342 let compute_arities m =
347 let tms = Util.filter_map (function `Lam(_,t) -> Some t | _ -> None ) tms in
348 let arity = match max_arity_tms (m-n) tms with None -> -666 | Some x -> x in
349 arity :: (aux (n-1) tms)
350 in fun tms -> List.rev (aux m tms)
353 let compute_arities var special_k all_tms =
354 let tms = List.fold_left (fun acc t -> acc @ (get_first_args var t)) [] all_tms in
355 compute_arities special_k tms