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 `Type)
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 t =
50 match NCicReduction.whd ~subst context t with
51 | C.Meta (_,(_,(C.Irl 0 | C.Ctx []))) as t ->
53 | C.Meta (i,(_,lc)) as t ->
54 let len = match lc with C.Irl len->len | C.Ctx l->List.length l in
55 let metasenv, subst, _ =
57 NCicMetaSubst.restrict metasenv subst i (HExtlib.list_seq 1 (len+1))
58 else metasenv, subst, 0
61 | C.Sort _ -> metasenv, subst, t
66 localise metasenv subst context orig_s orig_t (name,s) t (t1, t2)
68 let metasenv, subst, t1 = force_to_sort metasenv subst context t1 in
69 let metasenv, subst, t2 =
70 force_to_sort metasenv subst ((name,C.Decl s)::context) t2 in
72 | C.Sort _, C.Sort C.Prop -> metasenv, subst, t2
73 | C.Sort (C.Type u1), C.Sort (C.Type u2) ->
74 metasenv, subst, C.Sort (C.Type (u1@u2))
75 | C.Sort C.Prop,C.Sort (C.Type _) -> metasenv, subst, t2
78 | C.Sort _, C.Meta _ -> metasenv, subst, t2
79 | x, (C.Sort _ | C.Meta _) | _, x ->
80 let y, context, orig =
81 if x == t1 then s, context, orig_s
82 else t, ((name,C.Decl s)::context), orig_t
84 raise (RefineFailure (lazy (localise orig,Printf.sprintf
85 "%s is expected to be a type, but its type is %s that is not a sort"
86 (NCicPp.ppterm ~subst ~metasenv ~context y)
87 (NCicPp.ppterm ~subst ~metasenv ~context x))))
90 let check_allowed_sort_elimination localise r orig =
93 | C.Appl l -> C.Appl (l @ [arg])
94 | t -> C.Appl [t;arg] in
95 (* ctx, ind_type @ lefts, sort_of_ind_ty@lefts, outsort *)
96 let rec aux metasenv subst context ind arity1 arity2 =
97 (*D*)inside 'S'; try let rc =
98 let arity1 = NCicReduction.whd ~subst context arity1 in
99 pp (lazy(NCicPp.ppterm ~subst ~metasenv ~context arity1 ^ " ... " ^
100 NCicPp.ppterm ~subst ~metasenv ~context arity2 ^ "\nNEV:"^
101 NCicPp.ppmetasenv ~subst metasenv
104 | C.Prod (name,so1,de1) (* , t ==?== C.Prod _ *) ->
105 let metasenv, meta, _ =
106 NCicMetaSubst.mk_meta metasenv ((name,C.Decl so1)::context) `Type
108 let metasenv, subst =
109 try NCicUnification.unify metasenv subst context
110 arity2 (C.Prod (name, so1, meta))
111 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
112 "expected %s, found %s" (* XXX localizzare meglio *)
113 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod (name, so1, meta)))
114 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
116 aux metasenv subst ((name, C.Decl so1)::context)
117 (mkapp (NCicSubstitution.lift 1 ind) (C.Rel 1)) de1 meta
118 | C.Sort _ (* , t ==?== C.Prod _ *) ->
119 let metasenv, meta, _ = NCicMetaSubst.mk_meta metasenv [] `Type in
120 let metasenv, subst =
121 try NCicUnification.unify metasenv subst context
122 arity2 (C.Prod ("_", ind, meta))
123 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
124 "expected %s, found %s" (* XXX localizzare meglio *)
125 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod ("_", ind, meta)))
126 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
128 (try NCicTypeChecker.check_allowed_sort_elimination
129 ~metasenv ~subst r context ind arity1 arity2;
131 with exc -> raise (wrap_exc (lazy (localise orig,
132 "Sort elimination not allowed ")) exc))
134 (*D*)in outside(); rc with exc -> outside (); raise exc
140 ?(localise=fun _ -> Stdpp.dummy_loc) metasenv subst context term expty
142 let force_ty metasenv subst context orig t infty expty =
143 (*D*)inside 'F'; try let rc =
148 | C.Lambda _ -> metasenv, subst, t, expty
151 (NCicPp.ppterm ~metasenv ~subst ~context infty) ^ " === " ^
152 (NCicPp.ppterm ~metasenv ~subst ~context expty)));
153 let metasenv, subst =
154 try NCicUnification.unify metasenv subst context infty expty
155 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
156 "The term %s has type %s but is here used with type %s"
157 (NCicPp.ppterm ~metasenv ~subst ~context t)
158 (NCicPp.ppterm ~metasenv ~subst ~context infty)
159 (NCicPp.ppterm ~metasenv ~subst ~context expty))) exc)
161 metasenv, subst, t, expty)
162 | None -> metasenv, subst, t, infty
163 (*D*)in outside(); rc with exc -> outside (); raise exc
165 let rec typeof_aux metasenv subst context expty =
167 (*D*)inside 'R'; try let rc =
168 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t));
169 let metasenv, subst, t, infty =
174 match List.nth context (n - 1) with
175 | (_,C.Decl ty) -> NCicSubstitution.lift n ty
176 | (_,C.Def (_,ty)) -> NCicSubstitution.lift n ty
178 raise (RefineFailure (lazy (localise t,"unbound variable"))))
180 metasenv, subst, t, infty
181 | C.Sort (C.Type [false,u]) -> metasenv,subst,t,(C.Sort (C.Type [true, u]))
182 | C.Sort (C.Type _) ->
183 raise (AssertFailure (lazy ("Cannot type an inferred type: "^
184 NCicPp.ppterm ~subst ~metasenv ~context t)))
185 | C.Sort _ -> metasenv,subst,t,(C.Sort (C.Type NCicEnvironment.type0))
186 | C.Implicit infos ->
187 let metasenv,t,ty = exp_implicit metasenv context expty infos in
188 metasenv, subst, t, ty
189 | C.Meta (n,l) as t ->
192 let _,_,_,ty = NCicUtils.lookup_subst n subst in ty
193 with NCicUtils.Subst_not_found _ -> try
194 let _,_,ty = NCicUtils.lookup_meta n metasenv in
195 match ty with C.Implicit _ -> assert false | _ -> ty
196 with NCicUtils.Meta_not_found _ ->
197 raise (AssertFailure (lazy (Printf.sprintf
198 "%s not found" (NCicPp.ppterm ~subst ~metasenv ~context t))))
200 metasenv, subst, t, NCicSubstitution.subst_meta l ty
202 metasenv, subst, t, NCicTypeChecker.typeof ~subst ~metasenv context t
203 | C.Prod (name,(s as orig_s),(t as orig_t)) ->
204 let metasenv, subst, s, s1 = typeof_aux metasenv subst context None s in
205 let context1 = (name,(C.Decl s))::context in
206 let metasenv, subst, t, s2 = typeof_aux metasenv subst context1 None t in
207 let metasenv, subst, ty =
208 sort_of_prod localise metasenv subst
209 context orig_s orig_t (name,s) t (s1,s2)
211 metasenv, subst, NCic.Prod(name,s,t), ty
212 | C.Lambda (n,(s as orig_s),t) ->
213 let exp_s, exp_ty_t =
217 match NCicReduction.whd ~subst context expty with
218 | C.Prod (_,s,t) -> Some s, Some t
221 let metasenv, subst, s, ty_s =
222 typeof_aux metasenv subst context None s in
223 let metasenv, subst, _ = force_to_sort metasenv subst context ty_s in
224 let (metasenv,subst), exp_ty_t =
227 (try NCicUnification.unify metasenv subst context s exp_s,exp_ty_t
228 with exc -> raise (wrap_exc (lazy (localise orig_s, Printf.sprintf
229 "Source type %s was expected to be %s" (NCicPp.ppterm ~metasenv
230 ~subst ~context s) (NCicPp.ppterm ~metasenv ~subst ~context
232 | None -> (metasenv, subst), None
234 (* XXX coerce_to_sort s *)
235 let context = (n,C.Decl s) :: context in
236 let metasenv, subst, t, ty_t =
237 typeof_aux metasenv subst context exp_ty_t t
239 metasenv, subst, C.Lambda(n,s,t), C.Prod (n,s,ty_t)
240 | C.LetIn (n,ty,t,bo) ->
241 let metasenv, subst, ty, ty_ty =
242 typeof_aux metasenv subst context None ty in
243 let metasenv, subst, _ = force_to_sort metasenv subst context ty_ty in
244 let metasenv, subst, t, _ =
245 typeof_aux metasenv subst context (Some ty) t in
246 let context1 = (n, C.Def (t,ty)) :: context in
247 let metasenv, subst, bo, bo_ty =
248 typeof_aux metasenv subst context1 None bo
250 let bo_ty = NCicSubstitution.subst ~avoid_beta_redexes:true t bo_ty in
251 metasenv, subst, C.LetIn (n, ty, t, bo), bo_ty
252 | C.Appl ((he as orig_he)::(_::_ as args)) ->
253 let metasenv, subst, he, ty_he =
254 typeof_aux metasenv subst context None he in
255 eat_prods localise metasenv subst context orig_he he ty_he args
256 | C.Appl _ -> raise (AssertFailure (lazy "Appl of length < 2"))
257 | C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as r,
258 outtype,(term as orig_term),pl) as orig ->
259 let _, leftno, itl, _, _ = NCicEnvironment.get_checked_indtys r in
260 let _, _, arity, cl = List.nth itl tyno in
261 let constructorsno = List.length cl in
262 let _, metasenv, args =
263 NCicMetaSubst.saturate metasenv context arity 0 in
264 let ind = if args = [] then C.Const r else C.Appl (C.Const r::args) in
265 let metasenv, subst, term, _ =
266 typeof_aux metasenv subst context (Some ind) term in
267 let metasenv, subst, outtype, outsort =
268 typeof_aux metasenv subst context None outtype in
269 let parameters, arguments = HExtlib.split_nth leftno args in
270 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
272 if parameters = [] then C.Const r
273 else C.Appl ((C.Const r)::parameters) in
274 let metasenv, subst, ind, ind_ty =
275 typeof_aux metasenv subst context None ind in
276 let metasenv, subst =
277 check_allowed_sort_elimination localise r orig_term metasenv subst
278 context ind ind_ty outsort
280 (* let's check if the type of branches are right *)
281 if List.length pl <> constructorsno then
282 raise (RefineFailure (lazy (localise orig,
283 "Wrong number of cases in a match")));
284 let _, metasenv, subst, pl_rev =
286 (fun (j, metasenv, subst, branches) p ->
288 let cons = Ref.mk_constructor j r in
289 if parameters = [] then C.Const cons
290 else C.Appl (C.Const cons::parameters)
292 let metasenv, subst, cons, ty_cons =
293 typeof_aux metasenv subst context None cons in
295 NCicTypeChecker.type_of_branch
296 ~subst context leftno outtype cons ty_cons in
297 pp (lazy ("TYPEOFBRANCH: " ^
298 NCicPp.ppterm ~metasenv ~subst ~context p ^ " ::: " ^
299 NCicPp.ppterm ~metasenv ~subst ~context ty_branch ));
300 let metasenv, subst, p, _ =
301 typeof_aux metasenv subst context (Some ty_branch) p in
302 j+1, metasenv, subst, p :: branches)
303 (1, metasenv, subst, []) pl
306 C.Match (r, outtype, term, List.rev pl_rev),
307 NCicReduction.head_beta_reduce (C.Appl (outtype::arguments@[term]))
308 | C.Match _ -> assert false
310 force_ty metasenv subst context orig t infty expty
311 (*D*)in outside(); rc with exc -> outside (); raise exc
313 typeof_aux metasenv subst context expty term
315 and eat_prods localise metasenv subst context orig_he he ty_he args =
316 (*D*)inside 'E'; try let rc =
317 let rec aux metasenv subst args_so_far ty_he = function
318 | [] -> metasenv, subst, NCic.Appl (he :: List.rev args_so_far), ty_he
320 match NCicReduction.whd ~subst context ty_he with
322 let metasenv, subst, arg, _ =
323 typeof ~localise metasenv subst context arg (Some s) in
324 let t = NCicSubstitution.subst ~avoid_beta_redexes:true arg t in
325 aux metasenv subst (arg :: args_so_far) t tl
327 let metasenv, subst, arg, ty_arg =
328 typeof ~localise metasenv subst context arg None in
329 let metasenv, meta, _ =
330 NCicMetaSubst.mk_meta metasenv
331 (("_",C.Decl ty_arg) :: context) `Type
333 let flex_prod = C.Prod ("_", ty_arg, meta) in
334 pp (lazy ( "UNIFICATION in CTX:\n"^
335 NCicPp.ppcontext ~metasenv ~subst context
337 NCicPp.ppterm ~metasenv ~subst ~context t ^ " === " ^
338 NCicPp.ppterm ~metasenv ~subst ~context flex_prod ^ "\n"));
339 let metasenv, subst =
340 try NCicUnification.unify metasenv subst context t flex_prod
341 with exc -> raise (wrap_exc (lazy (localise orig_he, Printf.sprintf
342 ("The term %s is here applied to %d arguments but expects " ^^
343 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
344 (List.length args) (List.length args_so_far))) exc)
345 (* XXX coerce to funclass *)
347 let meta = NCicSubstitution.subst ~avoid_beta_redexes:true arg meta in
348 aux metasenv subst (arg :: args_so_far) meta tl
350 aux metasenv subst [] ty_he args
351 (*D*)in outside(); rc with exc -> outside (); raise exc
353 (* vim:set foldmethod=marker: *)