]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/acic_procedural/proceduralHelpers.ml
Procedural: some comments added in the generated script
[helm.git] / helm / software / components / acic_procedural / proceduralHelpers.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 Rf   = CicRefine
28 module Un   = CicUniv
29 module Pp   = CicPp
30 module TC   = CicTypeChecker
31 module PEH  = ProofEngineHelpers
32 module E    = CicEnvironment
33 module UM   = UriManager
34 module D    = Deannotate
35 module PER  = ProofEngineReduction
36 module Ut   = CicUtil
37
38 (* fresh name generator *****************************************************)
39
40 let split name =
41    let rec aux i =
42       if i <= 0 then assert false else
43       let c = name.[pred i] in
44       if c >= '0' && c <= '9' then aux (pred i) 
45       else Str.string_before name i, Str.string_after name i
46    in
47    let before, after = aux (String.length name) in
48    let i = if after = "" then -1 else int_of_string after in
49    before, i
50
51 let join (s, i) =
52    C.Name (if i < 0 then s else s ^ string_of_int i)
53
54 let mk_fresh_name context (name, k) = 
55    let rec aux i = function
56       | []                            -> name, i
57       | Some (C.Name s, _) :: entries ->
58          let m, j = split s in
59          if m = name && j >= i then aux (succ j) entries else aux i entries
60       | _ :: entries                  -> aux i entries
61    in
62    join (aux k context)
63
64 let mk_fresh_name context = function
65    | C.Anonymous -> C.Anonymous
66    | C.Name s    -> mk_fresh_name context (split s)
67
68 (* helper functions *********************************************************)
69
70 let rec list_map_cps g map = function
71    | []       -> g []
72    | hd :: tl -> 
73       let h hd =
74          let g tl = g (hd :: tl) in
75          list_map_cps g map tl   
76       in
77       map h hd
78
79 let identity x = x
80
81 let compose f g x = f (g x)
82
83 let fst3 (x, _, _) = x
84
85 let refine c t =
86    try let t, _, _, _ = Rf.type_of_aux' [] c t Un.oblivion_ugraph in t
87    with e -> 
88       Printf.eprintf "REFINE EROR: %s\n" (Printexc.to_string e);
89       Printf.eprintf "Ref: context: %s\n" (Pp.ppcontext c);
90       Printf.eprintf "Ref: term   : %s\n" (Pp.ppterm t);
91       raise e
92
93 let get_type c t =
94    try let ty, _ = TC.type_of_aux' [] c t Un.oblivion_ugraph in ty
95    with e -> 
96       Printf.eprintf "TC: context: %s\n" (Pp.ppcontext c);
97       Printf.eprintf "TC: term   : %s\n" (Pp.ppterm t);
98       raise e
99
100 let get_tail c t =
101    match PEH.split_with_whd (c, t) with
102       | (_, hd) :: _, _ -> hd
103       | _               -> assert false
104
105 let is_proof c t =
106    match get_tail c (get_type c (get_type c t)) with
107       | C.Sort C.Prop -> true
108       | C.Sort _      -> false
109       | _             -> assert false 
110
111 let is_sort = function
112    | C.Sort _ -> true
113    | _        -> false 
114
115 let is_unsafe h (c, t) = true
116
117 let is_not_atomic = function
118    | C.Sort _
119    | C.Rel _
120    | C.Const _
121    | C.Var _
122    | C.MutInd _ 
123    | C.MutConstruct _ -> false
124    | _                -> true
125
126 let is_atomic t = not (is_not_atomic t)
127
128 let get_ind_type uri tyno =
129    match E.get_obj Un.oblivion_ugraph uri with
130       | C.InductiveDefinition (tys, _, lpsno, _), _ -> lpsno, List.nth tys tyno
131       | _                                           -> assert false
132
133 let get_default_eliminator context uri tyno ty =
134    let _, (name, _, _, _) = get_ind_type uri tyno in
135    let ext = match get_tail context (get_type context ty) with
136       | C.Sort C.Prop      -> "_ind"
137       | C.Sort C.Set       -> "_rec"
138       | C.Sort (C.CProp _) -> "_rect"
139       | C.Sort (C.Type _)  -> "_rect"
140       | t                  -> 
141          Printf.eprintf "CicPPP get_default_eliminator: %s\n" (Pp.ppterm t);
142          assert false
143    in
144    let buri = UM.buri_of_uri uri in
145    let uri = UM.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con") in
146    C.Const (uri, [])
147
148 let get_ind_parameters c t =
149    let ty = get_type c t in
150    let ps = match get_tail c ty with
151       | C.MutInd _                  -> []
152       | C.Appl (C.MutInd _ :: args) -> args
153       | _                           -> assert false
154    in
155    let disp = match get_tail c (get_type c ty) with
156       | C.Sort C.Prop -> 0
157       | C.Sort _      -> 1
158       | _             -> assert false
159    in
160    ps, disp
161
162 let cic = D.deannotate_term
163
164 let occurs c ~what ~where =
165    let result = ref false in
166    let equality c t1 t2 =
167       let r = Ut.alpha_equivalence t1 t2 in
168       result := !result || r; r
169    in
170    let context, what, with_what = c, [what], [C.Rel 0] in
171    let _ = PER.replace_lifting ~equality ~context ~what ~with_what ~where in
172    !result
173
174 let name_of_uri uri tyno cno =
175    let get_ind_type tys tyno =
176       let s, _, _, cs = List.nth tys tyno in s, cs
177    in
178    match (fst (E.get_obj Un.oblivion_ugraph uri)), tyno, cno with
179       | C.Variable (s, _, _, _, _), _, _                     -> s
180       | C.Constant (s, _, _, _, _), _, _                     -> s
181       | C.InductiveDefinition (tys, _, _, _), Some i, None   ->
182          let s, _ = get_ind_type tys i in s
183       | C.InductiveDefinition (tys, _, _, _), Some i, Some j ->
184          let _, cs = get_ind_type tys i in
185          let s, _ = List.nth cs (pred j) in s
186       | _                                                    -> assert false
187
188 (* Ensuring Barendregt convenction ******************************************)
189
190 let rec add_entries map c = function
191    | []       -> c
192    | hd :: tl ->
193       let sname, w = map hd in
194       let entry = Some (Cic.Name sname, C.Decl w) in
195       add_entries map (entry :: c) tl
196
197 let get_sname c i =
198    try match List.nth c (pred i) with
199       | Some (Cic.Name sname, _) -> sname
200       | _                        -> assert false
201    with 
202       | Failure _          -> assert false
203       | Invalid_argument _ -> assert false
204
205 let cic_bc c t =
206    let get_fix_decl (sname, i, w, v) = sname, w in
207    let get_cofix_decl (sname, w, v) = sname, w in
208    let rec bc c = function
209       | C.LetIn (name, v, ty, t) ->
210          let name = mk_fresh_name c name in
211          let entry = Some (name, C.Def (v, ty)) in
212          let v, ty, t = bc c v, bc c ty, bc (entry :: c) t in
213          C.LetIn (name, v, ty, t)
214       | C.Lambda (name, w, t) ->
215          let name = mk_fresh_name c name in
216          let entry = Some (name, C.Decl w) in
217          let w, t = bc c w, bc (entry :: c) t in
218          C.Lambda (name, w, t)
219       | C.Prod (name, w, t) ->
220          let name = mk_fresh_name c name in
221          let entry = Some (name, C.Decl w) in
222          let w, t = bc c w, bc (entry :: c) t in
223          C.Prod (name, w, t)
224       | C.Appl vs -> 
225          let vs = List.map (bc c) vs in
226          C.Appl vs
227       | C.MutCase (uri, tyno, u, v, ts) ->
228          let u, v, ts = bc c u, bc c v, List.map (bc c) ts in
229          C.MutCase (uri, tyno, u, v, ts)
230       | C.Cast (t, u) ->  
231          let t, u = bc c t, bc c u in
232          C.Cast (t, u)
233       | C.Fix (i, fixes) ->
234          let d = add_entries get_fix_decl c fixes in
235          let bc_fix (sname, i, w, v) = (sname, i, bc c w, bc d v) in
236          let fixes = List.map bc_fix fixes in
237          C.Fix (i, fixes)
238       | C.CoFix (i, cofixes) ->
239          let d = add_entries get_cofix_decl c cofixes in
240          let bc_cofix (sname, w, v) = (sname, bc c w, bc d v) in
241          let cofixes = List.map bc_cofix cofixes in
242          C.CoFix (i, cofixes)
243       | t -> t
244    in 
245    bc c t
246
247 let acic_bc c t =
248    let get_fix_decl (id, sname, i, w, v) = sname, cic w in
249    let get_cofix_decl (id, sname, w, v) = sname, cic w in
250    let rec bc c = function
251       | C.ALetIn (id, name, v, ty, t) ->
252          let name = mk_fresh_name c name in
253          let entry = Some (name, C.Def (cic v, cic ty)) in
254          let v, ty, t = bc c v, bc c ty, bc (entry :: c) t in
255          C.ALetIn (id, name, v, ty, t)
256       | C.ALambda (id, name, w, t) ->
257          let name = mk_fresh_name c name in
258          let entry = Some (name, C.Decl (cic w)) in
259          let w, t = bc c w, bc (entry :: c) t in
260          C.ALambda (id, name, w, t)
261       | C.AProd (id, name, w, t) ->
262          let name = mk_fresh_name c name in
263          let entry = Some (name, C.Decl (cic w)) in
264          let w, t = bc c w, bc (entry :: c) t in
265          C.AProd (id, name, w, t)
266       | C.AAppl (id, vs) -> 
267          let vs = List.map (bc c) vs in
268          C.AAppl (id, vs)
269       | C.AMutCase (id, uri, tyno, u, v, ts) ->
270          let u, v, ts = bc c u, bc c v, List.map (bc c) ts in
271          C.AMutCase (id, uri, tyno, u, v, ts)
272       | C.ACast (id, t, u) ->  
273          let t, u = bc c t, bc c u in
274          C.ACast (id, t, u)
275       | C.AFix (id, i, fixes) ->
276          let d = add_entries get_fix_decl c fixes in
277          let bc_fix (id, sname, i, w, v) = (id, sname, i, bc c w, bc d v) in
278          let fixes = List.map bc_fix fixes in
279          C.AFix (id, i, fixes)
280       | C.ACoFix (id, i, cofixes) ->
281          let d = add_entries get_cofix_decl c cofixes in
282          let bc_cofix (id, sname, w, v) = (id, sname, bc c w, bc d v) in
283          let cofixes = List.map bc_cofix cofixes in
284          C.ACoFix (id, i, cofixes)
285       | C.ARel (id1, id2, i, sname) ->
286          let sname = get_sname c i in
287          C.ARel (id1, id2, i, sname)
288       | t -> t
289    in 
290    bc c t