]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nTacStatus.ml
auto with intro
[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 saturate status (ctx,t) =
135   let n,h,metasenv,subst,k = status#obj in
136   let t, metasenv, args = NCicMetaSubst.saturate metasenv subst ctx t 0 in
137   let status = status#set_obj (n,h,metasenv,subst,k) in
138   status, (ctx,t), List.map (fun x -> ctx,x) args
139 ;;
140 let saturate a b = wrap "saturate" (saturate a) b;;
141   
142 let whd status ?delta ctx t =
143   let status, (_,t) = relocate status ctx t in
144   let _,_,_,subst,_ = status#obj in
145   let t = NCicReduction.whd ~subst ?delta ctx t in
146   status, (ctx, t)
147 ;;
148   
149 let normalize status ?delta ctx t =
150   let status, (_,t) = relocate status ctx t in
151   let _,_,_,subst,_ = status#obj in
152   let t = NCicTacReduction.normalize ~subst ?delta ctx t in
153   status, (ctx, t)
154 ;;
155   
156 let unify status ctx a b =
157   let status, (_,a) = relocate status ctx a in
158   let status, (_,b) = relocate status ctx b in
159   let n,h,metasenv,subst,o = status#obj in
160   let metasenv, subst = NCicUnification.unify status metasenv subst ctx a b in
161    status#set_obj (n,h,metasenv,subst,o)
162 ;;
163 let unify a b c d = wrap "unify" (unify a b c) d;;
164
165 let fix_sorts (ctx,t) =
166  let f () =
167   let t = NCicUnification.fix_sorts t in
168    ctx,t
169  in
170   wrap "fix_sorts" f ()
171 ;;
172
173 let refine status ctx term expty =
174   let status, (_,term) = relocate status ctx term in
175   let status, expty = 
176     match expty with
177       None -> status, None 
178     | Some e -> 
179         let status, (_, e) = relocate status ctx e in status, Some e
180   in
181   let name,height,metasenv,subst,obj = status#obj in
182   let metasenv,subst,t,ty = 
183    NCicRefiner.typeof status metasenv subst ctx term expty
184   in
185    status#set_obj (name,height,metasenv,subst,obj), (ctx,t), (ctx,ty)
186 ;;
187 let refine a b c d = wrap "refine" (refine a b c) d;;
188
189 let get_goalty status g =
190  let _,_,metasenv,_,_ = status#obj in
191  try
192    let _, ctx, ty = NCicUtils.lookup_meta g metasenv in
193    ctx, ty
194  with NCicUtils.Meta_not_found _ as exn -> fail ~exn (lazy "get_goalty")
195 ;;
196
197 let instantiate status i t =
198  let _,_,metasenv,_,_ = status#obj in
199  let gname, context, gty = List.assoc i metasenv in
200  let status, (_,t), (_,ty) = 
201    refine status context t (Some (context,gty)) 
202  in
203
204  let name,height,metasenv,subst,obj = status#obj in
205  let metasenv = List.filter (fun j,_ -> j <> i) metasenv in
206  let subst = (i, (gname, context, t, ty)) :: subst in
207   status#set_obj (name,height,metasenv,subst,obj)
208 ;;
209
210 let mk_meta status ?(attrs=[]) ctx bo_or_ty =
211   match bo_or_ty with
212   | `Decl ty ->
213       let status, (_,ty) = relocate status ctx ty in
214       let n,h,metasenv,subst,o = status#obj in
215       let metasenv, _, instance, _ = 
216         NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty)
217       in
218       let status = status#set_obj (n,h,metasenv,subst,o) in
219       status, (ctx,instance)
220   | `Def bo ->
221       let status, (_,bo_ as bo) = relocate status ctx bo in
222       let status, (_,ty) = typeof status ctx bo in
223       let n,h,metasenv,subst,o = status#obj in
224       let metasenv, metano, instance, _ = 
225         NCicMetaSubst.mk_meta ~attrs metasenv ctx (`WithType ty) in
226       let metasenv = List.filter (fun j,_ -> j <> metano) metasenv in
227       let subst = (metano, (attrs, ctx, bo_, ty)) :: subst in
228       let status = status#set_obj (n,h,metasenv,subst,o) in
229       status, (ctx,instance)
230 ;;
231
232 let mk_in_scope status t =
233   mk_meta status ~attrs:[`InScope] (ctx_of t) (`Def t)
234 ;;
235
236 let mk_out_scope n status t = 
237   mk_meta status ~attrs:[`OutScope n] (ctx_of t) (`Def t)
238 ;;
239
240 (* the following unification problem will be driven by 
241  *   select s ~found:mk_in_scope ~postprocess:(mk_out_scope argsno) t pattern
242  *
243  *   ? args = t
244  *
245  *   where argsn = length args and the pattern matches t
246  *
247  *  found is called on every selected term to map them
248  *  postprocess is called on the entire term after selection
249  *)
250 let select_term 
251   low_status ~found ~postprocess (context,term) (wanted,path) 
252 =
253   let is_found status ctx t wanted =
254     (* we could lift wanted step-by-step *)
255     try true, unify status ctx (ctx, t) wanted
256     with 
257     | Error (_, Some (NCicUnification.UnificationFailure _))
258     | Error (_, Some (NCicUnification.Uncertain _)) -> false, status
259   in
260   let match_term status ctx (wanted : cic_term) t =
261     let rec aux ctx (status,already_found) t =
262       let b, status = is_found status ctx t wanted in
263       if b then
264          let status , (_,t) = found status (ctx, t) in 
265          (status,true),t
266       else
267         let _,_,_,subst,_ = status#obj in
268         match t with
269         | NCic.Meta (i,lc) when List.mem_assoc i subst ->
270             let _,_,t,_ = NCicUtils.lookup_subst i subst in
271             aux ctx (status,already_found) t
272         | NCic.Meta _ -> (status,already_found),t
273         | _ ->
274           NCicUntrusted.map_term_fold_a (fun e c -> e::c) ctx aux
275            (status,already_found) t
276      in 
277        aux ctx (status,false) t
278   in 
279   let _,_,_,subst,_ = low_status#obj in
280   let rec select status ctx pat cic = 
281     match pat, cic with
282     | _, NCic.Meta (i,lc) when List.mem_assoc i subst ->
283         let cic = 
284           let _,_,t,_ = NCicUtils.lookup_subst i subst in
285           NCicSubstitution.subst_meta lc t
286         in
287         select status ctx pat cic
288     | NCic.LetIn (_,t1,s1,b1), NCic.LetIn (n,t2,s2,b2) ->
289         let status, t = select status ctx t1 t2 in
290         let status, s = select status ctx s1 s2 in
291         let ctx = (n, NCic.Def (s2,t2)) :: ctx in
292         let status, b = select status ctx b1 b2 in
293         status, NCic.LetIn (n,t,s,b)
294     | NCic.Lambda (_,s1,t1), NCic.Lambda (n,s2,t2) ->
295         let status, s = select status ctx s1 s2 in
296         let ctx = (n, NCic.Decl s2) :: ctx in
297         let status, t = select status ctx t1 t2 in
298         status, NCic.Lambda (n,s,t)
299     | NCic.Prod (_,s1,t1), NCic.Prod (n,s2,t2) ->
300         let status, s = select status ctx s1 s2 in
301         let ctx = (n, NCic.Decl s2) :: ctx in
302         let status, t = select status ctx t1 t2 in
303         status, NCic.Prod (n,s,t)
304     | NCic.Appl l1, NCic.Appl l2 ->
305         let status, l = 
306            List.fold_left2
307              (fun (status,l) x y -> 
308               let status, x = select status ctx x y in
309               status, x::l)
310              (status,[]) l1 l2
311         in
312         status, NCic.Appl (List.rev l)
313     | NCic.Match (_,ot1,t1,pl1), NCic.Match (u,ot2,t2,pl2) ->
314         let status, t = select status ctx t1 t2 in
315         let status, ot = select status ctx ot1 ot2 in
316         let status, pl = 
317            List.fold_left2
318              (fun (status,l) x y -> 
319               let status, x = select status ctx x y in
320               status, x::l)
321              (status,[]) pl1 pl2
322         in
323         status, NCic.Match (u,ot,t,List.rev pl)
324     | NCic.Implicit `Hole, t -> 
325         (match wanted with
326         | Some wanted -> 
327              let status', wanted = disambiguate status wanted None ctx in
328              let (status',found), t' = match_term status' ctx wanted t in
329               if found then status',t' else status,t
330         | None ->
331            let (status,_),t = match_term status ctx (ctx,t) t in
332             status,t)
333     | NCic.Implicit _, t -> status, t
334     | _,t -> 
335         fail (lazy ("malformed pattern: " ^ NCicPp.ppterm ~metasenv:[]
336           ~context:[] ~subst:[] pat ^ " against " ^ 
337           NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))
338   in
339   let status, term = select low_status context path term in
340   let term = (context, term) in
341   postprocess status term
342 ;;
343
344 let analyse_indty status ty = 
345  let status, reduct = whd status (ctx_of ty) ty in
346  let ref, args =
347   match reduct with
348    | _,NCic.Const ref -> ref, []
349    | _,NCic.Appl (NCic.Const (NRef.Ref (_,(NRef.Ind _)) as ref) :: args) -> 
350          ref, args
351    | _,_ -> fail (lazy ("not an inductive type")) in
352  let _,lno,tl,_,i = NCicEnvironment.get_checked_indtys ref in
353  let _,_,_,cl = List.nth tl i in
354  let consno = List.length cl in
355  let left, right = HExtlib.split_nth lno args in
356  status, (ref, consno, left, right)
357 ;;
358
359 let mk_cic_term c t = c,t ;;
360
361 let apply_subst status ctx t =
362  let status, (_,t) = relocate status ctx t in
363  let _,_,_,subst,_ = status#obj in
364   status, (ctx, NCicUntrusted.apply_subst subst ctx t)
365 ;;
366
367 (* ============= move this elsewhere ====================*)
368
369 class ['stack] status =
370  fun (o: NCic.obj) (s: 'stack) ->
371   object
372    inherit (pstatus o)
373    val stack = s
374    method stack = stack
375    method set_stack s = {< stack = s >}
376   end
377
378 class type lowtac_status = [unit] status
379
380 type 'status lowtactic = #lowtac_status as 'status -> int -> 'status
381
382 class type tac_status = [Continuationals.Stack.t] status
383
384 type 'status tactic = #tac_status as 'status -> 'status
385
386 module NCicInverseRelIndexable : Discrimination_tree.Indexable
387 with type input = cic_term and type constant_name = NUri.uri = struct
388
389 open Discrimination_tree
390
391 type input = cic_term
392 type constant_name = NUri.uri
393
394 let ppelem = function
395   | Constant (uri,arity) -> 
396       "("^NUri.name_of_uri uri ^ "," ^ string_of_int arity^")"
397   | Bound (i,arity) -> 
398       "("^string_of_int i ^ "," ^ string_of_int arity^")"
399   | Variable -> "?"
400   | Proposition -> "Prop"
401   | Datatype -> "Type"
402   | Dead -> "Dead"
403 ;;
404
405 let path_string_of (ctx,t) =
406   let len_ctx = List.length ctx in
407   let rec aux arity = function
408     | NCic.Appl ((NCic.Meta _|NCic.Implicit _)::_) -> [Variable]
409     | NCic.Appl (NCic.Lambda _ :: _) -> [Variable] (* maybe we should b-reduce *)
410     | NCic.Appl [] -> assert false
411     | NCic.Appl (hd::tl) ->
412         aux (List.length tl) hd @ List.flatten (List.map (aux 0) tl) 
413     | NCic.Lambda _ | NCic.Prod _ -> [Variable]
414         (* I think we should CicSubstitution.subst Implicit t *)
415     | NCic.LetIn _ -> [Variable] (* z-reduce? *)
416     | NCic.Meta _ | NCic.Implicit _ -> assert (arity = 0); [Variable]
417     | NCic.Rel i -> [Bound (len_ctx - i, arity)]
418     | NCic.Sort (NCic.Prop) -> assert (arity=0); [Proposition]
419     | NCic.Sort _ -> assert (arity=0); [Datatype]
420     | NCic.Const (NReference.Ref (u,_)) -> [Constant (u, arity)]
421     | NCic.Match _ -> [Dead]
422   in 
423     aux 0 t
424 ;;
425
426 let compare e1 e2 =
427   match e1,e2 with
428   | Constant (u1,a1),Constant (u2,a2) -> 
429        let x = NUri.compare u1 u2 in
430        if x = 0 then Pervasives.compare a1 a2 else x
431   | e1,e2 -> Pervasives.compare e1 e2
432 ;;
433
434 let string_of_path l = String.concat "." (List.map ppelem l) ;;
435
436 end
437
438 module Ncic_termOT : Set.OrderedType with type t = cic_term =
439  struct
440    type t = cic_term
441    let compare = Pervasives.compare
442  end
443
444 module Ncic_termSet : Set.S with type elt = cic_term = Set.Make(Ncic_termOT)
445
446 module InvRelDiscriminationTree = 
447    Discrimination_tree.Make(NCicInverseRelIndexable)(Ncic_termSet)
448