]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicReductionNaif.ml
95f24ebf39d6963bfc9df75e16873eefac315126
[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    prerr_endline (s ^ "\n" ^ List.fold_right debug_aux (t::env) "")
38 ;;
39
40 exception Impossible of int;;
41 exception ReferenceToConstant;;
42 exception ReferenceToVariable;;
43 exception ReferenceToCurrentProof;;
44 exception ReferenceToInductiveDefinition;;
45 exception RelToHiddenHypothesis;;
46
47 (* takes a well-typed term *)
48 let whd context =
49  let rec whdaux l =
50   let module C = Cic in
51   let module S = CicSubstitution in
52    function
53       C.Rel n as t ->
54        (match List.nth context (n-1) with
55            Some (_, C.Decl _) -> if l = [] then t else C.Appl (t::l)
56          | Some (_, C.Def (bo,_)) -> whdaux l (S.lift n bo)
57           | None -> raise RelToHiddenHypothesis
58        )
59     | C.Var (uri,exp_named_subst) as t ->
60        (match CicEnvironment.get_cooked_obj ~trust:false uri with
61            C.Constant _ -> raise ReferenceToConstant
62          | C.CurrentProof _ -> raise ReferenceToCurrentProof
63          | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
64          | C.Variable (_,None,_,_) -> if l = [] then t else C.Appl (t::l)
65          | C.Variable (_,Some body,_,_) ->
66             whdaux l (CicSubstitution.subst_vars exp_named_subst body)
67        )
68     | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
69     | C.Sort _ as t -> t (* l should be empty *)
70     | C.Implicit _ as t -> t
71     | C.Cast (te,ty) -> whdaux l te  (*CSC E' GIUSTO BUTTARE IL CAST? *)
72     | C.Prod _ as t -> t (* l should be empty *)
73     | C.Lambda (name,s,t) as t' ->
74        (match l with
75            [] -> t'
76          | he::tl -> whdaux tl (S.subst he t)
77            (* when name is Anonimous the substitution should be superfluous *)
78        )
79     | C.LetIn (n,s,t) -> whdaux l (S.subst (whdaux [] s) t)
80     | C.Appl (he::tl) -> whdaux (tl@l) he
81     | C.Appl [] -> raise (Impossible 1)
82     | C.Const (uri,exp_named_subst) as t ->
83        (match CicEnvironment.get_cooked_obj ~trust:false uri with
84            C.Constant (_,Some body,_,_) ->
85             whdaux l (CicSubstitution.subst_vars exp_named_subst body)
86          | C.Constant _ -> if l = [] then t else C.Appl (t::l)
87          | C.Variable _ -> raise ReferenceToVariable
88          | C.CurrentProof (_,_,body,_,_) ->
89             whdaux l (CicSubstitution.subst_vars exp_named_subst body)
90          | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
91        )
92     | C.MutInd _ as t -> if l = [] then t else C.Appl (t::l)
93     | C.MutConstruct _ as t -> if l = [] then t else C.Appl (t::l)
94     | C.MutCase (mutind,i,_,term,pl) as t->
95        let decofix =
96         function
97            C.CoFix (i,fl) as t ->
98             let (_,_,body) = List.nth fl i in
99              let body' =
100               let counter = ref (List.length fl) in
101                List.fold_right
102                 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
103                 fl
104                 body
105              in
106               whdaux [] body'
107          | C.Appl (C.CoFix (i,fl) :: tl) ->
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 tl body'
117          | t -> t
118        in
119         (match decofix (whdaux [] term) with
120             C.MutConstruct (_,_,j,_) -> whdaux l (List.nth pl (j-1))
121           | C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
122              let (arity, r) =
123               match CicEnvironment.get_obj mutind with
124                  C.InductiveDefinition (tl,ingredients,r) ->
125                    let (_,_,arity,_) = List.nth tl i in
126                     (arity,r)
127                | _ -> raise WrongUriToInductiveDefinition
128              in
129               let ts =
130                let rec eat_first =
131                 function
132                    (0,l) -> l
133                  | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
134                  | _ -> raise (Impossible 5)
135                in
136                 eat_first (r,tl)
137               in
138                whdaux (ts@l) (List.nth pl (j-1))
139           | C.Cast _ | C.Implicit _ ->
140              raise (Impossible 2) (* we don't trust our whd ;-) *)
141           | _ -> if l = [] then t else C.Appl (t::l)
142        )
143     | C.Fix (i,fl) as t ->
144        let (_,recindex,_,body) = List.nth fl i in
145         let recparam =
146          try
147           Some (List.nth l recindex)
148          with
149           _ -> None
150         in
151          (match recparam with
152              Some recparam ->
153               (match whdaux [] recparam with
154                   C.MutConstruct _
155                 | C.Appl ((C.MutConstruct _)::_) ->
156                    let body' =
157                     let counter = ref (List.length fl) in
158                      List.fold_right
159                       (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
160                       fl
161                       body
162                    in
163                     (* Possible optimization: substituting whd recparam in l *)
164                     whdaux l body'
165                | _ -> if l = [] then t else C.Appl (t::l)
166              )
167           | None -> if l = [] then t else C.Appl (t::l)
168          )
169     | C.CoFix (i,fl) as t ->
170        if l = [] then t else C.Appl (t::l)
171  in
172 (*CSC
173 function t ->
174 prerr_endline ("PRIMA WHD" ^ CicPp.ppterm t) ; flush stderr ;
175 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 ;
176 let res =
177 *)
178   whdaux []
179 (*CSC
180 t in prerr_endline "DOPO WHD" ; flush stderr ; res
181 *)
182 ;;
183
184 (* t1, t2 must be well-typed *)
185 let are_convertible =
186  let module U = UriManager in
187  let rec aux test_equality_only context t1 t2 =
188   let aux2 test_equality_only t1 t2 =
189    (* this trivial euristic cuts down the total time of about five times ;-) *)
190    (* this because most of the time t1 and t2 are "sintactically" the same   *)
191    if t1 = t2 then
192     true
193    else
194     begin
195      let module C = Cic in
196        match (t1,t2) with
197           (C.Rel n1, C.Rel n2) -> n1 = n2
198         | (C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2)) ->
199             U.eq uri1 uri2 &&
200              (try
201                List.fold_right2
202                 (fun (uri1,x) (uri2,y) b ->
203                   U.eq uri1 uri2 && aux test_equality_only context x y && b
204                 ) exp_named_subst1 exp_named_subst2 true 
205               with
206                Invalid_argument _ -> false
207              )
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 test_equality_only context t1' t2'
217               ) true l1 l2
218           (* TASSI: CONSTRAINTS *)
219         | (C.Sort (C.Type t1), C.Sort (C.Type t2)) when test_equality_only ->
220             CicUniv.add_eq t2 t1
221           (* TASSI: CONSTRAINTS *)
222         | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
223             CicUniv.add_ge t2 t1
224           (* TASSI: CONSTRAINTS *)
225         | (C.Sort s1, C.Sort (C.Type _)) -> not test_equality_only
226           (* TASSI: CONSTRAINTS *)
227         | (C.Sort s1, C.Sort s2) -> s1 = s2
228         | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
229            aux true context s1 s2 &&
230             aux test_equality_only ((Some (name1, (C.Decl s1)))::context) t1 t2
231         | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) ->
232            aux test_equality_only context s1 s2 &&
233             aux test_equality_only ((Some (name1, (C.Decl s1)))::context) t1 t2
234         | (C.LetIn (name1,s1,t1), C.LetIn(_,s2,t2)) ->
235            aux test_equality_only context s1 s2 &&
236             aux test_equality_only
237              ((Some (name1, (C.Def (s1,None))))::context) t1 t2
238         | (C.Appl l1, C.Appl l2) ->
239            (try
240              List.fold_right2
241               (fun  x y b -> aux test_equality_only context x y && b) l1 l2 true
242             with
243              Invalid_argument _ -> false
244            )
245         | (C.Const (uri1,exp_named_subst1), C.Const (uri2,exp_named_subst2)) ->
246             U.eq uri1 uri2 &&
247              (try
248                List.fold_right2
249                 (fun (uri1,x) (uri2,y) b ->
250                   U.eq uri1 uri2 && aux test_equality_only context x y && b
251                 ) exp_named_subst1 exp_named_subst2 true 
252               with
253                Invalid_argument _ -> false
254              )
255         | (C.MutInd (uri1,i1,exp_named_subst1),
256            C.MutInd (uri2,i2,exp_named_subst2)
257           ) ->
258             U.eq uri1 uri2 && i1 = i2 &&
259              (try
260                List.fold_right2
261                 (fun (uri1,x) (uri2,y) b ->
262                   U.eq uri1 uri2 && aux test_equality_only context x y && b
263                 ) exp_named_subst1 exp_named_subst2 true 
264               with
265                Invalid_argument _ -> false
266              )
267         | (C.MutConstruct (uri1,i1,j1,exp_named_subst1),
268            C.MutConstruct (uri2,i2,j2,exp_named_subst2)
269           ) ->
270             U.eq uri1 uri2 && i1 = i2 && j1 = j2 &&
271              (try
272                List.fold_right2
273                 (fun (uri1,x) (uri2,y) b ->
274                   U.eq uri1 uri2 && aux test_equality_only context x y && b
275                 ) exp_named_subst1 exp_named_subst2 true 
276               with
277                Invalid_argument _ -> false
278              )
279         | (C.MutCase (uri1,i1,outtype1,term1,pl1),
280            C.MutCase (uri2,i2,outtype2,term2,pl2)) -> 
281             U.eq uri1 uri2 && i1 = i2 &&
282              aux test_equality_only context outtype1 outtype2 &&
283              aux test_equality_only context term1 term2 &&
284              List.fold_right2
285               (fun x y b -> b && aux test_equality_only context x y)
286               pl1 pl2 true
287         | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
288            let tys =
289             List.map (function (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
290            in
291             i1 = i2 &&
292              List.fold_right2
293               (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) b ->
294                 b && recindex1 = recindex2 &&
295                  aux test_equality_only context ty1 ty2 &&
296                  aux test_equality_only (tys@context) bo1 bo2)
297               fl1 fl2 true
298         | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
299            let tys =
300             List.map (function (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
301            in
302             i1 = i2 &&
303              List.fold_right2
304               (fun (_,ty1,bo1) (_,ty2,bo2) b ->
305                 b && aux test_equality_only context ty1 ty2 &&
306                  aux test_equality_only (tys@context) bo1 bo2)
307               fl1 fl2 true
308         | (C.Cast _, _) | (_, C.Cast _)
309         | (C.Implicit _, _) | (_, C.Implicit _) ->
310             assert false
311         | (_,_) -> false
312     end
313   in
314    if aux2 test_equality_only t1 t2 then true
315    else
316     begin
317      debug t1 [t2] "PREWHD";
318      let t1' = whd context t1 in
319      let t2' = whd context t2 in
320       debug t1' [t2'] "POSTWHD";
321       aux2 test_equality_only t1' t2'
322     end
323  in
324   aux false
325 ;;