]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_kernel/nCicSubstitution.ml
Most warnings turned into errors and avoided
[helm.git] / matita / 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
16 let lift_from status ?(no_implicit=true) k n =
17  let rec liftaux k = function
18     | C.Rel m as t -> if m < k then t else C.Rel (m + n)
19     | C.Meta (i,(m,(C.Irl 0 as l))) when k <= m+1 -> C.Meta (i,(m,l))
20     | C.Meta (i,(m,l)) when k <= m+1 -> C.Meta (i,(m+n,l))
21     | C.Meta (_,(m,C.Irl l)) as t when k > l + m -> t
22     | C.Meta (i,(m,l)) -> 
23        let lctx = NCicUtils.expand_local_context l in
24        C.Meta (i, (m, C.Ctx (HExtlib.sharing_map (liftaux (k-m)) lctx)))
25     | C.Implicit _ as t -> (* was the identity *) 
26         if no_implicit then assert false
27         else t
28     | t -> NCicUtils.map status (fun _ k -> k + 1) k liftaux t
29  in
30  liftaux k
31 ;;
32
33 let lift status ?(from=1) ?(no_implicit=true) n t =
34   if n = 0 then t else lift_from status ~no_implicit from n t
35 ;;
36
37
38 (* subst t1 t2                                                          *)
39 (*  substitutes [t1] for [Rel 1] in [t2]                                *)
40 (*  if avoid_beta_redexes is true (default: false) no new beta redexes  *)
41 (*  are generated. WARNING: the substitution can diverge when t2 is not *)
42 (*  well typed and avoid_beta_redexes is true.                          *)
43 (*  map_arg is ReductionStrategy.from_env_for_unwind when psubst is     *)
44 (*  used to implement nCicReduction.unwind'                             *)
45 let rec psubst status ?(avoid_beta_redexes=false) ?(no_implicit=true) map_arg args = 
46  let nargs = List.length args in
47  let rec substaux k = function
48    | C.Rel n as t ->
49       (match n with
50       | n when n >= (k+nargs) ->
51          if nargs <> 0 then C.Rel (n - nargs) else t
52       | n when n < k -> t
53       | n (* k <= n < k+nargs *) ->
54        (try lift status ~no_implicit (k-1) (map_arg (List.nth args (n-k)))
55         with Failure _ | Invalid_argument _ -> assert false))
56    | C.Meta (i,(m,l)) as t when m >= k + nargs - 1 -> 
57        if nargs <> 0 then C.Meta (i,(m-nargs,l)) else t
58    | C.Meta (_,(m,(C.Irl l))) as t when k > l + m -> t
59    | C.Meta (i,(m,l)) -> 
60       let lctx = NCicUtils.expand_local_context l in
61        C.Meta (i,(0, 
62          C.Ctx (HExtlib.sharing_map 
63                   (fun x -> substaux k (lift status ~no_implicit m x)) lctx)))
64    | C.Implicit _ as t -> 
65        if no_implicit then assert false (* was identity *)
66        else t
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 status
78                   ~avoid_beta_redexes ~no_implicit
79                   Obj.magic [Obj.magic arg] bo) tl'
80             | _ -> if he == he' && args == tl then t else C.Appl (he'::args))
81       in
82       let tl = HExtlib.sharing_map (substaux k) tl in
83       avoid (substaux k he) tl
84    | t -> NCicUtils.map status (fun _ k -> k + 1) k substaux t
85  in
86   substaux 1
87 ;;
88
89 let subst status ?avoid_beta_redexes ?no_implicit arg = 
90   psubst status ?avoid_beta_redexes ?no_implicit(fun x -> x)[arg];;
91
92 (* subst_meta (n, C.Ctx [t_1 ; ... ; t_n]) t                                 *)
93 (*  returns the term [t] where [Rel i] is substituted with [t_i] lifted by n *)
94 (*  [t_i] is lifted as usual when it crosses an abstraction                  *)
95 (* subst_meta (n, (C.Irl _ | C.Ctx [])) t | -> lift status n t               *)
96 let subst_meta status = function 
97   | m, C.Irl _ 
98   | m, C.Ctx [] -> lift status m 
99   | m, C.Ctx l  -> psubst status (lift status m) l 
100 ;;