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
44 let aux_var l (n, ar) = (if n < l then n else n+m), ar in
45 let rec aux_i_num_var l =
47 `I(v,args) -> `I(aux_var l v, Listx.map (aux l) args)
48 | `Var v -> `Var(aux_var l v)
50 | `Match(t,v,lift,bs,args) ->
51 `Match(aux_i_num_var l t, v, lift + m, bs, List.map (aux l) args)
54 #i_num_var as x -> (aux_i_num_var l x :> nf)
55 | `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)
85 let rec t_of_i_num_var =
87 | `N n -> Scott.mk_n n
88 | `Var(v,_) -> Pure.V v
89 | `Match(t,_,liftno,bs,args) ->
90 let bs = List.map (fun (n,t) -> n, t_of_nf (lift liftno t)) !bs in
91 let t = t_of_i_num_var t in
92 let m = Scott.mk_match t bs in
93 List.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) m args
94 | `I((v,_), args) -> Listx.fold_left (fun acc t -> Pure.A(acc,t_of_nf t)) (Pure.V v) args
97 | #i_num_var as x -> t_of_i_num_var x
98 | `Lam(b,f) -> Pure.L (t_of_nf f)
103 (************ Pretty-printing ************************************)
105 let rec print ?(l=[]) =
107 `Var(n,_) -> print_name l n
108 | `N n -> string_of_int n
109 | `Match(t,_,bs_lift,bs,args) ->
110 "([" ^ print ~l (t :> nf) ^
111 " ? " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ print ~l (lift bs_lift t)) !bs) ^ "] " ^
112 String.concat " " (List.map (print ~l) args) ^ ")"
113 | `I((n,_),args) -> "(" ^ print_name l n ^ " " ^ String.concat " " (Listx.to_list (Listx.map (print ~l) args)) ^ ")"
115 let name = string_of_var (List.length l) in
116 "λ" ^ name ^ "." ^ print ~l:(name::l) (nf : nf)
119 let rec string_of_term l =
120 let rec string_of_term_w_pars l = function
121 | `Var(n,_) -> print_name l n
122 | `N n -> string_of_int n
123 | `I _ as t -> "(" ^ string_of_term_no_pars_app l (t :> nf) ^ ")"
124 | `Lam _ as t -> "(" ^ string_of_term_no_pars_lam l t ^ ")"
125 | `Match(t,_,bs_lift,bs,args) ->
126 "(match " ^ string_of_term_no_pars l (t :> nf) ^
127 " with " ^ String.concat " | " (List.map (fun (n,t) -> string_of_int n ^ " => " ^ string_of_term l (lift bs_lift t)) !bs) ^ "] " ^
128 String.concat " " (List.map (string_of_term l) args) ^ ")"
129 and string_of_term_no_pars_app l = function
130 | `I((n,_), args) -> print_name l n ^ " " ^ String.concat " " (List.map (string_of_term_w_pars l) (Listx.to_list args))
131 | #nf as t -> string_of_term_w_pars l t
132 and string_of_term_no_pars_lam l = function
133 | `Lam(_,t) -> let name = string_of_var (List.length l) in
134 "λ" ^ name ^ ". " ^ (string_of_term_no_pars_lam (name::l) t)
135 | _ as t -> string_of_term_no_pars l t
136 and string_of_term_no_pars l : nf -> string = function
137 | `Lam _ as t -> string_of_term_no_pars_lam l t
138 | #nf as t -> string_of_term_no_pars_app l t
139 in string_of_term_no_pars l
142 let print ?(l=[]) = string_of_term l;;
143 let string_of_nf t = string_of_term [] (t:>nf);;
145 (************ Hereditary substitutions ************************************)
149 #i_var as y -> (y : i_var)
151 prerr_endline (print (t :> nf));
152 assert false (* algorithm failed *)
154 let cast_to_i_n_var =
156 #i_n_var as y -> (y : i_n_var)
158 prerr_endline (print (t :> nf));
159 assert false (* algorithm failed *)
161 let cast_to_i_num_var =
163 #i_num_var as y -> (y : i_num_var)
165 prerr_endline (print (t :> nf));
166 assert false (* algorithm failed *)
168 let rec mk_app (h : nf) (arg : nf) =
171 `I(v,args) -> `I(v,Listx.append (Listx.Nil arg) args)
172 | `Var v -> `I(v, Listx.Nil arg)
173 | `Lam(_,nf) -> subst true 0 arg (nf : nf) (* AC FIXME sanity check on arity *)
174 | `Match(t,v,lift,bs,args) -> `Match(t,v,lift,bs,List.append args [arg])
175 | `N _ -> assert false (* Numbers cannot be applied *)
176 (*in let l = ["v0";"v1";"v2"] in
177 prerr_endline ("mk_app h:" ^ print ~l h ^ " arg:" ^ print ~l:l arg ^ " res:" ^ print ~l:l res); res*)
180 (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
181 List.fold_left mk_app h args
183 and mk_appx h args = Listx.fold_left mk_app h args
185 and mk_match t ar bs_lift bs args =
186 (*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));*)
190 let h = List.assoc m !bs in
191 let h = lift bs_lift h in
194 `Match (t,ar,bs_lift,bs,args))
195 | `I _ | `Var _ | `Match _ -> `Match(t,ar,bs_lift,bs,args)
197 and subst delift_by_one what (with_what : nf) (where : nf) =
198 let rec aux_i_num_var l =
202 mk_appx (lift l with_what) (Listx.map (aux l) args)
204 `I (((if delift_by_one && n >= l then n-1 else n), ar), Listx.map (aux l) args)
209 `Var((if delift_by_one && n >= l then n-1 else n), ar)
211 | `Match(t,v,bs_lift,bs,args) ->
212 let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
213 let l' = l - bs_lift in
214 let with_what' = lift l' with_what in
215 (* The following line should be the identity when delift_by_one = true because we
216 are assuming the ts to not contain lambda-bound variables. *)
217 bs := List.map (fun (n,t) -> n,subst false what with_what' t) !bs ;
218 mk_match (cast_to_i_num_var (aux_i_num_var l t)) v bs_lift bs (List.map (aux l) args)
220 (*function iii -> let res = match iii with*)
222 | #i_num_var as x -> aux_i_num_var l x
223 | `Lam(b, nf) -> `Lam(b, aux (l+1) nf)
224 (*in let ll = ["v0";"v1";"v2"] in
225 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*)
230 (************ Parsing ************************************)
233 let rec aux = function
234 | Parser.Lam t -> `Lam (true, aux t)
235 | Parser.App (t1, t2) -> mk_app (aux t1) (aux t2)
236 | Parser.Var v -> `Var(v,-666)
237 in let (tms, free) = Parser.parse_many strs
238 in (List.map aux tms, free)
241 (************** Algorithm(s) ************************)
243 let eta_compare x y =
244 (* let clex a b = let diff = ? a b in if diff = 0 then cont () else 0 in *)
245 let clex aux1 aux2 (a1,a2) (b1,b2) =
246 let diff = aux1 a1 b1 in if diff = 0 then aux2 a2 b2 else diff in
247 let rec lex aux l1 l2 =
252 | x::xs, y::ys -> clex aux (lex aux) (x,xs) (y,ys) in
253 let rec aux t1 t2 = match t1, t2 with
254 | `Var(n,_) , `Var(m,_) -> compare n m
255 | `I((n1,_), l1), `I((n2,_), l2) ->
256 clex compare (lex aux) (n1, Listx.to_list l1) (n2, Listx.to_list l2)
259 | `Lam(_,t1), `Lam(_,t2) -> aux t1 t2
260 | `Lam(_,t1), t2 -> - aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
261 | t2, `Lam(_,t1) -> aux t1 (mk_app (lift 1 t2) (`Var(0,-666)))
262 | `N n1, `N n2 -> compare n1 n2
263 | `Match(u,_,bs_lift,bs,args), `Match(u',_,bs_lift',bs',args') ->
264 let bs = List.sort (fun (n,_) (m,_) -> compare n m) !bs in
265 let bs' = List.sort (fun (n,_) (m,_) -> compare n m) !bs' in
266 clex aux (clex (lex (clex compare aux)) (lex aux)) ((u :> nf), (bs, args)) ((u' :> nf), (bs', args'))
276 let eta_eq (#nf as x) (#nf as y) = 0 = eta_compare x y ;;
278 let rec eta_subterm sub t =
279 if eta_eq sub t then true else
281 | `Lam(_,t') -> eta_subterm (lift 1 sub) t'
282 | `Match(u,ar,liftno,bs,args) ->
283 eta_subterm sub (u :> nf)
284 || List.exists (fun (_, t) -> eta_subterm sub (lift liftno t)) !bs
285 || List.exists (eta_subterm sub) args
286 | `I(v, args) -> List.exists (eta_subterm sub) (Listx.to_list args) || (match sub with
288 | `I(v', args') -> v = v'
289 && Listx.length args' < Listx.length args
290 && 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'))
293 | `N _ | `Var _ -> false
296 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;