]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/nCicProof.ml
0dbaa1a8f3261d39f601cb4aed85076036d600c7
[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               let module Pp = 
75                 Pp.Pp(NCicBlob.NCicBlob(
76                         struct
77                           let metasenv = [] let subst = [] let context = []
78                         end))
79               in  
80                prerr_endline ("term: " ^ Pp.pp_foterm ft);
81                prerr_endline ("path: " ^ String.concat "," 
82                  (List.map string_of_int p1));
83                prerr_endline ("leading to: " ^ Pp.pp_foterm t);
84                assert false
85           | Terms.Node l -> 
86               let l = 
87                 HExtlib.list_mapi
88                   (fun t i -> 
89                     if i = n then aux t tl 
90                     else extract amount (0::vl) t)
91                   l
92               in            
93               NCic.Appl l
94     in
95       NCic.Lambda("x", hole_type, aux ft (List.rev p1))
96     ;;
97
98   let mk_proof (bag : NCic.term Terms.bag) mp steps =
99     let module Subst = FoSubst in
100     let position i l = 
101       let rec aux = function
102        | [] -> assert false
103        | (j,_) :: tl when i = j -> 1
104        | _ :: tl -> 1 + aux tl
105       in
106         aux l
107     in
108     let vars_of i l = fst (List.assoc i l) in
109     let ty_of i l = snd (List.assoc i l) in
110     let close_with_lambdas vl t = 
111       List.fold_left 
112        (fun t i -> 
113           NCic.Lambda ("x"^string_of_int i, NCic.Implicit `Type, t))
114        t vl  
115     in
116     let close_with_forall vl t = 
117       List.fold_left 
118        (fun t i -> 
119           NCic.Prod ("x"^string_of_int i, NCic.Implicit `Type, t))
120        t vl  
121     in
122     let get_literal id =
123       let (_, lit, vl, proof),_ = Terms.get_from_bag id bag in
124       let lit =match lit with 
125           | Terms.Predicate t -> assert false 
126           | Terms.Equation (l,r,ty,_) -> 
127               Terms.Node [ Terms.Leaf eqP(); ty; l; r]
128       in
129         lit, vl, proof
130     in
131     let rec aux ongoal seen = function
132       | [] -> NCic.Rel 1
133       | id :: tl ->
134           let amount = List.length seen in
135           let lit,vl,proof = get_literal id in
136           if not ongoal && id = mp then
137             ((*prerr_endline ("Reached m point, id=" ^ (string_of_int id));*)
138              NCic.LetIn ("clause_" ^ string_of_int id, 
139                 extract amount [] lit, 
140                 (NCic.Appl [eq_refl();NCic.Implicit `Type;NCic.Implicit `Term]),
141                 aux true ((id,([],lit))::seen) (id::tl)))
142           else
143           match proof with
144           | Terms.Exact _ when tl=[] ->
145               (* prerr_endline ("Exact (tl=[]) for " ^ (string_of_int id));*)
146               aux ongoal seen tl
147           | Terms.Step _ when tl=[] -> assert false
148           | Terms.Exact ft ->
149               (* prerr_endline ("Exact for " ^ (string_of_int id));*)
150                NCic.LetIn ("clause_" ^ string_of_int id, 
151                  close_with_forall vl (extract amount vl lit),
152                  close_with_lambdas vl (extract amount vl ft),
153                  aux ongoal 
154                    ((id,(List.map (fun x -> Terms.Var x) vl,lit))::seen) tl)
155           | Terms.Step (_, id1, id2, dir, pos, subst) ->
156               let id, id1,(lit,vl,proof) =
157                 if ongoal then id1,id,get_literal id1
158                 else id,id1,(lit,vl,proof)
159               in
160               let vl = if ongoal then [](*Subst.filter subst vl*) else vl in
161               let proof_of_id id = 
162                 let vars = List.rev (vars_of id seen) in
163                 let args = List.map (Subst.apply_subst subst) vars in
164                 let args = List.map (extract amount vl) args in
165                 let rel_for_id = NCic.Rel (List.length vl + position id seen) in
166                   if args = [] then rel_for_id              
167                   else NCic.Appl (rel_for_id::args)
168               in
169               let p_id1 = proof_of_id id1 in
170               let p_id2 = proof_of_id id2 in
171               let pred, hole_type, l, r = 
172                 let id1_ty = ty_of id1 seen in
173                 let id2_ty,l,r = 
174                   match ty_of id2 seen with
175                   | Terms.Node [ _; t; l; r ] -> 
176                       extract amount vl (Subst.apply_subst subst t),
177                       extract amount vl (Subst.apply_subst subst l),
178                       extract amount vl (Subst.apply_subst subst r)
179                   | _ -> assert false
180                 in
181                   (*prerr_endline "mk_predicate :";
182                   if ongoal then prerr_endline "ongoal=true"
183                   else prerr_endline "ongoal=false";
184                   prerr_endline ("id=" ^ string_of_int id);
185                   prerr_endline ("id1=" ^ string_of_int id1);
186                   prerr_endline ("id2=" ^ string_of_int id2);
187                   prerr_endline ("Positions :" ^
188                                    (String.concat ", "
189                                       (List.map string_of_int pos)));*)
190                 mk_predicate 
191                   id2_ty amount (Subst.apply_subst subst id1_ty) pos vl,
192                 id2_ty,
193                 l,r
194               in
195               let l, r, eq_ind = 
196                 if (ongoal=true) = (dir=Terms.Left2Right) then
197                   r,l,eq_ind_r ()
198                 else
199                   l,r,eq_ind ()
200               in
201                NCic.LetIn ("clause_" ^ string_of_int id, 
202                  close_with_forall vl (extract amount vl lit),
203                            (* NCic.Implicit `Type, *)
204                  close_with_lambdas vl 
205                    (NCic.Appl [ eq_ind ; hole_type; l; pred; p_id1; r; p_id2 ]),
206                  aux ongoal 
207                    ((id,(List.map (fun x -> Terms.Var x) vl,lit))::seen) tl)
208     in 
209       aux false [] steps
210   ;;
211