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_______________________________________________________________ *)
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
14 let debug = ref false;;
16 if !debug then prerr_endline (Lazy.force x) else ()
19 exception Error of string lazy_t * exn option
20 let fail ?exn msg = raise (Error (msg,exn))
22 module NRef = NReference
27 | MultiPassDisambiguator.DisambiguationError _
28 | NCicRefiner.RefineFailure _
29 | NCicUnification.UnificationFailure _
30 | NCicTypeChecker.TypeCheckerFailure _
31 | NCicMetaSubst.MetaSubstFailure _ as exn -> fail ~exn (lazy fname)
37 inherit NEstatus.status
40 method set_obj o = {< obj = o >}
43 type tactic_term = CicNotationPt.term Disambiguate.disambiguator_input
44 type tactic_pattern = GrafiteAst.npattern Disambiguate.disambiguator_input
46 let pp_status status =
47 pp (lazy (NCicPp.ppobj status#obj))
50 type cic_term = NCic.context * NCic.term
51 let ctx_of (c,_) = c ;;
54 let uri,height,metasenv,subst,obj = status#obj in
56 NCicPp.ppterm ~metasenv ~subst ~context t
59 let ppcontext status c =
60 let uri,height,metasenv,subst,obj = status#obj in
61 NCicPp.ppcontext ~metasenv ~subst c
64 let ppterm_and_context status t =
65 let uri,height,metasenv,subst,obj = status#obj in
67 NCicPp.ppcontext ~metasenv ~subst context ^ "\n ⊢ "^
68 NCicPp.ppterm ~metasenv ~subst ~context t
71 let relocate status destination (source,t as orig) =
72 pp(lazy("relocate:\n" ^ ppterm_and_context status orig));
73 pp(lazy("relocate in:\n" ^ ppcontext status destination));
75 if source == destination then status, orig else
76 let u, d, metasenv, subst, o = status#obj in
77 let hole = NCic.Sort NCic.Prop in
78 (* XXX (Prop Prop) is so illtyped that
79 even the trie used for hints lookup complains. We say Prop.
81 let rec lcp ctx j i = function
82 | (n1, NCic.Decl t1 as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
84 NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
85 NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
87 HExtlib.mk_list hole j
88 | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Def (b2,t2))::cl2 ->
90 NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 &&
91 NCicReduction.are_convertible ctx ~subst ~metasenv b1 b2 then
92 NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
94 HExtlib.mk_list hole j
95 | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
97 NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
98 NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
100 HExtlib.mk_list hole j
101 | (n1, NCic.Decl _)::cl1, (n2, NCic.Def _)::cl2 -> assert false
102 | _::_, [] -> HExtlib.mk_list hole j
106 lcp [] (List.length destination) (List.length source)
107 (List.rev destination, List.rev source)
109 let lc = (0,NCic.Ctx (List.rev lc)) in
110 pp(lazy("delifting as " ^
111 NCicPp.ppterm ~metasenv ~subst ~context:source
112 (NCic.Meta (0,lc))));
113 let (metasenv, subst), t =
115 ~unify:(fun m s c t1 t2 ->
116 try Some (NCicUnification.unify status m s c t1 t2)
118 | NCicUnification.UnificationFailure _
119 | NCicUnification.Uncertain _ -> None)
120 metasenv subst source 0 lc t
122 let status = status#set_obj (u, d, metasenv, subst, o) in
123 status, (destination, t)
125 pp(lazy("relocated: " ^ ppterm (fst rc) (snd rc)));
128 let relocate a b c = wrap "relocate" (relocate a b) c;;
130 let term_of_cic_term s t c =
131 let s, (_,t) = relocate s c t in
135 let disambiguate status t ty context =
138 | None -> status, None
140 let status, (_,x) = relocate status context ty in status, Some x
142 let uri,height,metasenv,subst,obj = status#obj in
143 let metasenv, subst, status, t =
144 GrafiteDisambiguate.disambiguate_nterm expty status context metasenv subst t
146 let new_pstatus = uri,height,metasenv,subst,obj in
147 status#set_obj new_pstatus, (context, t)
149 let disambiguate a b c d = wrap "disambiguate" (disambiguate a b c) d;;
151 let typeof status ctx t =
152 let status, (_,t) = relocate status ctx t in
153 let _,_,metasenv,subst,_ = status#obj in
154 let ty = NCicTypeChecker.typeof ~subst ~metasenv ctx t in
157 let typeof a b c = wrap "typeof" (typeof a b) c;;
159 let saturate status (ctx,t) =
160 let n,h,metasenv,subst,k = status#obj in
161 let t, metasenv, args = NCicMetaSubst.saturate metasenv subst ctx t 0 in
162 let status = status#set_obj (n,h,metasenv,subst,k) in
163 status, (ctx,t), List.map (fun x -> ctx,x) args
165 let saturate a b = wrap "saturate" (saturate a) b;;
167 let whd status ?delta ctx t =
168 let status, (_,t) = relocate status ctx t in
169 let _,_,_,subst,_ = status#obj in
170 let t = NCicReduction.whd ~subst ?delta ctx t in
174 let normalize status ?delta ctx t =
175 let status, (_,t) = relocate status ctx t in
176 let _,_,_,subst,_ = status#obj in
177 let t = NCicTacReduction.normalize ~subst ?delta ctx t in
181 let unify status ctx a b =
182 let status, (_,a) = relocate status ctx a in
183 let status, (_,b) = relocate status ctx b in
184 let n,h,metasenv,subst,o = status#obj in
185 let metasenv, subst = NCicUnification.unify status metasenv subst ctx a b in
186 status#set_obj (n,h,metasenv,subst,o)
188 let unify a b c d = wrap "unify" (unify a b c) d;;
190 let fix_sorts (ctx,t) =
192 let t = NCicUnification.fix_sorts t in
195 wrap "fix_sorts" f ()
198 let refine status ctx term expty =
199 let status, (_,term) = relocate status ctx term in
204 let status, (_, e) = relocate status ctx e in status, Some e
206 let name,height,metasenv,subst,obj = status#obj in
207 let metasenv,subst,t,ty =
208 NCicRefiner.typeof status metasenv subst ctx term expty
210 status#set_obj (name,height,metasenv,subst,obj), (ctx,t), (ctx,ty)
212 let refine a b c d = wrap "refine" (refine a b c) d;;
214 let get_goalty status g =
215 let _,_,metasenv,_,_ = status#obj in
217 let _, ctx, ty = NCicUtils.lookup_meta g metasenv in
219 with NCicUtils.Meta_not_found _ as exn -> fail ~exn (lazy "get_goalty")
222 let instantiate status i t =
223 let _,_,metasenv,_,_ = status#obj in
224 let gname, context, gty = List.assoc i metasenv in
225 let status, (_,t), (_,ty) =
226 refine status context t (Some (context,gty))
229 let name,height,metasenv,subst,obj = status#obj in
230 let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
231 let subst = (i, (gname, context, t, ty)) :: subst in
232 status#set_obj (name,height,metasenv,subst,obj)
235 let mk_meta status ?(attrs=[]) ctx bo_or_ty =
238 let status, (_,ty) = relocate status ctx ty in
239 let n,h,metasenv,subst,o = status#obj in
240 let metasenv, _, instance, _ =
241 NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty)
243 let status = status#set_obj (n,h,metasenv,subst,o) in
244 status, (ctx,instance)
246 let status, (_,bo_ as bo) = relocate status ctx bo in
247 let status, (_,ty) = typeof status ctx bo in
248 let n,h,metasenv,subst,o = status#obj in
249 let metasenv, metano, instance, _ =
250 NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty) in
251 let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
252 let subst = (metano, (attrs, ctx, bo_, ty)) :: subst in
253 let status = status#set_obj (n,h,metasenv,subst,o) in
254 status, (ctx,instance)
257 let mk_in_scope status t =
258 mk_meta status ~attrs:[`InScope] (ctx_of t) (`Def t)
261 let mk_out_scope n status t =
262 mk_meta status ~attrs:[`OutScope n] (ctx_of t) (`Def t)
265 (* the following unification problem will be driven by
266 * select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
270 * where argsn = length args and the pattern matches t
272 * found is called on every selected term to map them
273 * postprocess is called on the entire term after selection
276 low_status ~found ~postprocess (context,term) (wanted,path)
278 let is_found status ctx t wanted =
279 (* we could lift wanted step-by-step *)
280 pp(lazy("is_found: "^ppterm status (ctx,t)));
281 try true, unify status ctx (ctx, t) wanted
283 | Error (_, Some (NCicUnification.UnificationFailure _))
284 | Error (_, Some (NCicUnification.Uncertain _)) -> false, status
286 let match_term status ctx (wanted : cic_term) t =
287 let rec aux ctx (status,already_found) t =
288 let b, status = is_found status ctx t wanted in
290 let status , (_,t) = found status (ctx, t) in
293 let _,_,_,subst,_ = status#obj in
295 | NCic.Meta (i,lc) when List.mem_assoc i subst ->
296 let _,_,t,_ = NCicUtils.lookup_subst i subst in
297 aux ctx (status,already_found) t
298 | NCic.Meta _ -> (status,already_found),t
300 NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux
301 (status,already_found) t
303 aux ctx (status,false) t
305 let _,_,_,subst,_ = low_status#obj in
306 let rec select status ctx pat cic =
308 | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
310 let _,_,t,_ = NCicUtils.lookup_subst i subst in
311 NCicSubstitution.subst_meta lc t
313 select status ctx pat cic
314 | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
315 let status, t = select status ctx t1 t2 in
316 let status, s = select status ctx s1 s2 in
317 let ctx = (n, NCic.Def (s2,t2)) :: ctx in
318 let status, b = select status ctx b1 b2 in
319 status, NCic.LetIn (n,t,s,b)
320 | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
321 let status, s = select status ctx s1 s2 in
322 let ctx = (n, NCic.Decl s2) :: ctx in
323 let status, t = select status ctx t1 t2 in
324 status, NCic.Lambda (n,s,t)
325 | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
326 let status, s = select status ctx s1 s2 in
327 let ctx = (n, NCic.Decl s2) :: ctx in
328 let status, t = select status ctx t1 t2 in
329 status, NCic.Prod (n,s,t)
330 | NCic.Appl l1, NCic.Appl l2 ->
333 (fun (status,l) x y ->
334 let status, x = select status ctx x y in
338 status, NCic.Appl (List.rev l)
339 | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
340 let status, t = select status ctx t1 t2 in
341 let status, ot = select status ctx ot1 ot2 in
344 (fun (status,l) x y ->
345 let status, x = select status ctx x y in
349 status, NCic.Match (u,ot,t,List.rev pl)
350 | NCic.Implicit `Hole, t ->
353 let status', wanted = disambiguate status wanted None ctx in
354 pp(lazy("wanted: "^ppterm status' wanted));
355 let (status',found), t' = match_term status' ctx wanted t in
356 if found then status',t' else status,t
358 let (status,_),t = match_term status ctx (ctx,t) t in
360 | NCic.Implicit _, t -> status, t
362 fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
363 ~context:[] ~subst:[] pat ^ " against " ^
364 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
366 pp(lazy ("select in: "^ppterm low_status (context,term)));
367 let status, term = select low_status context path term in
368 let term = (context, term) in
369 pp(lazy ("postprocess: "^ppterm low_status term));
370 postprocess status term
373 let analyse_indty status ty =
374 let status, reduct = whd status (ctx_of ty) ty in
377 | _,NCic.Const ref -> ref, []
378 | _,NCic.Appl (NCic.Const (NRef.Ref (_,(NRef.Ind _)) as ref) :: args) ->
380 | _,_ -> fail (lazy ("not an inductive type")) in
381 let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
382 let _,_,_,cl = List.nth tl i in
383 let consno = List.length cl in
384 let left, right = HExtlib.split_nth lno args in
385 status, (ref, consno, left, right)
388 let mk_cic_term c t = c,t ;;
390 let apply_subst status ctx t =
391 let status, (_,t) = relocate status ctx t in
392 let _,_,_,subst,_ = status#obj in
393 status, (ctx, NCicUntrusted.apply_subst subst ctx t)
396 (* ============= move this elsewhere ====================*)
398 class ['stack] status =
399 fun (o: NCic.obj) (s: 'stack) ->
404 method set_stack s = {< stack = s >}
407 class type lowtac_status = [unit] status
409 type 'status lowtactic = #lowtac_status as 'status -> int -> 'status
411 class type tac_status = [Continuationals.Stack.t] status
413 type 'status tactic = #tac_status as 'status -> 'status
415 module NCicInverseRelIndexable : Discrimination_tree.Indexable
416 with type input = cic_term and type constant_name = NUri.uri = struct
418 open Discrimination_tree
420 type input = cic_term
421 type constant_name = NUri.uri
423 let ppelem = function
424 | Constant (uri,arity) ->
425 "("^NUri.name_of_uri uri ^ "," ^ string_of_int arity^")"
427 "("^string_of_int i ^ "," ^ string_of_int arity^")"
429 | Proposition -> "Prop"
434 let path_string_of (ctx,t) =
435 let len_ctx = List.length ctx in
436 let rec aux arity = function
437 | NCic.Appl ((NCic.Meta _|NCic.Implicit _)::_) -> [Variable]
438 | NCic.Appl (NCic.Lambda _ :: _) -> [Variable] (* maybe we should b-reduce *)
439 | NCic.Appl [] -> assert false
440 | NCic.Appl (hd::tl) ->
441 aux (List.length tl) hd @ List.flatten (List.map (aux 0) tl)
442 | NCic.Lambda _ | NCic.Prod _ -> [Variable]
443 (* I think we should CicSubstitution.subst Implicit t *)
444 | NCic.LetIn _ -> [Variable] (* z-reduce? *)
445 | NCic.Meta _ | NCic.Implicit _ -> assert (arity = 0); [Variable]
446 | NCic.Rel i -> [Bound (len_ctx - i, arity)]
447 | NCic.Sort (NCic.Prop) -> assert (arity=0); [Proposition]
448 | NCic.Sort _ -> assert (arity=0); [Datatype]
449 | NCic.Const (NReference.Ref (u,_)) -> [Constant (u, arity)]
450 | NCic.Match _ -> [Dead]
457 | Constant (u1,a1),Constant (u2,a2) ->
458 let x = NUri.compare u1 u2 in
459 if x = 0 then Pervasives.compare a1 a2 else x
460 | e1,e2 -> Pervasives.compare e1 e2
463 let string_of_path l = String.concat "." (List.map ppelem l) ;;
467 module Ncic_termOT : Set.OrderedType with type t = cic_term =
470 let compare = Pervasives.compare
473 module Ncic_termSet : Set.S with type elt = cic_term = Set.Make(Ncic_termOT)
475 module InvRelDiscriminationTree =
476 Discrimination_tree.Make(NCicInverseRelIndexable)(Ncic_termSet)