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);;
26 prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
31 let wrap_exc msg = function
32 | NCicUnification.Uncertain _ -> Uncertain msg
33 | NCicUnification.UnificationFailure _ -> RefineFailure msg
34 | NCicTypeChecker.TypeCheckerFailure _ -> RefineFailure msg
38 let exp_implicit metasenv context expty =
39 let foo x = match expty with Some t -> `WithType t | None -> x in
41 | `Closed -> NCicMetaSubst.mk_meta metasenv [] (foo `Type)
42 | `Type -> NCicMetaSubst.mk_meta metasenv context (foo `Type)
43 | `Term -> NCicMetaSubst.mk_meta metasenv context (foo `Term)
47 let force_to_sort metasenv subst context t =
48 match NCicReduction.whd ~subst context t with
49 | C.Meta (_,(_,(C.Irl 0 | C.Ctx []))) as t ->
51 | C.Meta (i,(_,lc)) as t ->
52 let len = match lc with C.Irl len->len | C.Ctx l->List.length l in
53 let metasenv, subst, _ =
55 NCicMetaSubst.restrict metasenv subst i (HExtlib.list_seq 1 (len+1))
56 else metasenv, subst, 0
59 | C.Sort _ -> metasenv, subst, t
64 localise metasenv subst context orig_s orig_t (name,s) t (t1, t2)
66 let metasenv, subst, t1 = force_to_sort metasenv subst context t1 in
67 let metasenv, subst, t2 =
68 force_to_sort metasenv subst ((name,C.Decl s)::context) t2 in
70 | C.Sort _, C.Sort C.Prop -> metasenv, subst, t2
71 | C.Sort (C.Type u1), C.Sort (C.Type u2) ->
72 metasenv, subst, C.Sort (C.Type (u1@u2))
73 | C.Sort C.Prop,C.Sort (C.Type _) -> metasenv, subst, t2
76 | C.Sort _, C.Meta _ -> metasenv, subst, t2
77 | x, (C.Sort _ | C.Meta _) | _, x ->
78 let y, context, orig =
79 if x == t1 then s, context, orig_s
80 else t, ((name,C.Decl s)::context), orig_t
82 raise (RefineFailure (lazy (localise orig,Printf.sprintf
83 "%s is expected to be a type, but its type is %s that is not a sort"
84 (NCicPp.ppterm ~subst ~metasenv ~context y)
85 (NCicPp.ppterm ~subst ~metasenv ~context x))))
88 let check_allowed_sort_elimination localise r orig =
91 | C.Appl l -> C.Appl (l @ [arg])
92 | t -> C.Appl [t;arg] in
93 (* ctx, ind_type @ lefts, sort_of_ind_ty@lefts, outsort *)
94 let rec aux metasenv subst context ind arity1 arity2 =
95 (*D*)inside 'S'; try let rc =
96 let arity1 = NCicReduction.whd ~subst context arity1 in
97 pp (lazy(NCicPp.ppterm ~subst ~metasenv ~context arity1 ^ " ... " ^
98 NCicPp.ppterm ~subst ~metasenv ~context arity2 ^ "\nNEV:"^
99 NCicPp.ppmetasenv ~subst metasenv
102 | C.Prod (name,so1,de1) (* , t ==?== C.Prod _ *) ->
103 let metasenv, meta, _ =
104 NCicMetaSubst.mk_meta metasenv ((name,C.Decl so1)::context) `Type
106 let metasenv, subst =
107 try NCicUnification.unify metasenv subst context
108 arity2 (C.Prod (name, so1, meta))
109 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
110 "expected %s, found %s" (* XXX localizzare meglio *)
111 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod (name, so1, meta)))
112 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
114 aux metasenv subst ((name, C.Decl so1)::context)
115 (mkapp (NCicSubstitution.lift 1 ind) (C.Rel 1)) de1 meta
116 | C.Sort _ (* , t ==?== C.Prod _ *) ->
117 let metasenv, meta, _ = NCicMetaSubst.mk_meta metasenv [] `Type in
118 let metasenv, subst =
119 try NCicUnification.unify metasenv subst context
120 arity2 (C.Prod ("_", ind, meta))
121 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
122 "expected %s, found %s" (* XXX localizzare meglio *)
123 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod ("_", ind, meta)))
124 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
126 (try NCicTypeChecker.check_allowed_sort_elimination
127 ~metasenv ~subst r context ind arity1 arity2;
129 with exc -> raise (wrap_exc (lazy (localise orig,
130 "Sort elimination not allowed ")) exc))
132 (*D*)in outside(); rc with exc -> outside (); raise exc
138 ?(localise=fun _ -> Stdpp.dummy_loc) metasenv subst context term expty
140 let force_ty metasenv subst context orig t infty expty =
141 (*D*)inside 'F'; try let rc =
146 | C.Lambda _ -> metasenv, subst, t, expty
149 (NCicPp.ppterm ~metasenv ~subst ~context infty) ^ " === " ^
150 (NCicPp.ppterm ~metasenv ~subst ~context expty)));
151 let metasenv, subst =
152 try NCicUnification.unify metasenv subst context infty expty
153 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
154 "The term %s has type %s but is here used with type %s"
155 (NCicPp.ppterm ~metasenv ~subst ~context t)
156 (NCicPp.ppterm ~metasenv ~subst ~context infty)
157 (NCicPp.ppterm ~metasenv ~subst ~context expty))) exc)
159 metasenv, subst, t, expty)
160 | None -> metasenv, subst, t, infty
161 (*D*)in outside(); rc with exc -> outside (); raise exc
163 let rec typeof_aux metasenv subst context expty =
165 (*D*)inside 'R'; try let rc =
166 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t));
167 let metasenv, subst, t, infty =
172 match List.nth context (n - 1) with
173 | (_,C.Decl ty) -> NCicSubstitution.lift n ty
174 | (_,C.Def (_,ty)) -> NCicSubstitution.lift n ty
176 raise (RefineFailure (lazy (localise t,"unbound variable"))))
178 metasenv, subst, t, infty
179 | C.Sort (C.Type [false,u]) -> metasenv,subst,t,(C.Sort (C.Type [true, u]))
180 | C.Sort (C.Type _) ->
181 raise (AssertFailure (lazy ("Cannot type an inferred type: "^
182 NCicPp.ppterm ~subst ~metasenv ~context t)))
183 | C.Sort _ -> metasenv,subst,t,(C.Sort (C.Type NCicEnvironment.type0))
184 | C.Implicit infos ->
185 let metasenv,t,ty = exp_implicit metasenv context expty infos in
186 metasenv, subst, t, ty
187 | C.Meta (n,l) as t ->
190 let _,_,_,ty = NCicUtils.lookup_subst n subst in ty
191 with NCicUtils.Subst_not_found _ -> try
192 let _,_,ty = NCicUtils.lookup_meta n metasenv in
193 match ty with C.Implicit _ -> assert false | _ -> ty
194 with NCicUtils.Meta_not_found _ ->
195 raise (AssertFailure (lazy (Printf.sprintf
196 "%s not found" (NCicPp.ppterm ~subst ~metasenv ~context t))))
198 metasenv, subst, t, NCicSubstitution.subst_meta l ty
200 metasenv, subst, t, NCicTypeChecker.typeof ~subst ~metasenv context t
201 | C.Prod (name,(s as orig_s),(t as orig_t)) ->
202 let metasenv, subst, s, s1 = typeof_aux metasenv subst context None s in
203 let context1 = (name,(C.Decl s))::context in
204 let metasenv, subst, t, s2 = typeof_aux metasenv subst context1 None t in
205 let metasenv, subst, ty =
206 sort_of_prod localise metasenv subst
207 context orig_s orig_t (name,s) t (s1,s2)
209 metasenv, subst, NCic.Prod(name,s,t), ty
210 | C.Lambda (n,(s as orig_s),t) ->
211 let exp_s, exp_ty_t =
215 match NCicReduction.whd ~subst context expty with
216 | C.Prod (_,s,t) -> Some s, Some t
219 let metasenv, subst, s, ty_s =
220 typeof_aux metasenv subst context None s in
221 let metasenv, subst, _ = force_to_sort metasenv subst context ty_s in
222 let (metasenv,subst), exp_ty_t =
225 (try NCicUnification.unify metasenv subst context s exp_s,exp_ty_t
226 with exc -> raise (wrap_exc (lazy (localise orig_s, Printf.sprintf
227 "Source type %s was expected to be %s" (NCicPp.ppterm ~metasenv
228 ~subst ~context s) (NCicPp.ppterm ~metasenv ~subst ~context
230 | None -> (metasenv, subst), None
232 (* XXX coerce_to_sort s *)
233 let context = (n,C.Decl s) :: context in
234 let metasenv, subst, t, ty_t =
235 typeof_aux metasenv subst context exp_ty_t t
237 metasenv, subst, C.Lambda(n,s,t), C.Prod (n,s,ty_t)
238 | C.LetIn (n,ty,t,bo) ->
239 let metasenv, subst, ty, ty_ty =
240 typeof_aux metasenv subst context None ty in
241 let metasenv, subst, _ = force_to_sort metasenv subst context ty_ty in
242 let metasenv, subst, t, _ =
243 typeof_aux metasenv subst context (Some ty) t in
244 let context1 = (n, C.Def (t,ty)) :: context in
245 let metasenv, subst, bo, bo_ty =
246 typeof_aux metasenv subst context1 None bo
248 let bo_ty = NCicSubstitution.subst ~avoid_beta_redexes:true t bo_ty in
249 metasenv, subst, C.LetIn (n, ty, t, bo), bo_ty
250 | C.Appl ((he as orig_he)::(_::_ as args)) ->
251 let metasenv, subst, he, ty_he =
252 typeof_aux metasenv subst context None he in
253 eat_prods localise metasenv subst context orig_he he ty_he args
254 | C.Appl _ -> raise (AssertFailure (lazy "Appl of length < 2"))
255 | C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as r,
256 outtype,(term as orig_term),pl) as orig ->
257 let _, leftno, itl, _, _ = NCicEnvironment.get_checked_indtys r in
258 let _, _, arity, cl = List.nth itl tyno in
259 let constructorsno = List.length cl in
260 let _, metasenv, args =
261 NCicMetaSubst.saturate metasenv context arity 0 in
262 let ind = if args = [] then C.Const r else C.Appl (C.Const r::args) in
263 let metasenv, subst, term, _ =
264 typeof_aux metasenv subst context (Some ind) term in
265 let metasenv, subst, outtype, outsort =
266 typeof_aux metasenv subst context None outtype in
267 let parameters, arguments = HExtlib.split_nth leftno args in
268 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
270 if parameters = [] then C.Const r
271 else C.Appl ((C.Const r)::parameters) in
272 let metasenv, subst, ind, ind_ty =
273 typeof_aux metasenv subst context None ind in
274 let metasenv, subst =
275 check_allowed_sort_elimination localise r orig_term metasenv subst
276 context ind ind_ty outsort
278 (* let's check if the type of branches are right *)
279 if List.length pl <> constructorsno then
280 raise (RefineFailure (lazy (localise orig,
281 "Wrong number of cases in a match")));
282 let _, metasenv, subst, pl_rev =
284 (fun (j, metasenv, subst, branches) p ->
286 let cons = Ref.mk_constructor j r in
287 if parameters = [] then C.Const cons
288 else C.Appl (C.Const cons::parameters)
290 let metasenv, subst, cons, ty_cons =
291 typeof_aux metasenv subst context None cons in
293 NCicTypeChecker.type_of_branch
294 ~subst context leftno outtype cons ty_cons in
295 let metasenv, subst, p, _ =
296 typeof_aux metasenv subst context (Some ty_branch) p in
297 j+1, metasenv, subst, p :: branches)
298 (1, metasenv, subst, []) pl
301 C.Match (r, outtype, term, List.rev pl_rev),
302 NCicReduction.head_beta_reduce (C.Appl (outtype::arguments@[term]))
303 | C.Match _ -> assert false
305 force_ty metasenv subst context orig t infty expty
306 (*D*)in outside(); rc with exc -> outside (); raise exc
308 typeof_aux metasenv subst context expty term
310 and eat_prods localise metasenv subst context orig_he he ty_he args =
311 (*D*)inside 'E'; try let rc =
312 let rec aux metasenv subst args_so_far ty_he = function
313 | [] -> metasenv, subst, NCic.Appl (he :: List.rev args_so_far), ty_he
315 match NCicReduction.whd ~subst context ty_he with
317 let metasenv, subst, arg, _ =
318 typeof ~localise metasenv subst context arg (Some s) in
319 let t = NCicSubstitution.subst ~avoid_beta_redexes:true arg t in
320 aux metasenv subst (arg :: args_so_far) t tl
322 let metasenv, subst, arg, ty_arg =
323 typeof ~localise metasenv subst context arg None in
324 let metasenv, meta, _ =
325 NCicMetaSubst.mk_meta metasenv
326 (("_",C.Decl ty_arg) :: context) `Type
328 let flex_prod = C.Prod ("_", ty_arg, meta) in
329 pp (lazy ( "UNIFICATION in CTX:\n"^
330 NCicPp.ppcontext ~metasenv ~subst context ^ "\nMENV:\n" ^
331 NCicPp.ppmetasenv ~subst metasenv ^ "\nOF: " ^
332 NCicPp.ppterm ~metasenv ~subst ~context t ^ " === " ^
333 NCicPp.ppterm ~metasenv ~subst ~context flex_prod ^ "\n"));
334 let metasenv, subst =
335 try NCicUnification.unify metasenv subst context t flex_prod
336 with exc -> raise (wrap_exc (lazy (localise orig_he, Printf.sprintf
337 ("The term %s is here applied to %d arguments but expects " ^^
338 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
339 (List.length args) (List.length args_so_far))) exc)
340 (* XXX coerce to funclass *)
342 let meta = NCicSubstitution.subst ~avoid_beta_redexes:true arg meta in
343 aux metasenv subst (arg :: args_so_far) meta tl
345 aux metasenv subst [] ty_he args
346 (*D*)in outside(); rc with exc -> outside (); raise exc
348 (* vim:set foldmethod=marker: *)