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