]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nTacStatus.ml
tactic cases works! delift clears tags
[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 context (name,ctx,t as term) =
47   let is_prefix l1 l2 =
48     let rec aux = function
49       | [],[] -> true
50       | x::xs, y::ys -> x=y && aux (xs,ys)
51       | _ -> false
52     in
53       aux (List.rev l1, List.rev l2)
54   in
55   if ctx == context then term else 
56   if ctx = context then term else 
57   if is_prefix ctx context then 
58     (name, context, 
59       NCicSubstitution.lift (List.length context - List.length ctx) t)
60   else
61     assert false
62 ;;
63
64 let term_of_cic_term t c = 
65   let _,_,t = relocate c t in
66   t
67 ;;
68
69 type ast_term = string * int * CicNotationPt.term
70
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
74  let expty = 
75    match ty with 
76    | None -> None | Some ty -> let _,_,x = relocate context ty in Some x 
77  in
78  let metasenv, subst, lexicon_status, t = 
79    GrafiteDisambiguate.disambiguate_nterm expty
80     status.lstatus context metasenv subst t 
81  in
82  let new_pstatus = uri,height,metasenv,subst,obj in
83  { lstatus = lexicon_status; pstatus = new_pstatus }, (None, context, t) 
84 ;;
85
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
90   None, ctx, ty
91 ;;
92   
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
97   name, ctx, t
98 ;;
99   
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
106   in
107   { status with pstatus = n,h,metasenv,subst,o }
108 ;;
109
110 let refine status ctx term expty =
111   let nt,_,term = relocate ctx term in
112   let ne, expty = 
113     match expty with None -> None, None 
114     | Some e -> let n,_, e = relocate ctx e in n, Some e
115   in
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
122   in
123   { status with pstatus = (name,height,metasenv,subst,obj) }, 
124   (nt,ctx,t), (ne,ctx,ty)
125 ;;
126
127 let get_goalty (status : lowtac_status) (g : int) =
128  let _,_,metasenv,_,_ = status.pstatus in
129  List.assoc g metasenv
130 ;;
131
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) 
136  in
137
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) }
142 ;;
143
144 let mk_meta status ?name ctx bo_or_ty =
145   let n,h,metasenv,subst,o = status.pstatus in
146   match bo_or_ty with
147   | `Decl ty ->
148       let _,_,ty = relocate ctx ty in
149       let metasenv, _, instance, _ = 
150         NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty)
151       in
152       let status = { status with pstatus = n,h,metasenv,subst,o } in
153       status, (None,ctx,instance)
154   | `Def bo ->
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)
163 ;;
164
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
169     with 
170     | NCicUnification.UnificationFailure _ 
171     | NCicUnification.Uncertain _ -> false, status
172   in
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
176       if b then 
177         let status, (_,_,t) = 
178           mk_meta status ~name:NCicMetaSubst.in_scope_tag 
179             ctx (`Def (None, ctx, t))
180         in    
181         status, t
182       else NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux status t
183      in 
184        aux ctx status t
185   in 
186   let rec select status ctx pat cic = 
187     match pat, cic with
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 ->
205         let status, l = 
206            List.fold_left2
207              (fun (status,l) x y -> 
208               let status, x = select status ctx x y in
209               status, x::l)
210              (status,[]) l1 l2
211         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
216         let status, pl = 
217            List.fold_left2
218              (fun (status,l) x y -> 
219               let status, x = select status ctx x y in
220               status, x::l)
221              (status,[]) pl1 pl2
222         in
223         status, NCic.Match (u,ot,t,List.rev pl)
224     | NCic.Implicit `Hole, t -> 
225         (match wanted with
226         | Some wanted -> 
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
231     | _,t -> 
232         fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
233           ~context:[] ~subst:[] pat))
234   in
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)
238 ;;
239
240 let analyse_indty status ty = 
241  let ref, args =
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
251 ;;
252
253 let mk_cic_term c t = None,c,t ;;