]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/nCicProof.ml
Various fixes
[helm.git] / helm / software / components / ng_paramodulation / nCicProof.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 (* $Id: orderings.ml 9869 2009-06-11 22:52:38Z denes $ *)
13
14   let eqP () = 
15     let r = 
16       OCic2NCic.reference_of_oxuri 
17        (UriManager.uri_of_string 
18          "cic:/matita/logic/equality/eq.ind#xpointer(1/1)")
19     in
20     NCic.Const r
21   ;;
22
23   let eq_ind () = 
24     let r = 
25       OCic2NCic.reference_of_oxuri 
26        (UriManager.uri_of_string 
27          "cic:/matita/logic/equality/eq_ind.con")
28     in
29     NCic.Const r
30   ;;
31
32   let eq_ind_r () = 
33     let r = 
34       OCic2NCic.reference_of_oxuri 
35        (UriManager.uri_of_string 
36          "cic:/matita/logic/equality/eq_elim_r.con")
37     in
38     NCic.Const r
39   ;;
40
41   let eq_refl () = 
42     let r = 
43       OCic2NCic.reference_of_oxuri 
44        (UriManager.uri_of_string 
45          "cic:/matita/logic/equality/eq.ind#xpointer(1/1/1)")
46     in
47     NCic.Const r
48   ;;
49
50   let extract lift vl t =
51     let rec pos i = function 
52       | [] -> raise Not_found 
53       | j :: tl when j <> i -> 1+ pos i tl
54       | _ -> 1
55     in
56     let vl_len = List.length vl in
57     let rec extract = function
58       | Terms.Leaf x -> NCicSubstitution.lift (vl_len+lift) x
59       | Terms.Var j -> 
60            (try NCic.Rel (pos j vl) with Not_found -> NCic.Implicit `Term) 
61       | Terms.Node l -> NCic.Appl (List.map extract l)
62     in
63       extract t
64   ;;
65
66    let mk_predicate hole_type amount ft p1 vl =
67     let rec aux t p = 
68       match p with
69       | [] -> NCic.Rel 1
70       | n::tl ->
71           match t with
72           | Terms.Leaf _ 
73           | Terms.Var _ -> 
74 (*                prerr_endline ("term: " ^ ppfot ft);             *)
75                prerr_endline ("path: " ^ String.concat "," 
76                  (List.map string_of_int p1));
77                assert false
78           | Terms.Node l -> 
79               let l = 
80                 HExtlib.list_mapi
81                   (fun t i -> 
82                     if i = n then aux t tl 
83                     else extract amount (0::vl) t)
84                   l
85               in            
86               NCic.Appl l
87     in
88       NCic.Lambda("x", hole_type, aux ft (List.rev p1))
89     ;;
90
91   let mk_proof (bag : NCic.term Terms.bag) mp steps =
92     let module Subst = FoSubst in
93     let position i l = 
94       let rec aux = function
95        | [] -> assert false
96        | (j,_) :: tl when i = j -> 1
97        | _ :: tl -> 1 + aux tl
98       in
99         aux l
100     in
101     let vars_of i l = fst (List.assoc i l) in
102     let ty_of i l = snd (List.assoc i l) in
103     let close_with_lambdas vl t = 
104       List.fold_left 
105        (fun t i -> 
106           NCic.Lambda ("x"^string_of_int i, NCic.Implicit `Type, t))
107        t vl  
108     in
109     let close_with_forall vl t = 
110       List.fold_left 
111        (fun t i -> 
112           NCic.Prod ("x"^string_of_int i, NCic.Implicit `Type, t))
113        t vl  
114     in
115     let get_literal id =
116       let (_, lit, vl, proof),_ = Terms.M.find id bag in
117       let lit =match lit with 
118           | Terms.Predicate t -> assert false 
119           | Terms.Equation (l,r,ty,_) -> 
120               Terms.Node [ Terms.Leaf eqP(); ty; l; r]
121       in
122         lit, vl, proof
123     in
124     let rec aux ongoal seen = function
125       | [] -> NCic.Rel 1
126       | id :: tl ->
127           let amount = List.length seen in
128           let lit,vl,proof = get_literal id in
129           if not ongoal && id = mp then
130             ((*prerr_endline ("Reached m point, id=" ^ (string_of_int id));*)
131              NCic.LetIn ("clause_" ^ string_of_int id, 
132                 extract amount [] lit, 
133                 (NCic.Appl [eq_refl();NCic.Implicit `Type;NCic.Implicit `Term]),
134                 aux true ((id,([],lit))::seen) (id::tl)))
135           else
136           match proof with
137           | Terms.Exact _ when tl=[] ->
138               (* prerr_endline ("Exact (tl=[]) for " ^ (string_of_int id));*)
139               aux ongoal seen tl
140           | Terms.Step _ when tl=[] -> assert false
141           | Terms.Exact ft ->
142               (* prerr_endline ("Exact for " ^ (string_of_int id));*)
143                NCic.LetIn ("clause_" ^ string_of_int id, 
144                  close_with_forall vl (extract amount vl lit),
145                  close_with_lambdas vl (extract amount vl ft),
146                  aux ongoal 
147                    ((id,(List.map (fun x -> Terms.Var x) vl,lit))::seen) tl)
148           | Terms.Step (_, id1, id2, dir, pos, subst) ->
149               let id, id1,(lit,vl,proof) =
150                 if ongoal then id1,id,get_literal id1
151                 else id,id1,(lit,vl,proof)
152               in
153               let vl = if ongoal then Subst.filter subst vl else vl in
154               let proof_of_id id = 
155                 let vars = List.rev (vars_of id seen) in
156                 let args = List.map (Subst.apply_subst subst) vars in
157                 let args = List.map (extract amount vl) args in
158                 let rel_for_id = NCic.Rel (List.length vl + position id seen) in
159                   if args = [] then rel_for_id              
160                   else NCic.Appl (rel_for_id::args)
161               in
162               let p_id1 = proof_of_id id1 in
163               let p_id2 = proof_of_id id2 in
164               let pred, hole_type, l, r = 
165                 let id1_ty = ty_of id1 seen in
166                 let id2_ty,l,r = 
167                   match ty_of id2 seen with
168                   | Terms.Node [ _; t; l; r ] -> 
169                       extract amount vl (Subst.apply_subst subst t),
170                       extract amount vl (Subst.apply_subst subst l),
171                       extract amount vl (Subst.apply_subst subst r)
172                   | _ -> assert false
173                 in
174                   (*prerr_endline "mk_predicate :";
175                   if ongoal then prerr_endline "ongoal=true"
176                   else prerr_endline "ongoal=false";
177                   prerr_endline ("id=" ^ string_of_int id);
178                   prerr_endline ("id1=" ^ string_of_int id1);
179                   prerr_endline ("id2=" ^ string_of_int id2);
180                   prerr_endline ("Positions :" ^
181                                    (String.concat ", "
182                                       (List.map string_of_int pos)));*)
183                 mk_predicate 
184                   id2_ty amount (Subst.apply_subst subst id1_ty) pos vl,
185                 id2_ty,
186                 l,r
187               in
188               let l, r, eq_ind = 
189                 if (ongoal=true) = (dir=Terms.Left2Right) then
190                   r,l,eq_ind_r ()
191                 else
192                   l,r,eq_ind ()
193               in
194                NCic.LetIn ("clause_" ^ string_of_int id, 
195                  close_with_forall vl (extract amount vl lit),
196                            (* NCic.Implicit `Type, *)
197                  close_with_lambdas vl 
198                    (NCic.Appl [ eq_ind ; hole_type; l; pred; p_id1; r; p_id2 ]),
199                  aux ongoal 
200                    ((id,(List.map (fun x -> Terms.Var x) vl,lit))::seen) tl)
201     in 
202       aux false [] steps
203   ;;
204