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