2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
12 (* $Id: terms.mli 9822 2009-06-03 15:37:06Z tassi $ *)
14 module type NCicContext =
16 val metasenv : NCic.metasenv
17 val subst : NCic.substitution
18 val context : NCic.context
21 module NCicBlob(C : NCicContext) : Terms.Blob with type t = NCic.term = struct
25 let eq x y = NCicReduction.alpha_eq C.metasenv C.subst C.context x y;;
29 | NCic.Rel i, NCic.Rel j -> i-j
30 | NCic.Meta (i,_), NCic.Meta (j,_) -> i-j
31 | NCic.Const r1, NCic.Const r2 -> NReference.compare r1 r2
32 | NCic.Appl l1, NCic.Appl l2 -> FoUtils.lexicograph compare l1 l2
33 | NCic.Rel _, ( NCic.Meta _ | NCic.Const _ | NCic.Appl _ ) -> ~-1
34 | ( NCic.Meta _ | NCic.Const _ | NCic.Appl _ ), NCic.Rel _ -> 1
35 | NCic.Const _, ( NCic.Meta _ | NCic.Appl _ ) -> ~-1
36 | ( NCic.Meta _ | NCic.Appl _ ), NCic.Const _ -> 1
37 | NCic.Appl _, NCic.Meta _ -> ~-1
38 | NCic.Meta _, NCic.Appl _ -> 1
43 if NCicReduction.alpha_eq C.metasenv C.subst C.context x y then 0
48 NCicPp.ppterm ~context:C.context ~metasenv:C.metasenv ~subst:C.subst t;;
50 let rec embed = function
51 | NCic.Meta (i,_) -> Terms.Var i, [i]
53 let rec aux acc l = function
55 |hd::tl -> if List.mem hd l then aux acc l tl else aux (hd::acc) l tl
57 let res,vars = List.fold_left
58 (fun (r,v) t -> let r1,v1 = embed t in (r1::r),aux [] v v1) ([],[]) l
59 in (Terms.Node (List.rev res)), vars
60 | t -> Terms.Leaf t, []
63 let embed t = fst (embed t) ;;
67 NCicMetaSubst.saturate ~delta:max_int C.metasenv C.subst C.context
71 if args = [] then Terms.Leaf t
72 else Terms.Node (Terms.Leaf t :: List.map embed args)
74 let sty = embed sty in
80 OCic2NCic.reference_of_oxuri
81 (UriManager.uri_of_string
82 "cic:/matita/logic/equality/eq.ind#xpointer(1/1)")
89 OCic2NCic.reference_of_oxuri
90 (UriManager.uri_of_string
91 "cic:/matita/logic/equality/eq_ind.con")
98 OCic2NCic.reference_of_oxuri
99 (UriManager.uri_of_string
100 "cic:/matita/logic/equality/eq_elim_r.con")
105 let extract lift vl t =
106 let rec pos i = function
107 | [] -> raise Not_found
108 | j :: tl when j <> i -> 1+ pos i tl
111 let vl_len = List.length vl in
112 let rec extract = function
113 | Terms.Leaf x -> NCicSubstitution.lift (vl_len+lift) x
115 (try NCic.Rel (pos j vl) with Not_found -> NCic.Implicit `Term)
116 | Terms.Node l -> NCic.Appl (List.map extract l)
121 let rec ppfot = function
122 | Terms.Leaf _ -> "."
123 | Terms.Var i -> "?" ^ string_of_int i
124 | Terms.Node l -> "(" ^ String.concat " " (List.map ppfot l) ^ ")"
127 let mk_predicate amount ft p vl =
135 prerr_endline ("term: " ^ ppfot ft);
136 prerr_endline ("path: " ^ String.concat ","
137 (List.map string_of_int p));
143 if i = n then aux t tl
144 else extract amount (0::vl) t)
149 NCic.Lambda("x", NCic.Implicit `Type, aux ft (List.rev p))
152 let mk_proof (bag : NCic.term Terms.bag) steps =
153 let module Subst = FoSubst in
155 let rec aux = function
157 | (j,_) :: tl when i = j -> 1
158 | _ :: tl -> 1 + aux tl
162 let vars_of i l = fst (List.assoc i l) in
163 let ty_of i l = snd (List.assoc i l) in
164 let close_with_lambdas vl t =
167 NCic.Lambda ("x"^string_of_int i, NCic.Implicit `Type, t))
170 let rec aux seen = function
173 (* prerr_endline ("Let4 : " ^string_of_int id); *)
174 let amount = List.length seen in
175 let _, lit, vl, proof = Terms.M.find id bag in
178 | Terms.Predicate t -> assert false
179 | Terms.Equation (l,r,ty,_) ->
180 Terms.Node [ Terms.Leaf eqP; ty; l; r]
182 (* prerr_endline ("X" ^ ppfot lit); *)
185 NCic.LetIn ("clause_" ^ string_of_int id, NCic.Implicit `Type,
186 close_with_lambdas vl (extract amount vl ft),
187 aux ((id,(List.map (fun x -> Terms.Var x) vl,lit))::seen) tl)
188 | Terms.Step (_, id1, id2, dir, pos, subst) ->
190 let vars = vars_of id seen in
191 let args = List.map (Subst.apply_subst subst) vars in
192 let args = List.map (extract amount vl) args in
193 NCic.Appl (NCic.Rel (List.length vl + position id seen) :: args)
195 let p_id1 = proof_of_id id1 in
196 let p_id2 = proof_of_id id2 in
198 let id1_ty = ty_of id1 seen in
199 mk_predicate amount (Subst.apply_subst subst id1_ty) pos vl
201 let eq_ind = if dir=Terms.Left2Right then eq_ind else eq_ind_r in
202 NCic.LetIn ("clause_" ^ string_of_int id, NCic.Implicit `Type,
203 close_with_lambdas vl
204 (NCic.Appl [ eq_ind ; NCic.Implicit `Type;
205 pred; NCic.Implicit `Term; p_id1;
206 NCic.Implicit `Term; p_id2 ]),
207 aux ((id,(List.map (fun x -> Terms.Var x) vl,lit))::seen) tl)