]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicTypeChecker.ml
Initial revision
[helm.git] / helm / ocaml / cic_proof_checking / cicTypeChecker.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 of int;;
27 exception NotWellTyped of string;;
28 exception WrongUriToConstant of string;;
29 exception WrongUriToVariable of string;;
30 exception WrongUriToMutualInductiveDefinitions of string;;
31 exception ListTooShort;;
32 exception NotPositiveOccurrences of string;;
33 exception NotWellFormedTypeOfInductiveConstructor of string;;
34 exception WrongRequiredArgument of string;;
35 exception RelToHiddenHypothesis;;
36 exception MetasenvInconsistency;;
37
38 let fdebug = ref 0;;
39 let debug t context =
40  let rec debug_aux t i =
41   let module C = Cic in
42   let module U = UriManager in
43    CicPp.ppobj (C.Variable ("DEBUG", None, t)) ^ "\n" ^ i
44  in
45   if !fdebug = 0 then
46    raise (NotWellTyped ("\n" ^ List.fold_right debug_aux (t::context) ""))
47    (*print_endline ("\n" ^ List.fold_right debug_aux (t::context) "") ; flush stdout*)
48 ;;
49
50 let rec split l n =
51  match (l,n) with
52     (l,0) -> ([], l)
53   | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
54   | (_,_) -> raise ListTooShort
55 ;;
56
57 exception CicEnvironmentError;;
58
59 let rec cooked_type_of_constant uri cookingsno =
60  let module C = Cic in
61  let module R = CicReduction in
62  let module U = UriManager in
63   let cobj =
64    match CicEnvironment.is_type_checked uri cookingsno with
65       CicEnvironment.CheckedObj cobj -> cobj
66     | CicEnvironment.UncheckedObj uobj ->
67        Logger.log (`Start_type_checking uri) ;
68        (* let's typecheck the uncooked obj *)
69        (match uobj with
70            C.Definition (_,te,ty,_) ->
71              let _ = type_of ty in
72               if not (R.are_convertible [] (type_of te) ty) then
73                raise (NotWellTyped ("Constant " ^ (U.string_of_uri uri)))
74          | C.Axiom (_,ty,_) ->
75            (* only to check that ty is well-typed *)
76            let _ = type_of ty in ()
77          | C.CurrentProof (_,conjs,te,ty) ->
78              (*CSC: bisogna controllare anche il metasenv!!! *)
79              let _ = type_of_aux' conjs [] ty in
80               if not (R.are_convertible [] (type_of_aux' conjs [] te) ty)
81               then
82                raise (NotWellTyped ("CurrentProof" ^ (U.string_of_uri uri)))
83          | _ -> raise (WrongUriToConstant (U.string_of_uri uri))
84        ) ;
85        CicEnvironment.set_type_checking_info uri ;
86        Logger.log (`Type_checking_completed uri) ;
87        match CicEnvironment.is_type_checked uri cookingsno with
88           CicEnvironment.CheckedObj cobj -> cobj
89         | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
90   in
91    match cobj with
92       C.Definition (_,_,ty,_) -> ty
93     | C.Axiom (_,ty,_) -> ty
94     | C.CurrentProof (_,_,_,ty) -> ty
95     | _ -> raise (WrongUriToConstant (U.string_of_uri uri))
96
97 and type_of_variable uri =
98  let module C = Cic in
99  let module R = CicReduction in
100  let module U = UriManager in
101   (* 0 because a variable is never cooked => no partial cooking at one level *)
102   match CicEnvironment.is_type_checked uri 0 with
103      CicEnvironment.CheckedObj (C.Variable (_,_,ty)) -> ty
104    | CicEnvironment.UncheckedObj (C.Variable (_,bo,ty)) ->
105       Logger.log (`Start_type_checking uri) ;
106       (* only to check that ty is well-typed *)
107       let _ = type_of ty in
108        (match bo with
109            None -> ()
110          | Some bo ->
111             if not (R.are_convertible [] (type_of bo) ty) then
112              raise (NotWellTyped ("Variable " ^ (U.string_of_uri uri)))
113        ) ;
114        CicEnvironment.set_type_checking_info uri ;
115        Logger.log (`Type_checking_completed uri) ;
116        ty
117    |  _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
118
119 and does_not_occur context n nn te =
120  let module C = Cic in
121    (*CSC: whd sembra essere superflua perche' un caso in cui l'occorrenza *)
122    (*CSC: venga mangiata durante la whd sembra presentare problemi di *)
123    (*CSC: universi                                                    *)
124    match CicReduction.whd context te with
125       C.Rel m when m > n && m <= nn -> false
126     | C.Rel _
127     | C.Var _
128     | C.Meta _
129     | C.Sort _
130     | C.Implicit -> true
131     | C.Cast (te,ty) ->
132        does_not_occur context n nn te && does_not_occur context n nn ty
133     | C.Prod (name,so,dest) ->
134        does_not_occur context n nn so &&
135         does_not_occur((Some (name,(C.Decl so)))::context) (n + 1) (nn + 1) dest
136     | C.Lambda (name,so,dest) ->
137        does_not_occur context n nn so &&
138         does_not_occur((Some (name,(C.Decl so)))::context) (n + 1) (nn + 1) dest
139     | C.LetIn (name,so,dest) ->
140        does_not_occur context n nn so &&
141         does_not_occur ((Some (name,(C.Def so)))::context) (n + 1) (nn + 1) dest
142     | C.Appl l ->
143        List.fold_right (fun x i -> i && does_not_occur context n nn x) l true
144     | C.Const _
145     | C.MutInd _
146     | C.MutConstruct _ -> true
147     | C.MutCase (_,_,_,out,te,pl) ->
148        does_not_occur context n nn out && does_not_occur context n nn te &&
149         List.fold_right (fun x i -> i && does_not_occur context n nn x) pl true
150     | C.Fix (_,fl) ->
151        let len = List.length fl in
152         let n_plus_len = n + len in
153         let nn_plus_len = nn + len in
154         let tys =
155          List.map (fun (n,_,ty,_) -> Some (C.Name n,(Cic.Decl ty))) fl
156         in
157          List.fold_right
158           (fun (_,_,ty,bo) i ->
159             i && does_not_occur context n nn ty &&
160             does_not_occur (tys @ context) n_plus_len nn_plus_len bo
161           ) fl true
162     | C.CoFix (_,fl) ->
163        let len = List.length fl in
164         let n_plus_len = n + len in
165         let nn_plus_len = nn + len in
166         let tys =
167          List.map (fun (n,ty,_) -> Some (C.Name n,(Cic.Decl ty))) fl
168         in
169          List.fold_right
170           (fun (_,ty,bo) i ->
171             i && does_not_occur context n nn ty &&
172             does_not_occur (tys @ context) n_plus_len nn_plus_len bo
173           ) fl true
174
175 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
176 (*CSC questa funzione e' simile alla are_all_occurrences_positive, ma fa *)
177 (*CSC dei controlli leggermente diversi. Viene invocata solamente dalla  *)
178 (*CSC strictly_positive                                                  *)
179 (*CSC definizione (giusta???) tratta dalla mail di Hugo ;-)              *)
180 and weakly_positive context n nn uri te =
181  let module C = Cic in
182 (*CSC: Che schifo! Bisogna capire meglio e trovare una soluzione ragionevole!*)
183   let dummy_mutind =
184    C.MutInd (UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind",0,0)
185   in
186   (*CSC mettere in cicSubstitution *)
187   let rec subst_inductive_type_with_dummy_mutind =
188    function
189       C.MutInd (uri',_,0) when UriManager.eq uri' uri ->
190        dummy_mutind
191     | C.Appl ((C.MutInd (uri',_,0))::tl) when UriManager.eq uri' uri ->
192        dummy_mutind
193     | C.Cast (te,ty) -> subst_inductive_type_with_dummy_mutind te
194     | C.Prod (name,so,ta) ->
195        C.Prod (name, subst_inductive_type_with_dummy_mutind so,
196         subst_inductive_type_with_dummy_mutind ta)
197     | C.Lambda (name,so,ta) ->
198        C.Lambda (name, subst_inductive_type_with_dummy_mutind so,
199         subst_inductive_type_with_dummy_mutind ta)
200     | C.Appl tl ->
201        C.Appl (List.map subst_inductive_type_with_dummy_mutind tl)
202     | C.MutCase (uri,cookingsno,i,outtype,term,pl) ->
203        C.MutCase (uri,cookingsno,i,
204         subst_inductive_type_with_dummy_mutind outtype,
205         subst_inductive_type_with_dummy_mutind term,
206         List.map subst_inductive_type_with_dummy_mutind pl)
207     | C.Fix (i,fl) ->
208        C.Fix (i,List.map (fun (name,i,ty,bo) -> (name,i,
209         subst_inductive_type_with_dummy_mutind ty,
210         subst_inductive_type_with_dummy_mutind bo)) fl)
211     | C.CoFix (i,fl) ->
212        C.CoFix (i,List.map (fun (name,ty,bo) -> (name,
213         subst_inductive_type_with_dummy_mutind ty,
214         subst_inductive_type_with_dummy_mutind bo)) fl)
215     | t -> t
216   in
217   match CicReduction.whd context te with
218      C.Appl ((C.MutInd (uri',_,0))::tl) when UriManager.eq uri' uri -> true
219    | C.MutInd (uri',_,0) when UriManager.eq uri' uri -> true
220    | C.Prod (C.Anonimous,source,dest) ->
221       strictly_positive context n nn
222        (subst_inductive_type_with_dummy_mutind source) &&
223        weakly_positive ((Some (C.Anonimous,(C.Decl source)))::context)
224         (n + 1) (nn + 1) uri dest
225    | C.Prod (name,source,dest) when
226       does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
227        (* dummy abstraction, so we behave as in the anonimous case *)
228        strictly_positive context n nn
229         (subst_inductive_type_with_dummy_mutind source) &&
230         weakly_positive ((Some (name,(C.Decl source)))::context)
231          (n + 1) (nn + 1) uri dest
232    | C.Prod (name,source,dest) ->
233       does_not_occur context n nn
234        (subst_inductive_type_with_dummy_mutind source)&&
235        weakly_positive ((Some (name,(C.Decl source)))::context)
236         (n + 1) (nn + 1) uri dest
237    | _ -> raise (NotWellFormedTypeOfInductiveConstructor ("Guess where the error is ;-)"))
238
239 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C                             *)
240 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
241 and instantiate_parameters params c =
242  let module C = Cic in
243   match (c,params) with
244      (c,[]) -> c
245    | (C.Prod (_,_,ta), he::tl) ->
246        instantiate_parameters tl
247         (CicSubstitution.subst he ta)
248    | (C.Cast (te,_), _) -> instantiate_parameters params te
249    | (t,l) -> raise (Impossible 1)
250
251 and strictly_positive context n nn te =
252  let module C = Cic in
253  let module U = UriManager in
254   match CicReduction.whd context te with
255      C.Rel _ -> true
256    | C.Cast (te,ty) ->
257       (*CSC: bisogna controllare ty????*)
258       strictly_positive context n nn te
259    | C.Prod (name,so,ta) ->
260       does_not_occur context n nn so &&
261        strictly_positive ((Some (name,(C.Decl so)))::context) (n+1) (nn+1) ta
262    | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
263       List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
264    | C.Appl ((C.MutInd (uri,_,i))::tl) -> 
265       let (ok,paramsno,ity,cl,name) =
266        match CicEnvironment.get_obj uri with
267            C.InductiveDefinition (tl,_,paramsno) ->
268             let (name,_,ity,cl) = List.nth tl i in
269              (List.length tl = 1, paramsno, ity, cl, name)
270          | _ -> raise(WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
271       in
272        let (params,arguments) = split tl paramsno in
273        let lifted_params = List.map (CicSubstitution.lift 1) params in
274        let cl' =
275         List.map (fun (_,te,_) -> instantiate_parameters lifted_params te) cl
276        in
277         ok &&
278          List.fold_right
279           (fun x i -> i && does_not_occur context n nn x)
280           arguments true &&
281          (*CSC: MEGAPATCH3 (sara' quella giusta?)*)
282          List.fold_right
283           (fun x i ->
284             i &&
285              weakly_positive
286               ((Some (C.Name name,(Cic.Decl ity)))::context) (n+1) (nn+1) uri x
287           ) cl' true
288    | t -> does_not_occur context n nn t
289
290 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
291 and are_all_occurrences_positive context uri indparamsno i n nn te =
292  let module C = Cic in
293   match CicReduction.whd context te with
294      C.Appl ((C.Rel m)::tl) when m = i ->
295       (*CSC: riscrivere fermandosi a 0 *)
296       (* let's check if the inductive type is applied at least to *)
297       (* indparamsno parameters                                   *)
298       let last =
299        List.fold_left
300         (fun k x ->
301           if k = 0 then 0
302           else
303            match CicReduction.whd context x with
304               C.Rel m when m = n - (indparamsno - k) -> k - 1
305             | _ -> raise (WrongRequiredArgument (UriManager.string_of_uri uri))
306         ) indparamsno tl
307       in
308        if last = 0 then
309         List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
310        else
311         raise (WrongRequiredArgument (UriManager.string_of_uri uri))
312    | C.Rel m when m = i ->
313       if indparamsno = 0 then
314        true
315       else
316        raise (WrongRequiredArgument (UriManager.string_of_uri uri))
317    | C.Prod (C.Anonimous,source,dest) ->
318       strictly_positive context n nn source &&
319        are_all_occurrences_positive
320         ((Some (C.Anonimous,(C.Decl source)))::context) uri indparamsno
321         (i+1) (n + 1) (nn + 1) dest
322    | C.Prod (name,source,dest) when
323       does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
324       (* dummy abstraction, so we behave as in the anonimous case *)
325       strictly_positive context n nn source &&
326        are_all_occurrences_positive
327         ((Some (name,(C.Decl source)))::context) uri indparamsno
328         (i+1) (n + 1) (nn + 1) dest
329    | C.Prod (name,source,dest) ->
330       does_not_occur context n nn source &&
331        are_all_occurrences_positive ((Some (name,(C.Decl source)))::context)
332         uri indparamsno (i+1) (n + 1) (nn + 1) dest
333    | _ -> raise (NotWellFormedTypeOfInductiveConstructor (UriManager.string_of_uri uri))
334
335 (*CSC: cambiare il nome, torna unit! *)
336 and cooked_mutual_inductive_defs uri =
337  let module U = UriManager in
338   function
339      Cic.InductiveDefinition (itl, _, indparamsno) ->
340       (* let's check if the arity of the inductive types are well *)
341       (* formed                                                   *)
342       List.iter (fun (_,_,x,_) -> let _ = type_of x in ()) itl ;
343
344       (* let's check if the types of the inductive constructors  *)
345       (* are well formed.                                        *)
346       (* In order not to use type_of_aux we put the types of the *)
347       (* mutual inductive types at the head of the types of the  *)
348       (* constructors using Prods                                *)
349       (*CSC: piccola??? inefficienza                             *)
350       let len = List.length itl in
351 (*CSC: siamo sicuri che non debba fare anche un List.rev? Il bug *)
352 (*CSC: si manifesterebbe solamene con tipi veramente mutualmente *)
353 (*CSC: induttivi...                                              *)
354        let tys =
355         List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
356        let _ =
357         List.fold_right
358          (fun (_,_,_,cl) i ->
359            List.iter
360             (fun (name,te,r) -> 
361               let augmented_term =
362                List.fold_right
363                 (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
364                 itl te
365               in
366                let _ = type_of augmented_term in
367                 (* let's check also the positivity conditions *)
368                 if
369                  not
370                   (are_all_occurrences_positive tys uri indparamsno i 0 len te)
371                 then
372                  raise (NotPositiveOccurrences (U.string_of_uri uri))
373                 else
374                  match !r with
375                     Some _ -> raise (Impossible 2)
376                   | None -> r := Some (recursive_args tys 0 len te)
377             ) cl ;
378            (i + 1)
379         ) itl 1
380        in
381         ()
382    | _ ->
383      raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
384
385 and cooked_type_of_mutual_inductive_defs uri cookingsno i =
386  let module C = Cic in
387  let module R = CicReduction in
388  let module U = UriManager in
389   let cobj =
390    match CicEnvironment.is_type_checked uri cookingsno with
391       CicEnvironment.CheckedObj cobj -> cobj
392     | CicEnvironment.UncheckedObj uobj ->
393        Logger.log (`Start_type_checking uri) ;
394        cooked_mutual_inductive_defs uri uobj ;
395        CicEnvironment.set_type_checking_info uri ;
396        Logger.log (`Type_checking_completed uri) ;
397        (match CicEnvironment.is_type_checked uri cookingsno with
398           CicEnvironment.CheckedObj cobj -> cobj
399         | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
400        )
401   in
402    match cobj with
403       C.InductiveDefinition (dl,_,_) ->
404        let (_,_,arity,_) = List.nth dl i in
405         arity
406     | _ -> raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
407
408 and cooked_type_of_mutual_inductive_constr uri cookingsno i j =
409  let module C = Cic in
410  let module R = CicReduction in
411  let module U = UriManager in
412   let cobj =
413    match CicEnvironment.is_type_checked uri cookingsno with
414       CicEnvironment.CheckedObj cobj -> cobj
415     | CicEnvironment.UncheckedObj uobj ->
416        Logger.log (`Start_type_checking uri) ;
417        cooked_mutual_inductive_defs uri uobj ;
418        CicEnvironment.set_type_checking_info uri ;
419        Logger.log (`Type_checking_completed uri) ;
420        (match CicEnvironment.is_type_checked uri cookingsno with
421           CicEnvironment.CheckedObj cobj -> cobj
422         | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
423        )
424   in
425    match cobj with
426       C.InductiveDefinition (dl,_,_) ->
427        let (_,_,_,cl) = List.nth dl i in
428         let (_,ty,_) = List.nth cl (j-1) in
429          ty
430     | _ -> raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
431
432 and recursive_args context n nn te =
433  let module C = Cic in
434   match CicReduction.whd context te with
435      C.Rel _ -> []
436    | C.Var _
437    | C.Meta _
438    | C.Sort _
439    | C.Implicit
440    | C.Cast _ (*CSC ??? *) -> raise (Impossible 3) (* due to type-checking *)
441    | C.Prod (name,so,de) ->
442       (not (does_not_occur context n nn so)) ::
443        (recursive_args ((Some (name,(C.Decl so)))::context) (n+1) (nn + 1) de)
444    | C.Lambda _
445    | C.LetIn _ -> raise (Impossible 4) (* due to type-checking *)
446    | C.Appl _ -> []
447    | C.Const _ -> raise (Impossible 5)
448    | C.MutInd _
449    | C.MutConstruct _
450    | C.MutCase _
451    | C.Fix _
452    | C.CoFix _ -> raise (Impossible 6) (* due to type-checking *)
453
454 and get_new_safes context p c rl safes n nn x =
455  let module C = Cic in
456  let module U = UriManager in
457  let module R = CicReduction in
458   match (R.whd context c, R.whd context p, rl) with
459      (C.Prod (_,so,ta1), C.Lambda (name,_,ta2), b::tl) ->
460        (* we are sure that the two sources are convertible because we *)
461        (* have just checked this. So let's go along ...               *)
462        let safes' =
463         List.map (fun x -> x + 1) safes
464        in
465         let safes'' =
466          if b then 1::safes' else safes'
467         in
468          get_new_safes ((Some (name,(C.Decl so)))::context)
469           ta2 ta1 tl safes'' (n+1) (nn+1) (x+1)
470    | (C.Prod _, (C.MutConstruct _ as e), _)
471    | (C.Prod _, (C.Rel _ as e), _)
472    | (C.MutInd _, e, [])
473    | (C.Appl _, e, []) -> (e,safes,n,nn,x,context)
474    | (_,_,_) ->
475       (* CSC: If the next exception is raised, it just means that   *)
476       (* CSC: the proof-assistant allows to use very strange things *)
477       (* CSC: as a branch of a case whose type is a Prod. In        *)
478       (* CSC: particular, this means that a new (C.Prod, x,_) case  *)
479       (* CSC: must be considered in this match. (e.g. x = MutCase)  *)
480       raise (Impossible 7)
481
482 and split_prods context n te =
483  let module C = Cic in
484  let module R = CicReduction in
485   match (n, R.whd context te) with
486      (0, _) -> context,te
487    | (n, C.Prod (name,so,ta)) when n > 0 ->
488        split_prods ((Some (name,(C.Decl so)))::context) (n - 1) ta
489    | (_, _) -> raise (Impossible 8)
490
491 and eat_lambdas context n te =
492  let module C = Cic in
493  let module R = CicReduction in
494   match (n, R.whd context te) with
495      (0, _) -> (te, 0, context)
496    | (n, C.Lambda (name,so,ta)) when n > 0 ->
497       let (te, k, context') =
498        eat_lambdas ((Some (name,(C.Decl so)))::context) (n - 1) ta
499       in
500        (te, k + 1, context')
501    | (_, _) -> raise (Impossible 9)
502
503 (*CSC: Tutto quello che segue e' l'intuzione di luca ;-) *)
504 and check_is_really_smaller_arg context n nn kl x safes te =
505  (*CSC: forse la whd si puo' fare solo quando serve veramente. *)
506  (*CSC: cfr guarded_by_destructors                             *)
507  let module C = Cic in
508  let module U = UriManager in
509  match CicReduction.whd context te with
510      C.Rel m when List.mem m safes -> true
511    | C.Rel _ -> false
512    | C.Var _
513    | C.Meta _
514    | C.Sort _
515    | C.Implicit 
516    | C.Cast _
517 (*   | C.Cast (te,ty) ->
518       check_is_really_smaller_arg n nn kl x safes te &&
519        check_is_really_smaller_arg n nn kl x safes ty*)
520 (*   | C.Prod (_,so,ta) ->
521       check_is_really_smaller_arg n nn kl x safes so &&
522        check_is_really_smaller_arg (n+1) (nn+1) kl (x+1)
523         (List.map (fun x -> x + 1) safes) ta*)
524    | C.Prod _ -> raise (Impossible 10)
525    | C.Lambda (name,so,ta) ->
526       check_is_really_smaller_arg context n nn kl x safes so &&
527        check_is_really_smaller_arg ((Some (name,(C.Decl so)))::context)
528         (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
529    | C.LetIn (name,so,ta) ->
530       check_is_really_smaller_arg context n nn kl x safes so &&
531        check_is_really_smaller_arg ((Some (name,(C.Def so)))::context)
532         (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
533    | C.Appl (he::_) ->
534       (*CSC: sulla coda ci vogliono dei controlli? secondo noi no, ma *)
535       (*CSC: solo perche' non abbiamo trovato controesempi            *)
536       check_is_really_smaller_arg context n nn kl x safes he
537    | C.Appl [] -> raise (Impossible 11)
538    | C.Const _
539    | C.MutInd _ -> raise (Impossible 12)
540    | C.MutConstruct _ -> false
541    | C.MutCase (uri,_,i,outtype,term,pl) ->
542       (match term with
543           C.Rel m when List.mem m safes || m = x ->
544            let (isinductive,paramsno,cl) =
545             match CicEnvironment.get_obj uri with
546                C.InductiveDefinition (tl,_,paramsno) ->
547                 let tys =
548                  List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) tl
549                 in
550                  let (_,isinductive,_,cl) = List.nth tl i in
551                   let cl' =
552                    List.map
553                     (fun (id,ty,r) ->
554                       (id, snd (split_prods tys paramsno ty), r)) cl
555                   in
556                    (isinductive,paramsno,cl')
557              | _ ->
558                raise (WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
559            in
560             if not isinductive then
561               List.fold_right
562                (fun p i ->
563                  i && check_is_really_smaller_arg context n nn kl x safes p)
564                pl true
565             else
566               List.fold_right
567                (fun (p,(_,c,rl)) i ->
568                  let rl' =
569                   match !rl with
570                      Some rl' ->
571                       let (_,rl'') = split rl' paramsno in
572                        rl''
573                    | None -> raise (Impossible 13)
574                  in
575                   let (e,safes',n',nn',x',context') =
576                    get_new_safes context p c rl' safes n nn x
577                   in
578                    i &&
579                    check_is_really_smaller_arg context' n' nn' kl x' safes' e
580                ) (List.combine pl cl) true
581         | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
582            let (isinductive,paramsno,cl) =
583             match CicEnvironment.get_obj uri with
584                C.InductiveDefinition (tl,_,paramsno) ->
585                 let (_,isinductive,_,cl) = List.nth tl i in
586                  let tys =
587                   List.map (fun (n,_,ty,_) -> Some(Cic.Name n,(Cic.Decl ty))) tl
588                  in
589                   let cl' =
590                    List.map
591                     (fun (id,ty,r) ->
592                       (id, snd (split_prods tys paramsno ty), r)) cl
593                   in
594                   (isinductive,paramsno,cl')
595              | _ ->
596                raise (WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
597            in
598             if not isinductive then
599               List.fold_right
600                (fun p i ->
601                  i && check_is_really_smaller_arg context n nn kl x safes p)
602                pl true
603             else
604               (*CSC: supponiamo come prima che nessun controllo sia necessario*)
605               (*CSC: sugli argomenti di una applicazione                      *)
606               List.fold_right
607                (fun (p,(_,c,rl)) i ->
608                  let rl' =
609                   match !rl with
610                      Some rl' ->
611                       let (_,rl'') = split rl' paramsno in
612                        rl''
613                    | None -> raise (Impossible 14)
614                  in
615                   let (e, safes',n',nn',x',context') =
616                    get_new_safes context p c rl' safes n nn x
617                   in
618                    i &&
619                    check_is_really_smaller_arg context' n' nn' kl x' safes' e
620                ) (List.combine pl cl) true
621         | _ ->
622           List.fold_right
623            (fun p i ->
624              i && check_is_really_smaller_arg context n nn kl x safes p
625            ) pl true
626       )
627    | C.Fix (_, fl) ->
628       let len = List.length fl in
629        let n_plus_len = n + len
630        and nn_plus_len = nn + len
631        and x_plus_len = x + len
632        and tys = List.map (fun (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
633        and safes' = List.map (fun x -> x + len) safes in
634         List.fold_right
635          (fun (_,_,ty,bo) i ->
636            i &&
637             check_is_really_smaller_arg (tys@context) n_plus_len nn_plus_len kl
638              x_plus_len safes' bo
639          ) fl true
640    | C.CoFix (_, fl) ->
641       let len = List.length fl in
642        let n_plus_len = n + len
643        and nn_plus_len = nn + len
644        and x_plus_len = x + len
645        and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
646        and safes' = List.map (fun x -> x + len) safes in
647         List.fold_right
648          (fun (_,ty,bo) i ->
649            i &&
650             check_is_really_smaller_arg (tys@context) n_plus_len nn_plus_len kl
651              x_plus_len safes' bo
652          ) fl true
653
654 and guarded_by_destructors context n nn kl x safes =
655  let module C = Cic in
656  let module U = UriManager in
657   function
658      C.Rel m when m > n && m <= nn -> false
659    | C.Rel n ->
660       (match List.nth context (n-1) with
661           Some (_,C.Decl _) -> true
662         | Some (_,C.Def bo) -> guarded_by_destructors context n nn kl x safes bo
663         | None -> raise RelToHiddenHypothesis
664       )
665    | C.Var _
666    | C.Meta _
667    | C.Sort _
668    | C.Implicit -> true
669    | C.Cast (te,ty) ->
670       guarded_by_destructors context n nn kl x safes te &&
671        guarded_by_destructors context n nn kl x safes ty
672    | C.Prod (name,so,ta) ->
673       guarded_by_destructors context n nn kl x safes so &&
674        guarded_by_destructors ((Some (name,(C.Decl so)))::context)
675         (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
676    | C.Lambda (name,so,ta) ->
677       guarded_by_destructors context n nn kl x safes so &&
678        guarded_by_destructors ((Some (name,(C.Decl so)))::context)
679         (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
680    | C.LetIn (name,so,ta) ->
681       guarded_by_destructors context n nn kl x safes so &&
682        guarded_by_destructors ((Some (name,(C.Def so)))::context)
683         (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
684    | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
685       let k = List.nth kl (m - n - 1) in
686        if not (List.length tl > k) then false
687        else
688         List.fold_right
689          (fun param i ->
690            i && guarded_by_destructors context n nn kl x safes param
691          ) tl true &&
692          check_is_really_smaller_arg context n nn kl x safes (List.nth tl k)
693    | C.Appl tl ->
694       List.fold_right
695        (fun t i -> i && guarded_by_destructors context n nn kl x safes t)
696        tl true
697    | C.Const _
698    | C.MutInd _
699    | C.MutConstruct _ -> true
700    | C.MutCase (uri,_,i,outtype,term,pl) ->
701       (match term with
702           C.Rel m when List.mem m safes || m = x ->
703            let (isinductive,paramsno,cl) =
704             match CicEnvironment.get_obj uri with
705                C.InductiveDefinition (tl,_,paramsno) ->
706                 let (_,isinductive,_,cl) = List.nth tl i in
707                  let tys =
708                   List.map (fun (n,_,ty,_) -> Some(Cic.Name n,(Cic.Decl ty))) tl
709                  in
710                   let cl' =
711                    List.map
712                     (fun (id,ty,r) ->
713                       (id, snd (split_prods tys paramsno ty), r)) cl
714                   in
715                    (isinductive,paramsno,cl')
716              | _ ->
717                raise (WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
718            in
719             if not isinductive then
720              guarded_by_destructors context n nn kl x safes outtype &&
721               guarded_by_destructors context n nn kl x safes term &&
722               (*CSC: manca ??? il controllo sul tipo di term? *)
723               List.fold_right
724                (fun p i ->
725                  i && guarded_by_destructors context n nn kl x safes p)
726                pl true
727             else
728              guarded_by_destructors context n nn kl x safes outtype &&
729               (*CSC: manca ??? il controllo sul tipo di term? *)
730               List.fold_right
731                (fun (p,(_,c,rl)) i ->
732                  let rl' =
733                   match !rl with
734                      Some rl' ->
735                       let (_,rl'') = split rl' paramsno in
736                        rl''
737                    | None -> raise (Impossible 15)
738                  in
739                   let (e,safes',n',nn',x',context') =
740                    get_new_safes context p c rl' safes n nn x
741                   in
742                    i &&
743                    guarded_by_destructors context' n' nn' kl x' safes' e
744                ) (List.combine pl cl) true
745         | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
746            let (isinductive,paramsno,cl) =
747             match CicEnvironment.get_obj uri with
748                C.InductiveDefinition (tl,_,paramsno) ->
749                 let (_,isinductive,_,cl) = List.nth tl i in
750                  let tys =
751                   List.map (fun (n,_,ty,_) -> Some(Cic.Name n,(Cic.Decl ty))) tl
752                  in
753                   let cl' =
754                    List.map
755                     (fun (id,ty,r) ->
756                       (id, snd (split_prods tys paramsno ty), r)) cl
757                   in
758                    (isinductive,paramsno,cl')
759              | _ ->
760                raise (WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
761            in
762             if not isinductive then
763              guarded_by_destructors context n nn kl x safes outtype &&
764               guarded_by_destructors context n nn kl x safes term &&
765               (*CSC: manca ??? il controllo sul tipo di term? *)
766               List.fold_right
767                (fun p i ->
768                  i && guarded_by_destructors context n nn kl x safes p)
769                pl true
770             else
771              guarded_by_destructors context n nn kl x safes outtype &&
772               (*CSC: manca ??? il controllo sul tipo di term? *)
773               List.fold_right
774                (fun t i ->
775                  i && guarded_by_destructors context n nn kl x safes t)
776                tl true &&
777               List.fold_right
778                (fun (p,(_,c,rl)) i ->
779                  let rl' =
780                   match !rl with
781                      Some rl' ->
782                       let (_,rl'') = split rl' paramsno in
783                        rl''
784                    | None -> raise (Impossible 16)
785                  in
786                   let (e, safes',n',nn',x',context') =
787                    get_new_safes context p c rl' safes n nn x
788                   in
789                    i &&
790                    guarded_by_destructors context' n' nn' kl x' safes' e
791                ) (List.combine pl cl) true
792         | _ ->
793           guarded_by_destructors context n nn kl x safes outtype &&
794            guarded_by_destructors context n nn kl x safes term &&
795            (*CSC: manca ??? il controllo sul tipo di term? *)
796            List.fold_right
797             (fun p i -> i && guarded_by_destructors context n nn kl x safes p)
798             pl true
799       )
800    | C.Fix (_, fl) ->
801       let len = List.length fl in
802        let n_plus_len = n + len
803        and nn_plus_len = nn + len
804        and x_plus_len = x + len
805        and tys = List.map (fun (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
806        and safes' = List.map (fun x -> x + len) safes in
807         List.fold_right
808          (fun (_,_,ty,bo) i ->
809            i && guarded_by_destructors context n nn kl x_plus_len safes' ty &&
810             guarded_by_destructors (tys@context) n_plus_len nn_plus_len kl
811              x_plus_len safes' bo
812          ) fl true
813    | C.CoFix (_, fl) ->
814       let len = List.length fl in
815        let n_plus_len = n + len
816        and nn_plus_len = nn + len
817        and x_plus_len = x + len
818        and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
819        and safes' = List.map (fun x -> x + len) safes in
820         List.fold_right
821          (fun (_,ty,bo) i ->
822            i &&
823             guarded_by_destructors context n nn kl x_plus_len safes' ty &&
824             guarded_by_destructors (tys@context) n_plus_len nn_plus_len kl
825              x_plus_len safes' bo
826          ) fl true
827
828 (* the boolean h means already protected *)
829 (* args is the list of arguments the type of the constructor that may be *)
830 (* found in head position must be applied to.                            *)
831 (*CSC: coInductiveTypeURI non cambia mai di ricorsione in ricorsione *)
832 and guarded_by_constructors context n nn h te args coInductiveTypeURI =
833  let module C = Cic in
834   (*CSC: There is a lot of code replication between the cases X and    *)
835   (*CSC: (C.Appl X tl). Maybe it will be better to define a function   *)
836   (*CSC: that maps X into (C.Appl X []) when X is not already a C.Appl *)
837   match CicReduction.whd context te with
838      C.Rel m when m > n && m <= nn -> h
839    | C.Rel _
840    | C.Var _  -> true
841    | C.Meta _
842    | C.Sort _
843    | C.Implicit
844    | C.Cast _
845    | C.Prod _
846    | C.LetIn _ ->
847       raise (Impossible 17) (* the term has just been type-checked *)
848    | C.Lambda (name,so,de) ->
849       does_not_occur context n nn so &&
850        guarded_by_constructors ((Some (name,(C.Decl so)))::context)
851         (n + 1) (nn + 1) h de args coInductiveTypeURI
852    | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
853       h &&
854        List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
855    | C.Appl ((C.MutConstruct (uri,cookingsno,i,j))::tl) ->
856       let consty =
857        match CicEnvironment.get_cooked_obj uri cookingsno with
858           C.InductiveDefinition (itl,_,_) ->
859            let (_,_,_,cl) = List.nth itl i in
860             let (_,cons,_) = List.nth cl (j - 1) in cons
861         | _ ->
862          raise (WrongUriToMutualInductiveDefinitions
863           (UriManager.string_of_uri uri))
864       in
865        let rec analyse_branch context ty te =
866         match CicReduction.whd context ty with
867            C.Meta _ -> raise (Impossible 34)
868          | C.Rel _
869          | C.Var _
870          | C.Sort _ ->
871             does_not_occur context n nn te
872          | C.Implicit
873          | C.Cast _ -> raise (Impossible 24) (* due to type-checking *)
874          | C.Prod (name,so,de) ->
875             analyse_branch ((Some (name,(C.Decl so)))::context) de te
876          | C.Lambda _
877          | C.LetIn _ -> raise (Impossible 25) (* due to type-checking *)
878          | C.Appl ((C.MutInd (uri,_,_))::tl) as ty
879             when uri == coInductiveTypeURI -> 
880              guarded_by_constructors context n nn true te [] coInductiveTypeURI
881          | C.Appl ((C.MutInd (uri,_,_))::tl) as ty -> 
882             guarded_by_constructors context n nn true te tl coInductiveTypeURI
883          | C.Appl _ ->
884             does_not_occur context n nn te
885          | C.Const _ -> raise (Impossible 26)
886          | C.MutInd (uri,_,_) when uri == coInductiveTypeURI ->
887             guarded_by_constructors context n nn true te [] coInductiveTypeURI
888          | C.MutInd _ ->
889             does_not_occur context n nn te
890          | C.MutConstruct _ -> raise (Impossible 27)
891          (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
892          (*CSC: in head position.                                       *)
893          | C.MutCase _
894          | C.Fix _
895          | C.CoFix _ -> raise (Impossible 28) (* due to type-checking *)
896        in
897        let rec analyse_instantiated_type context ty l =
898         match CicReduction.whd context ty with
899            C.Rel _
900          | C.Var _
901          | C.Meta _
902          | C.Sort _
903          | C.Implicit
904          | C.Cast _ -> raise (Impossible 29) (* due to type-checking *)
905          | C.Prod (name,so,de) ->
906             begin
907              match l with
908                 [] -> true
909               | he::tl ->
910                  analyse_branch context so he &&
911                   analyse_instantiated_type ((Some (name,(C.Decl so)))::context)
912                    de tl
913             end
914          | C.Lambda _
915          | C.LetIn _ -> raise (Impossible 30) (* due to type-checking *)
916          | C.Appl _ -> 
917             List.fold_left
918              (fun i x -> i && does_not_occur context n nn x) true l
919          | C.Const _ -> raise (Impossible 31)
920          | C.MutInd _ ->
921             List.fold_left
922              (fun i x -> i && does_not_occur context n nn x) true l
923          | C.MutConstruct _ -> raise (Impossible 32)
924          (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
925          (*CSC: in head position.                                       *)
926          | C.MutCase _
927          | C.Fix _
928          | C.CoFix _ -> raise (Impossible 33) (* due to type-checking *)
929        in
930         let rec instantiate_type args consty =
931          function
932             [] -> true
933           | tlhe::tltl as l ->
934              let consty' = CicReduction.whd context consty in
935               match args with 
936                  he::tl ->
937                   begin
938                    match consty' with
939                       C.Prod (_,_,de) ->
940                        let instantiated_de = CicSubstitution.subst he de in
941                         (*CSC: siamo sicuri che non sia troppo forte? *)
942                         does_not_occur context n nn tlhe &
943                          instantiate_type tl instantiated_de tltl
944                     | _ ->
945                       (*CSC:We do not consider backbones with a MutCase, a    *)
946                       (*CSC:FixPoint, a CoFixPoint and so on in head position.*)
947                       raise (Impossible 23)
948                   end
949                | [] -> analyse_instantiated_type context consty' l
950                   (* These are all the other cases *)
951        in
952         instantiate_type args consty tl
953    | C.Appl ((C.CoFix (_,fl))::tl) ->
954       List.fold_left (fun i x -> i && does_not_occur context n nn x) true tl &&
955        let len = List.length fl in
956         let n_plus_len = n + len
957         and nn_plus_len = nn + len
958         (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
959         and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl in
960          List.fold_right
961           (fun (_,ty,bo) i ->
962             i && does_not_occur context n nn ty &&
963              guarded_by_constructors (tys@context) n_plus_len nn_plus_len h bo
964               args coInductiveTypeURI
965           ) fl true
966    | C.Appl ((C.MutCase (_,_,_,out,te,pl))::tl) ->
967        List.fold_left (fun i x -> i && does_not_occur context n nn x) true tl &&
968         does_not_occur context n nn out &&
969          does_not_occur context n nn te &&
970           List.fold_right
971            (fun x i ->
972              i &&
973              guarded_by_constructors context n nn h x args coInductiveTypeURI
974            ) pl true
975    | C.Appl l ->
976       List.fold_right (fun x i -> i && does_not_occur context n nn x) l true
977    | C.Const _ -> true
978    | C.MutInd _ -> assert false
979    | C.MutConstruct _ -> true
980    | C.MutCase (_,_,_,out,te,pl) ->
981        does_not_occur context n nn out &&
982         does_not_occur context n nn te &&
983          List.fold_right
984           (fun x i ->
985             i &&
986              guarded_by_constructors context n nn h x args coInductiveTypeURI
987           ) pl true
988    | C.Fix (_,fl) ->
989       let len = List.length fl in
990        let n_plus_len = n + len
991        and nn_plus_len = nn + len
992        (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
993        and tys = List.map (fun (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl in
994         List.fold_right
995          (fun (_,_,ty,bo) i ->
996            i && does_not_occur context n nn ty &&
997             does_not_occur (tys@context) n_plus_len nn_plus_len bo
998          ) fl true
999    | C.CoFix (_,fl) ->
1000       let len = List.length fl in
1001        let n_plus_len = n + len
1002        and nn_plus_len = nn + len
1003        (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1004        and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl in
1005         List.fold_right
1006          (fun (_,ty,bo) i ->
1007            i && does_not_occur context n nn ty &&
1008             guarded_by_constructors (tys@context) n_plus_len nn_plus_len h bo
1009              args coInductiveTypeURI
1010          ) fl true
1011
1012 and check_allowed_sort_elimination context uri i need_dummy ind arity1 arity2 =
1013  let module C = Cic in
1014  let module U = UriManager in
1015   match (CicReduction.whd context arity1, CicReduction.whd context arity2) with
1016      (C.Prod (_,so1,de1), C.Prod (_,so2,de2))
1017       when CicReduction.are_convertible context so1 so2 ->
1018        check_allowed_sort_elimination context uri i need_dummy
1019         (C.Appl [CicSubstitution.lift 1 ind ; C.Rel 1]) de1 de2
1020    | (C.Sort C.Prop, C.Sort C.Prop) when need_dummy -> true
1021    | (C.Sort C.Prop, C.Sort C.Set) when need_dummy ->
1022        (match CicEnvironment.get_obj uri with
1023            C.InductiveDefinition (itl,_,_) ->
1024             let (_,_,_,cl) = List.nth itl i in
1025              (* is a singleton definition? *)
1026              List.length cl = 1
1027          | _ ->
1028            raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
1029        )
1030    | (C.Sort C.Set, C.Sort C.Prop) when need_dummy -> true
1031    | (C.Sort C.Set, C.Sort C.Set) when need_dummy -> true
1032    | (C.Sort C.Set, C.Sort C.Type) when need_dummy ->
1033        (match CicEnvironment.get_obj uri with
1034            C.InductiveDefinition (itl,_,paramsno) ->
1035             let tys =
1036              List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl
1037             in
1038              let (_,_,_,cl) = List.nth itl i in
1039               List.fold_right
1040                (fun (_,x,_) i -> i && is_small tys paramsno x) cl true
1041          | _ ->
1042            raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
1043        )
1044    | (C.Sort C.Type, C.Sort _) when need_dummy -> true
1045    | (C.Sort C.Prop, C.Prod (name,so,ta)) when not need_dummy ->
1046        let res = CicReduction.are_convertible context so ind
1047        in
1048         res &&
1049         (match CicReduction.whd ((Some (name,(C.Decl so)))::context) ta with
1050             C.Sort C.Prop -> true
1051           | C.Sort C.Set ->
1052              (match CicEnvironment.get_obj uri with
1053                  C.InductiveDefinition (itl,_,_) ->
1054                   let (_,_,_,cl) = List.nth itl i in
1055                    (* is a singleton definition? *)
1056                    List.length cl = 1
1057                | _ ->
1058                  raise (WrongUriToMutualInductiveDefinitions
1059                   (U.string_of_uri uri))
1060              )
1061           | _ -> false
1062         )
1063    | (C.Sort C.Set, C.Prod (name,so,ta)) when not need_dummy ->
1064        let res = CicReduction.are_convertible context so ind
1065        in
1066         res &&
1067         (match CicReduction.whd ((Some (name,(C.Decl so)))::context) ta with
1068             C.Sort C.Prop
1069           | C.Sort C.Set  -> true
1070           | C.Sort C.Type ->
1071              (match CicEnvironment.get_obj uri with
1072                  C.InductiveDefinition (itl,_,paramsno) ->
1073                   let (_,_,_,cl) = List.nth itl i in
1074                    let tys =
1075                     List.map
1076                      (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl
1077                    in
1078                     List.fold_right
1079                      (fun (_,x,_) i -> i && is_small tys paramsno x) cl true
1080                | _ ->
1081                  raise (WrongUriToMutualInductiveDefinitions
1082                   (U.string_of_uri uri))
1083              )
1084           | _ -> raise (Impossible 19)
1085         )
1086    | (C.Sort C.Type, C.Prod (_,so,_)) when not need_dummy ->
1087        CicReduction.are_convertible context so ind
1088    | (_,_) -> false
1089   
1090 and type_of_branch context argsno need_dummy outtype term constype =
1091  let module C = Cic in
1092  let module R = CicReduction in
1093   match R.whd context constype with
1094      C.MutInd (_,_,_) ->
1095       if need_dummy then
1096        outtype
1097       else
1098        C.Appl [outtype ; term]
1099    | C.Appl (C.MutInd (_,_,_)::tl) ->
1100       let (_,arguments) = split tl argsno
1101       in
1102        if need_dummy && arguments = [] then
1103         outtype
1104        else
1105         C.Appl (outtype::arguments@(if need_dummy then [] else [term]))
1106    | C.Prod (name,so,de) ->
1107       let term' =
1108        match CicSubstitution.lift 1 term with
1109           C.Appl l -> C.Appl (l@[C.Rel 1])
1110         | t -> C.Appl [t ; C.Rel 1]
1111       in
1112        C.Prod (C.Anonimous,so,type_of_branch
1113         ((Some (name,(C.Decl so)))::context) argsno need_dummy
1114         (CicSubstitution.lift 1 outtype) term' de)
1115   | _ -> raise (Impossible 20)
1116
1117 (* check_metasenv_consistency checks that the "canonical" context of a
1118 metavariable is consitent - up to relocation via the relocation list l -
1119 with the actual context *)
1120
1121 and check_metasenv_consistency metasenv context canonical_context l =
1122   let module C = Cic in
1123   let module R = CicReduction in
1124   let module S = CicSubstitution in
1125    let lifted_canonical_context = 
1126     let rec aux i =
1127      function
1128         [] -> []
1129       | (Some (n,C.Decl t))::tl ->
1130          (Some (n,C.Decl (S.lift_meta l (S.lift i t))))::(aux (i+1) tl)
1131       | (Some (n,C.Def t))::tl ->
1132          (Some (n,C.Def (S.lift_meta l (S.lift i t))))::(aux (i+1) tl)
1133       | None::tl -> None::(aux (i+1) tl)
1134     in
1135      aux 1 canonical_context
1136    in
1137     List.iter2 
1138      (fun t ct -> 
1139        let res =
1140         match (t,ct) with
1141            _,None -> true
1142          | Some t,Some (_,C.Def ct) ->
1143             R.are_convertible context t ct
1144          | Some t,Some (_,C.Decl ct) ->
1145             R.are_convertible context (type_of_aux' metasenv context t) ct
1146          | _, _  -> false
1147        in
1148         if not res then raise MetasenvInconsistency
1149      ) l lifted_canonical_context 
1150
1151 (* type_of_aux' is just another name (with a different scope) for type_of_aux *)
1152 and type_of_aux' metasenv context t =
1153  let rec type_of_aux context =
1154   let module C = Cic in
1155   let module R = CicReduction in
1156   let module S = CicSubstitution in
1157   let module U = UriManager in
1158    function
1159       C.Rel n ->
1160        (try
1161          match List.nth context (n - 1) with
1162             Some (_,C.Decl t) -> S.lift n t
1163           | Some (_,C.Def bo) -> type_of_aux context (S.lift n bo)
1164           | None -> raise RelToHiddenHypothesis
1165         with
1166          _ -> raise (NotWellTyped "Not a close term")
1167        )
1168     | C.Var uri ->
1169       incr fdebug ;
1170       let ty = type_of_variable uri in
1171        decr fdebug ;
1172        ty
1173     | C.Meta (n,l) -> 
1174        let (_,canonical_context,ty) =
1175         List.find (function (m,_,_) -> n = m) metasenv
1176        in
1177         check_metasenv_consistency metasenv context canonical_context l;
1178         CicSubstitution.lift_meta l ty
1179     | C.Sort s -> C.Sort C.Type (*CSC manca la gestione degli universi!!! *)
1180     | C.Implicit -> raise (Impossible 21)
1181     | C.Cast (te,ty) ->
1182        let _ = type_of_aux context ty in
1183         if R.are_convertible context (type_of_aux context te) ty then ty
1184         else raise (NotWellTyped "Cast")
1185     | C.Prod (name,s,t) ->
1186        let sort1 = type_of_aux context s
1187        and sort2 = type_of_aux ((Some (name,(C.Decl s)))::context) t in
1188         sort_of_prod context (name,s) (sort1,sort2)
1189    | C.Lambda (n,s,t) ->
1190        let sort1 = type_of_aux context s
1191        and type2 = type_of_aux ((Some (n,(C.Decl s)))::context) t in
1192         let sort2 = type_of_aux ((Some (n,(C.Decl s)))::context) type2 in
1193          (* only to check if the product is well-typed *)
1194          let _ = sort_of_prod context (n,s) (sort1,sort2) in
1195           C.Prod (n,s,type2)
1196    | C.LetIn (n,s,t) ->
1197       (* only to check if s is well-typed *)
1198       let _ = type_of_aux context s in
1199        C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t)
1200    | C.Appl (he::tl) when List.length tl > 0 ->
1201       let hetype = type_of_aux context he
1202       and tlbody_and_type = List.map (fun x -> (x, type_of_aux context x)) tl in
1203        eat_prods context hetype tlbody_and_type
1204    | C.Appl _ -> raise (NotWellTyped "Appl: no arguments")
1205    | C.Const (uri,cookingsno) ->
1206       incr fdebug ;
1207       let cty = cooked_type_of_constant uri cookingsno in
1208        decr fdebug ;
1209        cty
1210    | C.MutInd (uri,cookingsno,i) ->
1211       incr fdebug ;
1212       let cty = cooked_type_of_mutual_inductive_defs uri cookingsno i in
1213        decr fdebug ;
1214        cty
1215    | C.MutConstruct (uri,cookingsno,i,j) ->
1216       let cty = cooked_type_of_mutual_inductive_constr uri cookingsno i j
1217       in
1218        cty
1219    | C.MutCase (uri,cookingsno,i,outtype,term,pl) ->
1220       let outsort = type_of_aux context outtype in
1221       let (need_dummy, k) =
1222        let rec guess_args context t =
1223         match CicReduction.whd context t with
1224            C.Sort _ -> (true, 0)
1225          | C.Prod (name, s, t) ->
1226             let (b, n) = guess_args ((Some (name,(C.Decl s)))::context) t in
1227              if n = 0 then
1228               (* last prod before sort *)
1229               match CicReduction.whd context s with
1230                  (*CSC vedi nota delirante su cookingsno in cicReduction.ml *)
1231                  C.MutInd (uri',_,i') when U.eq uri' uri && i' = i -> (false, 1)
1232                | C.Appl ((C.MutInd (uri',_,i')) :: _)
1233                   when U.eq uri' uri && i' = i -> (false, 1)
1234                | _ -> (true, 1)
1235              else
1236               (b, n + 1)
1237          | _ -> raise (NotWellTyped "MutCase: outtype ill-formed")
1238        in
1239         (*CSC whd non serve dopo type_of_aux ? *)
1240         let (b, k) = guess_args context outsort in
1241          if not b then (b, k - 1) else (b, k)
1242       in
1243       let (parameters, arguments) =
1244         match R.whd context (type_of_aux context term) with
1245            (*CSC manca il caso dei CAST *)
1246            C.MutInd (uri',_,i') ->
1247             (*CSC vedi nota delirante sui cookingsno in cicReduction.ml*)
1248             if U.eq uri uri' && i = i' then ([],[])
1249             else raise (NotWellTyped ("MutCase: the term is of type " ^
1250              (U.string_of_uri uri') ^ "," ^ string_of_int i' ^
1251              " instead of type " ^ (U.string_of_uri uri') ^ "," ^
1252              string_of_int i))
1253          | C.Appl (C.MutInd (uri',_,i') :: tl) ->
1254             if U.eq uri uri' && i = i' then split tl (List.length tl - k)
1255             else raise (NotWellTyped ("MutCase: the term is of type " ^
1256              (U.string_of_uri uri') ^ "," ^ string_of_int i' ^
1257              " instead of type " ^ (U.string_of_uri uri) ^ "," ^
1258              string_of_int i))
1259          | _ -> raise (NotWellTyped "MutCase: the term is not an inductive one")
1260       in
1261        (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
1262        let sort_of_ind_type =
1263         if parameters = [] then
1264          C.MutInd (uri,cookingsno,i)
1265         else
1266          C.Appl ((C.MutInd (uri,cookingsno,i))::parameters)
1267        in
1268         if not (check_allowed_sort_elimination context uri i need_dummy
1269          sort_of_ind_type (type_of_aux context sort_of_ind_type) outsort)
1270         then
1271          raise (NotWellTyped "MutCase: not allowed sort elimination") ;
1272
1273         (* let's check if the type of branches are right *)
1274         let (cl,parsno) =
1275          match CicEnvironment.get_cooked_obj uri cookingsno with
1276             C.InductiveDefinition (tl,_,parsno) ->
1277              let (_,_,_,cl) = List.nth tl i in (cl,parsno)
1278           | _ ->
1279             raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
1280         in
1281          let (_,branches_ok) =
1282           List.fold_left
1283            (fun (j,b) (p,(_,c,_)) ->
1284              let cons =
1285               if parameters = [] then
1286                (C.MutConstruct (uri,cookingsno,i,j))
1287               else
1288                (C.Appl (C.MutConstruct (uri,cookingsno,i,j)::parameters))
1289              in
1290               (j + 1, b &&
1291                R.are_convertible context (type_of_aux context p)
1292                 (type_of_branch context parsno need_dummy outtype cons
1293                   (type_of_aux context cons))
1294               )
1295            ) (1,true) (List.combine pl cl)
1296          in
1297           if not branches_ok then
1298            raise (NotWellTyped "MutCase: wrong type of a branch") ;
1299
1300           if not need_dummy then
1301            C.Appl ((outtype::arguments)@[term])
1302           else if arguments = [] then
1303            outtype
1304           else
1305            C.Appl (outtype::arguments)
1306    | C.Fix (i,fl) ->
1307       let types_times_kl =
1308        List.rev
1309         (List.map
1310           (fun (n,k,ty,_) ->
1311             let _ = type_of_aux context ty in
1312              (Some (C.Name n,(C.Decl ty)),k)) fl)
1313       in
1314       let (types,kl) = List.split types_times_kl in
1315        let len = List.length types in
1316         List.iter
1317          (fun (name,x,ty,bo) ->
1318            if
1319             (R.are_convertible (types@context) (type_of_aux (types@context) bo)
1320              (CicSubstitution.lift len ty))
1321            then
1322             begin
1323              let (m, eaten, context') =
1324               eat_lambdas (types @ context) (x + 1) bo
1325              in
1326               (*let's control the guarded by destructors conditions D{f,k,x,M}*)
1327               if
1328                not
1329                 (guarded_by_destructors context' eaten (len + eaten) kl 1 [] m)
1330               then
1331                raise (NotWellTyped "Fix: not guarded by destructors")
1332             end
1333            else
1334             raise (NotWellTyped "Fix: ill-typed bodies")
1335          ) fl ;
1336       
1337         (*CSC: controlli mancanti solo su D{f,k,x,M} *)
1338         let (_,_,ty,_) = List.nth fl i in
1339         ty
1340    | C.CoFix (i,fl) ->
1341       let types =
1342        List.rev
1343         (List.map
1344           (fun (n,ty,_) -> 
1345             let _ = type_of_aux context ty in Some (C.Name n,(C.Decl ty))) fl)
1346       in
1347        let len = List.length types in
1348         List.iter
1349          (fun (_,ty,bo) ->
1350            if
1351             (R.are_convertible (types @ context)
1352              (type_of_aux (types @ context) bo) (CicSubstitution.lift len ty))
1353            then
1354             begin
1355              (* let's control that the returned type is coinductive *)
1356              match returns_a_coinductive context ty with
1357                 None ->
1358                  raise(NotWellTyped "CoFix: does not return a coinductive type")
1359               | Some uri ->
1360                  (*let's control the guarded by constructors conditions C{f,M}*)
1361                  if
1362                   not
1363                    (guarded_by_constructors (types @ context) 0 len false bo
1364                      [] uri)
1365                  then
1366                   raise (NotWellTyped "CoFix: not guarded by constructors")
1367             end
1368            else
1369             raise (NotWellTyped "CoFix: ill-typed bodies")
1370          ) fl ;
1371       
1372         let (_,ty,_) = List.nth fl i in
1373          ty
1374
1375  and sort_of_prod context (name,s) (t1, t2) =
1376   let module C = Cic in
1377    let t1' = CicReduction.whd context t1 in
1378    let t2' = CicReduction.whd ((Some (name,C.Decl s))::context) t2 in
1379    match (t1', t2') with
1380       (C.Sort s1, C.Sort s2)
1381         when (s2 = C.Prop or s2 = C.Set) -> (* different from Coq manual!!! *)
1382          C.Sort s2
1383     | (C.Sort s1, C.Sort s2) -> C.Sort C.Type (*CSC manca la gestione degli universi!!! *)
1384     | (_,_) ->
1385       raise
1386        (NotWellTyped
1387         ("Prod: sort1= " ^ CicPp.ppterm t1' ^ " ; sort2= " ^ CicPp.ppterm t2'))
1388
1389  and eat_prods context hetype =
1390   (*CSC: siamo sicuri che le are_convertible non lavorino con termini non *)
1391   (*CSC: cucinati                                                         *)
1392   function
1393      [] -> hetype
1394    | (hete, hety)::tl ->
1395     (match (CicReduction.whd context hetype) with
1396         Cic.Prod (n,s,t) ->
1397          if CicReduction.are_convertible context s hety then
1398           (CicReduction.fdebug := -1 ;
1399            eat_prods context (CicSubstitution.subst hete t) tl
1400           )
1401          else
1402           begin
1403            CicReduction.fdebug := 0 ;
1404            ignore (CicReduction.are_convertible context s hety) ;
1405            fdebug := 0 ;
1406            debug s [hety] ;
1407            raise (NotWellTyped "Appl: wrong parameter-type")
1408           end
1409       | _ -> raise (NotWellTyped "Appl: wrong Prod-type")
1410     )
1411
1412  and returns_a_coinductive context ty =
1413   let module C = Cic in
1414    match CicReduction.whd context ty with
1415       C.MutInd (uri,cookingsno,i) ->
1416        (*CSC: definire una funzioncina per questo codice sempre replicato *)
1417        (match CicEnvironment.get_cooked_obj uri cookingsno with
1418            C.InductiveDefinition (itl,_,_) ->
1419             let (_,is_inductive,_,cl) = List.nth itl i in
1420              if is_inductive then None else (Some uri)
1421          | _ ->
1422            raise (WrongUriToMutualInductiveDefinitions
1423             (UriManager.string_of_uri uri))
1424         )
1425     | C.Appl ((C.MutInd (uri,_,i))::_) ->
1426        (match CicEnvironment.get_obj uri with
1427            C.InductiveDefinition (itl,_,_) ->
1428             let (_,is_inductive,_,_) = List.nth itl i in
1429              if is_inductive then None else (Some uri)
1430          | _ ->
1431            raise (WrongUriToMutualInductiveDefinitions
1432             (UriManager.string_of_uri uri))
1433         )
1434     | C.Prod (n,so,de) ->
1435        returns_a_coinductive ((Some (n,C.Decl so))::context) de
1436     | _ -> None
1437
1438  in
1439 (*CSC
1440 prerr_endline ("INIZIO TYPE_OF_AUX " ^ CicPp.ppterm t) ; flush stderr ;
1441 let res =
1442 *)
1443   type_of_aux context t
1444 (*
1445 in prerr_endline "FINE TYPE_OF_AUX" ; flush stderr ; res
1446 *)
1447
1448 (* is a small constructor? *)
1449 (*CSC: ottimizzare calcolando staticamente *)
1450 and is_small context paramsno c =
1451  let rec is_small_aux context c =
1452   let module C = Cic in
1453    match CicReduction.whd context c with
1454       C.Prod (n,so,de) ->
1455        (*CSC: [] is an empty metasenv. Is it correct? *)
1456        let s = type_of_aux' [] context so in
1457         (s = C.Sort C.Prop || s = C.Sort C.Set) &&
1458         is_small_aux ((Some (n,(C.Decl so)))::context) de
1459     | _ -> true (*CSC: we trust the type-checker *)
1460  in
1461   let (context',dx) = split_prods context paramsno c in
1462    is_small_aux context' dx
1463
1464 and type_of t =
1465 (*CSC
1466 prerr_endline ("INIZIO TYPE_OF_AUX' " ^ CicPp.ppterm t) ; flush stderr ;
1467 let res =
1468 *)
1469  type_of_aux' [] [] t
1470 (*CSC
1471 in prerr_endline "FINE TYPE_OF_AUX'" ; flush stderr ; res
1472 *)
1473 ;;
1474
1475 let typecheck uri =
1476  let module C = Cic in
1477  let module R = CicReduction in
1478  let module U = UriManager in
1479   match CicEnvironment.is_type_checked uri 0 with
1480      CicEnvironment.CheckedObj _ -> ()
1481    | CicEnvironment.UncheckedObj uobj ->
1482       (* let's typecheck the uncooked object *)
1483       Logger.log (`Start_type_checking uri) ;
1484       (match uobj with
1485           C.Definition (_,te,ty,_) ->
1486            let _ = type_of ty in
1487             if not (R.are_convertible [] (type_of te ) ty) then
1488              raise (NotWellTyped ("Constant " ^ (U.string_of_uri uri)))
1489         | C.Axiom (_,ty,_) ->
1490           (* only to check that ty is well-typed *)
1491           let _ = type_of ty in ()
1492         | C.CurrentProof (_,conjs,te,ty) ->
1493             (*CSC: bisogna controllare anche il metasenv!!! *)
1494             let _ = type_of_aux' conjs [] ty in
1495              debug (type_of_aux' conjs [] te) [] ;
1496              if not (R.are_convertible [] (type_of_aux' conjs [] te) ty) then
1497               raise (NotWellTyped ("CurrentProof" ^ (U.string_of_uri uri)))
1498         | C.Variable (_,bo,ty) ->
1499            (* only to check that ty is well-typed *)
1500            let _ = type_of ty in
1501             (match bo with
1502                 None -> ()
1503               | Some bo ->
1504                  if not (R.are_convertible [] (type_of bo) ty) then
1505                   raise (NotWellTyped ("Variable" ^ (U.string_of_uri uri)))
1506             )
1507         | C.InductiveDefinition _ ->
1508            cooked_mutual_inductive_defs uri uobj
1509       ) ;
1510       CicEnvironment.set_type_checking_info uri ;
1511       Logger.log (`Type_checking_completed uri)
1512 ;;