]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicReduction.ml
LetIn reduction (alias zeta-reduction) is now performed during whd.
[helm.git] / helm / ocaml / cic_proof_checking / cicReduction.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 CicReductionInternalError;;
27 exception WrongUriToInductiveDefinition;;
28
29 let fdebug = ref 1;;
30 let debug t env s =
31  let rec debug_aux t i =
32   let module C = Cic in
33   let module U = UriManager in
34    CicPp.ppobj (C.Variable ("DEBUG", None,
35     C.Prod (C.Name "-9", C.Const (U.uri_of_string "cic:/dummy-9",0),
36      C.Prod (C.Name "-8", C.Const (U.uri_of_string "cic:/dummy-8",0),
37       C.Prod (C.Name "-7", C.Const (U.uri_of_string "cic:/dummy-7",0),
38        C.Prod (C.Name "-6", C.Const (U.uri_of_string "cic:/dummy-6",0),
39         C.Prod (C.Name "-5", C.Const (U.uri_of_string "cic:/dummy-5",0),
40          C.Prod (C.Name "-4", C.Const (U.uri_of_string "cic:/dummy-4",0),
41           C.Prod (C.Name "-3", C.Const (U.uri_of_string "cic:/dummy-3",0),
42            C.Prod (C.Name "-2", C.Const (U.uri_of_string "cic:/dummy-2",0),
43             C.Prod (C.Name "-1", C.Const (U.uri_of_string "cic:/dummy-1",0),
44              t
45             )
46            )
47           )
48          )
49         )
50        )
51       )
52      )
53     )
54     )) ^ "\n" ^ i
55  in
56   if !fdebug = 0 then
57    begin
58     print_endline (s ^ "\n" ^ List.fold_right debug_aux (t::env) "") ;
59     flush stdout
60    end
61 ;;
62
63 exception Impossible of int;;
64 exception ReferenceToDefinition;;
65 exception ReferenceToAxiom;;
66 exception ReferenceToVariable;;
67 exception ReferenceToCurrentProof;;
68 exception ReferenceToInductiveDefinition;;
69
70 (* takes a well-typed term *)
71 let whd =
72  let rec whdaux l =
73   let module C = Cic in
74   let module S = CicSubstitution in
75    function
76       C.Rel _ as t -> if l = [] then t else C.Appl (t::l)
77     | C.Var _ as t -> if l = [] then t else C.Appl (t::l)
78     | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
79     | C.Sort _ as t -> t (* l should be empty *)
80     | C.Implicit as t -> t
81     | C.Cast (te,ty) -> whdaux l te  (*CSC E' GIUSTO BUTTARE IL CAST? *)
82     | C.Prod _ as t -> t (* l should be empty *)
83     | C.Lambda (name,s,t) as t' ->
84        (match l with
85            [] -> t'
86          | he::tl -> whdaux tl (S.subst he t)
87            (* when name is Anonimous the substitution should be superfluous *)
88        )
89     | C.LetIn (n,s,t) -> whdaux l (S.subst (whdaux [] s) t)
90     | C.Appl (he::tl) -> whdaux (tl@l) he
91     | C.Appl [] -> raise (Impossible 1)
92     | C.Const (uri,cookingsno) as t ->
93        (match CicEnvironment.get_cooked_obj uri cookingsno with
94            C.Definition (_,body,_,_) -> whdaux l body
95          | C.Axiom _ -> if l = [] then t else C.Appl (t::l)
96          (*CSC: Prossima riga sbagliata: Var punta alle variabili, non Const *)
97          | C.Variable _ -> if l = [] then t else C.Appl (t::l)
98          | C.CurrentProof (_,_,body,_) -> whdaux l body
99          | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
100        )
101     | C.Abst _ as t -> t (*CSC l should be empty ????? *)
102     | C.MutInd (uri,_,_) as t -> if l = [] then t else C.Appl (t::l)
103     | C.MutConstruct (uri,_,_,_) as t -> if l = [] then t else C.Appl (t::l)
104     | C.MutCase (mutind,cookingsno,i,_,term,pl) as t ->
105        let decofix =
106         function
107            C.CoFix (i,fl) as t ->
108             let (_,_,body) = List.nth fl i in
109              let body' =
110               let counter = ref (List.length fl) in
111                List.fold_right
112                 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
113                 fl
114                 body
115              in
116               whdaux [] body'
117          | C.Appl (C.CoFix (i,fl) :: tl) ->
118             let (_,_,body) = List.nth fl i in
119              let body' =
120               let counter = ref (List.length fl) in
121                List.fold_right
122                 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
123                 fl
124                 body
125              in
126               whdaux tl body'
127          | t -> t
128        in
129         (match decofix (whdaux [] term) with
130             C.MutConstruct (_,_,_,j) -> whdaux l (List.nth pl (j-1))
131           | C.Appl (C.MutConstruct (_,_,_,j) :: tl) ->
132              let (arity, r, num_ingredients) =
133               match CicEnvironment.get_obj mutind with
134                  C.InductiveDefinition (tl,ingredients,r) ->
135                    let (_,_,arity,_) = List.nth tl i
136                    and num_ingredients =
137                     List.fold_right
138                      (fun (k,l) i ->
139                        if k < cookingsno then i + List.length l else i
140                      ) ingredients 0
141                    in
142                     (arity,r,num_ingredients)
143                | _ -> raise WrongUriToInductiveDefinition
144              in
145               let ts =
146                let num_to_eat = r + num_ingredients in
147                 let rec eat_first =
148                  function
149                     (0,l) -> l
150                   | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
151                   | _ -> raise (Impossible 5)
152                 in
153                  eat_first (num_to_eat,tl)
154               in
155                whdaux (ts@l) (List.nth pl (j-1))
156          | C.Abst _| C.Cast _ | C.Implicit ->
157             raise (Impossible 2) (* we don't trust our whd ;-) *)
158          | _ -> t
159        )
160     | C.Fix (i,fl) as t ->
161        let (_,recindex,_,body) = List.nth fl i in
162         let recparam =
163          try
164           Some (List.nth l recindex)
165          with
166           _ -> None
167         in
168          (match recparam with
169              Some recparam ->
170               (match whdaux [] recparam with
171                   C.MutConstruct _
172                 | C.Appl ((C.MutConstruct _)::_) ->
173                    let body' =
174                     let counter = ref (List.length fl) in
175                      List.fold_right
176                       (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
177                       fl
178                       body
179                    in
180                     (* Possible optimization: substituting whd recparam in l *)
181                     whdaux l body'
182                | _ -> if l = [] then t else C.Appl (t::l)
183              )
184           | None -> if l = [] then t else C.Appl (t::l)
185          )
186     | C.CoFix (i,fl) as t ->
187        (*CSC vecchio codice
188        let (_,_,body) = List.nth fl i in
189         let body' =
190          let counter = ref (List.length fl) in
191           List.fold_right
192            (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
193            fl
194            body
195         in
196          whdaux l body'
197        *)
198        if l = [] then t else C.Appl (t::l)
199  in
200   whdaux []
201 ;;
202
203 (* t1, t2 must be well-typed *)
204 let are_convertible t1 t2 =
205  let module U = UriManager in
206  let rec aux t1 t2 =
207   debug t1 [t2] "PREWHD";
208   (* this trivial euristic cuts down the total time of about five times ;-) *)
209   (* this because most of the time t1 and t2 are "sintactically" the same   *)
210   if t1 = t2 then
211    true
212   else
213    begin
214     let module C = Cic in
215      let t1' = whd t1 
216      and t2' = whd t2 in
217      debug t1' [t2'] "POSTWHD";
218      (*if !fdebug = 0 then ignore(Unix.system "read" );*)
219       match (t1',t2') with
220          (C.Rel n1, C.Rel n2) -> n1 = n2
221        | (C.Var uri1, C.Var uri2) -> U.eq uri1 uri2
222        | (C.Meta n1, C.Meta n2) -> n1 = n2
223        | (C.Sort s1, C.Sort s2) -> true (*CSC da finire con gli universi *)
224        | (C.Prod (_,s1,t1), C.Prod(_,s2,t2)) ->
225           aux s1 s2 && aux t1 t2
226        | (C.Lambda (_,s1,t1), C.Lambda(_,s2,t2)) ->
227           aux s1 s2 && aux t1 t2
228        | (C.Appl l1, C.Appl l2) ->
229           (try
230             List.fold_right2 (fun  x y b -> aux x y && b) l1 l2 true 
231            with
232             Invalid_argument _ -> false
233           )
234        | (C.Const (uri1,_), C.Const (uri2,_)) ->
235            (*CSC: questo commento e' chiaro o delirante? Io lo sto scrivendo *)
236            (*CSC: mentre sono delirante, quindi ...                          *)
237            (* WARNING: it is really important that the two cookingsno are not *)
238            (* checked for equality. This allows not to cook an object with no *)
239            (* ingredients only to update the cookingsno. E.g: if a term t has *)
240            (* a reference to a term t1 which does not depend on any variable  *)
241            (* and t1 depends on a term t2 (that can't depend on any variable  *)
242            (* because of t1), then t1 cooked at every level could be the same *)
243            (* as t1 cooked at level 0. Doing so, t2 will be extended in t     *)
244            (* with cookingsno 0 and not 2. But this will not cause any trouble*)
245            (* if here we don't check that the two cookingsno are equal.       *)
246            U.eq uri1 uri2
247        | (C.MutInd (uri1,k1,i1), C.MutInd (uri2,k2,i2)) ->
248            (* WARNIG: see the previous warning *)
249            U.eq uri1 uri2 && i1 = i2
250        | (C.MutConstruct (uri1,_,i1,j1), C.MutConstruct (uri2,_,i2,j2)) ->
251            (* WARNIG: see the previous warning *)
252            U.eq uri1 uri2 && i1 = i2 && j1 = j2
253        | (C.MutCase (uri1,_,i1,outtype1,term1,pl1),
254           C.MutCase (uri2,_,i2,outtype2,term2,pl2)) -> 
255            (* WARNIG: see the previous warning *)
256            (* aux outtype1 outtype2 should be true if aux pl1 pl2 *)
257            U.eq uri1 uri2 && i1 = i2 && aux outtype1 outtype2 &&
258             aux term1 term2 &&
259             List.fold_right2 (fun x y b -> b && aux x y) pl1 pl2 true
260        | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
261           i1 = i2 &&
262            List.fold_right2
263             (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) b ->
264               b && recindex1 = recindex2 && aux ty1 ty2 && aux bo1 bo2)
265             fl1 fl2 true
266        | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
267           i1 = i2 &&
268            List.fold_right2
269             (fun (_,ty1,bo1) (_,ty2,bo2) b ->
270               b && aux ty1 ty2 && aux bo1 bo2)
271             fl1 fl2 true
272        | (C.Abst _, _) | (_, C.Abst _) | (C.Cast _, _) | (_, C.Cast _)
273        | (C.Implicit, _) | (_, C.Implicit) ->
274           raise (Impossible 3) (* we don't trust our whd ;-) *)
275        | (_,_) -> false
276    end
277  in
278   aux t1 t2
279 ;;