1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 exception NotImplemented;;
27 exception Impossible of int;;
28 exception NotWellTyped of string;;
29 exception WrongUriToConstant of string;;
30 exception WrongUriToVariable of string;;
31 exception WrongUriToMutualInductiveDefinitions of string;;
32 exception ListTooShort;;
33 exception NotPositiveOccurrences of string;;
34 exception NotWellFormedTypeOfInductiveConstructor of string;;
35 exception WrongRequiredArgument of string;;
38 let module U = UriManager in
41 `Start_type_checking uri ->
43 (String.make !indent ' ') ^
44 "<div style=\"margin-left: " ^
45 string_of_float (float_of_int !indent *. 0.5) ^ "cm\">" ^
46 "Type-Checking of " ^ (U.string_of_uri uri) ^ " started</div>\n"
50 | `Type_checking_completed uri ->
53 (String.make !indent ' ') ^
54 "<div style=\"color: green ; margin-left: " ^
55 string_of_float (float_of_int !indent *. 0.5) ^ "cm\">" ^
56 "Type-Checking of " ^ (U.string_of_uri uri) ^ " completed.</div>\n"
63 let rec debug_aux t i =
65 let module U = UriManager in
66 CicPp.ppobj (C.Variable ("DEBUG", None, t)) ^ "\n" ^ i
69 raise (NotWellTyped ("\n" ^ List.fold_right debug_aux (t::env) ""))
70 (*print_endline ("\n" ^ List.fold_right debug_aux (t::env) "") ; flush stdout*)
76 | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
77 | (_,_) -> raise ListTooShort
80 exception CicEnvironmentError;;
82 let rec cooked_type_of_constant uri cookingsno =
84 let module R = CicReduction in
85 let module U = UriManager in
87 match CicEnvironment.is_type_checked uri cookingsno with
88 CicEnvironment.CheckedObj cobj -> cobj
89 | CicEnvironment.UncheckedObj uobj ->
90 log (`Start_type_checking uri) ;
91 (* let's typecheck the uncooked obj *)
93 C.Definition (_,te,ty,_) ->
95 if not (R.are_convertible (type_of te) ty) then
96 raise (NotWellTyped ("Constant " ^ (U.string_of_uri uri)))
98 (* only to check that ty is well-typed *)
99 let _ = type_of ty in ()
100 | C.CurrentProof (_,_,te,ty) ->
101 let _ = type_of ty in
102 if not (R.are_convertible (type_of te) ty) then
103 raise (NotWellTyped ("CurrentProof" ^ (U.string_of_uri uri)))
104 | _ -> raise (WrongUriToConstant (U.string_of_uri uri))
106 CicEnvironment.set_type_checking_info uri ;
107 log (`Type_checking_completed uri) ;
108 match CicEnvironment.is_type_checked uri cookingsno with
109 CicEnvironment.CheckedObj cobj -> cobj
110 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
113 C.Definition (_,_,ty,_) -> ty
114 | C.Axiom (_,ty,_) -> ty
115 | C.CurrentProof (_,_,_,ty) -> ty
116 | _ -> raise (WrongUriToConstant (U.string_of_uri uri))
118 and type_of_variable uri =
119 let module C = Cic in
120 let module R = CicReduction in
121 let module U = UriManager in
122 (* 0 because a variable is never cooked => no partial cooking at one level *)
123 match CicEnvironment.is_type_checked uri 0 with
124 CicEnvironment.CheckedObj (C.Variable (_,_,ty)) -> ty
125 | CicEnvironment.UncheckedObj (C.Variable (_,bo,ty)) ->
126 log (`Start_type_checking uri) ;
127 (* only to check that ty is well-typed *)
128 let _ = type_of ty in
132 if not (R.are_convertible (type_of bo) ty) then
133 raise (NotWellTyped ("Variable " ^ (U.string_of_uri uri)))
135 CicEnvironment.set_type_checking_info uri ;
136 log (`Type_checking_completed uri) ;
138 | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
140 and does_not_occur n nn te =
141 let module C = Cic in
142 (*CSC: whd sembra essere superflua perche' un caso in cui l'occorrenza *)
143 (*CSC: venga mangiata durante la whd sembra presentare problemi di *)
145 match CicReduction.whd te with
146 C.Rel m when m > n && m <= nn -> false
152 | C.Cast (te,ty) -> does_not_occur n nn te && does_not_occur n nn ty
153 | C.Prod (_,so,dest) ->
154 does_not_occur n nn so && does_not_occur (n + 1) (nn + 1) dest
155 | C.Lambda (_,so,dest) ->
156 does_not_occur n nn so && does_not_occur (n + 1) (nn + 1) dest
157 | C.LetIn (_,so,dest) ->
158 does_not_occur n nn so && does_not_occur (n + 1) (nn + 1) dest
160 List.fold_right (fun x i -> i && does_not_occur n nn x) l true
164 | C.MutConstruct _ -> true
165 | C.MutCase (_,_,_,out,te,pl) ->
166 does_not_occur n nn out && does_not_occur n nn te &&
167 List.fold_right (fun x i -> i && does_not_occur n nn x) pl true
169 let len = List.length fl in
170 let n_plus_len = n + len in
171 let nn_plus_len = nn + len in
173 (fun (_,_,ty,bo) i ->
174 i && does_not_occur n_plus_len nn_plus_len ty &&
175 does_not_occur n_plus_len nn_plus_len bo
178 let len = List.length fl in
179 let n_plus_len = n + len in
180 let nn_plus_len = nn + len in
183 i && does_not_occur n_plus_len nn_plus_len ty &&
184 does_not_occur n_plus_len nn_plus_len bo
187 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
188 (*CSC questa funzione e' simile alla are_all_occurrences_positive, ma fa *)
189 (*CSC dei controlli leggermente diversi. Viene invocata solamente dalla *)
190 (*CSC strictly_positive *)
191 (*CSC definizione (giusta???) tratta dalla mail di Hugo ;-) *)
192 and weakly_positive n nn uri te =
193 let module C = Cic in
194 (*CSC mettere in cicSubstitution *)
195 let rec subst_inductive_type_with_dummy_rel =
197 C.MutInd (uri',_,0) when UriManager.eq uri' uri ->
198 C.Rel 0 (* dummy rel *)
199 | C.Appl ((C.MutInd (uri',_,0))::tl) when UriManager.eq uri' uri ->
200 C.Rel 0 (* dummy rel *)
201 | C.Cast (te,ty) -> subst_inductive_type_with_dummy_rel te
202 | C.Prod (name,so,ta) ->
203 C.Prod (name, subst_inductive_type_with_dummy_rel so,
204 subst_inductive_type_with_dummy_rel ta)
205 | C.Lambda (name,so,ta) ->
206 C.Lambda (name, subst_inductive_type_with_dummy_rel so,
207 subst_inductive_type_with_dummy_rel ta)
209 C.Appl (List.map subst_inductive_type_with_dummy_rel tl)
210 | C.MutCase (uri,cookingsno,i,outtype,term,pl) ->
211 C.MutCase (uri,cookingsno,i,
212 subst_inductive_type_with_dummy_rel outtype,
213 subst_inductive_type_with_dummy_rel term,
214 List.map subst_inductive_type_with_dummy_rel pl)
216 C.Fix (i,List.map (fun (name,i,ty,bo) -> (name,i,
217 subst_inductive_type_with_dummy_rel ty,
218 subst_inductive_type_with_dummy_rel bo)) fl)
220 C.CoFix (i,List.map (fun (name,ty,bo) -> (name,
221 subst_inductive_type_with_dummy_rel ty,
222 subst_inductive_type_with_dummy_rel bo)) fl)
225 match CicReduction.whd te with
226 C.Appl ((C.MutInd (uri',_,0))::tl) when UriManager.eq uri' uri -> true
227 | C.MutInd (uri',_,0) when UriManager.eq uri' uri -> true
228 | C.Prod (C.Anonimous,source,dest) ->
229 strictly_positive n nn (subst_inductive_type_with_dummy_rel source) &&
230 weakly_positive (n + 1) (nn + 1) uri dest
231 | C.Prod (name,source,dest) when does_not_occur 0 n dest ->
232 (* dummy abstraction, so we behave as in the anonimous case *)
233 strictly_positive n nn (subst_inductive_type_with_dummy_rel source) &&
234 weakly_positive (n + 1) (nn + 1) uri dest
235 | C.Prod (_,source,dest) ->
236 does_not_occur n nn (subst_inductive_type_with_dummy_rel source) &&
237 weakly_positive (n + 1) (nn + 1) uri dest
238 | _ -> raise (NotWellFormedTypeOfInductiveConstructor ("Guess where the error is ;-)"))
240 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C *)
241 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
242 and instantiate_parameters params c =
243 let module C = Cic in
244 match (c,params) with
246 | (C.Prod (_,_,ta), he::tl) ->
247 instantiate_parameters tl
248 (CicSubstitution.subst he ta)
249 | (C.Cast (te,_), _) -> instantiate_parameters params te
250 | (t,l) -> raise (Impossible 1)
252 and strictly_positive n nn te =
253 let module C = Cic in
254 let module U = UriManager in
255 match CicReduction.whd te with
258 (*CSC: bisogna controllare ty????*)
259 strictly_positive n nn te
260 | C.Prod (_,so,ta) ->
261 does_not_occur n nn so &&
262 strictly_positive (n+1) (nn+1) ta
263 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
264 List.fold_right (fun x i -> i && does_not_occur n nn x) tl true
265 | C.Appl ((C.MutInd (uri,_,i))::tl) ->
266 let (ok,paramsno,cl) =
267 match CicEnvironment.get_obj uri with
268 C.InductiveDefinition (tl,_,paramsno) ->
269 let (_,_,_,cl) = List.nth tl i in
270 (List.length tl = 1, paramsno, cl)
271 | _ -> raise(WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
273 let (params,arguments) = split tl paramsno in
274 let lifted_params = List.map (CicSubstitution.lift 1) params in
276 List.map (fun (_,te,_) -> instantiate_parameters lifted_params te) cl
280 (fun x i -> i && does_not_occur n nn x)
282 (*CSC: MEGAPATCH3 (sara' quella giusta?)*)
286 weakly_positive (n+1) (nn+1) uri x
288 | C.MutInd (uri,_,i) ->
289 (match CicEnvironment.get_obj uri with
290 C.InductiveDefinition (tl,_,_) ->
292 | _ -> raise (WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
294 | t -> does_not_occur n nn t
296 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
297 and are_all_occurrences_positive uri indparamsno i n nn te =
298 let module C = Cic in
299 match CicReduction.whd te with
300 C.Appl ((C.Rel m)::tl) when m = i ->
301 (*CSC: riscrivere fermandosi a 0 *)
302 (* let's check if the inductive type is applied at least to *)
303 (* indparamsno parameters *)
309 match CicReduction.whd x with
310 C.Rel m when m = n - (indparamsno - k) -> k - 1
311 | _ -> raise (WrongRequiredArgument (UriManager.string_of_uri uri))
315 List.fold_right (fun x i -> i && does_not_occur n nn x) tl true
317 raise (WrongRequiredArgument (UriManager.string_of_uri uri))
318 | C.Rel m when m = i ->
319 if indparamsno = 0 then
322 raise (WrongRequiredArgument (UriManager.string_of_uri uri))
323 | C.Prod (C.Anonimous,source,dest) ->
324 strictly_positive n nn source &&
325 are_all_occurrences_positive uri indparamsno (i+1) (n + 1) (nn + 1) dest
326 | C.Prod (name,source,dest) when does_not_occur 0 n dest ->
327 (* dummy abstraction, so we behave as in the anonimous case *)
328 strictly_positive n nn source &&
329 are_all_occurrences_positive uri indparamsno (i+1) (n + 1) (nn + 1) dest
330 | C.Prod (_,source,dest) ->
331 does_not_occur n nn source &&
332 are_all_occurrences_positive uri indparamsno (i+1) (n + 1) (nn + 1) dest
333 | _ -> raise (NotWellFormedTypeOfInductiveConstructor (UriManager.string_of_uri uri))
335 (*CSC: cambiare il nome, torna unit! *)
336 and cooked_mutual_inductive_defs uri =
337 let module U = UriManager in
339 Cic.InductiveDefinition (itl, _, indparamsno) ->
340 (* let's check if the arity of the inductive types are well *)
342 List.iter (fun (_,_,x,_) -> let _ = type_of x in ()) itl ;
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
358 (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
361 let _ = type_of augmented_term in
362 (* let's check also the positivity conditions *)
363 if not (are_all_occurrences_positive uri indparamsno i 0 len te)
365 raise (NotPositiveOccurrences (U.string_of_uri uri))
368 Some _ -> raise (Impossible 2)
369 | None -> r := Some (recursive_args 0 len te)
376 raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
378 and cooked_type_of_mutual_inductive_defs uri cookingsno i =
379 let module C = Cic in
380 let module R = CicReduction in
381 let module U = UriManager in
383 match CicEnvironment.is_type_checked uri cookingsno with
384 CicEnvironment.CheckedObj cobj -> cobj
385 | CicEnvironment.UncheckedObj uobj ->
386 log (`Start_type_checking uri) ;
387 cooked_mutual_inductive_defs uri uobj ;
388 CicEnvironment.set_type_checking_info uri ;
389 log (`Type_checking_completed uri) ;
390 (match CicEnvironment.is_type_checked uri cookingsno with
391 CicEnvironment.CheckedObj cobj -> cobj
392 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
396 C.InductiveDefinition (dl,_,_) ->
397 let (_,_,arity,_) = List.nth dl i in
399 | _ -> raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
401 and cooked_type_of_mutual_inductive_constr uri cookingsno i j =
402 let module C = Cic in
403 let module R = CicReduction in
404 let module U = UriManager in
406 match CicEnvironment.is_type_checked uri cookingsno with
407 CicEnvironment.CheckedObj cobj -> cobj
408 | CicEnvironment.UncheckedObj uobj ->
409 log (`Start_type_checking uri) ;
410 cooked_mutual_inductive_defs uri uobj ;
411 CicEnvironment.set_type_checking_info uri ;
412 log (`Type_checking_completed uri) ;
413 (match CicEnvironment.is_type_checked uri cookingsno with
414 CicEnvironment.CheckedObj cobj -> cobj
415 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
419 C.InductiveDefinition (dl,_,_) ->
420 let (_,_,_,cl) = List.nth dl i in
421 let (_,ty,_) = List.nth cl (j-1) in
423 | _ -> raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
425 and recursive_args n nn te =
426 let module C = Cic in
427 match CicReduction.whd te with
433 | C.Cast _ (*CSC ??? *) -> raise (Impossible 3) (* due to type-checking *)
434 | C.Prod (_,so,de) ->
435 (not (does_not_occur n nn so))::(recursive_args (n+1) (nn + 1) de)
437 | C.LetIn _ -> raise (Impossible 4) (* due to type-checking *)
440 | C.Abst _ -> raise (Impossible 5)
445 | C.CoFix _ -> raise (Impossible 6) (* due to type-checking *)
447 and get_new_safes p c rl safes n nn x =
448 let module C = Cic in
449 let module U = UriManager in
450 let module R = CicReduction in
451 match (R.whd c, R.whd p, rl) with
452 (C.Prod (_,_,ta1), C.Lambda (_,_,ta2), b::tl) ->
453 (* we are sure that the two sources are convertible because we *)
454 (* have just checked this. So let's go along ... *)
456 List.map (fun x -> x + 1) safes
459 if b then 1::safes' else safes'
461 get_new_safes ta2 ta1 tl safes'' (n+1) (nn+1) (x+1)
462 | (C.MutInd _, e, []) -> (e,safes,n,nn,x)
463 | (C.Appl _, e, []) -> (e,safes,n,nn,x)
464 | (_,_,_) -> raise (Impossible 7)
466 and split_prods n te =
467 let module C = Cic in
468 let module R = CicReduction in
469 match (n, R.whd te) with
471 | (n, C.Prod (_,so,ta)) when n > 0 ->
472 let (l1,l2) = split_prods (n - 1) ta in
474 | (_, _) -> raise (Impossible 8)
476 and eat_lambdas n te =
477 let module C = Cic in
478 let module R = CicReduction in
479 match (n, R.whd te) with
481 | (n, C.Lambda (_,_,ta)) when n > 0 ->
482 let (te, k) = eat_lambdas (n - 1) ta in
484 | (_, _) -> raise (Impossible 9)
486 (*CSC: Tutto quello che segue e' l'intuzione di luca ;-) *)
487 and check_is_really_smaller_arg n nn kl x safes te =
488 (*CSC: forse la whd si puo' fare solo quando serve veramente. *)
489 (*CSC: cfr guarded_by_destructors *)
490 let module C = Cic in
491 let module U = UriManager in
492 match CicReduction.whd te with
493 C.Rel m when List.mem m safes -> true
500 (* | C.Cast (te,ty) ->
501 check_is_really_smaller_arg n nn kl x safes te &&
502 check_is_really_smaller_arg n nn kl x safes ty*)
503 (* | C.Prod (_,so,ta) ->
504 check_is_really_smaller_arg n nn kl x safes so &&
505 check_is_really_smaller_arg (n+1) (nn+1) kl (x+1)
506 (List.map (fun x -> x + 1) safes) ta*)
507 | C.Prod _ -> raise (Impossible 10)
508 | C.Lambda (_,so,ta) ->
509 check_is_really_smaller_arg n nn kl x safes so &&
510 check_is_really_smaller_arg (n+1) (nn+1) kl (x+1)
511 (List.map (fun x -> x + 1) safes) ta
512 | C.LetIn (_,so,ta) ->
513 check_is_really_smaller_arg n nn kl x safes so &&
514 check_is_really_smaller_arg (n+1) (nn+1) kl (x+1)
515 (List.map (fun x -> x + 1) safes) ta
517 (*CSC: sulla coda ci vogliono dei controlli? secondo noi no, ma *)
518 (*CSC: solo perche' non abbiamo trovato controesempi *)
519 check_is_really_smaller_arg n nn kl x safes he
520 | C.Appl [] -> raise (Impossible 11)
523 | C.MutInd _ -> raise (Impossible 12)
524 | C.MutConstruct _ -> false
525 | C.MutCase (uri,_,i,outtype,term,pl) ->
527 C.Rel m when List.mem m safes || m = x ->
528 let (isinductive,paramsno,cl) =
529 match CicEnvironment.get_obj uri with
530 C.InductiveDefinition (tl,_,paramsno) ->
531 let (_,isinductive,_,cl) = List.nth tl i in
533 List.map (fun (id,ty,r) -> (id, snd (split_prods paramsno ty), r)) cl
535 (isinductive,paramsno,cl')
537 raise (WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
539 if not isinductive then
541 (fun p i -> i && check_is_really_smaller_arg n nn kl x safes p)
545 (fun (p,(_,c,rl)) i ->
549 let (_,rl'') = split rl' paramsno in
551 | None -> raise (Impossible 13)
553 let (e,safes',n',nn',x') =
554 get_new_safes p c rl' safes n nn x
557 check_is_really_smaller_arg n' nn' kl x' safes' e
558 ) (List.combine pl cl) true
559 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
560 let (isinductive,paramsno,cl) =
561 match CicEnvironment.get_obj uri with
562 C.InductiveDefinition (tl,_,paramsno) ->
563 let (_,isinductive,_,cl) = List.nth tl i in
565 List.map (fun (id,ty,r) -> (id, snd (split_prods paramsno ty), r)) cl
567 (isinductive,paramsno,cl')
569 raise (WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
571 if not isinductive then
573 (fun p i -> i && check_is_really_smaller_arg n nn kl x safes p)
576 (*CSC: supponiamo come prima che nessun controllo sia necessario*)
577 (*CSC: sugli argomenti di una applicazione *)
579 (fun (p,(_,c,rl)) i ->
583 let (_,rl'') = split rl' paramsno in
585 | None -> raise (Impossible 14)
587 let (e, safes',n',nn',x') =
588 get_new_safes p c rl' safes n nn x
591 check_is_really_smaller_arg n' nn' kl x' safes' e
592 ) (List.combine pl cl) true
595 (fun p i -> i && check_is_really_smaller_arg n nn kl x safes p)
599 let len = List.length fl in
600 let n_plus_len = n + len
601 and nn_plus_len = nn + len
602 and x_plus_len = x + len
603 and safes' = List.map (fun x -> x + len) safes in
605 (fun (_,_,ty,bo) i ->
607 check_is_really_smaller_arg n_plus_len nn_plus_len kl x_plus_len
611 let len = List.length fl in
612 let n_plus_len = n + len
613 and nn_plus_len = nn + len
614 and x_plus_len = x + len
615 and safes' = List.map (fun x -> x + len) safes in
619 check_is_really_smaller_arg n_plus_len nn_plus_len kl x_plus_len
623 and guarded_by_destructors n nn kl x safes =
624 let module C = Cic in
625 let module U = UriManager in
627 C.Rel m when m > n && m <= nn -> false
634 guarded_by_destructors n nn kl x safes te &&
635 guarded_by_destructors n nn kl x safes ty
636 | C.Prod (_,so,ta) ->
637 guarded_by_destructors n nn kl x safes so &&
638 guarded_by_destructors (n+1) (nn+1) kl (x+1)
639 (List.map (fun x -> x + 1) safes) ta
640 | C.Lambda (_,so,ta) ->
641 guarded_by_destructors n nn kl x safes so &&
642 guarded_by_destructors (n+1) (nn+1) kl (x+1)
643 (List.map (fun x -> x + 1) safes) ta
644 | C.LetIn (_,so,ta) ->
645 guarded_by_destructors n nn kl x safes so &&
646 guarded_by_destructors (n+1) (nn+1) kl (x+1)
647 (List.map (fun x -> x + 1) safes) ta
648 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
649 let k = List.nth kl (m - n - 1) in
650 if not (List.length tl > k) then false
654 i && guarded_by_destructors n nn kl x safes param
656 check_is_really_smaller_arg n nn kl x safes (List.nth tl k)
658 List.fold_right (fun t i -> i && guarded_by_destructors n nn kl x safes t)
663 | C.MutConstruct _ -> true
664 | C.MutCase (uri,_,i,outtype,term,pl) ->
666 C.Rel m when List.mem m safes || m = x ->
667 let (isinductive,paramsno,cl) =
668 match CicEnvironment.get_obj uri with
669 C.InductiveDefinition (tl,_,paramsno) ->
670 let (_,isinductive,_,cl) = List.nth tl i in
672 List.map (fun (id,ty,r) -> (id, snd (split_prods paramsno ty), r)) cl
674 (isinductive,paramsno,cl')
676 raise (WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
678 if not isinductive then
679 guarded_by_destructors n nn kl x safes outtype &&
680 guarded_by_destructors n nn kl x safes term &&
681 (*CSC: manca ??? il controllo sul tipo di term? *)
683 (fun p i -> i && guarded_by_destructors n nn kl x safes p)
686 guarded_by_destructors n nn kl x safes outtype &&
687 (*CSC: manca ??? il controllo sul tipo di term? *)
689 (fun (p,(_,c,rl)) i ->
693 let (_,rl'') = split rl' paramsno in
695 | None -> raise (Impossible 15)
697 let (e,safes',n',nn',x') =
698 get_new_safes p c rl' safes n nn x
701 guarded_by_destructors n' nn' kl x' safes' e
702 ) (List.combine pl cl) true
703 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
704 let (isinductive,paramsno,cl) =
705 match CicEnvironment.get_obj uri with
706 C.InductiveDefinition (tl,_,paramsno) ->
707 let (_,isinductive,_,cl) = List.nth tl i in
709 List.map (fun (id,ty,r) -> (id, snd (split_prods paramsno ty), r)) cl
711 (isinductive,paramsno,cl')
713 raise (WrongUriToMutualInductiveDefinitions(U.string_of_uri uri))
715 if not isinductive then
716 guarded_by_destructors n nn kl x safes outtype &&
717 guarded_by_destructors n nn kl x safes term &&
718 (*CSC: manca ??? il controllo sul tipo di term? *)
720 (fun p i -> i && guarded_by_destructors n nn kl x safes p)
723 guarded_by_destructors n nn kl x safes outtype &&
724 (*CSC: manca ??? il controllo sul tipo di term? *)
726 (fun t i -> i && guarded_by_destructors n nn kl x safes t)
729 (fun (p,(_,c,rl)) i ->
733 let (_,rl'') = split rl' paramsno in
735 | None -> raise (Impossible 16)
737 let (e, safes',n',nn',x') =
738 get_new_safes p c rl' safes n nn x
741 guarded_by_destructors n' nn' kl x' safes' e
742 ) (List.combine pl cl) true
744 guarded_by_destructors n nn kl x safes outtype &&
745 guarded_by_destructors n nn kl x safes term &&
746 (*CSC: manca ??? il controllo sul tipo di term? *)
748 (fun p i -> i && guarded_by_destructors n nn kl x safes p)
752 let len = List.length fl in
753 let n_plus_len = n + len
754 and nn_plus_len = nn + len
755 and x_plus_len = x + len
756 and safes' = List.map (fun x -> x + len) safes in
758 (fun (_,_,ty,bo) i ->
759 i && guarded_by_destructors n_plus_len nn_plus_len kl x_plus_len
761 guarded_by_destructors n_plus_len nn_plus_len kl x_plus_len
765 let len = List.length fl in
766 let n_plus_len = n + len
767 and nn_plus_len = nn + len
768 and x_plus_len = x + len
769 and safes' = List.map (fun x -> x + len) safes in
772 i && guarded_by_destructors n_plus_len nn_plus_len kl x_plus_len
774 guarded_by_destructors n_plus_len nn_plus_len kl x_plus_len safes'
778 (*CSC h = 0 significa non ancora protetto *)
779 and guarded_by_constructors n nn h =
780 let module C = Cic in
782 C.Rel m when m > n && m <= nn -> h = 1
787 | C.Implicit -> true (*CSC: ma alcuni sono impossibili!!!! vedi Prod *)
789 guarded_by_constructors n nn h te &&
790 guarded_by_constructors n nn h ty
791 | C.Prod (_,so,de) ->
792 raise (Impossible 17) (* the term has just been type-checked *)
793 | C.Lambda (_,so,de) ->
794 does_not_occur n nn so &&
795 guarded_by_constructors (n + 1) (nn + 1) h de
796 | C.LetIn (_,so,de) ->
797 does_not_occur n nn so &&
798 guarded_by_constructors (n + 1) (nn + 1) h de
799 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
801 List.fold_right (fun x i -> i && does_not_occur n nn x) tl true
802 | C.Appl ((C.MutConstruct (uri,cookingsno,i,j))::tl) ->
803 let (is_coinductive, rl) =
804 match CicEnvironment.get_cooked_obj uri cookingsno with
805 C.InductiveDefinition (itl,_,_) ->
806 let (_,is_inductive,_,cl) = List.nth itl i in
807 let (_,cons,rrec_args) = List.nth cl (j - 1) in
808 (match !rrec_args with
809 None -> raise (Impossible 18)
810 | Some rec_args -> (not is_inductive, rec_args)
813 raise (WrongUriToMutualInductiveDefinitions
814 (UriManager.string_of_uri uri))
821 guarded_by_constructors n nn 1 x
823 does_not_occur n nn x
824 ) (List.combine tl rl) true
826 List.fold_right (fun x i -> i && does_not_occur n nn x) l true
830 | C.MutConstruct _ -> true (*CSC: ma alcuni sono impossibili!!!! vedi Prod *)
831 | C.MutCase (_,_,_,out,te,pl) ->
832 let rec returns_a_coinductive =
834 (*CSC: per le regole di tipaggio, la chiamata ricorsiva verra' *)
835 (*CSC: effettata solo una volta, per mangiarsi l'astrazione *)
837 C.Lambda (_,_,de) -> returns_a_coinductive de
838 | C.MutInd (uri,_,i) ->
839 (*CSC: definire una funzioncina per questo codice sempre replicato *)
840 (match CicEnvironment.get_obj uri with
841 C.InductiveDefinition (itl,_,_) ->
842 let (_,is_inductive,_,_) = List.nth itl i in
845 raise (WrongUriToMutualInductiveDefinitions
846 (UriManager.string_of_uri uri))
848 (*CSC: bug nella prossima riga (manca la whd) *)
849 | C.Appl ((C.MutInd (uri,_,i))::_) ->
850 (match CicEnvironment.get_obj uri with
851 C.InductiveDefinition (itl,_,_) ->
852 let (_,is_inductive,_,_) = List.nth itl i in
855 raise (WrongUriToMutualInductiveDefinitions
856 (UriManager.string_of_uri uri))
860 does_not_occur n nn out &&
861 does_not_occur n nn te &&
862 if returns_a_coinductive out then
864 (fun x i -> i && guarded_by_constructors n nn h x) pl true
866 List.fold_right (fun x i -> i && does_not_occur n nn x) pl true
868 let len = List.length fl in
869 let n_plus_len = n + len
870 and nn_plus_len = nn + len in
872 (fun (_,_,ty,bo) i ->
873 i && does_not_occur n_plus_len nn_plus_len ty &&
874 does_not_occur n_plus_len nn_plus_len bo
877 let len = List.length fl in
878 let n_plus_len = n + len
879 and nn_plus_len = nn + len in
882 i && does_not_occur n_plus_len nn_plus_len ty &&
883 does_not_occur n_plus_len nn_plus_len bo
886 and check_allowed_sort_elimination uri i need_dummy ind arity1 arity2 =
887 let module C = Cic in
888 let module U = UriManager in
889 match (CicReduction.whd arity1, CicReduction.whd arity2) with
890 (C.Prod (_,so1,de1), C.Prod (_,so2,de2))
891 when CicReduction.are_convertible so1 so2 ->
892 check_allowed_sort_elimination uri i need_dummy
893 (C.Appl [CicSubstitution.lift 1 ind ; C.Rel 1]) de1 de2
894 | (C.Sort C.Prop, C.Sort C.Prop) when need_dummy -> true
895 | (C.Sort C.Prop, C.Sort C.Set) when need_dummy ->
896 (match CicEnvironment.get_obj uri with
897 C.InductiveDefinition (itl,_,_) ->
898 let (_,_,_,cl) = List.nth itl i in
899 (* is a singleton definition? *)
902 raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
904 | (C.Sort C.Set, C.Sort C.Prop) when need_dummy -> true
905 | (C.Sort C.Set, C.Sort C.Set) when need_dummy -> true
906 | (C.Sort C.Set, C.Sort C.Type) when need_dummy ->
907 (match CicEnvironment.get_obj uri with
908 C.InductiveDefinition (itl,_,paramsno) ->
909 let (_,_,_,cl) = List.nth itl i in
910 List.fold_right (fun (_,x,_) i -> i && is_small paramsno x) cl true
912 raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
914 | (C.Sort C.Type, C.Sort _) when need_dummy -> true
915 | (C.Sort C.Prop, C.Prod (_,so,ta)) when not need_dummy ->
916 let res = CicReduction.are_convertible so ind
919 (match CicReduction.whd ta with
920 C.Sort C.Prop -> true
922 (match CicEnvironment.get_obj uri with
923 C.InductiveDefinition (itl,_,_) ->
924 let (_,_,_,cl) = List.nth itl i in
925 (* is a singleton definition? *)
928 raise (WrongUriToMutualInductiveDefinitions
929 (U.string_of_uri uri))
933 | (C.Sort C.Set, C.Prod (_,so,ta)) when not need_dummy ->
934 let res = CicReduction.are_convertible so ind
937 (match CicReduction.whd ta with
939 | C.Sort C.Set -> true
941 (match CicEnvironment.get_obj uri with
942 C.InductiveDefinition (itl,_,paramsno) ->
943 let (_,_,_,cl) = List.nth itl i in
945 (fun (_,x,_) i -> i && is_small paramsno x) cl true
947 raise (WrongUriToMutualInductiveDefinitions
948 (U.string_of_uri uri))
950 | _ -> raise (Impossible 19)
952 | (C.Sort C.Type, C.Prod (_,so,_)) when not need_dummy ->
953 CicReduction.are_convertible so ind
956 and type_of_branch argsno need_dummy outtype term constype =
957 let module C = Cic in
958 let module R = CicReduction in
959 match R.whd constype with
964 C.Appl [outtype ; term]
965 | C.Appl (C.MutInd (_,_,_)::tl) ->
966 let (_,arguments) = split tl argsno
968 if need_dummy && arguments = [] then
971 C.Appl (outtype::arguments@(if need_dummy then [] else [term]))
972 | C.Prod (name,so,de) ->
973 C.Prod (C.Name "pippo",so,type_of_branch argsno need_dummy
974 (CicSubstitution.lift 1 outtype)
975 (C.Appl [CicSubstitution.lift 1 term ; C.Rel 1]) de)
976 | _ -> raise (Impossible 20)
979 (* type_of_aux' is just another name (with a different scope) for type_of_aux *)
980 and type_of_aux' env t =
981 let rec type_of_aux env =
982 let module C = Cic in
983 let module R = CicReduction in
984 let module S = CicSubstitution in
985 let module U = UriManager in
992 _ -> raise (NotWellTyped "Not a close term")
997 let ty = type_of_variable uri in
1000 | C.Meta n -> raise NotImplemented
1001 | C.Sort s -> C.Sort C.Type (*CSC manca la gestione degli universi!!! *)
1002 | C.Implicit -> raise (Impossible 21)
1004 let _ = type_of ty in
1005 if R.are_convertible (type_of_aux env te) ty then ty
1006 else raise (NotWellTyped "Cast")
1008 let sort1 = type_of_aux env s
1009 and sort2 = type_of_aux (s::env) t in
1010 sort_of_prod (sort1,sort2)
1011 | C.Lambda (n,s,t) ->
1012 let sort1 = type_of_aux env s
1013 and type2 = type_of_aux (s::env) t in
1014 let sort2 = type_of_aux (s::env) type2 in
1015 (* only to check if the product is well-typed *)
1016 let _ = sort_of_prod (sort1,sort2) in
1018 | C.LetIn (n,s,t) ->
1019 let type1 = type_of_aux env s in
1020 let type2 = type_of_aux (type1::env) t in
1022 | C.Appl (he::tl) when List.length tl > 0 ->
1023 let hetype = type_of_aux env he
1024 and tlbody_and_type = List.map (fun x -> (x, type_of_aux env x)) tl in
1025 eat_prods hetype tlbody_and_type
1026 | C.Appl _ -> raise (NotWellTyped "Appl: no arguments")
1027 | C.Const (uri,cookingsno) ->
1029 let cty = cooked_type_of_constant uri cookingsno in
1032 | C.Abst _ -> raise (Impossible 22)
1033 | C.MutInd (uri,cookingsno,i) ->
1035 let cty = cooked_type_of_mutual_inductive_defs uri cookingsno i in
1038 | C.MutConstruct (uri,cookingsno,i,j) ->
1039 let cty = cooked_type_of_mutual_inductive_constr uri cookingsno i j
1042 | C.MutCase (uri,cookingsno,i,outtype,term,pl) ->
1043 let outsort = type_of_aux env outtype in
1044 let (need_dummy, k) =
1045 let rec guess_args t =
1046 match CicReduction.whd t with
1047 C.Sort _ -> (true, 0)
1048 | C.Prod (_, s, t) ->
1049 let (b, n) = guess_args t in
1051 (* last prod before sort *)
1052 match CicReduction.whd s with
1053 (*CSC vedi nota delirante su cookingsno in cicReduction.ml *)
1054 C.MutInd (uri',_,i') when U.eq uri' uri && i' = i -> (false, 1)
1055 | C.Appl ((C.MutInd (uri',_,i')) :: _)
1056 when U.eq uri' uri && i' = i -> (false, 1)
1060 | _ -> raise (NotWellTyped "MutCase: outtype ill-formed")
1062 (*CSC whd non serve dopo type_of_aux ? *)
1063 let (b, k) = guess_args outsort in
1064 if not b then (b, k - 1) else (b, k)
1066 let (parameters, arguments) =
1067 match R.whd (type_of_aux env term) with
1068 (*CSC manca il caso dei CAST *)
1069 C.MutInd (uri',_,i') ->
1070 (*CSC vedi nota delirante sui cookingsno in cicReduction.ml*)
1071 if U.eq uri uri' && i = i' then ([],[])
1072 else raise (NotWellTyped ("MutCase: the term is of type " ^
1073 (U.string_of_uri uri') ^ "," ^ string_of_int i' ^
1074 " instead of type " ^ (U.string_of_uri uri') ^ "," ^
1076 | C.Appl (C.MutInd (uri',_,i') :: tl) ->
1077 if U.eq uri uri' && i = i' then split tl (List.length tl - k)
1078 else raise (NotWellTyped ("MutCase: the term is of type " ^
1079 (U.string_of_uri uri') ^ "," ^ string_of_int i' ^
1080 " instead of type " ^ (U.string_of_uri uri) ^ "," ^
1082 | _ -> raise (NotWellTyped "MutCase: the term is not an inductive one")
1084 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
1085 let sort_of_ind_type =
1086 if parameters = [] then
1087 C.MutInd (uri,cookingsno,i)
1089 C.Appl ((C.MutInd (uri,cookingsno,i))::parameters)
1091 if not (check_allowed_sort_elimination uri i need_dummy
1092 sort_of_ind_type (type_of_aux env sort_of_ind_type) outsort)
1094 raise (NotWellTyped "MutCase: not allowed sort elimination") ;
1096 (* let's check if the type of branches are right *)
1098 match CicEnvironment.get_cooked_obj uri cookingsno with
1099 C.InductiveDefinition (tl,_,parsno) ->
1100 let (_,_,_,cl) = List.nth tl i in (cl,parsno)
1102 raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
1104 let (_,branches_ok) =
1106 (fun (j,b) (p,(_,c,_)) ->
1108 if parameters = [] then
1109 (C.MutConstruct (uri,cookingsno,i,j))
1111 (C.Appl (C.MutConstruct (uri,cookingsno,i,j)::parameters))
1114 R.are_convertible (type_of_aux env p)
1115 (type_of_branch parsno need_dummy outtype cons
1116 (type_of_aux env cons))
1118 ) (1,true) (List.combine pl cl)
1120 if not branches_ok then
1121 raise (NotWellTyped "MutCase: wrong type of a branch") ;
1123 if not need_dummy then
1124 C.Appl ((outtype::arguments)@[term])
1125 else if arguments = [] then
1128 C.Appl (outtype::arguments)
1130 let types_times_kl =
1132 (List.map (fun (_,k,ty,_) -> let _ = type_of_aux env ty in (ty,k)) fl)
1134 let (types,kl) = List.split types_times_kl in
1135 let len = List.length types in
1137 (fun (name,x,ty,bo) ->
1138 if (R.are_convertible (type_of_aux (types @ env) bo)
1139 (CicSubstitution.lift len ty))
1142 let (m, eaten) = eat_lambdas (x + 1) bo in
1143 (*let's control the guarded by destructors conditions D{f,k,x,M}*)
1144 if not (guarded_by_destructors eaten (len + eaten) kl 1 [] m) then
1145 raise (NotWellTyped "Fix: not guarded by destructors")
1148 raise (NotWellTyped "Fix: ill-typed bodies")
1151 (*CSC: controlli mancanti solo su D{f,k,x,M} *)
1152 let (_,_,ty,_) = List.nth fl i in
1156 List.rev (List.map (fun (_,ty,_) -> let _ = type_of_aux env ty in ty) fl)
1158 let len = List.length types in
1161 if (R.are_convertible (type_of_aux (types @ env) bo)
1162 (CicSubstitution.lift len ty))
1165 (* let's control the guarded by constructors conditions C{f,M} *)
1166 if not (guarded_by_constructors 0 len 0 bo) then
1167 raise (NotWellTyped "CoFix: not guarded by constructors")
1170 raise (NotWellTyped "CoFix: ill-typed bodies")
1173 let (_,ty,_) = List.nth fl i in
1176 and sort_of_prod (t1, t2) =
1177 let module C = Cic in
1178 let t1' = CicReduction.whd t1 in
1179 let t2' = CicReduction.whd t2 in
1180 match (t1', t2') with
1181 (C.Sort s1, C.Sort s2)
1182 when (s2 = C.Prop or s2 = C.Set) -> (* different from Coq manual!!! *)
1184 | (C.Sort s1, C.Sort s2) -> C.Sort C.Type (*CSC manca la gestione degli universi!!! *)
1188 ("Prod: sort1= " ^ CicPp.ppterm t1' ^ " ; sort2= " ^ CicPp.ppterm t2'))
1190 and eat_prods hetype =
1191 (*CSC: siamo sicuri che le are_convertible non lavorino con termini non *)
1195 | (hete, hety)::tl ->
1196 (match (CicReduction.whd hetype) with
1198 if CicReduction.are_convertible s hety then
1199 (CicReduction.fdebug := -1 ;
1200 eat_prods (CicSubstitution.subst hete t) tl
1204 CicReduction.fdebug := 0 ;
1205 ignore (CicReduction.are_convertible s hety) ;
1208 raise (NotWellTyped "Appl: wrong parameter-type")
1210 | _ -> raise (NotWellTyped "Appl: wrong Prod-type")
1215 (* is a small constructor? *)
1216 (*CSC: ottimizzare calcolando staticamente *)
1217 and is_small paramsno c =
1218 let rec is_small_aux env c =
1219 let module C = Cic in
1220 match CicReduction.whd c with
1222 let s = type_of_aux' env so in
1223 (s = C.Sort C.Prop || s = C.Sort C.Set) &&
1224 is_small_aux (so::env) de
1225 | _ -> true (*CSC: we trust the type-checker *)
1227 let (sx,dx) = split_prods paramsno c in
1228 is_small_aux (List.rev sx) dx
1235 let module C = Cic in
1236 let module R = CicReduction in
1237 let module U = UriManager in
1238 match CicEnvironment.is_type_checked uri 0 with
1239 CicEnvironment.CheckedObj _ -> ()
1240 | CicEnvironment.UncheckedObj uobj ->
1241 (* let's typecheck the uncooked object *)
1242 log (`Start_type_checking uri) ;
1244 C.Definition (_,te,ty,_) ->
1245 let _ = type_of ty in
1246 if not (R.are_convertible (type_of te ) ty) then
1247 raise (NotWellTyped ("Constant " ^ (U.string_of_uri uri)))
1248 | C.Axiom (_,ty,_) ->
1249 (* only to check that ty is well-typed *)
1250 let _ = type_of ty in ()
1251 | C.CurrentProof (_,_,te,ty) ->
1253 let _ = type_of ty in
1254 debug (type_of te) [] ;
1255 if not (R.are_convertible (type_of te) ty) then
1256 raise (NotWellTyped ("CurrentProof" ^ (U.string_of_uri uri)))
1257 | C.Variable (_,bo,ty) ->
1258 (* only to check that ty is well-typed *)
1259 let _ = type_of ty in
1263 if not (R.are_convertible (type_of bo) ty) then
1264 raise (NotWellTyped ("Variable" ^ (U.string_of_uri uri)))
1266 | C.InductiveDefinition _ ->
1267 cooked_mutual_inductive_defs uri uobj
1269 CicEnvironment.set_type_checking_info uri ;
1270 log (`Type_checking_completed uri)