1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 exception Impossible;;
27 exception NotImplemented of int * string;;
28 exception WrongUriToConstant;;
29 exception WrongUriToVariable of string;;
30 exception WrongUriToInductiveDefinition;;
32 (* mem x lol is true if x is a member of one *)
33 (* of the lists of the list of (int * list) lol *)
35 List.fold_right (fun (_,l) i -> i || List.mem x l) lol false
39 let cook curi cookingsno var is_letin =
45 n when n >= k -> C.Rel (n + 1)
49 if UriManager.eq uri var then
55 | C.Implicit as t -> t
56 | C.Cast (te, ty) -> C.Cast (aux k te, aux k ty)
57 | C.Prod (n,s,t) -> C.Prod (n, aux k s, aux (k + 1) t)
58 | C.Lambda (n,s,t) -> C.Lambda (n, aux k s, aux (k + 1) t)
59 | C.LetIn (n,s,t) -> C.LetIn (n, aux k s, aux (k + 1) t)
61 (* Get rid of C.Appl (C.Appl l1) l2 *)
62 let newtl = List.map (aux k) tl in
64 C.Appl (he'::tl') -> C.Appl (he'::(tl'@newtl))
65 | t -> C.Appl (t::newtl)
67 | C.Appl [] -> raise Impossible
69 if not is_letin && match CicEnvironment.get_obj uri with
70 C.Definition (_,_,_,params) when mem var params -> true
71 | C.Definition _ -> false
72 | C.Axiom (_,_,params) when mem var params -> true
75 raise (NotImplemented (2,(UriManager.string_of_uri uri)))
76 | _ -> raise WrongUriToConstant
79 ((C.Const (uri,UriManager.relative_depth curi uri cookingsno))::
82 C.Const (uri,UriManager.relative_depth curi uri cookingsno)
83 | C.MutInd (uri,_,i) ->
84 if not is_letin && match CicEnvironment.get_obj uri with
85 C.InductiveDefinition (_,params,_) when mem var params -> true
86 | C.InductiveDefinition _ -> false
87 | _ -> raise WrongUriToInductiveDefinition
89 C.Appl ((C.MutInd (uri,UriManager.relative_depth curi uri cookingsno,i))::[C.Rel k])
91 C.MutInd (uri,UriManager.relative_depth curi uri cookingsno,i)
92 | C.MutConstruct (uri,_,i,j) ->
93 if not is_letin && match CicEnvironment.get_obj uri with
94 C.InductiveDefinition (_,params,_) when mem var params -> true
95 | C.InductiveDefinition _ -> false
96 | _ -> raise WrongUriToInductiveDefinition
98 C.Appl ((C.MutConstruct (uri,UriManager.relative_depth curi uri cookingsno,i,j))::[C.Rel k])
100 C.MutConstruct (uri,UriManager.relative_depth curi uri cookingsno,i,j)
101 | C.MutCase (uri,_,i,outt,term,pl) ->
105 C.MutCase (uri,UriManager.relative_depth curi uri cookingsno,i,
106 aux k outt,aux k term, substitutedfl)
108 let len = List.length fl in
111 (fun (name,i,ty,bo) -> (name,i,aux k ty, aux (k+len) bo))
114 C.Fix (i, substitutedfl)
116 let len = List.length fl in
119 (fun (name,ty,bo) -> (name,aux k ty, aux (k+len) bo))
122 C.CoFix (i, substitutedfl)
127 let cook_gen add_binder curi cookingsno ty vars =
128 let module C = Cic in
129 let module U = UriManager in
133 let (varname, varbody, vartype) =
134 match CicEnvironment.get_obj var with
135 C.Variable (varname, varbody, vartype) -> (varname, varbody, vartype)
136 | _ -> raise (WrongUriToVariable (U.string_of_uri var))
139 add_binder (C.Name varname) varbody vartype
141 Some _ -> cook curi cookingsno var true ty
142 | None -> cook curi cookingsno var false ty
145 cookrec cooked_once tl
152 cook_gen (fun n b s t ->
154 None -> Cic.Prod (n,s,t)
155 | Some b -> Cic.LetIn (n,b,t)
158 cook_gen (fun n b s t ->
160 None -> Cic.Lambda (n,s,t)
161 | Some b -> Cic.LetIn (n,b,t)
165 (*CSC: sbagliato da rifare e completare *)
166 let cook_one_level obj curi cookingsno vars =
167 let module C = Cic in
169 C.Definition (id,te,ty,params) ->
170 let ty' = cook_prod curi cookingsno ty vars in
171 let te' = cook_lambda curi cookingsno te vars in
172 C.Definition (id,te',ty',params)
173 | C.Axiom (id,ty,parameters) ->
174 let ty' = cook_prod curi cookingsno ty vars in
175 C.Axiom (id,ty',parameters)
176 | C.Variable _ as obj -> obj
177 | C.CurrentProof (id,conjs,te,ty) ->
178 let ty' = cook_prod curi cookingsno ty vars in
179 let te' = cook_lambda curi cookingsno te vars in
180 C.CurrentProof (id,conjs,te',ty')
181 | C.InductiveDefinition (dl, params, n_ind_params) ->
184 (fun (name,inductive,arity,constructors) ->
190 None -> raise Impossible
191 | Some r -> List.map (fun _ -> false) vars @ r
193 (name,cook_prod curi cookingsno ty vars,ref (Some r'))
196 (name,inductive,cook_prod curi cookingsno arity vars,constructors')
199 let number_of_variables_without_a_body =
200 let is_not_letin uri =
201 match CicEnvironment.get_obj uri with
202 C.Variable (_,None,_) -> true
203 | C.Variable (_,Some _,_) -> false
204 | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
207 (fun i uri -> if is_not_letin uri then i + 1 else i) 0 vars
209 C.InductiveDefinition
210 (dl', params, n_ind_params + number_of_variables_without_a_body)
213 let cook_obj obj uri =
214 let module C = Cic in
217 C.Definition (_,_,_,params) -> params
218 | C.Axiom (_,_,params) -> params
220 | C.CurrentProof _ -> []
221 | C.InductiveDefinition (_,params,_) -> params
223 let rec cook_all_levels obj =
227 let cooked_obj = cook_one_level obj uri (n + 1) (List.rev vars) in
228 (n,cooked_obj)::(cook_all_levels cooked_obj tl)
230 cook_all_levels obj (List.rev params)
234 CicEnvironment.set_cooking_function cook_obj