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