]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nTacStatus.ml
- change implemented in 4 lines
[helm.git] / helm / software / components / ng_tactics / nTacStatus.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
13
14 exception Error of string lazy_t
15 let fail msg = raise (Error msg)
16
17 type lowtac_status = {
18         pstatus : NCic.obj;
19         lstatus : LexiconEngine.status
20 }
21
22 type lowtactic = lowtac_status -> int -> lowtac_status 
23
24 type tac_status = {
25         gstatus : Continuationals.Stack.t; 
26         istatus : lowtac_status;
27
28
29 type tactic = tac_status -> tac_status
30
31 type tactic_term = CicNotationPt.term Disambiguate.disambiguator_input
32 type tactic_pattern = GrafiteAst.npattern Disambiguate.disambiguator_input
33
34 let pp_tac_status status = 
35   prerr_endline (NCicPp.ppobj status.istatus.pstatus)
36 ;;
37
38 let pp_lowtac_status status = 
39   prerr_endline "--------------------------------------------";
40   prerr_endline (NCicPp.ppobj status.pstatus)
41 ;;
42
43 type cic_term = NCic.conjecture (* name, context, term *)
44 let ctx_of (_,c,_) = c ;;
45
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 ->
51         if n1 = n2 && 
52            NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
53           NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
54         else
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 ->
58         if n1 = n2 && 
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)
62         else
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 ->
66         if n1 = n2 && 
67            NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
68           NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
69         else
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
73     | _::_, [] -> 
74           HExtlib.mk_list (NCic.Appl 
75             [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
76     | _ -> []
77   in
78   let lc = 
79     lcp [] (List.length destination) (List.length source) 
80       (List.rev destination, List.rev source)
81   in
82   let lc = (0,NCic.Ctx (List.rev lc)) in
83   let db = NCicUnifHint.db () in (* XXX fixme *)
84   let (metasenv, subst), t =
85     NCicMetaSubst.delift 
86        ~unify:(fun m s c t1 t2 -> 
87          try Some (NCicUnification.unify db m s c t1 t2)
88          with 
89           | NCicUnification.UnificationFailure _ 
90           | NCicUnification.Uncertain _ -> None) 
91      metasenv subst source 0 lc t
92   in
93   let status = { status with pstatus = u, d, metasenv, subst, o } in
94   status, (name, destination, t)
95 ;;
96
97 let term_of_cic_term s t c = 
98   let s, (_,_,t) = relocate s c t in
99   s, t
100 ;;
101
102 let disambiguate status t ty context =
103  let status, expty = 
104    match ty with 
105    | None -> status, None 
106    | Some ty -> 
107        let status, (_,_,x) = relocate status context ty in status, Some x 
108  in
109  let uri,height,metasenv,subst,obj = status.pstatus in
110  let metasenv, subst, lexicon_status, t = 
111    GrafiteDisambiguate.disambiguate_nterm expty
112     status.lstatus context metasenv subst t 
113  in
114  let new_pstatus = uri,height,metasenv,subst,obj in
115  { lstatus = lexicon_status; pstatus = new_pstatus }, (None, context, t) 
116 ;;
117
118 let typeof status ctx t =
119   let status, (_,_,t) = relocate status ctx t in
120   let _,_,metasenv,subst,_ = status.pstatus in
121   let ty = NCicTypeChecker.typeof ~subst ~metasenv ctx t in
122   status, (None, ctx, ty)
123 ;;
124   
125 let whd status ?delta ctx t =
126   let status, (name,_,t) = relocate status ctx t in
127   let _,_,_,subst,_ = status.pstatus in
128   let t = NCicReduction.whd ~subst ?delta ctx t in
129   status, (name, ctx, t)
130 ;;
131   
132 let unify status ctx a b =
133   let status, (_,_,a) = relocate status ctx a in
134   let status, (_,_,b) = relocate status ctx b in
135   let n,h,metasenv,subst,o = status.pstatus in
136   let metasenv, subst = 
137     NCicUnification.unify (NCicUnifHint.db ()) metasenv subst ctx a b
138   in
139   { status with pstatus = n,h,metasenv,subst,o }
140 ;;
141
142 let refine status ctx term expty =
143   let status, (nt,_,term) = relocate status ctx term in
144   let status, ne, expty = 
145     match expty with None -> status, None, None 
146     | Some e -> 
147         let status, (n,_, e) = relocate status ctx e in status, n, Some e
148   in
149   let name,height,metasenv,subst,obj = status.pstatus in
150   let db = NCicUnifHint.db () in (* XXX fixme *)
151   let coercion_db = NCicCoercion.db () in
152   let look_for_coercion = NCicCoercion.look_for_coercion coercion_db in
153   let metasenv, subst, t, ty = 
154    NCicRefiner.typeof db ~look_for_coercion metasenv subst ctx term expty
155   in
156   { status with pstatus = name,height,metasenv,subst,obj }, 
157   (nt,ctx,t), (ne,ctx,ty)
158 ;;
159
160 let get_goalty (status : lowtac_status) (g : int) =
161  let _,_,metasenv,_,_ = status.pstatus in
162  List.assoc g metasenv
163 ;;
164
165 let instantiate status i t =
166  let (gname, context, _ as gty) = get_goalty status i in
167  let status, (_,_,t), (_,_,ty) = 
168    refine status (ctx_of gty) t (Some gty) 
169  in
170
171  let name,height,metasenv,subst,obj = status.pstatus in
172  let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
173  let subst = (i, (gname, context, t, ty)) :: subst in
174  { status with pstatus = (name,height,metasenv,subst,obj) }
175 ;;
176
177 let mk_meta status ?name ctx bo_or_ty =
178   match bo_or_ty with
179   | `Decl ty ->
180       let status, (_,_,ty) = relocate status ctx ty in
181       let n,h,metasenv,subst,o = status.pstatus in
182       let metasenv, _, instance, _ = 
183         NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty)
184       in
185       let status = { status with pstatus = n,h,metasenv,subst,o } in
186       status, (None,ctx,instance)
187   | `Def bo ->
188       let status, (_,_,bo_ as bo) = relocate status ctx bo in
189       let status, (_,_,ty) = typeof status ctx bo in
190       let n,h,metasenv,subst,o = status.pstatus in
191       let metasenv, metano, instance, _ = 
192         NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty) in
193       let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
194       let subst = (metano, (name, ctx, bo_, ty)) :: subst in
195       let status = { status with pstatus = n,h,metasenv,subst,o } in
196       status, (None,ctx,instance)
197 ;;
198
199 let mk_in_scope status t = 
200   mk_meta status ~name:NCicMetaSubst.in_scope_tag (ctx_of t) (`Def t)
201 ;;
202
203 let mk_out_scope n status t = 
204   mk_meta status ~name:(NCicMetaSubst.out_scope_tag n) (ctx_of t) (`Def t)
205 ;;
206
207 (* the following unification problem will be driven by 
208  *   select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
209  *
210  *   ? args = t
211  *
212  *   where argsn = length args and the pattern matches t
213  *
214  *  found is called on every selected term to map them
215  *  postprocess is called on the entire term after selection
216  *)
217 let select_term 
218   low_status ~found ~postprocess (name,context,term) (wanted,path) 
219 =
220   let is_found status ctx t wanted =
221     (* we could lift wanted step-by-step *)
222     try true, unify status ctx (None, ctx, t) wanted
223     with 
224     | NCicUnification.UnificationFailure _ 
225     | NCicUnification.Uncertain _ -> false, status
226   in
227   let match_term status ctx (wanted : cic_term) t =
228     let rec aux ctx status t =
229       let b, status = is_found status ctx t wanted in
230       if b then
231          let status , (_,_,t) = found status (None, ctx, t) in 
232          status, t
233       else
234         let _,_,_,subst,_ = status.pstatus in
235         match t with
236         | NCic.Meta (i,lc) when List.mem_assoc i subst ->
237             let _,_,t,_ = NCicUtils.lookup_subst i subst in
238             aux ctx status t
239         | NCic.Meta _ -> status, t
240         | _ ->      
241             NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux status t
242      in 
243        aux ctx status t
244   in 
245   let _,_,_,subst,_ = low_status.pstatus in
246   let rec select status ctx pat cic = 
247     match pat, cic with
248     | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
249         let cic = 
250           let _,_,t,_ = NCicUtils.lookup_subst i subst in
251           NCicSubstitution.subst_meta lc t
252         in
253         select status ctx pat cic
254     | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
255         let status, t = select status ctx t1 t2 in
256         let status, s = select status ctx s1 s2 in
257         let ctx = (n, NCic.Def (s2,t2)) :: ctx in
258         let status, b = select status ctx b1 b2 in
259         status, NCic.LetIn (n,t,s,b)
260     | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
261         let status, s = select status ctx s1 s2 in
262         let ctx = (n, NCic.Decl s2) :: ctx in
263         let status, t = select status ctx t1 t2 in
264         status, NCic.Lambda (n,s,t)
265     | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
266         let status, s = select status ctx s1 s2 in
267         let ctx = (n, NCic.Decl s2) :: ctx in
268         let status, t = select status ctx t1 t2 in
269         status, NCic.Prod (n,s,t)
270     | NCic.Appl l1, NCic.Appl l2 ->
271         let status, l = 
272            List.fold_left2
273              (fun (status,l) x y -> 
274               let status, x = select status ctx x y in
275               status, x::l)
276              (status,[]) l1 l2
277         in
278         status, NCic.Appl (List.rev l)
279     | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
280         let status, t = select status ctx t1 t2 in
281         let status, ot = select status ctx ot1 ot2 in
282         let status, pl = 
283            List.fold_left2
284              (fun (status,l) x y -> 
285               let status, x = select status ctx x y in
286               status, x::l)
287              (status,[]) pl1 pl2
288         in
289         status, NCic.Match (u,ot,t,List.rev pl)
290     | NCic.Implicit `Hole, t -> 
291         (match wanted with
292         | Some wanted -> 
293              let status, wanted = disambiguate status wanted None ctx in
294              match_term status ctx wanted t
295         | None -> match_term status ctx (None,ctx,t) t)
296     | NCic.Implicit _, t -> status, t
297     | _,t -> 
298         fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
299           ~context:[] ~subst:[] pat ^ " against " ^ 
300           NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
301   in
302   let status, term = select low_status context path term in
303   let term = (name, context, term) in
304   postprocess status term
305 ;;
306
307 let analyse_indty status ty = 
308  let status, reduct = whd status (ctx_of ty) ty in
309  let ref, args =
310   match reduct with
311    | _,_,NCic.Const ref -> ref, []
312    | _,_,NCic.Appl (NCic.Const ref :: args) -> ref, args
313    | _,_,_ -> fail (lazy ("not an inductive type")) in
314  let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
315  let _,_,_,cl = List.nth tl i in
316  let consno = List.length cl in
317  let left, right = HExtlib.split_nth lno args in
318  status, (ref, consno, left, right)
319 ;;
320
321 let mk_cic_term c t = None,c,t ;;