]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nTacStatus.ml
New file nTacStatus to:
[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
65 type ast_term = string * int * CicNotationPt.term
66
67 let disambiguate (status : lowtac_status) (t : ast_term)  
68                  (ty : cic_term option) context =
69  let uri,height,metasenv,subst,obj = status.pstatus in
70  let expty = 
71    match ty with 
72    | None -> None | Some ty -> let _,_,x = relocate context ty in Some x 
73  in
74  let metasenv, subst, lexicon_status, t = 
75    GrafiteDisambiguate.disambiguate_nterm expty
76     status.lstatus context metasenv subst t 
77  in
78  let new_pstatus = uri,height,metasenv,subst,obj in
79  { lstatus = lexicon_status; pstatus = new_pstatus }, (None, context, t) 
80 ;;
81
82 let typeof status ctx t =
83   let _,_,metasenv,subst,_ = status.pstatus in
84   let _,_,t = relocate ctx t in
85   let ty = NCicTypeChecker.typeof ~subst ~metasenv ctx t in
86   None, ctx, ty
87 ;;
88   
89 let whd status ?delta ctx t =
90   let _,_,metasenv,subst,_ = status.pstatus in
91   let name,_,t = relocate ctx t in
92   let t = NCicReduction.whd ~subst ?delta ctx t in
93   name, ctx, t
94 ;;
95   
96 let unify status ctx a b =
97   let n,h,metasenv,subst,o = status.pstatus in
98   let _,_,a = relocate ctx a in
99   let _,_,b = relocate ctx b in
100   let metasenv, subst = 
101     NCicUnification.unify (NCicUnifHint.db ()) metasenv subst ctx a b
102   in
103   { status with pstatus = n,h,metasenv,subst,o }
104 ;;
105
106 let refine status ctx term expty =
107   let nt,_,term = relocate ctx term in
108   let ne, expty = 
109     match expty with None -> None, None 
110     | Some e -> let n,_, e = relocate ctx e in n, Some e
111   in
112   let name,height,metasenv,subst,obj = status.pstatus in
113   let db = NCicUnifHint.db () in (* XXX fixme *)
114   let coercion_db = NCicCoercion.db () in
115   let look_for_coercion = NCicCoercion.look_for_coercion coercion_db in
116   let metasenv, subst, t, ty = 
117    NCicRefiner.typeof db ~look_for_coercion metasenv subst ctx term expty
118   in
119   { status with pstatus = (name,height,metasenv,subst,obj) }, 
120   (nt,ctx,t), (ne,ctx,ty)
121 ;;
122
123 let get_goalty (status : lowtac_status) (g : int) =
124  let _,_,metasenv,_,_ = status.pstatus in
125  List.assoc g metasenv
126 ;;
127
128 let instantiate status i t =
129  let (gname, context, _ as gty) = get_goalty status i in
130  let status, (_,_,t), (_,_,ty) = 
131    refine status (ctx_of gty) t (Some gty) 
132  in
133
134  let name,height,metasenv,subst,obj = status.pstatus in
135  let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
136  let subst = (i, (gname, context, t, ty)) :: subst in
137  { status with pstatus = (name,height,metasenv,subst,obj) }
138 ;;
139
140 let mk_meta status ?name ctx bo_or_ty =
141   let n,h,metasenv,subst,o = status.pstatus in
142   match bo_or_ty with
143   | `Decl ty ->
144       let _,_,ty = relocate ctx ty in
145       let metasenv, _, instance, _ = 
146         NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty)
147       in
148       let status = { status with pstatus = n,h,metasenv,subst,o } in
149       status, (None,ctx,instance)
150   | `Def bo ->
151       let _,_,bo_ as bo = relocate ctx bo in
152       let _,_,ty = typeof status ctx bo in
153       let metasenv, metano, instance, _ = 
154         NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty) in
155       let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
156       let subst = (metano, (name, ctx, bo_, ty)) :: subst in
157       let status = { status with pstatus = n,h,metasenv,subst,o } in
158       status, (None,ctx,instance)
159 ;;
160
161 let in_scope_tag = "tag:in_scope" ;;
162 let out_scope_tag = "tag:out_scope" ;;
163
164 let select_term low_status (name,context,term) (wanted,path) =
165   let found status ctx t wanted =
166     (* we could lift wanted step-by-step *)
167     try true, unify status ctx (None, ctx, t) wanted
168     with 
169     | NCicUnification.UnificationFailure _ 
170     | NCicUnification.Uncertain _ -> false, status
171   in
172   let match_term status ctx (wanted : cic_term) t =
173     let rec aux ctx status t =
174       let b, status = found status ctx t wanted in
175       if b then 
176         let status, (_,_,t) = 
177           mk_meta status ~name:in_scope_tag ctx (`Def (None, ctx, t))
178         in    
179         status, t
180       else NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux status t
181      in 
182        aux ctx status t
183   in 
184   let rec select status ctx pat cic = 
185     match pat, cic with
186     | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
187         let status, t = select status ctx t1 t2 in
188         let status, s = select status ctx s1 s2 in
189         let ctx = (n, NCic.Def (s2,t2)) :: ctx in
190         let status, b = select status ctx b1 b2 in
191         status, NCic.LetIn (n,t,s,b)
192     | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
193         let status, s = select status ctx s1 s2 in
194         let ctx = (n, NCic.Decl s2) :: ctx in
195         let status, t = select status ctx t1 t2 in
196         status, NCic.Lambda (n,s,t)
197     | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
198         let status, s = select status ctx s1 s2 in
199         let ctx = (n, NCic.Decl s2) :: ctx in
200         let status, t = select status ctx t1 t2 in
201         status, NCic.Prod (n,s,t)
202     | NCic.Appl l1, NCic.Appl l2 ->
203         let status, l = 
204            List.fold_left2
205              (fun (status,l) x y -> 
206               let status, x = select status ctx x y in
207               status, x::l)
208              (status,[]) l1 l2
209         in
210         status, NCic.Appl (List.rev l)
211     | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
212         let status, t = select status ctx t1 t2 in
213         let status, ot = select status ctx ot1 ot2 in
214         let status, pl = 
215            List.fold_left2
216              (fun (status,l) x y -> 
217               let status, x = select status ctx x y in
218               status, x::l)
219              (status,[]) pl1 pl2
220         in
221         status, NCic.Match (u,ot,t,List.rev pl)
222     | NCic.Implicit `Hole, t -> 
223         (match wanted with
224         | Some wanted -> 
225              let status, wanted = disambiguate status wanted None ctx in
226              match_term status ctx wanted t
227         | None -> match_term status ctx (None,ctx,t) t)
228     | NCic.Implicit _, t -> status, t
229     | _,t -> 
230         fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
231           ~context:[] ~subst:[] pat))
232   in
233   let status, term = select low_status context path term in
234   let term = (name, context, term) in
235   mk_meta status ~name:out_scope_tag context (`Def term)
236 ;;
237
238 let analyse_indty status ty = 
239  let ref, args =
240   match whd status (ctx_of ty) ty with
241    | _,_,NCic.Const ref -> ref, []
242    | _,_,NCic.Appl (NCic.Const ref :: args) -> ref, args
243    | _,_,_ -> fail (lazy ("not an inductive type")) in
244  let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
245  let _,_,_,cl = List.nth tl i in
246  let consno = List.length cl in
247  let left, right = HExtlib.split_nth lno args in
248  ref, consno, left, right
249 ;;
250
251 let mk_cic_term c t = None,c,t ;;