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
15 let fail msg = raise (Error msg)
17 type lowtac_status = {
19 lstatus : LexiconEngine.status
22 type lowtactic = lowtac_status -> int -> lowtac_status
25 gstatus : Continuationals.Stack.t;
26 istatus : lowtac_status;
29 type tactic = tac_status -> tac_status
31 type tactic_term = CicNotationPt.term Disambiguate.disambiguator_input
32 type tactic_pattern = GrafiteAst.npattern Disambiguate.disambiguator_input
34 let pp_tac_status status =
35 prerr_endline (NCicPp.ppobj status.istatus.pstatus)
38 let pp_lowtac_status status =
39 prerr_endline "--------------------------------------------";
40 prerr_endline (NCicPp.ppobj status.pstatus)
43 type cic_term = NCic.conjecture (* name, context, term *)
44 let ctx_of (_,c,_) = c ;;
46 let relocate status destination (name,source,t as orig) =
47 if source == destination then status, orig else
48 let u, d, metasenv, subst, o = status.pstatus 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 db = NCicUnifHint.db () in (* XXX fixme *)
84 let (metasenv, subst), t =
86 ~unify:(fun m s c t1 t2 ->
87 try Some (NCicUnification.unify db m s c t1 t2)
89 | NCicUnification.UnificationFailure _
90 | NCicUnification.Uncertain _ -> None)
91 metasenv subst source 0 lc t
93 let status = { status with pstatus = u, d, metasenv, subst, o } in
94 status, (name, destination, t)
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.pstatus in
104 let _,context,t = t 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.pstatus in
116 let metasenv, subst, lexicon_status, t =
117 GrafiteDisambiguate.disambiguate_nterm expty
118 status.lstatus context metasenv subst t
120 let new_pstatus = uri,height,metasenv,subst,obj in
121 { lstatus = lexicon_status; pstatus = new_pstatus }, (None, context, t)
124 let typeof status ctx t =
125 let status, (_,_,t) = relocate status ctx t in
126 let _,_,metasenv,subst,_ = status.pstatus in
127 let ty = NCicTypeChecker.typeof ~subst ~metasenv ctx t in
128 status, (None, ctx, ty)
131 let whd status ?delta ctx t =
132 let status, (name,_,t) = relocate status ctx t in
133 let _,_,_,subst,_ = status.pstatus in
134 let t = NCicReduction.whd ~subst ?delta ctx t in
135 status, (name, ctx, t)
138 let unify status ctx a b =
139 let status, (_,_,a) = relocate status ctx a in
140 let status, (_,_,b) = relocate status ctx b in
141 let n,h,metasenv,subst,o = status.pstatus in
142 let metasenv, subst =
143 NCicUnification.unify (NCicUnifHint.db ()) metasenv subst ctx a b
145 { status with pstatus = n,h,metasenv,subst,o }
148 let refine status ctx term expty =
149 let status, (nt,_,term) = relocate status ctx term in
150 let status, ne, expty =
151 match expty with None -> status, None, None
153 let status, (n,_, e) = relocate status ctx e in status, n, Some e
155 let name,height,metasenv,subst,obj = status.pstatus in
156 let db = NCicUnifHint.db () in (* XXX fixme *)
157 let coercion_db = NCicCoercion.db () in
158 let look_for_coercion = NCicCoercion.look_for_coercion coercion_db in
159 let metasenv, subst, t, ty =
160 NCicRefiner.typeof db ~look_for_coercion metasenv subst ctx term expty
162 { status with pstatus = name,height,metasenv,subst,obj },
163 (nt,ctx,t), (ne,ctx,ty)
166 let get_goalty (status : lowtac_status) (g : int) =
167 let _,_,metasenv,_,_ = status.pstatus in
168 List.assoc g metasenv
171 let instantiate status i t =
172 let (gname, context, _ as gty) = get_goalty status i in
173 let status, (_,_,t), (_,_,ty) =
174 refine status (ctx_of gty) t (Some gty)
177 let name,height,metasenv,subst,obj = status.pstatus in
178 let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
179 let subst = (i, (gname, context, t, ty)) :: subst in
180 { status with pstatus = (name,height,metasenv,subst,obj) }
183 let mk_meta status ?name ctx bo_or_ty =
186 let status, (_,_,ty) = relocate status ctx ty in
187 let n,h,metasenv,subst,o = status.pstatus in
188 let metasenv, _, instance, _ =
189 NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty)
191 let status = { status with pstatus = n,h,metasenv,subst,o } in
192 status, (None,ctx,instance)
194 let status, (_,_,bo_ as bo) = relocate status ctx bo in
195 let status, (_,_,ty) = typeof status ctx bo in
196 let n,h,metasenv,subst,o = status.pstatus in
197 let metasenv, metano, instance, _ =
198 NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty) in
199 let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
200 let subst = (metano, (name, ctx, bo_, ty)) :: subst in
201 let status = { status with pstatus = n,h,metasenv,subst,o } in
202 status, (None,ctx,instance)
205 let mk_in_scope status t =
206 mk_meta status ~name:NCicMetaSubst.in_scope_tag (ctx_of t) (`Def t)
209 let mk_out_scope n status t =
210 mk_meta status ~name:(NCicMetaSubst.out_scope_tag n) (ctx_of t) (`Def t)
213 (* the following unification problem will be driven by
214 * select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
218 * where argsn = length args and the pattern matches t
220 * found is called on every selected term to map them
221 * postprocess is called on the entire term after selection
224 low_status ~found ~postprocess (name,context,term) (wanted,path)
226 let is_found status ctx t wanted =
227 (* we could lift wanted step-by-step *)
228 try true, unify status ctx (None, ctx, t) wanted
230 | NCicUnification.UnificationFailure _
231 | NCicUnification.Uncertain _ -> false, status
233 let match_term status ctx (wanted : cic_term) t =
234 let rec aux ctx status t =
235 let b, status = is_found status ctx t wanted in
237 let status , (_,_,t) = found status (None, ctx, t) in
240 let _,_,_,subst,_ = status.pstatus in
242 | NCic.Meta (i,lc) when List.mem_assoc i subst ->
243 let _,_,t,_ = NCicUtils.lookup_subst i subst in
245 | NCic.Meta _ -> status, t
247 NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux status t
251 let _,_,_,subst,_ = low_status.pstatus in
252 let rec select status ctx pat cic =
254 | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
256 let _,_,t,_ = NCicUtils.lookup_subst i subst in
257 NCicSubstitution.subst_meta lc t
259 select status ctx pat cic
260 | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
261 let status, t = select status ctx t1 t2 in
262 let status, s = select status ctx s1 s2 in
263 let ctx = (n, NCic.Def (s2,t2)) :: ctx in
264 let status, b = select status ctx b1 b2 in
265 status, NCic.LetIn (n,t,s,b)
266 | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
267 let status, s = select status ctx s1 s2 in
268 let ctx = (n, NCic.Decl s2) :: ctx in
269 let status, t = select status ctx t1 t2 in
270 status, NCic.Lambda (n,s,t)
271 | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
272 let status, s = select status ctx s1 s2 in
273 let ctx = (n, NCic.Decl s2) :: ctx in
274 let status, t = select status ctx t1 t2 in
275 status, NCic.Prod (n,s,t)
276 | NCic.Appl l1, NCic.Appl l2 ->
279 (fun (status,l) x y ->
280 let status, x = select status ctx x y in
284 status, NCic.Appl (List.rev l)
285 | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
286 let status, t = select status ctx t1 t2 in
287 let status, ot = select status ctx ot1 ot2 in
290 (fun (status,l) x y ->
291 let status, x = select status ctx x y in
295 status, NCic.Match (u,ot,t,List.rev pl)
296 | NCic.Implicit `Hole, t ->
299 let status, wanted = disambiguate status wanted None ctx in
300 match_term status ctx wanted t
301 | None -> match_term status ctx (None,ctx,t) t)
302 | NCic.Implicit _, t -> status, t
304 fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
305 ~context:[] ~subst:[] pat ^ " against " ^
306 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
308 let status, term = select low_status context path term in
309 let term = (name, context, term) in
310 postprocess status term
313 let analyse_indty status ty =
314 let status, reduct = whd status (ctx_of ty) ty in
317 | _,_,NCic.Const ref -> ref, []
318 | _,_,NCic.Appl (NCic.Const ref :: args) -> ref, args
319 | _,_,_ -> fail (lazy ("not an inductive type")) in
320 let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
321 let _,_,_,cl = List.nth tl i in
322 let consno = List.length cl in
323 let left, right = HExtlib.split_nth lno args in
324 status, (ref, consno, left, right)
327 let mk_cic_term c t = None,c,t ;;
329 (* CSC: next two functions to be moved elsewhere *)
330 let rec apply_subst subst ctx =
334 let _,_,t,_ = NCicUtils.lookup_subst i subst in
335 let t = NCicSubstitution.subst_meta lc t in
336 apply_subst subst ctx t
340 _,NCic.Irl _ -> NCic.Meta (i,lc)
345 apply_subst subst ctx (NCicSubstitution.lift n t)) l))))
346 | t -> NCicUtils.map (fun item ctx -> item::ctx) ctx (apply_subst subst) t
349 let apply_subst_obj subst =
351 NCic.Constant (relev,name,bo,ty,attrs) ->
353 (relev,name,HExtlib.map_option (apply_subst subst []) bo,
354 apply_subst subst [] ty,attrs)
355 | NCic.Fixpoint (ind,fl,attrs) ->
358 (function (relevance,name,recno,ty,bo) ->
359 relevance,name,recno,apply_subst subst [] ty,apply_subst subst [] bo
362 NCic.Fixpoint (ind,fl,attrs)
363 | _ -> assert false (* not implemented yet *)
366 let apply_subst status ctx t =
367 let status, (name,_,t) = relocate status ctx t in
368 let _,_,_,subst,_ = status.pstatus in
369 status, (name, ctx, apply_subst subst ctx t)