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 exception Error of string lazy_t * exn option
15 let fail ?exn msg = raise (Error (msg,exn))
20 | MultiPassDisambiguator.DisambiguationError _
21 | NCicRefiner.RefineFailure _
22 | NCicUnification.UnificationFailure _
23 | NCicTypeChecker.TypeCheckerFailure _
24 | NCicMetaSubst.MetaSubstFailure _ as exn -> fail ~exn (lazy "")
30 inherit NEstatus.status
33 method set_obj o = {< obj = o >}
36 type tactic_term = CicNotationPt.term Disambiguate.disambiguator_input
37 type tactic_pattern = GrafiteAst.npattern Disambiguate.disambiguator_input
39 let pp_status status =
40 prerr_endline (NCicPp.ppobj status#obj)
43 type cic_term = NCic.context * NCic.term
44 let ctx_of (c,_) = c ;;
46 let relocate status destination (source,t as orig) =
47 if source == destination then status, orig else
48 let u, d, metasenv, subst, o = status#obj in
49 let rec lcp ctx j i = function
50 | (n1, NCic.Decl t1 as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
52 NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
53 NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
55 HExtlib.mk_list (NCic.Appl
56 [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
57 | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Def (b2,t2))::cl2 ->
59 NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 &&
60 NCicReduction.are_convertible ctx ~subst ~metasenv b1 b2 then
61 NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
63 HExtlib.mk_list (NCic.Appl
64 [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
65 | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
67 NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
68 NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
70 HExtlib.mk_list (NCic.Appl
71 [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
72 | (n1, NCic.Decl _)::cl1, (n2, NCic.Def _)::cl2 -> assert false
74 HExtlib.mk_list (NCic.Appl
75 [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
79 lcp [] (List.length destination) (List.length source)
80 (List.rev destination, List.rev source)
82 let lc = (0,NCic.Ctx (List.rev lc)) in
83 let (metasenv, subst), t =
85 ~unify:(fun m s c t1 t2 ->
86 try Some (NCicUnification.unify status m s c t1 t2)
88 | NCicUnification.UnificationFailure _
89 | NCicUnification.Uncertain _ -> None)
90 metasenv subst source 0 lc t
92 let status = status#set_obj (u, d, metasenv, subst, o) in
93 status, (destination, t)
95 let relocate a b c = wrap (relocate a b) c;;
97 let term_of_cic_term s t c =
98 let s, (_,t) = relocate s c t in
102 let ppterm status t =
103 let uri,height,metasenv,subst,obj = status#obj in
105 NCicPp.ppterm ~metasenv ~subst ~context t
108 let disambiguate status t ty context =
111 | None -> status, None
113 let status, (_,x) = relocate status context ty in status, Some x
115 let uri,height,metasenv,subst,obj = status#obj in
116 let metasenv, subst, status, t =
117 GrafiteDisambiguate.disambiguate_nterm expty status context metasenv subst t
119 let new_pstatus = uri,height,metasenv,subst,obj in
120 status#set_obj new_pstatus, (context, t)
122 let disambiguate a b c d = wrap (disambiguate a b c) d;;
124 let typeof status ctx t =
125 let status, (_,t) = relocate status ctx t in
126 let _,_,metasenv,subst,_ = status#obj in
127 let ty = NCicTypeChecker.typeof ~subst ~metasenv ctx t in
130 let typeof a b c = wrap (typeof a b) c;;
132 let whd status ?delta ctx t =
133 let status, (_,t) = relocate status ctx t in
134 let _,_,_,subst,_ = status#obj in
135 let t = NCicReduction.whd ~subst ?delta ctx t in
139 let normalize status ?delta ctx t =
140 let status, (_,t) = relocate status ctx t in
141 let _,_,_,subst,_ = status#obj in
142 let t = NCicTacReduction.normalize ~subst ?delta ctx t in
146 let unify status ctx a b =
147 let status, (_,a) = relocate status ctx a in
148 let status, (_,b) = relocate status ctx b in
149 let n,h,metasenv,subst,o = status#obj in
150 let metasenv, subst = NCicUnification.unify status metasenv subst ctx a b in
151 status#set_obj (n,h,metasenv,subst,o)
153 let unify a b c d = wrap (unify a b c) d;;
155 let fix_sorts (ctx,t) =
157 let t = NCicUnification.fix_sorts t in
163 let refine status ctx term expty =
164 let status, (_,term) = relocate status ctx term in
169 let status, (_, e) = relocate status ctx e in status, Some e
171 let name,height,metasenv,subst,obj = status#obj in
172 let metasenv,subst,t,ty =
173 NCicRefiner.typeof status metasenv subst ctx term expty
175 status#set_obj (name,height,metasenv,subst,obj), (ctx,t), (ctx,ty)
177 let refine a b c d = wrap (refine a b c) d;;
179 let get_goalty status g =
180 let _,_,metasenv,_,_ = status#obj in
181 let _, a, b = List.assoc g metasenv in
185 let instantiate status i t =
186 let _,_,metasenv,_,_ = status#obj in
187 let gname, context, gty = List.assoc i metasenv in
188 let status, (_,t), (_,ty) =
189 refine status context t (Some (context,gty))
192 let name,height,metasenv,subst,obj = status#obj in
193 let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
194 let subst = (i, (gname, context, t, ty)) :: subst in
195 status#set_obj (name,height,metasenv,subst,obj)
198 let mk_meta status ?(attrs=[]) ctx bo_or_ty =
201 let status, (_,ty) = relocate status ctx ty in
202 let n,h,metasenv,subst,o = status#obj in
203 let metasenv, _, instance, _ =
204 NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty)
206 let status = status#set_obj (n,h,metasenv,subst,o) in
207 status, (ctx,instance)
209 let status, (_,bo_ as bo) = relocate status ctx bo in
210 let status, (_,ty) = typeof status ctx bo in
211 let n,h,metasenv,subst,o = status#obj in
212 let metasenv, metano, instance, _ =
213 NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty) in
214 let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
215 let subst = (metano, (attrs, ctx, bo_, ty)) :: subst in
216 let status = status#set_obj (n,h,metasenv,subst,o) in
217 status, (ctx,instance)
220 let mk_in_scope status t =
221 mk_meta status ~attrs:[`InScope] (ctx_of t) (`Def t)
224 let mk_out_scope n status t =
225 mk_meta status ~attrs:[`OutScope n] (ctx_of t) (`Def t)
228 (* the following unification problem will be driven by
229 * select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
233 * where argsn = length args and the pattern matches t
235 * found is called on every selected term to map them
236 * postprocess is called on the entire term after selection
239 low_status ~found ~postprocess (context,term) (wanted,path)
241 let is_found status ctx t wanted =
242 (* we could lift wanted step-by-step *)
243 try true, unify status ctx (ctx, t) wanted
245 | Error (_, Some (NCicUnification.UnificationFailure _))
246 | Error (_, Some (NCicUnification.Uncertain _)) -> false, status
248 let match_term status ctx (wanted : cic_term) t =
249 let rec aux ctx (status,already_found) t =
250 let b, status = is_found status ctx t wanted in
252 let status , (_,t) = found status (ctx, t) in
255 let _,_,_,subst,_ = status#obj in
257 | NCic.Meta (i,lc) when List.mem_assoc i subst ->
258 let _,_,t,_ = NCicUtils.lookup_subst i subst in
259 aux ctx (status,already_found) t
260 | NCic.Meta _ -> (status,already_found),t
262 NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux
263 (status,already_found) t
265 aux ctx (status,false) t
267 let _,_,_,subst,_ = low_status#obj in
268 let rec select status ctx pat cic =
270 | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
272 let _,_,t,_ = NCicUtils.lookup_subst i subst in
273 NCicSubstitution.subst_meta lc t
275 select status ctx pat cic
276 | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
277 let status, t = select status ctx t1 t2 in
278 let status, s = select status ctx s1 s2 in
279 let ctx = (n, NCic.Def (s2,t2)) :: ctx in
280 let status, b = select status ctx b1 b2 in
281 status, NCic.LetIn (n,t,s,b)
282 | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
283 let status, s = select status ctx s1 s2 in
284 let ctx = (n, NCic.Decl s2) :: ctx in
285 let status, t = select status ctx t1 t2 in
286 status, NCic.Lambda (n,s,t)
287 | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
288 let status, s = select status ctx s1 s2 in
289 let ctx = (n, NCic.Decl s2) :: ctx in
290 let status, t = select status ctx t1 t2 in
291 status, NCic.Prod (n,s,t)
292 | NCic.Appl l1, NCic.Appl l2 ->
295 (fun (status,l) x y ->
296 let status, x = select status ctx x y in
300 status, NCic.Appl (List.rev l)
301 | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
302 let status, t = select status ctx t1 t2 in
303 let status, ot = select status ctx ot1 ot2 in
306 (fun (status,l) x y ->
307 let status, x = select status ctx x y in
311 status, NCic.Match (u,ot,t,List.rev pl)
312 | NCic.Implicit `Hole, t ->
315 let status', wanted = disambiguate status wanted None ctx in
316 let (status',found), t' = match_term status' ctx wanted t in
317 if found then status',t' else status,t
319 let (status,_),t = match_term status ctx (ctx,t) t in
321 | NCic.Implicit _, t -> status, t
323 fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
324 ~context:[] ~subst:[] pat ^ " against " ^
325 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
327 let status, term = select low_status context path term in
328 let term = (context, term) in
329 postprocess status term
332 let analyse_indty status ty =
333 let status, reduct = whd status (ctx_of ty) ty in
336 | _,NCic.Const ref -> ref, []
337 | _,NCic.Appl (NCic.Const ref :: args) -> ref, args
338 | _,_ -> fail (lazy ("not an inductive type")) in
339 let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
340 let _,_,_,cl = List.nth tl i in
341 let consno = List.length cl in
342 let left, right = HExtlib.split_nth lno args in
343 status, (ref, consno, left, right)
346 let mk_cic_term c t = c,t ;;
348 let apply_subst status ctx t =
349 let status, (_,t) = relocate status ctx t in
350 let _,_,_,subst,_ = status#obj in
351 status, (ctx, NCicUntrusted.apply_subst subst ctx t)
354 (* ============= move this elsewhere ====================*)
356 class ['stack] status =
357 fun (o: NCic.obj) (s: 'stack) ->
362 method set_stack s = {< stack = s >}
365 class type lowtac_status = [unit] status
367 type 'status lowtactic = #lowtac_status as 'status -> int -> 'status
369 class type tac_status = [Continuationals.Stack.t] status
371 type 'status tactic = #tac_status as 'status -> 'status