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 cons x (a,b) = a, x::b in
78 let rec lcp ctx j i = function
79 | (n1, NCic.Decl t1 as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
81 NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
82 cons (NCic.Rel i) (lcp (e::ctx)(j-1) (i-1) (cl1,cl2))
85 | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Def (b2,t2))::cl2 ->
87 NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 &&
88 NCicReduction.are_convertible ctx ~subst ~metasenv b1 b2 then
89 cons (NCic.Rel i) (lcp (e::ctx)(j-1) (i-1) (cl1,cl2))
92 | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
94 NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
95 cons (NCic.Rel i) (lcp (e::ctx)(j-1) (i-1) (cl1,cl2))
98 | (n1, NCic.Decl _)::cl1, (n2, NCic.Def _)::cl2 -> assert false
103 lcp [] (List.length destination) (List.length source)
104 (List.rev destination, List.rev source)
106 let lc = (shift,NCic.Ctx (List.rev lc)) in
107 pp(lazy("delifting as " ^
108 NCicPp.ppterm ~metasenv ~subst ~context:source
109 (NCic.Meta (0,lc))));
110 let (metasenv, subst), t =
112 ~unify:(fun m s c t1 t2 ->
113 try Some (NCicUnification.unify status m s c t1 t2)
115 | NCicUnification.UnificationFailure _
116 | NCicUnification.Uncertain _ -> None)
117 metasenv subst source 0 lc t
119 let status = status#set_obj (u, d, metasenv, subst, o) in
120 status, (destination, t)
122 pp(lazy("relocated: " ^ ppterm (fst rc) (snd rc)));
125 let relocate a b c = wrap "relocate" (relocate a b) c;;
127 let term_of_cic_term s t c =
128 let s, (_,t) = relocate s c t in
132 let disambiguate status t ty context =
135 | None -> status, None
137 let status, (_,x) = relocate status context ty in status, Some x
139 let uri,height,metasenv,subst,obj = status#obj in
140 let metasenv, subst, status, t =
141 GrafiteDisambiguate.disambiguate_nterm expty status context metasenv subst t
143 let new_pstatus = uri,height,metasenv,subst,obj in
144 status#set_obj new_pstatus, (context, t)
146 let disambiguate a b c d = wrap "disambiguate" (disambiguate a b c) d;;
148 let typeof status ctx t =
149 let status, (_,t) = relocate status ctx t in
150 let _,_,metasenv,subst,_ = status#obj in
151 let ty = NCicTypeChecker.typeof ~subst ~metasenv ctx t in
154 let typeof a b c = wrap "typeof" (typeof a b) c;;
156 let saturate status (ctx,t) =
157 let n,h,metasenv,subst,k = status#obj in
158 let t, metasenv, args = NCicMetaSubst.saturate metasenv subst ctx t 0 in
159 let status = status#set_obj (n,h,metasenv,subst,k) in
160 status, (ctx,t), List.map (fun x -> ctx,x) args
162 let saturate a b = wrap "saturate" (saturate a) b;;
164 let whd status ?delta ctx t =
165 let status, (_,t) = relocate status ctx t in
166 let _,_,_,subst,_ = status#obj in
167 let t = NCicReduction.whd ~subst ?delta ctx t in
171 let normalize status ?delta ctx t =
172 let status, (_,t) = relocate status ctx t in
173 let _,_,_,subst,_ = status#obj in
174 let t = NCicTacReduction.normalize ~subst ?delta ctx t in
178 let unify status ctx a b =
179 let status, (_,a) = relocate status ctx a in
180 let status, (_,b) = relocate status ctx b in
181 let n,h,metasenv,subst,o = status#obj in
182 let metasenv, subst = NCicUnification.unify status metasenv subst ctx a b in
183 status#set_obj (n,h,metasenv,subst,o)
185 let unify a b c d = wrap "unify" (unify a b c) d;;
187 let fix_sorts (ctx,t) =
189 let t = NCicUnification.fix_sorts t in
192 wrap "fix_sorts" f ()
195 let refine status ctx term expty =
196 let status, (_,term) = relocate status ctx term in
201 let status, (_, e) = relocate status ctx e in status, Some e
203 let name,height,metasenv,subst,obj = status#obj in
204 let metasenv,subst,t,ty =
205 NCicRefiner.typeof status metasenv subst ctx term expty
207 status#set_obj (name,height,metasenv,subst,obj), (ctx,t), (ctx,ty)
209 let refine a b c d = wrap "refine" (refine a b c) d;;
211 let get_goalty status g =
212 let _,_,metasenv,_,_ = status#obj in
214 let _, ctx, ty = NCicUtils.lookup_meta g metasenv in
216 with NCicUtils.Meta_not_found _ as exn -> fail ~exn (lazy "get_goalty")
219 let instantiate status i t =
220 let _,_,metasenv,_,_ = status#obj in
221 let gname, context, gty = List.assoc i metasenv in
222 let status, (_,t), (_,ty) =
223 refine status context t (Some (context,gty))
226 let name,height,metasenv,subst,obj = status#obj in
227 let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
228 let subst = (i, (gname, context, t, ty)) :: subst in
229 status#set_obj (name,height,metasenv,subst,obj)
232 let mk_meta status ?(attrs=[]) ctx bo_or_ty =
235 let status, (_,ty) = relocate status ctx ty in
236 let n,h,metasenv,subst,o = status#obj in
237 let metasenv, _, instance, _ =
238 NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty)
240 let status = status#set_obj (n,h,metasenv,subst,o) in
241 status, (ctx,instance)
243 let status, (_,bo_ as bo) = relocate status ctx bo in
244 let status, (_,ty) = typeof status ctx bo in
245 let n,h,metasenv,subst,o = status#obj in
246 let metasenv, metano, instance, _ =
247 NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty) in
248 let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
249 let subst = (metano, (attrs, ctx, bo_, ty)) :: subst in
250 let status = status#set_obj (n,h,metasenv,subst,o) in
251 status, (ctx,instance)
254 let mk_in_scope status t =
255 mk_meta status ~attrs:[`InScope] (ctx_of t) (`Def t)
258 let mk_out_scope n status t =
259 mk_meta status ~attrs:[`OutScope n] (ctx_of t) (`Def t)
262 (* the following unification problem will be driven by
263 * select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
267 * where argsn = length args and the pattern matches t
269 * found is called on every selected term to map them
270 * postprocess is called on the entire term after selection
273 low_status ~found ~postprocess (context,term) (wanted,path)
275 let is_found status ctx t wanted =
276 (* we could lift wanted step-by-step *)
277 pp(lazy("is_found: "^ppterm status (ctx,t)));
278 try true, unify status ctx (ctx, t) wanted
280 | Error (_, Some (NCicUnification.UnificationFailure _))
281 | Error (_, Some (NCicUnification.Uncertain _)) -> false, status
283 let match_term status ctx (wanted : cic_term) t =
284 let rec aux ctx (status,already_found) t =
285 let b, status = is_found status ctx t wanted in
287 let status , (_,t) = found status (ctx, t) in
290 let _,_,_,subst,_ = status#obj in
292 | NCic.Meta (i,lc) when List.mem_assoc i subst ->
293 let _,_,t,_ = NCicUtils.lookup_subst i subst in
294 aux ctx (status,already_found) t
295 | NCic.Meta _ -> (status,already_found),t
297 NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux
298 (status,already_found) t
300 aux ctx (status,false) t
302 let _,_,_,subst,_ = low_status#obj in
303 let rec select status ctx pat cic =
305 | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
307 let _,_,t,_ = NCicUtils.lookup_subst i subst in
308 NCicSubstitution.subst_meta lc t
310 select status ctx pat cic
311 | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
312 let status, t = select status ctx t1 t2 in
313 let status, s = select status ctx s1 s2 in
314 let ctx = (n, NCic.Def (s2,t2)) :: ctx in
315 let status, b = select status ctx b1 b2 in
316 status, NCic.LetIn (n,t,s,b)
317 | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
318 let status, s = select status ctx s1 s2 in
319 let ctx = (n, NCic.Decl s2) :: ctx in
320 let status, t = select status ctx t1 t2 in
321 status, NCic.Lambda (n,s,t)
322 | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
323 let status, s = select status ctx s1 s2 in
324 let ctx = (n, NCic.Decl s2) :: ctx in
325 let status, t = select status ctx t1 t2 in
326 status, NCic.Prod (n,s,t)
327 | NCic.Appl l1, NCic.Appl l2 ->
330 (fun (status,l) x y ->
331 let status, x = select status ctx x y in
335 status, NCic.Appl (List.rev l)
336 | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
337 let status, t = select status ctx t1 t2 in
338 let status, ot = select status ctx ot1 ot2 in
341 (fun (status,l) x y ->
342 let status, x = select status ctx x y in
346 status, NCic.Match (u,ot,t,List.rev pl)
347 | NCic.Implicit `Hole, t ->
350 let status', wanted = disambiguate status wanted None ctx in
351 pp(lazy("wanted: "^ppterm status' wanted));
352 let (status',found), t' = match_term status' ctx wanted t in
353 if found then status',t' else status,t
355 let (status,_),t = match_term status ctx (ctx,t) t in
357 | NCic.Implicit _, t -> status, t
359 fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
360 ~context:[] ~subst:[] pat ^ " against " ^
361 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
363 pp(lazy ("select in: "^ppterm low_status (context,term)));
364 let status, term = select low_status context path term in
365 let term = (context, term) in
366 pp(lazy ("postprocess: "^ppterm low_status term));
367 postprocess status term
370 let analyse_indty status ty =
371 let status, reduct = whd status (ctx_of ty) ty in
374 | _,NCic.Const ref -> ref, []
375 | _,NCic.Appl (NCic.Const (NRef.Ref (_,(NRef.Ind _)) as ref) :: args) ->
377 | _,_ -> fail (lazy ("not an inductive type")) in
378 let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
379 let _,_,_,cl = List.nth tl i in
380 let consno = List.length cl in
381 let left, right = HExtlib.split_nth lno args in
382 status, (ref, consno, left, right)
385 let mk_cic_term c t = c,t ;;
387 let apply_subst status ctx t =
388 let status, (_,t) = relocate status ctx t in
389 let _,_,_,subst,_ = status#obj in
390 status, (ctx, NCicUntrusted.apply_subst subst ctx t)
393 (* ============= move this elsewhere ====================*)
395 class ['stack] status =
396 fun (o: NCic.obj) (s: 'stack) ->
401 method set_stack s = {< stack = s >}
404 class type lowtac_status = [unit] status
406 type 'status lowtactic = #lowtac_status as 'status -> int -> 'status
408 class type tac_status = [Continuationals.Stack.t] status
410 type 'status tactic = #tac_status as 'status -> 'status
412 module NCicInverseRelIndexable : Discrimination_tree.Indexable
413 with type input = cic_term and type constant_name = NUri.uri = struct
415 open Discrimination_tree
417 type input = cic_term
418 type constant_name = NUri.uri
420 let ppelem = function
421 | Constant (uri,arity) ->
422 "("^NUri.name_of_uri uri ^ "," ^ string_of_int arity^")"
424 "("^string_of_int i ^ "," ^ string_of_int arity^")"
426 | Proposition -> "Prop"
431 let path_string_of (ctx,t) =
432 let len_ctx = List.length ctx in
433 let rec aux arity = function
434 | NCic.Appl ((NCic.Meta _|NCic.Implicit _)::_) -> [Variable]
435 | NCic.Appl (NCic.Lambda _ :: _) -> [Variable] (* maybe we should b-reduce *)
436 | NCic.Appl [] -> assert false
437 | NCic.Appl (hd::tl) ->
438 aux (List.length tl) hd @ List.flatten (List.map (aux 0) tl)
439 | NCic.Lambda _ | NCic.Prod _ -> [Variable]
440 (* I think we should CicSubstitution.subst Implicit t *)
441 | NCic.LetIn _ -> [Variable] (* z-reduce? *)
442 | NCic.Meta _ | NCic.Implicit _ -> assert (arity = 0); [Variable]
443 | NCic.Rel i -> [Bound (len_ctx - i, arity)]
444 | NCic.Sort (NCic.Prop) -> assert (arity=0); [Proposition]
445 | NCic.Sort _ -> assert (arity=0); [Datatype]
446 | NCic.Const (NReference.Ref (u,_)) -> [Constant (u, arity)]
447 | NCic.Match _ -> [Dead]
454 | Constant (u1,a1),Constant (u2,a2) ->
455 let x = NUri.compare u1 u2 in
456 if x = 0 then Pervasives.compare a1 a2 else x
457 | e1,e2 -> Pervasives.compare e1 e2
460 let string_of_path l = String.concat "." (List.map ppelem l) ;;
464 module Ncic_termOT : Set.OrderedType with type t = cic_term =
467 let compare = Pervasives.compare
470 module Ncic_termSet : Set.S with type elt = cic_term = Set.Make(Ncic_termOT)
472 module InvRelDiscriminationTree =
473 Discrimination_tree.Make(NCicInverseRelIndexable)(Ncic_termSet)