]> matita.cs.unibo.it Git - helm.git/blob - components/acic_procedural/acic2Procedural.ml
dbdfb979209b146c32ee303f89dbe1f599adb096
[helm.git] / components / acic_procedural / acic2Procedural.ml
1 (* Copyright (C) 2003-2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 module C    = Cic
27 module I    = CicInspect
28 module D    = Deannotate
29 module DTI  = DoubleTypeInference
30 module TC   = CicTypeChecker 
31 module Un   = CicUniv
32 module UM   = UriManager
33 module Obj  = LibraryObjects
34 module HObj = HelmLibraryObjects
35 module A    = Cic2acic
36 module Ut   = CicUtil
37 module E    = CicEnvironment
38 module PER  = ProofEngineReduction
39
40 module P    = ProceduralPreprocess
41 module Cl   = ProceduralClassify
42 module M    = ProceduralMode
43 module T    = ProceduralTypes
44 module Cn   = ProceduralConversion
45
46 type status = {
47    sorts : (C.id, A.sort_kind) Hashtbl.t;
48    types : (C.id, A.anntypes) Hashtbl.t;
49    prefix: string;
50    max_depth: int option;
51    depth: int;
52    context: C.context;
53    intros: string list
54 }
55
56 (* helpers ******************************************************************)
57
58 let cic = D.deannotate_term
59
60 let split2_last l1 l2 =
61 try
62    let n = pred (List.length l1) in
63    let before1, after1 = T.list_split n l1 in
64    let before2, after2 = T.list_split n l2 in
65    before1, before2, List.hd after1, List.hd after2
66 with Invalid_argument _ -> failwith "A2P.split2_last"
67
68 let string_of_head = function
69    | C.ASort _         -> "sort"
70    | C.AConst _        -> "const"
71    | C.AMutInd _       -> "mutind"
72    | C.AMutConstruct _ -> "mutconstruct"
73    | C.AVar _          -> "var"
74    | C.ARel _          -> "rel"
75    | C.AProd _         -> "prod"
76    | C.ALambda _       -> "lambda"
77    | C.ALetIn _        -> "letin"
78    | C.AFix _          -> "fix"
79    | C.ACoFix _        -> "cofix"
80    | C.AAppl _         -> "appl"
81    | C.ACast _         -> "cast"
82    | C.AMutCase _      -> "mutcase"
83    | C.AMeta _         -> "meta"
84    | C.AImplicit _     -> "implict"
85
86 let clear st = {st with intros = []}
87
88 let next st = {(clear st) with depth = succ st.depth}
89
90 let add st entry intro =
91    {st with context = entry :: st.context; intros = intro :: st.intros}
92
93 let test_depth st =
94 try   
95    let msg = Printf.sprintf "Depth %u: " st.depth in
96    match st.max_depth with
97       | None   -> true, "" 
98       | Some d -> if st.depth < d then true, msg else false, "DEPTH EXCEDED: "
99 with Invalid_argument _ -> failwith "A2P.test_depth"
100
101 let is_rewrite_right = function
102    | C.AConst (_, uri, []) ->
103       UM.eq uri HObj.Logic.eq_ind_r_URI || Obj.is_eq_ind_r_URI uri
104    | _                     -> false
105
106 let is_rewrite_left = function
107    | C.AConst (_, uri, []) ->
108       UM.eq uri HObj.Logic.eq_ind_URI || Obj.is_eq_ind_URI uri
109    | _                     -> false
110
111 let is_fwd_rewrite_right hd tl =
112    if is_rewrite_right hd then match List.nth tl 3 with
113       | C.ARel _ -> true
114       | _        -> false
115    else false
116
117 let is_fwd_rewrite_left hd tl =
118    if is_rewrite_left hd then match List.nth tl 3 with
119       | C.ARel _ -> true
120       | _        -> false
121    else false
122 (*
123 let get_ind_name uri tno xcno =
124 try   
125    let ts = match E.get_obj Un.empty_ugraph uri with
126       | C.InductiveDefinition (ts, _, _,_), _ -> ts 
127       | _                                     -> assert false
128    in
129    let tname, cs = match List.nth ts tno with
130       | (name, _, _, cs) -> name, cs
131    in
132    match xcno with
133       | None     -> tname
134       | Some cno -> fst (List.nth cs (pred cno))
135 with Invalid_argument _ -> failwith "A2P.get_ind_name"
136 *)
137 let get_inner_types st v =
138 try
139    let id = Ut.id_of_annterm v in
140    try match Hashtbl.find st.types id with
141       | {A.annsynthesized = st; A.annexpected = Some et} -> Some (st, et)
142       | {A.annsynthesized = st; A.annexpected = None}    -> Some (st, st)
143    with Not_found -> None
144 with Invalid_argument _ -> failwith "A2P.get_inner_types"
145
146 let get_inner_sort st v =
147 try
148    let id = Ut.id_of_annterm v in
149    try Hashtbl.find st.sorts id
150    with Not_found -> `Type (CicUniv.fresh())
151 with Invalid_argument _ -> failwith "A2P.get_sort"
152
153 let get_type msg st bo =
154 try   
155    let ty, _ = TC.type_of_aux' [] st.context (cic bo) Un.empty_ugraph in
156    ty
157 with e -> failwith (msg ^ ": " ^ Printexc.to_string e)
158
159 (* proof construction *******************************************************)
160
161 let unused_premise = "UNUSED"
162
163 let defined_premise = "DEFINED"
164
165 let convert st ?name v = 
166    match get_inner_types st v with
167       | None          -> []
168       | Some (st, et) ->
169          let cst, cet = cic st, cic et in
170          if PER.alpha_equivalence cst cet then [] else 
171          let e = Cn.mk_pattern 0 (T.mk_arel 1 "") in
172          match name with
173             | None    -> [T.Change (st, et, None, e, "")]
174             | Some id -> [T.Change (st, et, Some (id, id), e, ""); T.ClearBody (id, "")]
175
176 let get_intro name t = 
177 try
178 match name with 
179    | C.Anonymous -> unused_premise
180    | C.Name s    -> 
181       if DTI.does_not_occur 1 (cic t) then unused_premise else s
182 with Invalid_argument _ -> failwith "A2P.get_intro"
183
184 let mk_intros st script =
185 try
186    if st.intros = [] then script else
187    let count = List.length st.intros in
188    T.Intros (Some count, List.rev st.intros, "") :: script
189 with Invalid_argument _ -> failwith "A2P.mk_intros"
190
191 let rec mk_atomic st dtext what =
192    if T.is_atomic what then 
193       match what with 
194       | C.ARel (_, _, _, name) -> convert st ~name what, what
195       | _                      -> [], what
196    else
197       let name = defined_premise in
198       let script = convert st ~name what in  
199       script @ mk_fwd_proof st dtext name what, T.mk_arel 0 name
200
201 and mk_fwd_rewrite st dtext name tl direction =   
202    assert (List.length tl = 6);
203    let what, where, predicate = List.nth tl 5, List.nth tl 3, List.nth tl 2 in
204    let e = Cn.mk_pattern 1 predicate in
205    match where with
206       | C.ARel (_, _, _, premise) ->
207          let script, what = mk_atomic st dtext what in
208          T.Rewrite (direction, what, Some (premise, name), e, dtext) :: script
209       | _                         -> assert false
210
211 and mk_rewrite st dtext script t what qs tl direction = 
212    assert (List.length tl = 5);
213    let predicate = List.nth tl 2 in
214    let e = Cn.mk_pattern 1 predicate in
215    List.rev script @ convert st t @
216    [T.Rewrite (direction, what, None, e, dtext); T.Branch (qs, "")]
217
218 and mk_fwd_proof st dtext name = function
219    | C.ALetIn (_, n, v, t)      ->
220       let entry = Some (n, C.Def (cic v, None)) in
221       let intro = get_intro n t in
222       let qt = mk_fwd_proof (add st entry intro) dtext name t in
223       let qv = mk_fwd_proof st "" intro v in
224       List.append qt qv
225    | C.AAppl (_, hd :: tl) as v -> 
226       if is_fwd_rewrite_right hd tl then mk_fwd_rewrite st dtext name tl true else
227       if is_fwd_rewrite_left hd tl then mk_fwd_rewrite st dtext name tl false else
228       let ty = get_type "TC1" st hd in
229       begin match get_inner_types st v with
230          | Some (ity, _) when M.bkd st.context ty ->
231             let qs = [[T.Id ""]; mk_proof (next st) v] in
232             [T.Branch (qs, ""); T.Cut (name, ity, dtext)]
233          | _                                      ->
234             let (classes, rc) as h = Cl.classify st.context ty in
235             let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
236             [T.LetIn (name, v, dtext ^ text)]
237       end
238    | C.AMutCase _               -> assert false
239    | C.ACast _                  -> assert false 
240    | v                          ->
241       match get_inner_types st v with
242          | Some (ity, _) ->
243             let qs = [[T.Id ""]; mk_proof (next st) v] in
244             [T.Branch (qs, ""); T.Cut (name, ity, dtext)]
245          | _             ->
246             [T.LetIn (name, v, dtext)]
247
248 and mk_proof st = function
249    | C.ALambda (_, name, v, t)        ->
250       let entry = Some (name, C.Decl (cic v)) in
251       let intro = get_intro name t in
252       mk_proof (add st entry intro) t
253    | C.ALetIn (_, name, v, t) as what ->
254       let proceed, dtext = test_depth st in
255       let script = if proceed then 
256          let entry = Some (name, C.Def (cic v, None)) in
257          let intro = get_intro name t in
258          let q = mk_proof (next (add st entry intro)) t in
259          List.rev_append (mk_fwd_proof st dtext intro v) q
260       else
261          [T.Apply (what, dtext)]
262       in
263       mk_intros st script
264    | C.ARel _ as what                 ->
265       let _, dtext = test_depth st in
266       let text = "assumption" in
267       let script = [T.Apply (what, dtext ^ text)] in 
268       mk_intros st script
269    | C.AMutConstruct _ as what        ->
270       let _, dtext = test_depth st in
271       let script = [T.Apply (what, dtext)] in 
272       mk_intros st script   
273    | C.AAppl (_, hd :: tl) as t       ->
274       let proceed, dtext = test_depth st in
275       let script = if proceed then
276          let ty = get_type "TC2" st hd in
277          let (classes, rc) as h = Cl.classify st.context ty in
278          let premises, _ = P.split st.context ty in
279          assert (List.length classes - List.length tl = 0);
280          let synth = I.S.singleton 0 in
281          let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
282          match rc with
283             | Some (i, j) when i > 1 && i <= List.length classes && M.is_eliminator premises ->
284                let classes, tl, _, what = split2_last classes tl in
285                let script, what = mk_atomic st dtext what in
286                let synth = I.S.add 1 synth in
287                let qs = mk_bkd_proofs (next st) synth classes tl in
288                if is_rewrite_right hd then 
289                   mk_rewrite st dtext script t what qs tl false
290                else if is_rewrite_left hd then 
291                   mk_rewrite st dtext script t what qs tl true
292                else   
293                   let l = succ (List.length tl) in
294                   let predicate = List.nth tl (l - i) in
295                   let e = Cn.mk_pattern j predicate in
296                   let using = Some hd in
297                   List.rev script @ convert st t @
298                   [T.Elim (what, using, e, dtext ^ text); T.Branch (qs, "")]
299             | _                                                  ->
300                let qs = mk_bkd_proofs (next st) synth classes tl in
301                let script, hd = mk_atomic st dtext hd in               
302                List.rev script @ convert st t @        
303                [T.Apply (hd, dtext ^ text); T.Branch (qs, "")]
304       else
305          [T.Apply (t, dtext)]
306       in
307       mk_intros st script
308    | C.AMutCase _                     -> assert false
309    | C.ACast _                        -> assert false
310    | t                                ->
311       let text = Printf.sprintf "%s: %s" "UNEXPANDED" (string_of_head t) in
312       let script = [T.Note text] in
313       mk_intros st script
314
315 and mk_bkd_proofs st synth classes ts =
316 try 
317    let _, dtext = test_depth st in   
318    let aux inv v =
319       if I.overlaps synth inv then None else
320       if I.S.is_empty inv then Some (mk_proof st v) else
321       Some [T.Apply (v, dtext ^ "dependent")]
322    in
323    T.list_map2_filter aux classes ts
324 with Invalid_argument _ -> failwith "A2P.mk_bkd_proofs"
325
326 (* object costruction *******************************************************)
327
328 let is_theorem pars =   
329    List.mem (`Flavour `Theorem) pars || List.mem (`Flavour `Fact) pars || 
330    List.mem (`Flavour `Remark) pars || List.mem (`Flavour `Lemma) pars
331
332 let mk_obj st = function
333    | C.AConstant (_, _, s, Some v, t, [], pars) when is_theorem pars ->
334       let ast = mk_proof st v in
335       let count = T.count_steps 0 ast in
336       let text = Printf.sprintf "tactics: %u" count in
337       T.Theorem (s, t, text) :: ast @ [T.Qed ""]
338    | _                                                               ->
339       failwith "not a theorem"
340
341 (* interface functions ******************************************************)
342
343 let acic2procedural ~ids_to_inner_sorts ~ids_to_inner_types ?depth prefix aobj = 
344    let st = {
345       sorts     = ids_to_inner_sorts;
346       types     = ids_to_inner_types;
347       prefix    = prefix;
348       max_depth = depth;
349       depth     = 0;
350       context   = [];
351       intros    = []
352    } in
353    HLog.debug "Level 2 transformation";
354    let steps = mk_obj st aobj in
355    HLog.debug "grafite rendering";
356    List.rev (T.render_steps [] steps)