]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nTacStatus.ml
generalized is half-implemented (still broken)
[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 mk_in_scope status t = 
166   mk_meta status ~name:NCicMetaSubst.in_scope_tag (ctx_of t) (`Def t)
167 ;;
168
169 let mk_out_scope n status t = 
170   mk_meta status ~name:(NCicMetaSubst.out_scope_tag n) (ctx_of t) (`Def t)
171 ;;
172
173 (* the following unification problem will be driven by 
174  *   select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
175  *
176  *   ? args = t
177  *
178  *   where argsn = length args and the pattern matches t
179  *
180  *  found is called on every selected term to map them
181  *  postprocess is called on the entire term after selection
182  *)
183 let select_term 
184   low_status ~found ~postprocess (name,context,term) (wanted,path) 
185 =
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
189     with 
190     | NCicUnification.UnificationFailure _ 
191     | NCicUnification.Uncertain _ -> false, status
192   in
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
196       if b then
197          let status , (_,_,t) = found status (None, ctx, t) in 
198          status, t
199       else
200         let _,_,_,subst,_ = status.pstatus in
201         match t with
202         | NCic.Meta (i,lc) when List.mem_assoc i subst ->
203             let _,_,t,_ = NCicUtils.lookup_subst i subst in
204             aux ctx status t
205         | NCic.Meta _ -> status, t
206         | _ ->      
207             NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux status t
208      in 
209        aux ctx status t
210   in 
211   let _,_,_,subst,_ = low_status.pstatus in
212   let rec select status ctx pat cic = 
213     match pat, cic with
214     | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
215         let cic = 
216           let _,_,t,_ = NCicUtils.lookup_subst i subst in
217           NCicSubstitution.subst_meta lc t
218         in
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 ->
237         let status, l = 
238            List.fold_left2
239              (fun (status,l) x y -> 
240               let status, x = select status ctx x y in
241               status, x::l)
242              (status,[]) l1 l2
243         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
248         let status, pl = 
249            List.fold_left2
250              (fun (status,l) x y -> 
251               let status, x = select status ctx x y in
252               status, x::l)
253              (status,[]) pl1 pl2
254         in
255         status, NCic.Match (u,ot,t,List.rev pl)
256     | NCic.Implicit `Hole, t -> 
257         (match wanted with
258         | Some wanted -> 
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
263     | _,t -> 
264         fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
265           ~context:[] ~subst:[] pat ^ " against " ^ 
266           NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
267   in
268   let status, term = select low_status context path term in
269   let term = (name, context, term) in
270   postprocess status term
271 ;;
272
273 let analyse_indty status ty = 
274  let ref, args =
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
284 ;;
285
286 let mk_cic_term c t = None,c,t ;;