]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nTacStatus.ml
Objects are now used to represent also the tactic status.
[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 class pstatus =
18  fun (o: NCic.obj) ->
19   object
20    inherit NEstatus.status
21    val obj = o
22    method obj = obj
23    method set_obj o = {< obj = o >}
24   end
25
26 type tactic_term = CicNotationPt.term Disambiguate.disambiguator_input
27 type tactic_pattern = GrafiteAst.npattern Disambiguate.disambiguator_input
28
29 let pp_tac_status status = 
30   prerr_endline (NCicPp.ppobj status#obj)
31 ;;
32
33 let pp_lowtac_status status = 
34   prerr_endline "--------------------------------------------";
35   prerr_endline (NCicPp.ppobj status#obj)
36 ;;
37
38 type cic_term = NCic.conjecture (* name, context, term *)
39 let ctx_of (_,c,_) = c ;;
40
41 let relocate status destination (name,source,t as orig) =
42  if source == destination then status, orig else
43   let u, d, metasenv, subst, o = status#obj in 
44   let rec lcp ctx j i = function
45     | (n1, NCic.Decl t1 as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
46         if n1 = n2 && 
47            NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
48           NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
49         else
50           HExtlib.mk_list (NCic.Appl 
51             [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
52     | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Def (b2,t2))::cl2 ->
53         if n1 = n2 && 
54            NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 &&
55            NCicReduction.are_convertible ctx ~subst ~metasenv b1 b2 then
56           NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
57         else
58           HExtlib.mk_list (NCic.Appl 
59             [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
60     | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
61         if n1 = n2 && 
62            NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
63           NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
64         else
65           HExtlib.mk_list (NCic.Appl 
66             [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
67     | (n1, NCic.Decl _)::cl1, (n2, NCic.Def _)::cl2 -> assert false
68     | _::_, [] -> 
69           HExtlib.mk_list (NCic.Appl 
70             [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
71     | _ -> []
72   in
73   let lc = 
74     lcp [] (List.length destination) (List.length source) 
75       (List.rev destination, List.rev source)
76   in
77   let lc = (0,NCic.Ctx (List.rev lc)) in
78   let (metasenv, subst), t =
79     NCicMetaSubst.delift 
80        ~unify:(fun m s c t1 t2 -> 
81          try Some (NCicUnification.unify status m s c t1 t2)
82          with 
83           | NCicUnification.UnificationFailure _ 
84           | NCicUnification.Uncertain _ -> None) 
85      metasenv subst source 0 lc t
86   in
87   let status = status#set_obj (u, d, metasenv, subst, o) in
88   status, (name, destination, t)
89 ;;
90
91 let term_of_cic_term s t c = 
92   let s, (_,_,t) = relocate s c t in
93   s, t
94 ;;
95
96 let ppterm status t =
97  let uri,height,metasenv,subst,obj = status#obj in
98  let _,context,t = t in
99   NCicPp.ppterm ~metasenv ~subst ~context 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#obj in
110  let metasenv, subst, status, t = 
111    GrafiteDisambiguate.disambiguate_nterm expty status context metasenv subst t 
112  in
113  let new_pstatus = uri,height,metasenv,subst,obj in
114   status#set_obj new_pstatus, (None, context, t) 
115 ;;
116
117 let typeof status ctx t =
118   let status, (_,_,t) = relocate status ctx t in
119   let _,_,metasenv,subst,_ = status#obj in
120   let ty = NCicTypeChecker.typeof ~subst ~metasenv ctx t in
121   status, (None, ctx, ty)
122 ;;
123   
124 let whd status ?delta ctx t =
125   let status, (name,_,t) = relocate status ctx t in
126   let _,_,_,subst,_ = status#obj in
127   let t = NCicReduction.whd ~subst ?delta ctx t in
128   status, (name, ctx, t)
129 ;;
130   
131 let normalize status ?delta ctx t =
132   let status, (name,_,t) = relocate status ctx t in
133   let _,_,_,subst,_ = status#obj in
134   let t = NCicTacReduction.normalize ~subst ?delta ctx t in
135   status, (name, ctx, t)
136 ;;
137   
138 let unify status ctx a b =
139   let status, (_,_,a) = relocate status ctx a in
140   let status, (_,_,b) = relocate status ctx b in
141   let n,h,metasenv,subst,o = status#obj in
142   let metasenv, subst = NCicUnification.unify status metasenv subst ctx a b in
143    status#set_obj (n,h,metasenv,subst,o)
144 ;;
145
146 let refine status ctx term expty =
147   let status, (nt,_,term) = relocate status ctx term in
148   let status, ne, expty = 
149     match expty with None -> status, None, None 
150     | Some e -> 
151         let status, (n,_, e) = relocate status ctx e in status, n, Some e
152   in
153   let name,height,metasenv,subst,obj = status#obj in
154   let metasenv,subst,t,ty = 
155    NCicRefiner.typeof status metasenv subst ctx term expty
156   in
157    status#set_obj (name,height,metasenv,subst,obj), (nt,ctx,t), (ne,ctx,ty)
158 ;;
159
160 let get_goalty status g =
161  let _,_,metasenv,_,_ = status#obj 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#obj in
172  let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
173  let subst = (i, (gname, context, t, ty)) :: subst in
174   status#set_obj (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#obj in
182       let metasenv, _, instance, _ = 
183         NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty)
184       in
185       let status = status#set_obj (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#obj 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#set_obj (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,already_found) 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,true),t
233       else
234         let _,_,_,subst,_ = status#obj 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,already_found) t
239         | NCic.Meta _ -> (status,already_found),t
240         | _ ->
241           NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux
242            (status,already_found) t
243      in 
244        aux ctx (status,false) t
245   in 
246   let _,_,_,subst,_ = low_status#obj in
247   let rec select status ctx pat cic = 
248     match pat, cic with
249     | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
250         let cic = 
251           let _,_,t,_ = NCicUtils.lookup_subst i subst in
252           NCicSubstitution.subst_meta lc t
253         in
254         select status ctx pat cic
255     | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
256         let status, t = select status ctx t1 t2 in
257         let status, s = select status ctx s1 s2 in
258         let ctx = (n, NCic.Def (s2,t2)) :: ctx in
259         let status, b = select status ctx b1 b2 in
260         status, NCic.LetIn (n,t,s,b)
261     | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
262         let status, s = select status ctx s1 s2 in
263         let ctx = (n, NCic.Decl s2) :: ctx in
264         let status, t = select status ctx t1 t2 in
265         status, NCic.Lambda (n,s,t)
266     | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
267         let status, s = select status ctx s1 s2 in
268         let ctx = (n, NCic.Decl s2) :: ctx in
269         let status, t = select status ctx t1 t2 in
270         status, NCic.Prod (n,s,t)
271     | NCic.Appl l1, NCic.Appl l2 ->
272         let status, l = 
273            List.fold_left2
274              (fun (status,l) x y -> 
275               let status, x = select status ctx x y in
276               status, x::l)
277              (status,[]) l1 l2
278         in
279         status, NCic.Appl (List.rev l)
280     | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
281         let status, t = select status ctx t1 t2 in
282         let status, ot = select status ctx ot1 ot2 in
283         let status, pl = 
284            List.fold_left2
285              (fun (status,l) x y -> 
286               let status, x = select status ctx x y in
287               status, x::l)
288              (status,[]) pl1 pl2
289         in
290         status, NCic.Match (u,ot,t,List.rev pl)
291     | NCic.Implicit `Hole, t -> 
292         (match wanted with
293         | Some wanted -> 
294              let status', wanted = disambiguate status wanted None ctx in
295              let (status',found), t' = match_term status' ctx wanted t in
296               if found then status',t' else status,t
297         | None ->
298            let (status,_),t = match_term status ctx (None,ctx,t) t in
299             status,t)
300     | NCic.Implicit _, t -> status, t
301     | _,t -> 
302         fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
303           ~context:[] ~subst:[] pat ^ " against " ^ 
304           NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
305   in
306   let status, term = select low_status context path term in
307   let term = (name, context, term) in
308   postprocess status term
309 ;;
310
311 let analyse_indty status ty = 
312  let status, reduct = whd status (ctx_of ty) ty in
313  let ref, args =
314   match reduct with
315    | _,_,NCic.Const ref -> ref, []
316    | _,_,NCic.Appl (NCic.Const ref :: args) -> ref, args
317    | _,_,_ -> fail (lazy ("not an inductive type")) in
318  let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
319  let _,_,_,cl = List.nth tl i in
320  let consno = List.length cl in
321  let left, right = HExtlib.split_nth lno args in
322  status, (ref, consno, left, right)
323 ;;
324
325 let mk_cic_term c t = None,c,t ;;
326
327 let apply_subst status ctx t =
328  let status, (name,_,t) = relocate status ctx t in
329  let _,_,_,subst,_ = status#obj in
330   status, (name, ctx, NCicUntrusted.apply_subst subst ctx t)
331 ;;
332
333 class ['stack] status =
334  fun (o: NCic.obj) (s: 'stack) ->
335   object
336    inherit (pstatus o)
337    val stack = s
338    method stack = stack
339    method set_stack s = {< stack = s >}
340   end
341
342 class type lowtac_status = [unit] status
343
344 type 'status lowtactic = #lowtac_status as 'status -> int -> 'status
345
346 class type tac_status = [Continuationals.Stack.t] status
347
348 type 'status tactic = #tac_status as 'status -> 'status