]> matita.cs.unibo.it Git - helm.git/blob - components/ng_kernel/nCicSubstitution.ml
matita 0.5.1 tagged
[helm.git] / components / ng_kernel / nCicSubstitution.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$ *)
13
14 module C = NCic 
15 module Ref = NReference
16
17 let debug_print = fun _ -> ();;
18
19 let lift_from k n =
20  let rec liftaux k = function
21     | C.Rel m as t -> if m < k then t else C.Rel (m + n)
22     | C.Meta (i,(m,l)) as t when k <= m ->
23        if n = 0 then t else C.Meta (i,(m+n,l))
24     | C.Meta (_,(m,C.Irl l)) as t when k > l + m -> t
25     | C.Meta (i,(m,l)) -> 
26        let lctx = NCicUtils.expand_local_context l in
27        C.Meta (i, (m, C.Ctx (HExtlib.sharing_map (liftaux (k-m)) lctx)))
28     | C.Implicit _ -> (* was the identity *) assert false
29     | t -> NCicUtils.map (fun _ k -> k + 1) k liftaux t
30  in
31  liftaux k
32 ;;
33
34 let lift ?(from=1) n t =
35   if n = 0 then t
36   else lift_from from n t
37 ;;
38
39 (* subst t1 t2                                                          *)
40 (*  substitutes [t1] for [Rel 1] in [t2]                                *)
41 (*  if avoid_beta_redexes is true (default: false) no new beta redexes  *)
42 (*  are generated. WARNING: the substitution can diverge when t2 is not *)
43 (*  well typed and avoid_beta_redexes is true.                          *)
44 (*  map_arg is ReductionStrategy.from_env_for_unwind when psubst is     *)
45 (*  used to implement nCicReduction.unwind'                             *)
46 let rec psubst ?(avoid_beta_redexes=false) map_arg args = 
47  let nargs = List.length args in
48  let rec substaux k = function
49    | C.Rel n as t ->
50       (match n with
51       | n when n >= (k+nargs) ->
52          if nargs <> 0 then C.Rel (n - nargs) else t
53       | n when n < k -> t
54       | n (* k <= n < k+nargs *) ->
55        (try lift (k-1) (map_arg (List.nth args (n-k)))
56         with Failure _ -> assert false))
57    | C.Meta (i,(m,l)) as t when m >= k + nargs - 1 -> 
58        if nargs <> 0 then C.Meta (i,(m-nargs,l)) else t
59    | C.Meta (i,(m,(C.Irl l as irl))) as t when k > l + m -> 
60        if nargs <> 0 then C.Meta (i,(m-nargs,irl)) else t
61    | C.Meta (i,(m,l)) -> 
62       let lctx = NCicUtils.expand_local_context l in
63       (* 1-nargs < k-m, when <= 0 is still reasonable because we will
64        * substitute args[ k-m ... k-m+nargs-1 > 0 ] *)
65       C.Meta (i,(m, C.Ctx (HExtlib.sharing_map (substaux (k-m)) lctx)))
66    | C.Implicit _ -> assert false (* was identity *)
67    | C.Appl (he::tl) as t ->
68       (* Invariant: no Appl applied to another Appl *)
69       let rec avoid he' = function
70         | [] -> he'
71         | arg::tl' as args->
72             (match he' with
73             | C.Appl l -> C.Appl (l@args)
74             | C.Lambda (_,_,bo) when avoid_beta_redexes ->
75                 (* map_arg is here \x.x, Obj magic is needed because 
76                  * we don't have polymorphic recursion w/o records *)
77                 avoid (psubst 
78                   ~avoid_beta_redexes Obj.magic [Obj.magic arg] bo) tl'
79             | _ -> if he == he' && args == tl then t else C.Appl (he'::args))
80       in
81       let tl = HExtlib.sharing_map (substaux k) tl in
82       avoid (substaux k he) tl
83    | t -> NCicUtils.map (fun _ k -> k + 1) k substaux t
84  in
85   substaux 1
86 ;;
87
88 let subst ?avoid_beta_redexes arg = 
89   psubst ?avoid_beta_redexes (fun x -> x)[arg];;
90
91 (* subst_meta (n, Some [t_1 ; ... ; t_n]) t                                  *)
92 (*  returns the term [t] where [Rel i] is substituted with [t_i] lifted by n *)
93 (*  [t_i] is lifted as usual when it crosses an abstraction                  *)
94 (* subst_meta (n, Non) t -> lift n t                                         *)
95 let subst_meta = function 
96   | m, C.Irl _ 
97   | m, C.Ctx [] -> lift m 
98   | m, C.Ctx l  -> psubst (lift m) l 
99 ;;