]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nTacStatus.ml
41319b4739d6c44f08f75fa39e70dc201debd9fc
[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 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 "")
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 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 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 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 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 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 a b c) d;;
180
181 let get_goalty status g =
182  let _,_,metasenv,_,_ = status#obj in
183  let _, a, b = List.assoc g metasenv in
184  a, b
185 ;;
186
187 let instantiate status i t =
188  let _,_,metasenv,_,_ = status#obj in
189  let gname, context, gty = List.assoc i metasenv in
190  let status, (_,t), (_,ty) = 
191    refine status context t (Some (context,gty)) 
192  in
193
194  let name,height,metasenv,subst,obj = status#obj in
195  let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
196  let subst = (i, (gname, context, t, ty)) :: subst in
197   status#set_obj (name,height,metasenv,subst,obj)
198 ;;
199
200 let mk_meta status ?(attrs=[]) ctx bo_or_ty =
201   match bo_or_ty with
202   | `Decl ty ->
203       let status, (_,ty) = relocate status ctx ty in
204       let n,h,metasenv,subst,o = status#obj in
205       let metasenv, _, instance, _ = 
206         NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty)
207       in
208       let status = status#set_obj (n,h,metasenv,subst,o) in
209       status, (ctx,instance)
210   | `Def bo ->
211       let status, (_,bo_ as bo) = relocate status ctx bo in
212       let status, (_,ty) = typeof status ctx bo in
213       let n,h,metasenv,subst,o = status#obj in
214       let metasenv, metano, instance, _ = 
215         NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty) in
216       let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
217       let subst = (metano, (attrs, ctx, bo_, ty)) :: subst in
218       let status = status#set_obj (n,h,metasenv,subst,o) in
219       status, (ctx,instance)
220 ;;
221
222 let mk_in_scope status t =
223   mk_meta status ~attrs:[`InScope] (ctx_of t) (`Def t)
224 ;;
225
226 let mk_out_scope n status t = 
227   mk_meta status ~attrs:[`OutScope n] (ctx_of t) (`Def t)
228 ;;
229
230 (* the following unification problem will be driven by 
231  *   select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
232  *
233  *   ? args = t
234  *
235  *   where argsn = length args and the pattern matches t
236  *
237  *  found is called on every selected term to map them
238  *  postprocess is called on the entire term after selection
239  *)
240 let select_term 
241   low_status ~found ~postprocess (context,term) (wanted,path) 
242 =
243   let is_found status ctx t wanted =
244     (* we could lift wanted step-by-step *)
245     try true, unify status ctx (ctx, t) wanted
246     with 
247     | Error (_, Some (NCicUnification.UnificationFailure _))
248     | Error (_, Some (NCicUnification.Uncertain _)) -> false, status
249   in
250   let match_term status ctx (wanted : cic_term) t =
251     let rec aux ctx (status,already_found) t =
252       let b, status = is_found status ctx t wanted in
253       if b then
254          let status , (_,t) = found status (ctx, t) in 
255          (status,true),t
256       else
257         let _,_,_,subst,_ = status#obj in
258         match t with
259         | NCic.Meta (i,lc) when List.mem_assoc i subst ->
260             let _,_,t,_ = NCicUtils.lookup_subst i subst in
261             aux ctx (status,already_found) t
262         | NCic.Meta _ -> (status,already_found),t
263         | _ ->
264           NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux
265            (status,already_found) t
266      in 
267        aux ctx (status,false) t
268   in 
269   let _,_,_,subst,_ = low_status#obj in
270   let rec select status ctx pat cic = 
271     match pat, cic with
272     | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
273         let cic = 
274           let _,_,t,_ = NCicUtils.lookup_subst i subst in
275           NCicSubstitution.subst_meta lc t
276         in
277         select status ctx pat cic
278     | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
279         let status, t = select status ctx t1 t2 in
280         let status, s = select status ctx s1 s2 in
281         let ctx = (n, NCic.Def (s2,t2)) :: ctx in
282         let status, b = select status ctx b1 b2 in
283         status, NCic.LetIn (n,t,s,b)
284     | NCic.Lambda (_,s1,t1), NCic.Lambda (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.Lambda (n,s,t)
289     | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
290         let status, s = select status ctx s1 s2 in
291         let ctx = (n, NCic.Decl s2) :: ctx in
292         let status, t = select status ctx t1 t2 in
293         status, NCic.Prod (n,s,t)
294     | NCic.Appl l1, NCic.Appl l2 ->
295         let status, l = 
296            List.fold_left2
297              (fun (status,l) x y -> 
298               let status, x = select status ctx x y in
299               status, x::l)
300              (status,[]) l1 l2
301         in
302         status, NCic.Appl (List.rev l)
303     | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
304         let status, t = select status ctx t1 t2 in
305         let status, ot = select status ctx ot1 ot2 in
306         let status, pl = 
307            List.fold_left2
308              (fun (status,l) x y -> 
309               let status, x = select status ctx x y in
310               status, x::l)
311              (status,[]) pl1 pl2
312         in
313         status, NCic.Match (u,ot,t,List.rev pl)
314     | NCic.Implicit `Hole, t -> 
315         (match wanted with
316         | Some wanted -> 
317              let status', wanted = disambiguate status wanted None ctx in
318              let (status',found), t' = match_term status' ctx wanted t in
319               if found then status',t' else status,t
320         | None ->
321            let (status,_),t = match_term status ctx (ctx,t) t in
322             status,t)
323     | NCic.Implicit _, t -> status, t
324     | _,t -> 
325         fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
326           ~context:[] ~subst:[] pat ^ " against " ^ 
327           NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
328   in
329   let status, term = select low_status context path term in
330   let term = (context, term) in
331   postprocess status term
332 ;;
333
334 let analyse_indty status ty = 
335  let status, reduct = whd status (ctx_of ty) ty in
336  let ref, args =
337   match reduct with
338    | _,NCic.Const ref -> ref, []
339    | _,NCic.Appl (NCic.Const (NRef.Ref (_,(NRef.Ind _)) as ref) :: args) -> 
340          ref, args
341    | _,_ -> fail (lazy ("not an inductive type")) in
342  let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
343  let _,_,_,cl = List.nth tl i in
344  let consno = List.length cl in
345  let left, right = HExtlib.split_nth lno args in
346  status, (ref, consno, left, right)
347 ;;
348
349 let mk_cic_term c t = c,t ;;
350
351 let apply_subst status ctx t =
352  let status, (_,t) = relocate status ctx t in
353  let _,_,_,subst,_ = status#obj in
354   status, (ctx, NCicUntrusted.apply_subst subst ctx t)
355 ;;
356
357 (* ============= move this elsewhere ====================*)
358
359 class ['stack] status =
360  fun (o: NCic.obj) (s: 'stack) ->
361   object
362    inherit (pstatus o)
363    val stack = s
364    method stack = stack
365    method set_stack s = {< stack = s >}
366   end
367
368 class type lowtac_status = [unit] status
369
370 type 'status lowtactic = #lowtac_status as 'status -> int -> 'status
371
372 class type tac_status = [Continuationals.Stack.t] status
373
374 type 'status tactic = #tac_status as 'status -> 'status
375