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 context (name,ctx,t as term) =
48 let rec aux = function
50 | x::xs, y::ys -> x=y && aux (xs,ys)
53 aux (List.rev l1, List.rev l2)
55 if ctx == context then term else
56 if ctx = context then term else
57 if is_prefix ctx context then
59 NCicSubstitution.lift (List.length context - List.length ctx) t)
64 let term_of_cic_term t c =
65 let _,_,t = relocate c t in
69 type ast_term = string * int * CicNotationPt.term
71 let disambiguate (status : lowtac_status) (t : ast_term)
72 (ty : cic_term option) context =
73 let uri,height,metasenv,subst,obj = status.pstatus in
76 | None -> None | Some ty -> let _,_,x = relocate context ty in Some x
78 let metasenv, subst, lexicon_status, t =
79 GrafiteDisambiguate.disambiguate_nterm expty
80 status.lstatus context metasenv subst t
82 let new_pstatus = uri,height,metasenv,subst,obj in
83 { lstatus = lexicon_status; pstatus = new_pstatus }, (None, context, t)
86 let typeof status ctx t =
87 let _,_,metasenv,subst,_ = status.pstatus in
88 let _,_,t = relocate ctx t in
89 let ty = NCicTypeChecker.typeof ~subst ~metasenv ctx t in
93 let whd status ?delta ctx t =
94 let _,_,metasenv,subst,_ = status.pstatus in
95 let name,_,t = relocate ctx t in
96 let t = NCicReduction.whd ~subst ?delta ctx t in
100 let unify status ctx a b =
101 let n,h,metasenv,subst,o = status.pstatus in
102 let _,_,a = relocate ctx a in
103 let _,_,b = relocate ctx b in
104 let metasenv, subst =
105 NCicUnification.unify (NCicUnifHint.db ()) metasenv subst ctx a b
107 { status with pstatus = n,h,metasenv,subst,o }
110 let refine status ctx term expty =
111 let nt,_,term = relocate ctx term in
113 match expty with None -> None, None
114 | Some e -> let n,_, e = relocate ctx e in n, Some e
116 let name,height,metasenv,subst,obj = status.pstatus in
117 let db = NCicUnifHint.db () in (* XXX fixme *)
118 let coercion_db = NCicCoercion.db () in
119 let look_for_coercion = NCicCoercion.look_for_coercion coercion_db in
120 let metasenv, subst, t, ty =
121 NCicRefiner.typeof db ~look_for_coercion metasenv subst ctx term expty
123 { status with pstatus = (name,height,metasenv,subst,obj) },
124 (nt,ctx,t), (ne,ctx,ty)
127 let get_goalty (status : lowtac_status) (g : int) =
128 let _,_,metasenv,_,_ = status.pstatus in
129 List.assoc g metasenv
132 let instantiate status i t =
133 let (gname, context, _ as gty) = get_goalty status i in
134 let status, (_,_,t), (_,_,ty) =
135 refine status (ctx_of gty) t (Some gty)
138 let name,height,metasenv,subst,obj = status.pstatus in
139 let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
140 let subst = (i, (gname, context, t, ty)) :: subst in
141 { status with pstatus = (name,height,metasenv,subst,obj) }
144 let mk_meta status ?name ctx bo_or_ty =
145 let n,h,metasenv,subst,o = status.pstatus in
148 let _,_,ty = relocate ctx ty in
149 let metasenv, _, instance, _ =
150 NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty)
152 let status = { status with pstatus = n,h,metasenv,subst,o } in
153 status, (None,ctx,instance)
155 let _,_,bo_ as bo = relocate ctx bo in
156 let _,_,ty = typeof status ctx bo in
157 let metasenv, metano, instance, _ =
158 NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty) in
159 let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
160 let subst = (metano, (name, ctx, bo_, ty)) :: subst in
161 let status = { status with pstatus = n,h,metasenv,subst,o } in
162 status, (None,ctx,instance)
165 let mk_in_scope status t =
166 mk_meta status ~name:NCicMetaSubst.in_scope_tag (ctx_of t) (`Def t)
169 let mk_out_scope n status t =
170 mk_meta status ~name:(NCicMetaSubst.out_scope_tag n) (ctx_of t) (`Def t)
173 (* the following unification problem will be driven by
174 * select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
178 * where argsn = length args and the pattern matches t
180 * found is called on every selected term to map them
181 * postprocess is called on the entire term after selection
184 low_status ~found ~postprocess (name,context,term) (wanted,path)
186 let is_found status ctx t wanted =
187 (* we could lift wanted step-by-step *)
188 try true, unify status ctx (None, ctx, t) wanted
190 | NCicUnification.UnificationFailure _
191 | NCicUnification.Uncertain _ -> false, status
193 let match_term status ctx (wanted : cic_term) t =
194 let rec aux ctx status t =
195 let b, status = is_found status ctx t wanted in
197 let status , (_,_,t) = found status (None, ctx, t) in
200 let _,_,_,subst,_ = status.pstatus in
202 | NCic.Meta (i,lc) when List.mem_assoc i subst ->
203 let _,_,t,_ = NCicUtils.lookup_subst i subst in
205 | NCic.Meta _ -> status, t
207 NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux status t
211 let _,_,_,subst,_ = low_status.pstatus in
212 let rec select status ctx pat cic =
214 | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
216 let _,_,t,_ = NCicUtils.lookup_subst i subst in
217 NCicSubstitution.subst_meta lc t
219 select status ctx pat cic
220 | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
221 let status, t = select status ctx t1 t2 in
222 let status, s = select status ctx s1 s2 in
223 let ctx = (n, NCic.Def (s2,t2)) :: ctx in
224 let status, b = select status ctx b1 b2 in
225 status, NCic.LetIn (n,t,s,b)
226 | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
227 let status, s = select status ctx s1 s2 in
228 let ctx = (n, NCic.Decl s2) :: ctx in
229 let status, t = select status ctx t1 t2 in
230 status, NCic.Lambda (n,s,t)
231 | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
232 let status, s = select status ctx s1 s2 in
233 let ctx = (n, NCic.Decl s2) :: ctx in
234 let status, t = select status ctx t1 t2 in
235 status, NCic.Prod (n,s,t)
236 | NCic.Appl l1, NCic.Appl l2 ->
239 (fun (status,l) x y ->
240 let status, x = select status ctx x y in
244 status, NCic.Appl (List.rev l)
245 | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
246 let status, t = select status ctx t1 t2 in
247 let status, ot = select status ctx ot1 ot2 in
250 (fun (status,l) x y ->
251 let status, x = select status ctx x y in
255 status, NCic.Match (u,ot,t,List.rev pl)
256 | NCic.Implicit `Hole, t ->
259 let status, wanted = disambiguate status wanted None ctx in
260 match_term status ctx wanted t
261 | None -> match_term status ctx (None,ctx,t) t)
262 | NCic.Implicit _, t -> status, t
264 fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
265 ~context:[] ~subst:[] pat ^ " against " ^
266 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
268 let status, term = select low_status context path term in
269 let term = (name, context, term) in
270 postprocess status term
273 let analyse_indty status ty =
275 match whd status (ctx_of ty) ty with
276 | _,_,NCic.Const ref -> ref, []
277 | _,_,NCic.Appl (NCic.Const ref :: args) -> ref, args
278 | _,_,_ -> fail (lazy ("not an inductive type")) in
279 let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
280 let _,_,_,cl = List.nth tl i in
281 let consno = List.length cl in
282 let left, right = HExtlib.split_nth lno args in
283 ref, consno, left, right
286 let mk_cic_term c t = None,c,t ;;