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 select_term low_status (name,context,term) (wanted,path) =
166 let found status ctx t wanted =
167 (* we could lift wanted step-by-step *)
168 try true, unify status ctx (None, ctx, t) wanted
170 | NCicUnification.UnificationFailure _
171 | NCicUnification.Uncertain _ -> false, status
173 let match_term status ctx (wanted : cic_term) t =
174 let rec aux ctx status t =
175 let b, status = found status ctx t wanted in
177 let status, (_,_,t) =
178 mk_meta status ~name:NCicMetaSubst.in_scope_tag
179 ctx (`Def (None, ctx, t))
182 else NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux status t
186 let rec select status ctx pat cic =
188 | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
189 let status, t = select status ctx t1 t2 in
190 let status, s = select status ctx s1 s2 in
191 let ctx = (n, NCic.Def (s2,t2)) :: ctx in
192 let status, b = select status ctx b1 b2 in
193 status, NCic.LetIn (n,t,s,b)
194 | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
195 let status, s = select status ctx s1 s2 in
196 let ctx = (n, NCic.Decl s2) :: ctx in
197 let status, t = select status ctx t1 t2 in
198 status, NCic.Lambda (n,s,t)
199 | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
200 let status, s = select status ctx s1 s2 in
201 let ctx = (n, NCic.Decl s2) :: ctx in
202 let status, t = select status ctx t1 t2 in
203 status, NCic.Prod (n,s,t)
204 | NCic.Appl l1, NCic.Appl l2 ->
207 (fun (status,l) x y ->
208 let status, x = select status ctx x y in
212 status, NCic.Appl (List.rev l)
213 | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
214 let status, t = select status ctx t1 t2 in
215 let status, ot = select status ctx ot1 ot2 in
218 (fun (status,l) x y ->
219 let status, x = select status ctx x y in
223 status, NCic.Match (u,ot,t,List.rev pl)
224 | NCic.Implicit `Hole, t ->
227 let status, wanted = disambiguate status wanted None ctx in
228 match_term status ctx wanted t
229 | None -> match_term status ctx (None,ctx,t) t)
230 | NCic.Implicit _, t -> status, t
232 fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
233 ~context:[] ~subst:[] pat))
235 let status, term = select low_status context path term in
236 let term = (name, context, term) in
237 mk_meta status ~name:(NCicMetaSubst.out_scope_tag 1) context (`Def term)
240 let analyse_indty status ty =
242 match whd status (ctx_of ty) ty with
243 | _,_,NCic.Const ref -> ref, []
244 | _,_,NCic.Appl (NCic.Const ref :: args) -> ref, args
245 | _,_,_ -> fail (lazy ("not an inductive type")) in
246 let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
247 let _,_,_,cl = List.nth tl i in
248 let consno = List.length cl in
249 let left, right = HExtlib.split_nth lno args in
250 ref, consno, left, right
253 let mk_cic_term c t = None,c,t ;;