]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/content_pres/acic2Procedural.ml
CoRN (new version) has been committed by Andrea in library/algebra/CoRN.
[helm.git] / helm / software / components / content_pres / 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 D    = Deannotate
28 module DTI  = DoubleTypeInference
29 module TC   = CicTypeChecker 
30 module Un   = CicUniv
31 module UM   = UriManager
32 module Obj  = LibraryObjects
33 module HObj = HelmLibraryObjects
34 module A    = Cic2acic
35 module Ut   = CicUtil
36 module E    = CicEnvironment
37
38 module Cl   = CicClassify
39 module T    = ProceduralTypes
40 module Cn   = ProceduralConversion
41
42 type status = {
43    sorts : (C.id, A.sort_kind) Hashtbl.t;
44    types : (C.id, A.anntypes) Hashtbl.t;
45    prefix: string;
46    max_depth: int option;
47    depth: int;
48    entries: C.context;
49    intros: string list;
50    ety: C.annterm option
51 }
52
53 (* helpers ******************************************************************)
54
55 let cic = D.deannotate_term
56
57 let split2_last l1 l2 =
58 try
59    let n = pred (List.length l1) in
60    let before1, after1 = T.list_split n l1 in
61    let before2, after2 = T.list_split n l2 in
62    before1, before2, List.hd after1, List.hd after2
63 with Invalid_argument _ -> failwith "A2P.split2_last"
64
65 let string_of_head = function
66    | C.ASort _         -> "sort"
67    | C.AConst _        -> "const"
68    | C.AMutInd _       -> "mutind"
69    | C.AMutConstruct _ -> "mutconstruct"
70    | C.AVar _          -> "var"
71    | C.ARel _          -> "rel"
72    | C.AProd _         -> "prod"
73    | C.ALambda _       -> "lambda"
74    | C.ALetIn _        -> "letin"
75    | C.AFix _          -> "fix"
76    | C.ACoFix _        -> "cofix"
77    | C.AAppl _         -> "appl"
78    | C.ACast _         -> "cast"
79    | C.AMutCase _      -> "mutcase"
80    | C.AMeta _         -> "meta"
81    | C.AImplicit _     -> "implict"
82
83 let next st = {st with depth = succ st.depth; intros = []; ety = None}
84
85 let set_ety st ety =
86    if st.ety = None then {st with ety = ety} else st
87
88 let add st entry intro ety = 
89    let st = set_ety st ety in
90    {st with entries = entry :: st.entries; intros = intro :: st.intros}
91
92 let test_depth st =
93 try   
94    let msg = Printf.sprintf "Depth %u: " st.depth in
95    match st.max_depth with
96       | None   -> true, "" 
97       | Some d -> 
98          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 get_ind_name uri tno xcno =
112 try   
113    let ts = match E.get_obj Un.empty_ugraph uri with
114       | C.InductiveDefinition (ts, _, _,_), _ -> ts 
115       | _                                     -> assert false
116    in
117    let tname, cs = match List.nth ts tno with
118       | (name, _, _, cs) -> name, cs
119    in
120    match xcno with
121       | None     -> tname
122       | Some cno -> fst (List.nth cs (pred cno))
123 with Invalid_argument _ -> failwith "A2P.get_ind_name"
124 *)
125 let get_inner_types st v =
126 try
127    let id = Ut.id_of_annterm v in
128    try match Hashtbl.find st.types id with
129       | {A.annsynthesized = st; A.annexpected = Some et} -> Some (st, et)
130       | {A.annsynthesized = st; A.annexpected = None}    -> Some (st, st)
131    with Not_found -> None
132 with Invalid_argument _ -> failwith "A2P.get_inner_types"
133
134 let get_inner_sort st v =
135 try
136    let id = Ut.id_of_annterm v in
137    try Hashtbl.find st.sorts id
138    with Not_found -> `Type (CicUniv.fresh())
139 with Invalid_argument _ -> failwith "A2P.get_sort"
140
141 (* proof construction *******************************************************)
142
143 let unused_premise = "UNUSED"
144
145 let defined_premise = "DEFINED"
146
147 let assumed_premise = "ASSUMED"
148
149 let get_intro name t = 
150 try
151 match name with 
152    | C.Anonymous -> unused_premise
153    | C.Name s    -> 
154       if DTI.does_not_occur 1 (cic t) then unused_premise else s
155 with Invalid_argument _ -> failwith "A2P.get_intro"
156
157 let mk_intros st script =
158 try
159    if st.intros = [] then script else
160    let count = List.length st.intros in
161    let p0 = T.Whd (count, "") in
162    let p1 = T.Intros (Some count, List.rev st.intros, "") in
163    match st.ety with
164       | Some ety when Cn.need_whd count ety -> p0 :: p1 :: script
165       | _                                   -> p1 :: script
166 with Invalid_argument _ -> failwith "A2P.mk_intros"
167 (*
168 let rec mk_premise st dtext = function
169    | C.ARel (_, _, _, binder)  -> [], binder
170    | where                     -> 
171       let name = contracted_premise in
172       mk_fwd_proof st dtext name where, name
173 *)
174 let rec mk_fwd_rewrite st dtext name tl direction =
175    let what, where = List.nth tl 5, List.nth tl 3 in
176    let rewrite premise =
177       [T.Rewrite (direction, what, Some (premise, name), dtext)]
178    in
179    match where with
180       | C.ARel (_, _, _, binder) -> rewrite binder
181       | _                        -> 
182          assert (get_inner_sort st where = `Prop);
183          let pred, old = List.nth tl 2, List.nth tl 1 in
184          let pred_name = defined_premise in
185          let pred_text = "extracted" in
186          let p1 = T.LetIn (pred_name, pred, pred_text) in
187          let cut_name = assumed_premise in
188          let cut_type = C.AAppl ("", [T.mk_arel 0 pred_name; old]) in
189          let cut_text = "" in
190          let p2 = T.Cut (cut_name, cut_type, cut_text) in
191          let qs = [rewrite cut_name; mk_proof (next st) where] in 
192          [T.Branch (qs, ""); p2; p1] 
193
194 and mk_fwd_proof st dtext name = function
195    | C.AAppl (_, hd :: tl) as v -> 
196       if is_rewrite_right hd then mk_fwd_rewrite st dtext name tl true else  
197       if is_rewrite_left hd then mk_fwd_rewrite st dtext name tl false else
198       begin match get_inner_types st v with
199          | Some (ity, _) ->
200             let qs = [[T.Id ""]; mk_proof (next st) v] in
201             [T.Branch (qs, ""); T.Cut (name, ity, dtext)]
202          | None          ->
203             let ty, _ = TC.type_of_aux' [] st.entries (cic hd) Un.empty_ugraph in
204             let (classes, rc) as h = Cl.classify ty in
205             let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
206             [T.LetIn (name, v, dtext ^ text)]
207       end
208    | v                          -> 
209       [T.LetIn (name, v, dtext)] 
210
211 and mk_proof st = function
212    | C.ALambda (_, name, v, t) as what -> 
213       let entry = Some (name, C.Decl (cic v)) in
214       let intro = get_intro name t in
215       let ety = match get_inner_types st what with
216          | Some (_, ety) -> Some ety
217          | None          -> None
218       in
219       mk_proof (add st entry intro ety) t
220    | C.ALetIn (_, name, v, t) as what ->
221       let proceed, dtext = test_depth st in
222       let script = if proceed then 
223          let entry = Some (name, C.Def (cic v, None)) in
224          let intro = get_intro name t in
225          let q = mk_proof (next (add st entry intro None)) t in
226          List.rev_append (mk_fwd_proof st dtext intro v) q
227       else
228          [T.Apply (what, dtext)]
229       in
230       mk_intros st script
231    | C.ARel _ as what           ->
232       let _, dtext = test_depth st in
233       let text = "assumption" in
234       let script = [T.Apply (what, dtext ^ text)] in 
235       mk_intros st script
236    | C.AMutConstruct _ as what   ->
237       let _, dtext = test_depth st in
238       let script = [T.Apply (what, dtext)] in 
239       mk_intros st script   
240    | C.AAppl (_, hd :: tl) as t  ->
241       let proceed, dtext = test_depth st in
242       let script = if proceed then
243          let ty, _ = TC.type_of_aux' [] st.entries (cic hd) Un.empty_ugraph in
244          let (classes, rc) as h = Cl.classify ty in
245          let synth = Cl.S.singleton 0 in
246          let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
247          match rc with
248             | Some (i, j) when i > 1 ->
249                let classes, tl, _, what = split2_last classes tl in
250                let synth = Cl.S.add 1 synth in
251                let qs = mk_bkd_proofs (next st) synth classes tl in
252                if is_rewrite_right hd then 
253                   [T.Rewrite (false, what, None, dtext); T.Branch (qs, "")]
254                else if is_rewrite_left hd then 
255                   [T.Rewrite (true, what, None, dtext); T.Branch (qs, "")]
256                else   
257                   let using = Some hd in
258                   [T.Elim (what, using, dtext ^ text); T.Branch (qs, "")]
259             | _                                             ->
260                let qs = mk_bkd_proofs (next st) synth classes tl in
261                [T.Apply (hd, dtext ^ text); T.Branch (qs, "")]
262       else
263          [T.Apply (t, dtext)]
264       in
265       mk_intros st script
266    | t ->
267       let text = Printf.sprintf "%s: %s" "UNEXPANDED" (string_of_head t) in
268       let script = [T.Note text] in
269       mk_intros st script
270
271 and mk_bkd_proofs st synth classes ts =
272 try 
273    let _, dtext = test_depth st in   
274    let aux inv v =
275       if Cl.overlaps synth inv then None else
276       if Cl.S.is_empty inv then Some (mk_proof st v) else
277       Some [T.Apply (v, dtext ^ "dependent")]
278    in
279    let l1, l2 = List.length classes, List.length ts in
280    if l1 > l2 then failwith "partial application" else
281    if l1 < l2 then failwith "too many arguments" else
282    T.list_map2_filter aux classes ts
283 with Invalid_argument _ -> failwith "A2P.mk_bkd_proofs"
284
285 (* object costruction *******************************************************)
286
287 let is_theorem pars =   
288    List.mem (`Flavour `Theorem) pars || List.mem (`Flavour `Fact) pars || 
289    List.mem (`Flavour `Remark) pars || List.mem (`Flavour `Lemma) pars
290
291 let mk_obj st = function
292    | C.AConstant (_, _, s, Some v, t, [], pars) when is_theorem pars ->
293       let ast = mk_proof (set_ety st (Some t)) v in
294       let count = T.count_steps 0 ast in
295       let text = Printf.sprintf "tactics: %u" count in
296       T.Theorem (s, t, text) :: ast @ [T.Qed ""]
297    | _                                                               ->
298       failwith "not a theorem"
299
300 (* interface functions ******************************************************)
301
302 let acic2procedural ~ids_to_inner_sorts ~ids_to_inner_types prefix aobj = 
303    let st = {
304       sorts     = ids_to_inner_sorts;
305       types     = ids_to_inner_types;
306       prefix    = prefix;
307       max_depth = None;
308       depth     = 0;
309       entries   = [];
310       intros    = [];
311       ety       = None
312    } in
313    prerr_endline "Level 2 transformation";
314    let steps = mk_obj st aobj in
315    prerr_endline "grafite rendering";
316    List.rev (T.render_steps [] steps)