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