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)
49 let force_to_sort metasenv subst context orig localise t =
50 match NCicReduction.whd ~subst context t with
51 | C.Meta (_,(0,(C.Irl 0 | C.Ctx []))) as t ->
53 | C.Meta (i,(_,(C.Irl 0 | C.Ctx []))) ->
54 metasenv, subst, C.Meta(i,(0,C.Irl 0))
55 | C.Meta (i,(_,lc)) ->
56 let len = match lc with C.Irl len->len | C.Ctx l->List.length l in
57 let metasenv, subst, newmeta =
59 NCicMetaSubst.restrict metasenv subst i (HExtlib.list_seq 1 (len+1))
60 else metasenv, subst, i
62 metasenv, subst, C.Meta (newmeta,(0,C.Irl 0))
63 | C.Sort _ as t -> metasenv, subst, t
65 raise (RefineFailure (lazy
68 NCicPp.ppterm ~metasenv ~subst ~context t)))
72 localise metasenv subst context orig_s orig_t (name,s) t (t1, t2)
74 let metasenv, subst, t1 =
75 force_to_sort metasenv subst context orig_s localise t1 in
76 let metasenv, subst, t2 =
77 force_to_sort metasenv subst ((name,C.Decl s)::context)
80 | C.Sort _, C.Sort C.Prop -> metasenv, subst, t2
81 | C.Sort (C.Type u1), C.Sort (C.Type u2) ->
82 metasenv, subst, C.Sort (C.Type (u1@u2))
83 | C.Sort C.Prop,C.Sort (C.Type _) -> metasenv, subst, t2
85 | C.Meta _, C.Meta (_,(_,_))
86 | C.Sort _, C.Meta (_,(_,_)) -> metasenv, subst, t2
87 | x, (C.Sort _ | C.Meta _) | _, x ->
88 let y, context, orig =
89 if x == t1 then s, context, orig_s
90 else t, ((name,C.Decl s)::context), orig_t
92 raise (RefineFailure (lazy (localise orig,Printf.sprintf
93 "%s is expected to be a type, but its type is %s that is not a sort"
94 (NCicPp.ppterm ~subst ~metasenv ~context y)
95 (NCicPp.ppterm ~subst ~metasenv ~context x))))
98 let check_allowed_sort_elimination localise r orig =
101 | C.Appl l -> C.Appl (l @ [arg])
102 | t -> C.Appl [t;arg] in
103 (* ctx, ind_type @ lefts, sort_of_ind_ty@lefts, outsort *)
104 let rec aux metasenv subst context ind arity1 arity2 =
105 (*D*)inside 'S'; try let rc =
106 let arity1 = NCicReduction.whd ~subst context arity1 in
107 pp (lazy(NCicPp.ppterm ~subst ~metasenv ~context arity1 ^ " elimsto " ^
108 NCicPp.ppterm ~subst ~metasenv ~context arity2 ^ "\nMENV:\n"^
109 NCicPp.ppmetasenv ~subst metasenv));
111 | C.Prod (name,so1,de1) (* , t ==?== C.Prod _ *) ->
112 let metasenv, meta, _ =
113 NCicMetaSubst.mk_meta metasenv ((name,C.Decl so1)::context) `Type
115 let metasenv, subst =
116 try NCicUnification.unify metasenv subst context
117 arity2 (C.Prod (name, so1, meta))
118 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
119 "expected %s, found %s" (* XXX localizzare meglio *)
120 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod (name, so1, meta)))
121 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
123 aux metasenv subst ((name, C.Decl so1)::context)
124 (mkapp (NCicSubstitution.lift 1 ind) (C.Rel 1)) de1 meta
125 | C.Sort _ (* , t ==?== C.Prod _ *) ->
126 let metasenv, meta, _ = NCicMetaSubst.mk_meta metasenv [] `Type in
127 let metasenv, subst =
128 try NCicUnification.unify metasenv subst context
129 arity2 (C.Prod ("_", ind, meta))
130 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
131 "expected %s, found %s" (* XXX localizzare meglio *)
132 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod ("_", ind, meta)))
133 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
135 (try NCicTypeChecker.check_allowed_sort_elimination
136 ~metasenv ~subst r context ind arity1 arity2;
138 with exc -> raise (wrap_exc (lazy (localise orig,
139 "Sort elimination not allowed ")) exc))
141 (*D*)in outside(); rc with exc -> outside (); raise exc
147 ?(localise=fun _ -> Stdpp.dummy_loc) metasenv subst context term expty
149 let force_ty metasenv subst context orig t infty expty =
150 (*D*)inside 'F'; try let rc =
155 | C.Lambda _ -> metasenv, subst, t, expty
158 (NCicPp.ppterm ~metasenv ~subst ~context infty) ^ " === " ^
159 (NCicPp.ppterm ~metasenv ~subst ~context expty)));
160 let metasenv, subst =
161 try NCicUnification.unify metasenv subst context infty expty
162 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
163 "The term %s has type %s but is here used with type %s"
164 (NCicPp.ppterm ~metasenv ~subst ~context t)
165 (NCicPp.ppterm ~metasenv ~subst ~context infty)
166 (NCicPp.ppterm ~metasenv ~subst ~context expty))) exc)
168 metasenv, subst, t, expty)
169 | None -> metasenv, subst, t, infty
170 (*D*)in outside(); rc with exc -> outside (); raise exc
172 let rec typeof_aux metasenv subst context expty =
174 (*D*)inside 'R'; try let rc =
175 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t));
176 pp (lazy (NCicPp.ppmetasenv ~subst metasenv));
177 let metasenv, subst, t, infty =
182 match List.nth context (n - 1) with
183 | (_,C.Decl ty) -> NCicSubstitution.lift n ty
184 | (_,C.Def (_,ty)) -> NCicSubstitution.lift n ty
186 raise (RefineFailure (lazy (localise t,"unbound variable"))))
188 metasenv, subst, t, infty
189 | C.Sort (C.Type [false,u]) -> metasenv,subst,t,(C.Sort (C.Type [true, u]))
190 | C.Sort (C.Type _) ->
191 raise (AssertFailure (lazy ("Cannot type an inferred type: "^
192 NCicPp.ppterm ~subst ~metasenv ~context t)))
193 | C.Sort _ -> metasenv,subst,t,(C.Sort (C.Type NCicEnvironment.type0))
194 | C.Implicit infos ->
195 let metasenv,t,ty = exp_implicit metasenv context expty infos in
196 metasenv, subst, t, ty
197 | C.Meta (n,l) as t ->
200 let _,_,_,ty = NCicUtils.lookup_subst n subst in ty
201 with NCicUtils.Subst_not_found _ -> try
202 let _,_,ty = NCicUtils.lookup_meta n metasenv in
203 match ty with C.Implicit _ -> assert false | _ -> ty
204 with NCicUtils.Meta_not_found _ ->
205 raise (AssertFailure (lazy (Printf.sprintf
206 "%s not found" (NCicPp.ppterm ~subst ~metasenv ~context t))))
208 metasenv, subst, t, NCicSubstitution.subst_meta l ty
210 metasenv, subst, t, NCicTypeChecker.typeof ~subst ~metasenv context t
211 | C.Prod (name,(s as orig_s),(t as orig_t)) ->
212 let metasenv, subst, s, s1 = typeof_aux metasenv subst context None s in
213 let context1 = (name,(C.Decl s))::context in
214 let metasenv, subst, t, s2 = typeof_aux metasenv subst context1 None t in
215 let metasenv, subst, ty =
216 sort_of_prod localise metasenv subst
217 context orig_s orig_t (name,s) t (s1,s2)
219 metasenv, subst, NCic.Prod(name,s,t), ty
220 | C.Lambda (n,(s as orig_s),t) ->
221 let exp_s, exp_ty_t =
225 match NCicReduction.whd ~subst context expty with
226 | C.Prod (_,s,t) -> Some s, Some t
229 let metasenv, subst, s, ty_s =
230 typeof_aux metasenv subst context None s in
231 let metasenv, subst, _ =
232 force_to_sort metasenv subst context orig_s localise ty_s in
233 let (metasenv,subst), exp_ty_t =
236 (try NCicUnification.unify metasenv subst context s exp_s,exp_ty_t
237 with exc -> raise (wrap_exc (lazy (localise orig_s, Printf.sprintf
238 "Source type %s was expected to be %s" (NCicPp.ppterm ~metasenv
239 ~subst ~context s) (NCicPp.ppterm ~metasenv ~subst ~context
241 | None -> (metasenv, subst), None
243 (* XXX coerce_to_sort s *)
244 let context = (n,C.Decl s) :: context in
245 let metasenv, subst, t, ty_t =
246 typeof_aux metasenv subst context exp_ty_t t
248 metasenv, subst, C.Lambda(n,s,t), C.Prod (n,s,ty_t)
249 | C.LetIn (n,(ty as orig_ty),t,bo) ->
250 let metasenv, subst, ty, ty_ty =
251 typeof_aux metasenv subst context None ty in
252 let metasenv, subst, _ =
253 force_to_sort metasenv subst context orig_ty localise ty_ty in
254 let metasenv, subst, t, _ =
255 typeof_aux metasenv subst context (Some ty) t in
256 let context1 = (n, C.Def (t,ty)) :: context in
257 let metasenv, subst, bo, bo_ty =
258 typeof_aux metasenv subst context1 None bo
260 let bo_ty = NCicSubstitution.subst ~avoid_beta_redexes:true t bo_ty in
261 metasenv, subst, C.LetIn (n, ty, t, bo), bo_ty
262 | C.Appl ((he as orig_he)::(_::_ as args)) ->
263 let metasenv, subst, he, ty_he =
264 typeof_aux metasenv subst context None he in
265 eat_prods localise metasenv subst context orig_he he ty_he args
266 | C.Appl _ -> raise (AssertFailure (lazy "Appl of length < 2"))
267 | C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as r,
268 outtype,(term as orig_term),pl) as orig ->
269 let _, leftno, itl, _, _ = NCicEnvironment.get_checked_indtys r in
270 let _, _, arity, cl = List.nth itl tyno in
271 let constructorsno = List.length cl in
272 let _, metasenv, args =
273 NCicMetaSubst.saturate metasenv context arity 0 in
274 let ind = if args = [] then C.Const r else C.Appl (C.Const r::args) in
275 let metasenv, subst, term, _ =
276 typeof_aux metasenv subst context (Some ind) term in
277 let metasenv, subst, outtype, outsort =
278 typeof_aux metasenv subst context None outtype in
279 let parameters, arguments = HExtlib.split_nth leftno args in
280 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
282 if parameters = [] then C.Const r
283 else C.Appl ((C.Const r)::parameters) in
284 let metasenv, subst, ind, ind_ty =
285 typeof_aux metasenv subst context None ind in
286 let metasenv, subst =
287 check_allowed_sort_elimination localise r orig_term metasenv subst
288 context ind ind_ty outsort
290 (* let's check if the type of branches are right *)
291 if List.length pl <> constructorsno then
292 raise (RefineFailure (lazy (localise orig,
293 "Wrong number of cases in a match")));
294 let _, metasenv, subst, pl_rev =
296 (fun (j, metasenv, subst, branches) p ->
298 let cons = Ref.mk_constructor j r in
299 if parameters = [] then C.Const cons
300 else C.Appl (C.Const cons::parameters)
302 let metasenv, subst, cons, ty_cons =
303 typeof_aux metasenv subst context None cons in
305 NCicTypeChecker.type_of_branch
306 ~subst context leftno outtype cons ty_cons in
307 pp (lazy ("TYPEOFBRANCH: " ^
308 NCicPp.ppterm ~metasenv ~subst ~context p ^ " ::: " ^
309 NCicPp.ppterm ~metasenv ~subst ~context ty_branch ));
310 let metasenv, subst, p, _ =
311 typeof_aux metasenv subst context (Some ty_branch) p in
312 j+1, metasenv, subst, p :: branches)
313 (1, metasenv, subst, []) pl
316 C.Match (r, outtype, term, List.rev pl_rev),
317 NCicReduction.head_beta_reduce (C.Appl (outtype::arguments@[term]))
318 | C.Match _ as orig ->
319 prerr_endline (NCicPp.ppterm ~metasenv ~subst ~context orig);
322 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t ^ " :: "^
323 NCicPp.ppterm ~metasenv ~subst ~context infty ));
324 force_ty metasenv subst context orig t infty expty
325 (*D*)in outside(); rc with exc -> outside (); raise exc
327 typeof_aux metasenv subst context expty term
329 and eat_prods localise metasenv subst context orig_he he ty_he args =
330 (*D*)inside 'E'; try let rc =
331 let rec aux metasenv subst args_so_far ty_he = function
332 | [] -> metasenv, subst, NCic.Appl (he :: List.rev args_so_far), ty_he
334 match NCicReduction.whd ~subst context ty_he with
336 let metasenv, subst, arg, _ =
337 typeof ~localise metasenv subst context arg (Some s) in
338 let t = NCicSubstitution.subst ~avoid_beta_redexes:true arg t in
339 aux metasenv subst (arg :: args_so_far) t tl
341 | C.Appl (C.Meta _ :: _) as t ->
342 let metasenv, subst, arg, ty_arg =
343 typeof ~localise metasenv subst context arg None in
344 let metasenv, meta, _ =
345 NCicMetaSubst.mk_meta metasenv
346 (("_",C.Decl ty_arg) :: context) `Type
348 let flex_prod = C.Prod ("_", ty_arg, meta) in
349 pp (lazy ( "UNIFICATION in CTX:\n"^
350 NCicPp.ppcontext ~metasenv ~subst context
352 NCicPp.ppterm ~metasenv ~subst ~context t ^ " === " ^
353 NCicPp.ppterm ~metasenv ~subst ~context flex_prod ^ "\n"));
354 let metasenv, subst =
355 try NCicUnification.unify metasenv subst context t flex_prod
356 with exc -> raise (wrap_exc (lazy (localise orig_he, Printf.sprintf
357 ("The term %s has an inferred type %s, but is applied to the" ^^
358 " argument %s of type %s")
359 (NCicPp.ppterm ~metasenv ~subst ~context he)
360 (NCicPp.ppterm ~metasenv ~subst ~context t)
361 (NCicPp.ppterm ~metasenv ~subst ~context arg)
362 (NCicPp.ppterm ~metasenv ~subst ~context ty_arg))) exc)
363 (* XXX coerce to funclass *)
365 let meta = NCicSubstitution.subst ~avoid_beta_redexes:true arg meta in
366 aux metasenv subst (arg :: args_so_far) meta tl
367 | C.Match (_,_,C.Meta _,_)
368 | C.Match (_,_,C.Appl (C.Meta _ :: _),_)
369 | C.Appl (C.Const (NReference.Ref (_, NReference.Fix _)) :: _) ->
370 raise (Uncertain (lazy (localise orig_he, Printf.sprintf
371 ("The term %s is here applied to %d arguments but expects " ^^
372 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
373 (List.length args) (List.length args_so_far))))
375 raise (RefineFailure (lazy (localise orig_he, Printf.sprintf
376 ("The term %s is here applied to %d arguments but expects " ^^
377 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
378 (List.length args) (List.length args_so_far))))
380 aux metasenv subst [] ty_he args
381 (*D*)in outside(); rc with exc -> outside (); raise exc
383 (* vim:set foldmethod=marker: *)