]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicCooking.ml
...
[helm.git] / helm / ocaml / cic_proof_checking / cicCooking.ml
1 (* Copyright (C) 2000, 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 exception Impossible;;
27 exception NotImplemented of int * string;;
28 exception WrongUriToConstant;;
29 exception WrongUriToVariable of string;;
30 exception WrongUriToInductiveDefinition;;
31
32 (* mem x lol is true if x is a member of one    *)
33 (* of the lists of the list of (int * list) lol *)
34 let mem x lol =
35  List.fold_right (fun (_,l) i -> i || List.mem x l) lol false
36 ;;
37
38 (* cook var term *)
39 let cook curi cookingsno var is_letin =
40  let rec aux k =
41   let module C = Cic in
42    function
43       C.Rel n as t ->
44        (match n with
45            n when n >= k -> C.Rel (n + 1)
46          | _ -> C.Rel n
47        )
48     | C.Var uri as t ->
49        if UriManager.eq uri var then
50         C.Rel k
51        else
52         t
53     | C.Meta _ as t -> t
54     | C.Sort _ as t -> t
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)
60     | C.Appl (he::tl) ->
61        (* Get rid of C.Appl (C.Appl l1) l2 *)
62        let newtl = List.map (aux k) tl in
63         (match aux k he with
64             C.Appl (he'::tl') -> C.Appl (he'::(tl'@newtl))
65           | t -> C.Appl (t::newtl)
66         )
67     | C.Appl [] -> raise Impossible
68     | C.Const (uri,_) ->
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
73          | C.Axiom _ -> false
74          | C.CurrentProof _ ->
75             raise (NotImplemented (2,(UriManager.string_of_uri uri)))
76          | _ -> raise WrongUriToConstant
77        then
78         C.Appl
79          ((C.Const (uri,UriManager.relative_depth curi uri cookingsno))::
80           [C.Rel k])
81        else
82         C.Const (uri,UriManager.relative_depth curi uri cookingsno)
83     | C.Abst _ as t -> t
84     | C.MutInd (uri,_,i) ->
85        if not is_letin && match CicEnvironment.get_obj uri with
86            C.InductiveDefinition (_,params,_) when mem var params -> true
87          | C.InductiveDefinition _ -> false
88          | _ -> raise WrongUriToInductiveDefinition
89        then
90         C.Appl ((C.MutInd (uri,UriManager.relative_depth curi uri cookingsno,i))::[C.Rel k])
91        else
92         C.MutInd (uri,UriManager.relative_depth curi uri cookingsno,i)
93     | C.MutConstruct (uri,_,i,j) ->
94        if not is_letin && match CicEnvironment.get_obj uri with
95            C.InductiveDefinition (_,params,_) when mem var params -> true
96          | C.InductiveDefinition _ -> false
97          | _ -> raise WrongUriToInductiveDefinition
98        then
99         C.Appl ((C.MutConstruct (uri,UriManager.relative_depth curi uri cookingsno,i,j))::[C.Rel k])
100        else
101         C.MutConstruct (uri,UriManager.relative_depth curi uri cookingsno,i,j)
102     | C.MutCase (uri,_,i,outt,term,pl) ->
103        let substitutedfl =
104         List.map (aux k) pl
105        in
106         C.MutCase (uri,UriManager.relative_depth curi uri cookingsno,i,
107          aux k outt,aux k term, substitutedfl)
108     | C.Fix (i,fl) ->
109        let len = List.length fl in
110        let substitutedfl =
111          List.map
112           (fun (name,i,ty,bo) -> (name,i,aux k ty, aux (k+len) bo))
113           fl
114        in
115         C.Fix (i, substitutedfl)
116     | C.CoFix (i,fl) ->
117        let len = List.length fl in
118        let substitutedfl =
119          List.map
120           (fun (name,ty,bo) -> (name,aux k ty, aux (k+len) bo))
121           fl
122        in
123         C.CoFix (i, substitutedfl)
124  in
125   aux 1 
126 ;;
127
128 let cook_gen add_binder curi cookingsno ty vars =
129  let module C = Cic in
130  let module U = UriManager in
131   let rec cookrec ty =
132    function
133      var::tl ->
134       let (varname, varbody, vartype) =
135        match CicEnvironment.get_obj var with
136           C.Variable (varname, varbody, vartype) -> (varname, varbody, vartype)
137         | _ -> raise (WrongUriToVariable (U.string_of_uri var))
138       in
139        let cooked_once =
140         add_binder (C.Name varname) varbody vartype
141          (match varbody with
142              Some _ -> cook curi cookingsno var true ty
143            | None -> cook curi cookingsno var false ty
144          )
145        in
146         cookrec cooked_once tl
147    | _ -> ty
148   in
149    cookrec ty vars
150 ;;
151
152 let cook_prod =
153  cook_gen (fun n b s t ->
154   match b with
155      None   -> Cic.Prod (n,s,t)
156    | Some b -> Cic.LetIn (n,b,t)
157  )
158 and cook_lambda =
159  cook_gen (fun n b s t ->
160   match b with
161      None   -> Cic.Lambda (n,s,t)
162    | Some b -> Cic.LetIn (n,b,t)
163  )
164 ;;
165
166 (*CSC: sbagliato da rifare e completare *)
167 let cook_one_level obj curi cookingsno vars =
168  let module C = Cic in
169   match obj with
170      C.Definition (id,te,ty,params) ->
171       let ty' = cook_prod curi cookingsno ty vars in
172       let te' = cook_lambda curi cookingsno te vars in
173        C.Definition (id,te',ty',params)
174    | C.Axiom (id,ty,parameters) ->
175       let ty' = cook_prod curi cookingsno ty vars in
176        C.Axiom (id,ty',parameters)
177    | C.Variable _ as obj -> obj
178    | C.CurrentProof (id,conjs,te,ty) ->
179       let ty' = cook_prod curi cookingsno ty vars in
180       let te' = cook_lambda curi cookingsno te vars in
181        C.CurrentProof (id,conjs,te',ty')
182    | C.InductiveDefinition (dl, params, n_ind_params) ->
183       let dl' =
184        List.map
185         (fun (name,inductive,arity,constructors) ->
186           let constructors' =
187           List.map
188            (fun (name,ty,r) ->
189              let r' = 
190               match !r with
191                  None -> raise Impossible
192                | Some r -> List.map (fun _ -> false) vars @ r
193              in
194              (name,cook_prod curi cookingsno ty vars,ref (Some r')) 
195            ) constructors
196           in
197            (name,inductive,cook_prod curi cookingsno arity vars,constructors')
198         ) dl
199       in
200        let number_of_variables_without_a_body =
201         let is_not_letin uri =
202          match CicEnvironment.get_obj uri with
203             C.Variable (_,None,_) -> true
204           | C.Variable (_,Some _,_) -> false
205           | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
206         in
207          List.fold_left
208           (fun i uri -> if is_not_letin uri then i + 1 else i) 0 vars
209        in
210         C.InductiveDefinition
211          (dl', params, n_ind_params + number_of_variables_without_a_body)
212 ;; 
213
214 let cook_obj obj uri =
215  let module C = Cic in
216   let params =
217    match obj with
218       C.Definition (_,_,_,params) -> params
219     | C.Axiom (_,_,params) -> params
220     | C.Variable _ -> []
221     | C.CurrentProof _ -> []
222     | C.InductiveDefinition (_,params,_) -> params
223   in
224    let rec cook_all_levels obj =
225     function
226        [] -> []
227      | (n,vars)::tl ->
228         let cooked_obj = cook_one_level obj uri (n + 1) (List.rev vars) in
229          (n,cooked_obj)::(cook_all_levels cooked_obj tl)
230    in
231     cook_all_levels obj (List.rev params)
232 ;;
233
234 let init () =
235    CicEnvironment.set_cooking_function cook_obj
236 ;;