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 type automation_cache = NDiscriminationTree.DiscriminationTree.t
20 type unit_eq_cache = NCicParamod.state
22 exception Error of string lazy_t * exn option
23 let fail ?exn msg = raise (Error (msg,exn))
25 module NRef = NReference
30 | MultiPassDisambiguator.DisambiguationError _
31 | NCicRefiner.RefineFailure _
32 | NCicRefiner.Uncertain _
33 | NCicUnification.UnificationFailure _
34 | NCicUnification.Uncertain _
35 | NCicTypeChecker.TypeCheckerFailure _
36 | NCicMetaSubst.MetaSubstFailure _
37 | NCicMetaSubst.Uncertain _ as exn -> fail ~exn (lazy fname)
40 class type g_eq_status =
42 method eq_cache : unit_eq_cache
47 val eq_cache = NCicParamod.empty_state
48 method eq_cache = eq_cache
49 method set_eq_cache v = {< eq_cache = v >}
51 : 'status. #g_eq_status as 'status -> 'self
52 = fun o -> self#set_eq_cache o#eq_cache
55 class type g_auto_status =
57 method auto_cache : automation_cache
62 val auto_cache = NDiscriminationTree.DiscriminationTree.empty
63 method auto_cache = auto_cache
64 method set_auto_cache v = {< auto_cache = v >}
65 method set_auto_status
66 : 'status. #g_auto_status as 'status -> 'self
67 = fun o -> self#set_auto_cache o#auto_cache
70 class type g_pstatus =
72 inherit GrafiteDisambiguate.g_status
78 class virtual pstatus =
81 inherit GrafiteDisambiguate.status
86 method set_obj o = {< obj = o >}
87 method set_pstatus : 'status. #g_pstatus as 'status -> 'self
89 (((self#set_disambiguate_status o)#set_obj o#obj)#set_auto_status o)#set_eq_status o
92 type tactic_term = NotationPt.term Disambiguate.disambiguator_input
93 type tactic_pattern = GrafiteAst.npattern Disambiguate.disambiguator_input
95 type cic_term = NCic.context * NCic.term
96 let ctx_of (c,_) = c ;;
97 let mk_cic_term c t = c,t ;;
99 let ppterm (status:#pstatus) t =
100 let uri,height,metasenv,subst,obj = status#obj in
102 status#ppterm ~metasenv ~subst ~context t
105 let ppcontext (status: #pstatus) c =
106 let uri,height,metasenv,subst,obj = status#obj in
107 status#ppcontext ~metasenv ~subst c
110 let ppterm_and_context (status: #pstatus) t =
111 let uri,height,metasenv,subst,obj = status#obj in
113 status#ppcontext ~metasenv ~subst context ^ "\n ⊢ "^
114 status#ppterm ~metasenv ~subst ~context t
117 let relocate status destination (source,t as orig) =
118 pp(lazy("relocate:\n" ^ ppterm_and_context status orig));
119 pp(lazy("relocate in:\n" ^ ppcontext status destination));
121 if source == destination then status, orig else
122 let _, _, metasenv, subst, _ = status#obj in
123 let rec compute_ops ctx = function (* destination, source *)
124 | (n1, NCic.Decl t1 as e)::cl1 as ex, (n2, NCic.Decl t2)::cl2 ->
126 NCicReduction.are_convertible status ctx ~subst ~metasenv t1 t2 then
127 compute_ops (e::ctx) (cl1,cl2)
129 [ `Delift ctx; `Lift (List.rev ex) ]
130 | (n1, NCic.Def (b1,t1) as e)::cl1 as ex, (n2, NCic.Def (b2,t2))::cl2 ->
132 NCicReduction.are_convertible status ctx ~subst ~metasenv t1 t2 &&
133 NCicReduction.are_convertible status ctx ~subst ~metasenv b1 b2 then
134 compute_ops (e::ctx) (cl1,cl2)
136 [ `Delift ctx; `Lift (List.rev ex) ]
137 | (n1, NCic.Def (b1,t1) as e)::cl1 as ex, (n2, NCic.Decl t2)::cl2 ->
139 NCicReduction.are_convertible status ctx ~subst ~metasenv t1 t2 then
140 compute_ops (e::ctx) (cl1,cl2)
142 [ `Delift ctx; `Lift (List.rev ex) ]
143 | (n1, NCic.Decl _)::cl1 as ex, (n2, NCic.Def _)::cl2 ->
144 [ `Delift ctx; `Lift (List.rev ex) ]
145 | _::_ as ex, [] -> [ `Lift (List.rev ex) ]
146 | [], _::_ -> [ `Delift ctx ]
149 let ops = compute_ops [] (List.rev destination, List.rev source) in
150 let rec mk_irl i j = if i > j then [] else NCic.Rel i :: mk_irl (i+1) j in
152 (fun (status, (source,t)) -> function
154 let len = List.length extra_ctx in
155 status, (extra_ctx@source, NCicSubstitution.lift status len t)
157 let len_ctx = List.length ctx in
158 let irl = mk_irl 1 (List.length ctx) in
159 let lc = List.length source - len_ctx, NCic.Ctx irl in
160 let u, d, metasenv, subst, o = status#obj in
161 pp(lazy("delifting as " ^
162 status#ppterm ~metasenv ~subst ~context:source
163 (NCic.Meta (-1,lc))));
164 let (metasenv, subst), t =
165 NCicMetaSubst.delift status
166 ~unify:(fun m s c t1 t2 ->
167 try Some (NCicUnification.unify status m s c t1 t2)
169 | NCicUnification.UnificationFailure _
170 | NCicUnification.Uncertain _ -> None)
171 metasenv subst source (-1) lc t
173 let status = status#set_obj (u, d, metasenv, subst, o) in
177 pp(lazy("relocated: " ^ ppterm (fst rc) (snd rc)));
180 let relocate a b c = wrap "relocate" (relocate a b) c;;
182 let term_of_cic_term s t c =
183 let s, (_,t) = relocate s c t in
187 let disambiguate status context t ty =
191 let status, (_,x) = relocate status context ty in status, `XTSome x
192 | `XTNone -> status, `XTNone
193 | `XTSort -> status, `XTSort
194 | `XTInd -> status, `XTInd
196 let uri,height,metasenv,subst,obj = status#obj in
197 let metasenv, subst, status, t =
198 GrafiteDisambiguate.disambiguate_nterm status expty context metasenv subst t
200 let new_pstatus = uri,height,metasenv,subst,obj in
201 status#set_obj new_pstatus, (context, t)
203 let disambiguate a b c d = wrap "disambiguate" (disambiguate a b c) d;;
205 let typeof status ctx t =
206 let status, (_,t) = relocate status ctx t in
207 let _,_,metasenv,subst,_ = status#obj in
208 let ty = NCicTypeChecker.typeof status ~subst ~metasenv ctx t in
211 let typeof a b c = wrap "typeof" (typeof a b) c;;
213 let saturate status ?delta (ctx,t) =
214 let n,h,metasenv,subst,k = status#obj in
215 let t,metasenv,args = NCicMetaSubst.saturate status ?delta metasenv subst ctx t 0 in
216 let status = status#set_obj (n,h,metasenv,subst,k) in
217 status, (ctx,t), List.map (fun x -> ctx,x) args
219 let saturate a ?delta b = wrap "saturate" (saturate a ?delta) b;;
221 let whd status ?delta ctx t =
222 let status, (_,t) = relocate status ctx t in
223 let _,_,_,subst,_ = status#obj in
224 let t = NCicReduction.whd status ~subst ?delta ctx t in
228 let normalize status ?delta ctx t =
229 let status, (_,t) = relocate status ctx t in
230 let _,_,_,subst,_ = status#obj in
231 let t = NCicTacReduction.normalize status ~subst ?delta ctx t in
235 let unify status ctx a b =
236 let status, (_,a) = relocate status ctx a in
237 let status, (_,b) = relocate status ctx b in
238 let n,h,metasenv,subst,o = status#obj in
239 let metasenv, subst = NCicUnification.unify status metasenv subst ctx a b in
240 status#set_obj (n,h,metasenv,subst,o)
242 let unify a b c d = wrap "unify" (unify a b c) d;;
244 let fix_sorts status (ctx,t) =
246 let name,height,metasenv,subst,obj = status#obj in
248 NCicUnification.fix_sorts status metasenv subst t in
249 let status = status#set_obj (name,height,metasenv,subst,obj) in
252 wrap "fix_sorts" f ()
255 let refine status ctx term expty =
256 let status, (_,term) = relocate status ctx term in
260 let status, (_, e) = relocate status ctx e in status, `XTSome e
261 | `XTNone -> status, `XTNone
262 | `XTSort -> status, `XTSort
263 | `XTInd -> status, `XTInd
265 let name,height,metasenv,subst,obj = status#obj in
266 let metasenv,subst,t,ty =
267 NCicRefiner.typeof status metasenv subst ctx term expty
269 status#set_obj (name,height,metasenv,subst,obj), (ctx,t), (ctx,ty)
271 let refine a b c d = wrap "refine" (refine a b c) d;;
273 let get_goalty status g =
274 let _,_,metasenv,_,_ = status#obj in
276 let _, ctx, ty = NCicUtils.lookup_meta g metasenv in
278 with NCicUtils.Meta_not_found _ as exn -> fail ~exn (lazy "get_goalty")
281 let get_subst status =
282 let _,_,_,subst,_ = status#obj in subst
285 let to_subst status i entry =
286 let name,height,metasenv,subst,obj = status#obj in
287 let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
288 let subst = (i, entry) :: subst in
289 status#set_obj (name,height,metasenv,subst,obj)
292 let instantiate status ?refine:(dorefine=true) i t =
293 let _,_,metasenv,_,_ = status#obj in
294 let gname, context, gty = List.assoc i metasenv in
296 let status, (_,t), (_,ty) = refine status context t (`XTSome (context,gty)) in
297 to_subst status i (gname,context,t,ty)
299 let status,(_,ty) = typeof status context t in
300 to_subst status i (gname,context,snd t,ty)
303 let instantiate_with_ast status i t =
304 let _,_,metasenv,_,_ = status#obj in
305 let gname, context, gty = List.assoc i metasenv in
306 let ggty = mk_cic_term context gty in
307 let status, (_,t) = disambiguate status context t (`XTSome ggty) in
308 to_subst status i (gname,context,t,gty)
311 let mk_meta status ?(attrs=[]) ctx bo_or_ty kind =
314 let status, (_,ty) = relocate status ctx ty in
315 let n,h,metasenv,subst,o = status#obj in
316 let metasenv, _, instance, _ =
317 NCicMetaSubst.mk_meta ~attrs metasenv ctx ~with_type:ty kind
319 let status = status#set_obj (n,h,metasenv,subst,o) in
320 status, (ctx,instance)
322 let status, (_,bo_ as bo) = relocate status ctx bo in
323 let status, (_,ty) = typeof status ctx bo in
324 let n,h,metasenv,subst,o = status#obj in
325 let metasenv, metano, instance, _ =
326 NCicMetaSubst.mk_meta ~attrs metasenv ctx ~with_type:ty kind in
327 let attrs,_,_ = NCicUtils.lookup_meta metano metasenv in
328 let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
329 let subst = (metano, (attrs, ctx, bo_, ty)) :: subst in
330 let status = status#set_obj (n,h,metasenv,subst,o) in
331 status, (ctx,instance)
334 let mk_in_scope status t =
335 mk_meta status ~attrs:[`InScope] (ctx_of t) (`Def t) `IsTerm
338 let mk_out_scope n status t =
339 mk_meta status ~attrs:[`OutScope n] (ctx_of t) (`Def t) `IsTerm
342 (* the following unification problem will be driven by
343 * select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
347 * where argsn = length args and the pattern matches t
349 * found is called on every selected term to map them
350 * postprocess is called on the entire term after selection
353 low_status ~found ~postprocess (context,term) (wanted,path)
355 let is_found status ctx t wanted =
356 (* we could lift wanted step-by-step *)
357 pp(lazy("is_found: "^ppterm status (ctx,t)));
358 try true, unify status ctx (ctx, t) wanted
360 | Error (_, Some (NCicUnification.UnificationFailure _))
361 | Error (_, Some (NCicUnification.Uncertain _)) -> false, status
363 let match_term status ctx (wanted : cic_term) t =
364 let rec aux ctx (status,already_found) t =
365 let b, status = is_found status ctx t wanted in
367 let status , (_,t) = found status (ctx, t) in
370 let _,_,_,subst,_ = status#obj in
372 | NCic.Meta (i,lc) when List.mem_assoc i subst ->
373 let _,_,t,_ = NCicUtils.lookup_subst i subst in
374 aux ctx (status,already_found) t
375 | NCic.Meta _ -> (status,already_found),t
377 NCicUntrusted.map_term_fold_a status (fun e c -> e::c) ctx aux
378 (status,already_found) t
380 aux ctx (status,false) t
382 let _,_,_,subst,_ = low_status#obj in
383 let rec select status ctx pat cic =
385 | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
387 let _,_,t,_ = NCicUtils.lookup_subst i subst in
388 NCicSubstitution.subst_meta status lc t
390 select status ctx pat cic
391 | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
392 let status, t = select status ctx t1 t2 in
393 let status, s = select status ctx s1 s2 in
394 let ctx = (n, NCic.Def (s2,t2)) :: ctx in
395 let status, b = select status ctx b1 b2 in
396 status, NCic.LetIn (n,t,s,b)
397 | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
398 let status, s = select status ctx s1 s2 in
399 let ctx = (n, NCic.Decl s2) :: ctx in
400 let status, t = select status ctx t1 t2 in
401 status, NCic.Lambda (n,s,t)
402 | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
403 let status, s = select status ctx s1 s2 in
404 let ctx = (n, NCic.Decl s2) :: ctx in
405 let status, t = select status ctx t1 t2 in
406 status, NCic.Prod (n,s,t)
407 | NCic.Appl l1, NCic.Appl l2 when List.length l1 = List.length l2 ->
410 (fun (status,l) x y ->
411 let status, x = select status ctx x y in
415 status, NCic.Appl (List.rev l)
416 | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2)
417 when List.length pl1 = List.length pl2 ->
418 let status, t = select status ctx t1 t2 in
419 let status, ot = select status ctx ot1 ot2 in
422 (fun (status,l) x y ->
423 let status, x = select status ctx x y in
427 status, NCic.Match (u,ot,t,List.rev pl)
428 | NCic.Implicit `Hole, t ->
431 let status', wanted = disambiguate status ctx wanted `XTNone in
432 pp(lazy("wanted: "^ppterm status' wanted));
433 let (status',found), t' = match_term status' ctx wanted t in
434 if found then status',t' else status,t
436 let (status,_),t = match_term status ctx (ctx,t) t in
438 | NCic.Implicit _, t -> status, t
440 fail (lazy ("malformed pattern: " ^ status#ppterm ~metasenv:[]
441 ~context:[] ~subst:[] pat ^ " against " ^
442 status#ppterm ~metasenv:[] ~subst:[] ~context:[] t))
444 pp(lazy ("select in: "^ppterm low_status (context,term)));
445 let status, term = select low_status context path term in
446 let term = (context, term) in
447 pp(lazy ("postprocess: "^ppterm low_status term));
448 postprocess status term
451 let analyse_indty status ty =
452 let status, reduct = whd status (ctx_of ty) ty in
455 | _,NCic.Const ref -> ref, []
456 | _,NCic.Appl (NCic.Const (NRef.Ref (_,(NRef.Ind _)) as ref) :: args) ->
458 | _,_ -> fail (lazy ("not an inductive type: " ^ ppterm status ty)) in
459 let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys status ref in
460 let _,_,_,cl = List.nth tl i in
461 let consno = List.length cl in
462 let left, right = HExtlib.split_nth lno args in
463 status, (ref, consno, left, right)
466 let apply_subst status ctx t =
467 let status, (_,t) = relocate status ctx t in
468 let _,_,_,subst,_ = status#obj in
469 status, (ctx, NCicUntrusted.apply_subst status subst ctx t)
472 let apply_subst_context status ~fix_projections ctx =
473 let _,_,_,subst,_ = status#obj in
474 NCicUntrusted.apply_subst_context status ~fix_projections subst ctx
477 let metas_of_term status (context,t) =
478 let _,_,_,subst,_ = status#obj in
479 NCicUntrusted.metas_of_term status subst context t
482 (* ============= move this elsewhere ====================*)
484 class type ['stack] g_status =
490 class virtual ['stack] status =
491 fun (o: NCic.obj) (s: 'stack) ->
496 method set_stack s = {< stack = s >}
497 method set_status : 'status. 'stack #g_status as 'status -> 'self
498 = fun o -> (self#set_pstatus o)#set_stack o#stack
501 class type virtual lowtac_status = [unit] status
503 type 'status lowtactic = #lowtac_status as 'status -> int -> 'status
505 class type virtual tac_status = [Continuationals.Stack.t] status
507 type 'status tactic = #tac_status as 'status -> 'status
509 let pp_tac_status (status: #tac_status) =
510 prerr_endline (status#ppobj status#obj);
511 prerr_endline ("STACK:\n" ^ Continuationals.Stack.pp status#stack)
514 module NCicInverseRelIndexable : Discrimination_tree.Indexable
515 with type input = cic_term and type constant_name = NUri.uri = struct
517 open Discrimination_tree
519 type input = cic_term
520 type constant_name = NUri.uri
522 let ppelem = function
523 | Constant (uri,arity) ->
524 "("^NUri.name_of_uri uri ^ "," ^ string_of_int arity^")"
526 "("^string_of_int i ^ "," ^ string_of_int arity^")"
528 | Proposition -> "Prop"
533 let string_of_path l = String.concat "." (List.map ppelem l) ;;
535 let path_string_of (ctx,t) =
536 let len_ctx = List.length ctx in
537 let rec aux arity = function
538 | NCic.Appl ((NCic.Meta _|NCic.Implicit _)::_) -> [Variable]
539 | NCic.Appl (NCic.Lambda _ :: _) -> [Variable] (* maybe we should b-reduce *)
540 | NCic.Appl [] -> assert false
541 | NCic.Appl (hd::tl) ->
542 aux (List.length tl) hd @ List.flatten (List.map (aux 0) tl)
543 | NCic.Lambda _ | NCic.Prod _ -> [Variable]
544 (* I think we should CicSubstitution.subst Implicit t *)
545 | NCic.LetIn _ -> [Variable] (* z-reduce? *)
546 | NCic.Meta _ | NCic.Implicit _ -> assert (arity = 0); [Variable]
547 | NCic.Rel i -> [Bound (len_ctx - i, arity)]
548 | NCic.Sort (NCic.Prop) -> assert (arity=0); [Proposition]
549 | NCic.Sort _ -> assert (arity=0); [Datatype]
550 | NCic.Const (NReference.Ref (u,_)) -> [Constant (u, arity)]
551 | NCic.Match _ -> [Dead]
553 let path = aux 0 t in
554 (* prerr_endline (string_of_path path); *)
560 | Constant (u1,a1),Constant (u2,a2) ->
561 let x = NUri.compare u1 u2 in
562 if x = 0 then Pervasives.compare a1 a2 else x
563 | e1,e2 -> Pervasives.compare e1 e2
569 module Ncic_termOT : Set.OrderedType with type t = cic_term =
572 let compare = Pervasives.compare
575 module Ncic_termSet : Set.S with type elt = cic_term = Set.Make(Ncic_termOT)
577 module InvRelDiscriminationTree =
578 Discrimination_tree.Make(NCicInverseRelIndexable)(Ncic_termSet)