]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nTacStatus.ml
035e65ac7b83089cd086b803045c8b24f165ed51
[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 * exn option
15 let fail ?exn msg = raise (Error (msg,exn))
16
17 let wrap f x = 
18   try f x 
19   with 
20   | MultiPassDisambiguator.DisambiguationError _ 
21   | NCicRefiner.RefineFailure _ 
22   | NCicUnification.UnificationFailure _ 
23   | NCicTypeChecker.TypeCheckerFailure _ 
24   | NCicMetaSubst.MetaSubstFailure _ as exn -> fail ~exn (lazy "")
25 ;;
26
27 class pstatus =
28  fun (o: NCic.obj) ->
29   object
30    inherit NEstatus.status
31    val obj = o
32    method obj = obj
33    method set_obj o = {< obj = o >}
34   end
35
36 type tactic_term = CicNotationPt.term Disambiguate.disambiguator_input
37 type tactic_pattern = GrafiteAst.npattern Disambiguate.disambiguator_input
38
39 let pp_status status = 
40   prerr_endline (NCicPp.ppobj status#obj)
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 u, d, metasenv, subst, o = status#obj in 
49   let rec lcp ctx j i = function
50     | (n1, NCic.Decl t1 as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
51         if n1 = n2 && 
52            NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
53           NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
54         else
55           HExtlib.mk_list (NCic.Appl 
56             [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
57     | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Def (b2,t2))::cl2 ->
58         if n1 = n2 && 
59            NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 &&
60            NCicReduction.are_convertible ctx ~subst ~metasenv b1 b2 then
61           NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
62         else
63           HExtlib.mk_list (NCic.Appl 
64             [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
65     | (n1, NCic.Def (b1,t1) as e)::cl1, (n2, NCic.Decl t2)::cl2 ->
66         if n1 = n2 && 
67            NCicReduction.are_convertible ctx ~subst ~metasenv t1 t2 then
68           NCic.Rel i :: lcp (e::ctx)(j-1) (i-1) (cl1,cl2)
69         else
70           HExtlib.mk_list (NCic.Appl 
71             [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
72     | (n1, NCic.Decl _)::cl1, (n2, NCic.Def _)::cl2 -> assert false
73     | _::_, [] -> 
74           HExtlib.mk_list (NCic.Appl 
75             [NCic.Sort NCic.Prop; NCic.Sort NCic.Prop]) j
76     | _ -> []
77   in
78   let lc = 
79     lcp [] (List.length destination) (List.length source) 
80       (List.rev destination, List.rev source)
81   in
82   let lc = (0,NCic.Ctx (List.rev lc)) in
83   let (metasenv, subst), t =
84     NCicMetaSubst.delift 
85        ~unify:(fun m s c t1 t2 -> 
86          try Some (NCicUnification.unify status m s c t1 t2)
87          with 
88           | NCicUnification.UnificationFailure _ 
89           | NCicUnification.Uncertain _ -> None) 
90      metasenv subst source 0 lc t
91   in
92   let status = status#set_obj (u, d, metasenv, subst, o) in
93   status, (name, destination, t)
94 ;;
95 let relocate a b c = wrap (relocate a b) c;;
96
97 let term_of_cic_term s t c = 
98   let s, (_,_,t) = relocate s c t in
99   s, t
100 ;;
101
102 let ppterm status t =
103  let uri,height,metasenv,subst,obj = status#obj in
104  let _,context,t = t in
105   NCicPp.ppterm ~metasenv ~subst ~context t
106 ;;
107
108 let disambiguate status t ty context =
109  let status, expty = 
110    match ty with 
111    | None -> status, None 
112    | Some ty -> 
113        let status, (_,_,x) = relocate status context ty in status, Some x 
114  in
115  let uri,height,metasenv,subst,obj = status#obj in
116  let metasenv, subst, status, t = 
117    GrafiteDisambiguate.disambiguate_nterm expty status context metasenv subst t 
118  in
119  let new_pstatus = uri,height,metasenv,subst,obj in
120   status#set_obj new_pstatus, (None, context, t) 
121 ;;
122 let disambiguate a b c d = wrap (disambiguate a b c) d;;
123
124 let typeof status ctx t =
125   let status, (_,_,t) = relocate status ctx t in
126   let _,_,metasenv,subst,_ = status#obj in
127   let ty = NCicTypeChecker.typeof ~subst ~metasenv ctx t in
128   status, (None, ctx, ty)
129 ;;
130 let typeof a b c = wrap (typeof a b) c;;
131   
132 let whd status ?delta ctx t =
133   let status, (name,_,t) = relocate status ctx t in
134   let _,_,_,subst,_ = status#obj in
135   let t = NCicReduction.whd ~subst ?delta ctx t in
136   status, (name, ctx, t)
137 ;;
138   
139 let normalize status ?delta ctx t =
140   let status, (name,_,t) = relocate status ctx t in
141   let _,_,_,subst,_ = status#obj in
142   let t = NCicTacReduction.normalize ~subst ?delta ctx t in
143   status, (name, ctx, t)
144 ;;
145   
146 let unify status ctx a b =
147   let status, (_,_,a) = relocate status ctx a in
148   let status, (_,_,b) = relocate status ctx b in
149   let n,h,metasenv,subst,o = status#obj in
150   let metasenv, subst = NCicUnification.unify status metasenv subst ctx a b in
151    status#set_obj (n,h,metasenv,subst,o)
152 ;;
153 let unify a b c d = wrap (unify a b c) d;;
154
155 let fix_sorts (name,ctx,t) =
156  let f () =
157   let t = NCicUnification.fix_sorts t in
158    name,ctx,t
159  in
160   wrap f ()
161 ;;
162
163 let refine status ctx term expty =
164   let status, (nt,_,term) = relocate status ctx term in
165   let status, ne, expty = 
166     match expty with None -> status, None, None 
167     | Some e -> 
168         let status, (n,_, e) = relocate status ctx e in status, n, Some e
169   in
170   let name,height,metasenv,subst,obj = status#obj in
171   let metasenv,subst,t,ty = 
172    NCicRefiner.typeof status metasenv subst ctx term expty
173   in
174    status#set_obj (name,height,metasenv,subst,obj), (nt,ctx,t), (ne,ctx,ty)
175 ;;
176 let refine a b c d = wrap (refine a b c) d;;
177
178 let get_goalty status g =
179  let _,_,metasenv,_,_ = status#obj in
180  List.assoc g metasenv
181 ;;
182
183 let instantiate status i t =
184  let (gname, context, _ as gty) = get_goalty status i in
185  let status, (_,_,t), (_,_,ty) = 
186    refine status (ctx_of gty) t (Some gty) 
187  in
188
189  let name,height,metasenv,subst,obj = status#obj in
190  let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
191  let subst = (i, (gname, context, t, ty)) :: subst in
192   status#set_obj (name,height,metasenv,subst,obj)
193 ;;
194
195 let mk_meta status ?name ctx bo_or_ty =
196   match bo_or_ty with
197   | `Decl ty ->
198       let status, (_,_,ty) = relocate status ctx ty in
199       let n,h,metasenv,subst,o = status#obj in
200       let metasenv, _, instance, _ = 
201         NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty)
202       in
203       let status = status#set_obj (n,h,metasenv,subst,o) in
204       status, (None,ctx,instance)
205   | `Def bo ->
206       let status, (_,_,bo_ as bo) = relocate status ctx bo in
207       let status, (_,_,ty) = typeof status ctx bo in
208       let n,h,metasenv,subst,o = status#obj in
209       let metasenv, metano, instance, _ = 
210         NCicMetaSubst.mk_meta ?name metasenv ctx (`WithType ty) in
211       let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
212       let subst = (metano, (name, ctx, bo_, ty)) :: subst in
213       let status = status#set_obj (n,h,metasenv,subst,o) in
214       status, (None,ctx,instance)
215 ;;
216
217 let mk_in_scope status t = 
218   mk_meta status ~name:NCicMetaSubst.in_scope_tag (ctx_of t) (`Def t)
219 ;;
220
221 let mk_out_scope n status t = 
222   mk_meta status ~name:(NCicMetaSubst.out_scope_tag n) (ctx_of t) (`Def t)
223 ;;
224
225 (* the following unification problem will be driven by 
226  *   select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
227  *
228  *   ? args = t
229  *
230  *   where argsn = length args and the pattern matches t
231  *
232  *  found is called on every selected term to map them
233  *  postprocess is called on the entire term after selection
234  *)
235 let select_term 
236   low_status ~found ~postprocess (name,context,term) (wanted,path) 
237 =
238   let is_found status ctx t wanted =
239     (* we could lift wanted step-by-step *)
240     try true, unify status ctx (None, ctx, t) wanted
241     with 
242     | NCicUnification.UnificationFailure _ 
243     | NCicUnification.Uncertain _ -> false, status
244   in
245   let match_term status ctx (wanted : cic_term) t =
246     let rec aux ctx (status,already_found) t =
247       let b, status = is_found status ctx t wanted in
248       if b then
249          let status , (_,_,t) = found status (None, ctx, t) in 
250          (status,true),t
251       else
252         let _,_,_,subst,_ = status#obj in
253         match t with
254         | NCic.Meta (i,lc) when List.mem_assoc i subst ->
255             let _,_,t,_ = NCicUtils.lookup_subst i subst in
256             aux ctx (status,already_found) t
257         | NCic.Meta _ -> (status,already_found),t
258         | _ ->
259           NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux
260            (status,already_found) t
261      in 
262        aux ctx (status,false) t
263   in 
264   let _,_,_,subst,_ = low_status#obj in
265   let rec select status ctx pat cic = 
266     match pat, cic with
267     | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
268         let cic = 
269           let _,_,t,_ = NCicUtils.lookup_subst i subst in
270           NCicSubstitution.subst_meta lc t
271         in
272         select status ctx pat cic
273     | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
274         let status, t = select status ctx t1 t2 in
275         let status, s = select status ctx s1 s2 in
276         let ctx = (n, NCic.Def (s2,t2)) :: ctx in
277         let status, b = select status ctx b1 b2 in
278         status, NCic.LetIn (n,t,s,b)
279     | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
280         let status, s = select status ctx s1 s2 in
281         let ctx = (n, NCic.Decl s2) :: ctx in
282         let status, t = select status ctx t1 t2 in
283         status, NCic.Lambda (n,s,t)
284     | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
285         let status, s = select status ctx s1 s2 in
286         let ctx = (n, NCic.Decl s2) :: ctx in
287         let status, t = select status ctx t1 t2 in
288         status, NCic.Prod (n,s,t)
289     | NCic.Appl l1, NCic.Appl l2 ->
290         let status, l = 
291            List.fold_left2
292              (fun (status,l) x y -> 
293               let status, x = select status ctx x y in
294               status, x::l)
295              (status,[]) l1 l2
296         in
297         status, NCic.Appl (List.rev l)
298     | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
299         let status, t = select status ctx t1 t2 in
300         let status, ot = select status ctx ot1 ot2 in
301         let status, pl = 
302            List.fold_left2
303              (fun (status,l) x y -> 
304               let status, x = select status ctx x y in
305               status, x::l)
306              (status,[]) pl1 pl2
307         in
308         status, NCic.Match (u,ot,t,List.rev pl)
309     | NCic.Implicit `Hole, t -> 
310         (match wanted with
311         | Some wanted -> 
312              let status', wanted = disambiguate status wanted None ctx in
313              let (status',found), t' = match_term status' ctx wanted t in
314               if found then status',t' else status,t
315         | None ->
316            let (status,_),t = match_term status ctx (None,ctx,t) t in
317             status,t)
318     | NCic.Implicit _, t -> status, t
319     | _,t -> 
320         fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
321           ~context:[] ~subst:[] pat ^ " against " ^ 
322           NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
323   in
324   let status, term = select low_status context path term in
325   let term = (name, context, term) in
326   postprocess status term
327 ;;
328
329 let analyse_indty status ty = 
330  let status, reduct = whd status (ctx_of ty) ty in
331  let ref, args =
332   match reduct with
333    | _,_,NCic.Const ref -> ref, []
334    | _,_,NCic.Appl (NCic.Const ref :: args) -> ref, args
335    | _,_,_ -> fail (lazy ("not an inductive type")) in
336  let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
337  let _,_,_,cl = List.nth tl i in
338  let consno = List.length cl in
339  let left, right = HExtlib.split_nth lno args in
340  status, (ref, consno, left, right)
341 ;;
342
343 let mk_cic_term c t = None,c,t ;;
344
345 let apply_subst status ctx t =
346  let status, (name,_,t) = relocate status ctx t in
347  let _,_,_,subst,_ = status#obj in
348   status, (name, ctx, NCicUntrusted.apply_subst subst ctx t)
349 ;;
350
351 (* ============= move this elsewhere ====================*)
352
353 class ['stack] status =
354  fun (o: NCic.obj) (s: 'stack) ->
355   object
356    inherit (pstatus o)
357    val stack = s
358    method stack = stack
359    method set_stack s = {< stack = s >}
360   end
361
362 class type lowtac_status = [unit] status
363
364 type 'status lowtactic = #lowtac_status as 'status -> int -> 'status
365
366 class type tac_status = [Continuationals.Stack.t] status
367
368 type 'status tactic = #tac_status as 'status -> 'status
369