2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
14 exception RefineFailure of (Stdpp.location * string) Lazy.t;;
15 exception Uncertain of (Stdpp.location * string) Lazy.t;;
16 exception AssertFailure of string Lazy.t;;
19 module Ref = NReference
22 let inside c = indent := !indent ^ String.make 1 c;;
23 let outside () = indent := String.sub !indent 0 (String.length !indent -1);;
27 prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
33 let wrap_exc msg = function
34 | NCicUnification.Uncertain _ -> Uncertain msg
35 | NCicUnification.UnificationFailure _ -> RefineFailure msg
36 | NCicTypeChecker.TypeCheckerFailure _ -> RefineFailure msg
40 let exp_implicit metasenv context expty =
41 let foo x = match expty with Some t -> `WithType t | None -> x in
43 | `Closed -> NCicMetaSubst.mk_meta metasenv [] (foo `Term)
44 | `Type -> NCicMetaSubst.mk_meta metasenv context (foo `Type)
45 | `Term -> NCicMetaSubst.mk_meta metasenv context (foo `Term)
50 let check_allowed_sort_elimination localise r orig =
53 | C.Appl l -> C.Appl (l @ [arg])
54 | t -> C.Appl [t;arg] in
55 (* ctx, ind_type @ lefts, sort_of_ind_ty@lefts, outsort *)
56 let rec aux metasenv subst context ind arity1 arity2 =
57 (*D*)inside 'S'; try let rc =
58 let arity1 = NCicReduction.whd ~subst context arity1 in
59 pp (lazy(NCicPp.ppterm ~subst ~metasenv ~context arity1 ^ " elimsto " ^
60 NCicPp.ppterm ~subst ~metasenv ~context arity2 ^ "\nMENV:\n"^
61 NCicPp.ppmetasenv ~subst metasenv));
63 | C.Prod (name,so1,de1) (* , t ==?== C.Prod _ *) ->
64 let metasenv, meta, _ =
65 NCicMetaSubst.mk_meta metasenv ((name,C.Decl so1)::context) `Type
68 try NCicUnification.unify metasenv subst context
69 arity2 (C.Prod (name, so1, meta))
70 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
71 "expected %s, found %s" (* XXX localizzare meglio *)
72 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod (name, so1, meta)))
73 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
75 aux metasenv subst ((name, C.Decl so1)::context)
76 (mkapp (NCicSubstitution.lift 1 ind) (C.Rel 1)) de1 meta
77 | C.Sort _ (* , t ==?== C.Prod _ *) ->
78 let metasenv, meta, _ = NCicMetaSubst.mk_meta metasenv [] `Type in
80 try NCicUnification.unify metasenv subst context
81 arity2 (C.Prod ("_", ind, meta))
82 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
83 "expected %s, found %s" (* XXX localizzare meglio *)
84 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod ("_", ind, meta)))
85 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
87 (try NCicTypeChecker.check_allowed_sort_elimination
88 ~metasenv ~subst r context ind arity1 arity2;
90 with exc -> raise (wrap_exc (lazy (localise orig,
91 "Sort elimination not allowed ")) exc))
93 (*D*)in outside(); rc with exc -> outside (); raise exc
99 ?(localise=fun _ -> Stdpp.dummy_loc)
100 ~look_for_coercion metasenv subst context term expty
102 let force_ty metasenv subst context orig t infty expty =
103 (*D*)inside 'F'; try let rc =
108 | C.Lambda _ -> metasenv, subst, t, expty
111 (NCicPp.ppterm ~metasenv ~subst ~context infty) ^ " === " ^
112 (NCicPp.ppterm ~metasenv ~subst ~context expty)));
114 let metasenv, subst =
115 NCicUnification.unify metasenv subst context infty expty
117 metasenv, subst, t, expty
119 try_coercions ~look_for_coercion ~localise
120 metasenv subst context t orig infty expty true exc)
121 | None -> metasenv, subst, t, infty
122 (*D*)in outside(); rc with exc -> outside (); raise exc
124 let rec typeof_aux metasenv subst context expty =
126 (*D*)inside 'R'; try let rc =
127 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t));
128 pp (lazy (NCicPp.ppmetasenv ~subst metasenv));
129 let metasenv, subst, t, infty =
134 match List.nth context (n - 1) with
135 | (_,C.Decl ty) -> NCicSubstitution.lift n ty
136 | (_,C.Def (_,ty)) -> NCicSubstitution.lift n ty
138 raise (RefineFailure (lazy (localise t,"unbound variable"))))
140 metasenv, subst, t, infty
141 | C.Sort (C.Type [false,u]) -> metasenv,subst,t,(C.Sort (C.Type [true, u]))
142 | C.Sort (C.Type _) ->
143 raise (AssertFailure (lazy ("Cannot type an inferred type: "^
144 NCicPp.ppterm ~subst ~metasenv ~context t)))
145 | C.Sort _ -> metasenv,subst,t,(C.Sort (C.Type NCicEnvironment.type0))
146 | C.Implicit infos ->
147 let metasenv,t,ty = exp_implicit metasenv context expty infos in
148 metasenv, subst, t, ty
149 | C.Meta (n,l) as t ->
152 let _,_,_,ty = NCicUtils.lookup_subst n subst in ty
153 with NCicUtils.Subst_not_found _ -> try
154 let _,_,ty = NCicUtils.lookup_meta n metasenv in
155 match ty with C.Implicit _ -> assert false | _ -> ty
156 with NCicUtils.Meta_not_found _ ->
157 raise (AssertFailure (lazy (Printf.sprintf
158 "%s not found" (NCicPp.ppterm ~subst ~metasenv ~context t))))
160 metasenv, subst, t, NCicSubstitution.subst_meta l ty
162 metasenv, subst, t, NCicTypeChecker.typeof ~subst ~metasenv context t
163 | C.Prod (name,(s as orig_s),(t as orig_t)) ->
164 let metasenv, subst, s, s1 = typeof_aux metasenv subst context None s in
165 let metasenv, subst, s, s1 =
166 force_to_sort ~look_for_coercion
167 metasenv subst context s orig_s localise s1 in
168 let context1 = (name,(C.Decl s))::context in
169 let metasenv, subst, t, s2 = typeof_aux metasenv subst context1 None t in
170 let metasenv, subst, t, s2 =
171 force_to_sort ~look_for_coercion
172 metasenv subst context1 t orig_t localise s2 in
173 let metasenv, subst, s, t, ty =
174 sort_of_prod ~look_for_coercion localise metasenv subst
175 context orig_s orig_t (name,s) t (s1,s2)
177 metasenv, subst, NCic.Prod(name,s,t), ty
178 | C.Lambda (n,(s as orig_s),t) ->
179 let exp_s, exp_ty_t =
183 match NCicReduction.whd ~subst context expty with
184 | C.Prod (_,s,t) -> Some s, Some t
185 | _ -> None, None (** XXX FUNCLASS |-> QUALCOSA *)
187 let metasenv, subst, s, ty_s =
188 typeof_aux metasenv subst context None s in
189 let metasenv, subst, s, _ =
190 force_to_sort ~look_for_coercion
191 metasenv subst context s orig_s localise ty_s in
192 let (metasenv,subst), exp_ty_t =
195 (try NCicUnification.unify metasenv subst context s exp_s,exp_ty_t
196 with exc -> raise (wrap_exc (lazy (localise orig_s, Printf.sprintf
197 "Source type %s was expected to be %s" (NCicPp.ppterm ~metasenv
198 ~subst ~context s) (NCicPp.ppterm ~metasenv ~subst ~context
200 | None -> (metasenv, subst), None
202 let context = (n,C.Decl s) :: context in
203 let metasenv, subst, t, ty_t =
204 typeof_aux metasenv subst context exp_ty_t t
206 metasenv, subst, C.Lambda(n,s,t), C.Prod (n,s,ty_t)
207 | C.LetIn (n,(ty as orig_ty),t,bo) ->
208 let metasenv, subst, ty, ty_ty =
209 typeof_aux metasenv subst context None ty in
210 let metasenv, subst, ty, _ =
211 force_to_sort ~look_for_coercion
212 metasenv subst context ty orig_ty localise ty_ty in
213 let metasenv, subst, t, _ =
214 typeof_aux metasenv subst context (Some ty) t in
215 let context1 = (n, C.Def (t,ty)) :: context in
216 let metasenv, subst, bo, bo_ty =
217 typeof_aux metasenv subst context1 None bo
219 let bo_ty = NCicSubstitution.subst ~avoid_beta_redexes:true t bo_ty in
220 metasenv, subst, C.LetIn (n, ty, t, bo), bo_ty
221 | C.Appl ((he as orig_he)::(_::_ as args)) ->
222 let metasenv, subst, he, ty_he =
223 typeof_aux metasenv subst context None he in
224 eat_prods ~localise ~look_for_coercion
225 metasenv subst context orig_he he ty_he args
226 | C.Appl _ -> raise (AssertFailure (lazy "Appl of length < 2"))
227 | C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as r,
228 outtype,(term as orig_term),pl) as orig ->
229 let _, leftno, itl, _, _ = NCicEnvironment.get_checked_indtys r in
230 let _, _, arity, cl = List.nth itl tyno in
231 let constructorsno = List.length cl in
232 let _, metasenv, args =
233 NCicMetaSubst.saturate metasenv subst context arity 0 in
234 let ind = if args = [] then C.Const r else C.Appl (C.Const r::args) in
235 let metasenv, subst, term, _ =
236 typeof_aux metasenv subst context (Some ind) term in
237 let metasenv, subst, outtype, outsort =
238 typeof_aux metasenv subst context None outtype in
239 let parameters, arguments = HExtlib.split_nth leftno args in
240 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
242 if parameters = [] then C.Const r
243 else C.Appl ((C.Const r)::parameters) in
244 let metasenv, subst, ind, ind_ty =
245 typeof_aux metasenv subst context None ind in
246 let metasenv, subst =
247 check_allowed_sort_elimination localise r orig_term metasenv subst
248 context ind ind_ty outsort
250 (* let's check if the type of branches are right *)
251 if List.length pl <> constructorsno then
252 raise (RefineFailure (lazy (localise orig,
253 "Wrong number of cases in a match")));
254 let _, metasenv, subst, pl_rev =
256 (fun (j, metasenv, subst, branches) p ->
258 let cons = Ref.mk_constructor j r in
259 if parameters = [] then C.Const cons
260 else C.Appl (C.Const cons::parameters)
262 let metasenv, subst, cons, ty_cons =
263 typeof_aux metasenv subst context None cons in
265 NCicTypeChecker.type_of_branch
266 ~subst context leftno outtype cons ty_cons in
267 pp (lazy ("TYPEOFBRANCH: " ^
268 NCicPp.ppterm ~metasenv ~subst ~context p ^ " ::: " ^
269 NCicPp.ppterm ~metasenv ~subst ~context ty_branch ));
270 let metasenv, subst, p, _ =
271 typeof_aux metasenv subst context (Some ty_branch) p in
272 j+1, metasenv, subst, p :: branches)
273 (1, metasenv, subst, []) pl
276 C.Match (r, outtype, term, List.rev pl_rev),
277 NCicReduction.head_beta_reduce (C.Appl (outtype::arguments@[term]))
278 | C.Match _ as orig ->
279 prerr_endline (NCicPp.ppterm ~metasenv ~subst ~context orig);
282 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t ^ " :: "^
283 NCicPp.ppterm ~metasenv ~subst ~context infty ));
284 force_ty metasenv subst context orig t infty expty
285 (*D*)in outside(); rc with exc -> outside (); raise exc
287 typeof_aux metasenv subst context expty term
290 ~localise ~look_for_coercion
291 metasenv subst context t orig_t infty expty perform_unification exc
293 (* we try with a coercion *)
294 let rec first exc = function
296 raise (wrap_exc (lazy (localise orig_t, Printf.sprintf
297 "The term %s has type %s but is here used with type %s"
298 (NCicPp.ppterm ~metasenv ~subst ~context t)
299 (NCicPp.ppterm ~metasenv ~subst ~context infty)
300 (NCicPp.ppterm ~metasenv ~subst ~context expty))) exc)
301 | (metasenv, newterm, newtype, meta)::tl ->
303 let metasenv, subst =
304 NCicUnification.unify metasenv subst context meta t
306 let metasenv, subst =
307 if perform_unification then
308 NCicUnification.unify metasenv subst context newtype expty
311 metasenv, subst, newterm, newtype
313 | NCicUnification.UnificationFailure _ -> first exc tl
314 | NCicUnification.Uncertain _ as exc -> first exc tl
317 (look_for_coercion metasenv subst context infty expty)
320 ~look_for_coercion metasenv subst context t orig_t localise ty
322 match NCicReduction.whd ~subst context ty with
323 | C.Meta (_,(0,(C.Irl 0 | C.Ctx []))) as ty ->
324 metasenv, subst, t, ty
325 | C.Meta (i,(_,(C.Irl 0 | C.Ctx []))) ->
326 metasenv, subst, t, C.Meta(i,(0,C.Irl 0))
327 | C.Meta (i,(_,lc)) ->
328 let len = match lc with C.Irl len->len | C.Ctx l->List.length l in
329 let metasenv, subst, newmeta =
331 NCicMetaSubst.restrict metasenv subst i (HExtlib.list_seq 1 (len+1))
332 else metasenv, subst, i
334 metasenv, subst, t, C.Meta (newmeta,(0,C.Irl 0))
335 | C.Sort _ as ty -> metasenv, subst, t, ty
337 try_coercions ~localise ~look_for_coercion metasenv subst context
338 t orig_t ty (NCic.Sort (NCic.Type NCicEnvironment.type0)) false
339 (Uncertain (lazy (localise orig_t,
340 "The type of " ^ NCicPp.ppterm ~metasenv ~subst ~context t
341 ^ " is not a sort: " ^ NCicPp.ppterm ~metasenv ~subst ~context ty)))
343 and sort_of_prod ~look_for_coercion
344 localise metasenv subst context orig_s orig_t (name,s) t (t1, t2)
346 (* force to sort is done in the Prod case in typeof *)
348 | C.Sort _, C.Sort C.Prop -> metasenv, subst, s, t, t2
349 | C.Sort (C.Type u1), C.Sort (C.Type u2) ->
350 metasenv, subst, s, t, C.Sort (C.Type (NCicEnvironment.max u1 u2))
351 | C.Sort C.Prop,C.Sort (C.Type _) -> metasenv, subst, s, t, t2
353 | C.Meta _, C.Meta (_,(_,_))
354 | C.Sort _, C.Meta (_,(_,_)) -> metasenv, subst, s, t, t2
355 | x, (C.Sort _ | C.Meta _) | _, x ->
356 let y, context, orig =
357 if x == t1 then s, context, orig_s
358 else t, ((name,C.Decl s)::context), orig_t
360 raise (RefineFailure (lazy (localise orig,Printf.sprintf
361 "%s is expected to be a type, but its type is %s that is not a sort"
362 (NCicPp.ppterm ~subst ~metasenv ~context y)
363 (NCicPp.ppterm ~subst ~metasenv ~context x))))
366 ~localise ~look_for_coercion metasenv subst context orig_he he ty_he args
368 (*D*)inside 'E'; try let rc =
369 let rec aux metasenv subst args_so_far he ty_he = function
370 | []->metasenv, subst, NCicUntrusted.mk_appl he (List.rev args_so_far), ty_he
372 match NCicReduction.whd ~subst context ty_he with
374 let metasenv, subst, arg, _ =
375 typeof ~look_for_coercion ~localise
376 metasenv subst context arg (Some s) in
377 let t = NCicSubstitution.subst ~avoid_beta_redexes:true arg t in
378 aux metasenv subst (arg :: args_so_far) he t tl
380 | C.Appl (C.Meta _ :: _) as t ->
381 let metasenv, subst, arg, ty_arg =
382 typeof ~look_for_coercion ~localise
383 metasenv subst context arg None in
384 let metasenv, meta, _ =
385 NCicMetaSubst.mk_meta metasenv
386 (("_",C.Decl ty_arg) :: context) `Type
388 let flex_prod = C.Prod ("_", ty_arg, meta) in
389 (* next line grants that ty_args is a type *)
390 let metasenv,subst, flex_prod, _ =
391 typeof ~look_for_coercion ~localise metasenv subst
392 context flex_prod None in
393 pp (lazy ( "UNIFICATION in CTX:\n"^
394 NCicPp.ppcontext ~metasenv ~subst context
396 NCicPp.ppterm ~metasenv ~subst ~context t ^ " === " ^
397 NCicPp.ppterm ~metasenv ~subst ~context flex_prod ^ "\n"));
398 let metasenv, subst =
399 try NCicUnification.unify metasenv subst context t flex_prod
400 with exc -> raise (wrap_exc (lazy (localise orig_he, Printf.sprintf
401 ("The term %s has an inferred type %s, but is applied to the" ^^
402 " argument %s of type %s")
403 (NCicPp.ppterm ~metasenv ~subst ~context he)
404 (NCicPp.ppterm ~metasenv ~subst ~context t)
405 (NCicPp.ppterm ~metasenv ~subst ~context arg)
406 (NCicPp.ppterm ~metasenv ~subst ~context ty_arg))) exc)
407 (* XXX coerce to funclass *)
409 let meta = NCicSubstitution.subst ~avoid_beta_redexes:true arg meta in
410 aux metasenv subst (arg :: args_so_far) he meta tl
411 | C.Match (_,_,C.Meta _,_)
412 | C.Match (_,_,C.Appl (C.Meta _ :: _),_)
413 | C.Appl (C.Const (NReference.Ref (_, NReference.Fix _)) :: _) ->
414 raise (Uncertain (lazy (localise orig_he, Printf.sprintf
415 ("The term %s is here applied to %d arguments but expects " ^^
416 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
417 (List.length args) (List.length args_so_far))))
419 let metasenv, subst, newhead, newheadty =
420 try_coercions ~localise ~look_for_coercion metasenv subst context
421 (NCicUntrusted.mk_appl he (List.rev args_so_far)) orig_he ty
422 (NCic.Prod ("_",NCic.Implicit `Term,NCic.Implicit `Term))
424 (RefineFailure (lazy (localise orig_he, Printf.sprintf
425 ("The term %s is here applied to %d arguments but expects " ^^
426 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
427 (List.length args) (List.length args_so_far))))
429 aux metasenv subst [] newhead newheadty (arg :: tl)
431 aux metasenv subst [] he ty_he args
432 (*D*)in outside(); rc with exc -> outside (); raise exc
434 (* vim:set foldmethod=marker: *)