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_ * (* arity of branches *) int * (*lift*) int * (*branches*)(int * 'nf) list ref * (*args*)'nf list
24 type 'nf nf_ = [ `Lam of (* was_unpacked *) bool * (* arity of variable*) int * '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,ar,lift,bs,args) ->
49 `Match(aux_i_num_var l t, ar,lift + m, bs, List.map (aux l) args)
52 #i_num_var as x -> (aux_i_num_var l x :> nf)
53 | `Lam(b,ar,nf) -> `Lam (b,ar,aux (l+1) nf)
58 (* put t under n lambdas, lifting t accordingtly *)
62 | n when n > 0 -> `Lam (false,-666,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;;
141 let string_of_nf t = string_of_term [] (t:>nf);;
143 (************ Hereditary substitutions ************************************)
147 #i_var as y -> (y : i_var)
149 prerr_endline (print (t :> nf));
150 assert false (* algorithm failed *)
152 let cast_to_i_n_var =
154 #i_n_var as y -> (y : i_n_var)
156 prerr_endline (print (t :> nf));
157 assert false (* algorithm failed *)
159 let cast_to_i_num_var =
161 #i_num_var as y -> (y : i_num_var)
163 prerr_endline (print (t :> nf));
164 assert false (* algorithm failed *)
166 let rec mk_app (h : nf) (arg : nf) =
169 `I(n,args) -> `I(n,Listx.append (Listx.Nil arg) args)
170 | `Var n -> `I(n, Listx.Nil arg)
171 | `Lam(_,_,nf) -> subst true 0 arg (nf : nf) (* AC FIXME sanity check on arity *)
172 | `Match(t,ar,lift,bs,args) -> `Match(t,ar,lift,bs,List.append args [arg])
173 | `N _ -> assert false (* Numbers cannot be applied *)
174 (*in let l = ["v0";"v1";"v2"] in
175 prerr_endline ("mk_app h:" ^ print ~l h ^ " arg:" ^ print ~l:l arg ^ " res:" ^ print ~l:l res); res*)
178 (*prerr_endline ("MK_APPL: " ^ print h ^ " " ^ String.concat " " (List.map print args));*)
179 List.fold_left mk_app h args
181 and mk_appx h args = Listx.fold_left mk_app h args
183 and mk_match t ar bs_lift bs args =
184 (*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));*)
188 let h = List.assoc m !bs in
189 let h = lift bs_lift h in
192 `Match (t,ar,bs_lift,bs,args))
193 | `I _ | `Var _ | `Match _ -> `Match(t,ar,bs_lift,bs,args)
195 and subst delift_by_one what (with_what : nf) (where : nf) =
196 let rec aux_i_num_var l =
200 mk_appx (lift l with_what) (Listx.map (aux l) args)
202 `I ((if delift_by_one && n >= l then n-1 else n), Listx.map (aux l) args)
207 `Var (if delift_by_one && n >= l then n-1 else n)
209 | `Match(t,ar,bs_lift,bs,args) ->
210 let bs_lift = bs_lift + if delift_by_one then -1 else 0 in
211 let l' = l - bs_lift in
212 let with_what' = lift l' with_what in
213 (* The following line should be the identity when delift_by_one = true because we
214 are assuming the ts to not contain lambda-bound variables. *)
215 bs := List.map (fun (n,t) -> n,subst false what with_what' t) !bs ;
216 mk_match (cast_to_i_num_var (aux_i_num_var l t)) ar bs_lift bs (List.map (aux l) args)
218 (*function iii -> let res = match iii with*)
220 | #i_num_var as x -> aux_i_num_var l x
221 | `Lam(b,ar,nf) -> `Lam(b,ar,aux (l+1) nf)
222 (*in let ll = ["v0";"v1";"v2"] in
223 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*)
228 (************ Parsing ************************************)
231 let rec aux = function
232 | Parser.Lam t -> `Lam (true,-666,aux t)
233 | Parser.App (t1, t2) -> mk_app (aux t1) (aux t2)
234 | Parser.Var v -> `Var v
235 in let (tms, free) = Parser.parse_many strs
236 in (List.map aux tms, free)
239 (************** Algorithm(s) ************************)
241 let eta_compare x y =
242 (* let clex a b = let diff = ? a b in if diff = 0 then cont () else 0 in *)
243 let clex aux1 aux2 (a1,a2) (b1,b2) =
244 let diff = aux1 a1 b1 in if diff = 0 then aux2 a2 b2 else diff in
245 let rec lex aux l1 l2 =
250 | x::xs, y::ys -> clex aux (lex aux) (x,xs) (y,ys) in
251 let rec aux t1 t2 = match t1, t2 with
252 | `Var n , `Var m -> compare n m
253 | `I(n1, l1), `I(n2, l2) ->
254 clex compare (lex aux) (n1, Listx.to_list l1) (n2, Listx.to_list l2)
257 | `Lam(_,_,t1), `Lam(_,_,t2) -> aux t1 t2
258 | `Lam(_,_,t1), t2 -> - aux t1 (mk_app (lift 1 t2) (`Var 0))
259 | t2, `Lam(_,_,t1) -> aux t1 (mk_app (lift 1 t2) (`Var 0))
260 | `N n1, `N n2 -> compare n1 n2
261 | `Match(u,_,bs_lift,bs,args), `Match(u',_,bs_lift',bs',args') ->
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 clex aux (clex (lex (clex compare aux)) (lex aux)) ((u :> nf), (bs, args)) ((u' :> nf), (bs', args'))
274 let eta_eq (#nf as x) (#nf as y) = 0 = eta_compare 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,ar,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 _ -> false
294 let eta_subterm (#nf as x) (#nf as y) = eta_subterm x y;;