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