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.
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_______________________________________________________________ *)
12 (* $Id: nCicSubstitution.ml 8135 2008-02-13 15:35:43Z tassi $ *)
14 exception Subst_not_found of int
15 exception Meta_not_found of int
17 let expand_local_context = function
19 let rec aux acc = function
21 | n -> aux (NCic.Rel n::acc) (n-1)
24 | NCic.Ctx lctx -> lctx
27 let lookup_subst n subst =
30 with Not_found -> raise (Subst_not_found n)
33 let lookup_meta index metasenv =
35 List.find (fun (index', _, _, _) -> index = index') metasenv
36 with Not_found -> raise (Meta_not_found index)
39 let fold g k f acc = function
40 | NCic.Meta _ -> assert false
45 | NCic.Appl [] | NCic.Appl [_] -> assert false
46 | NCic.Appl l -> List.fold_left (f k) acc l
48 | NCic.Lambda (n,s,t) -> f (g (n,NCic.Decl s) k) (f k acc s) t
49 | NCic.LetIn (n,ty,t,bo) -> f (g (n,NCic.Def (t,ty)) k) (f k (f k acc ty) t) bo
50 | NCic.Match (_,oty,t,pl) -> List.fold_left (f k) (f k (f k acc oty) t) pl
54 let unchanged = ref true in
55 let rec aux b = function
56 | [] as t -> unchanged := b; t
59 he1 :: aux (b && he1 == he) tl
61 let l1 = aux true l in
62 if !unchanged then l else l1
65 let map g k f = function
66 | NCic.Meta _ -> assert false
70 | NCic.Rel _ as t -> t
71 | NCic.Appl [] | NCic.Appl [_] -> assert false
72 | NCic.Appl l as orig ->
73 (match sharing_map (f k) l with
74 | NCic.Appl l :: tl -> NCic.Appl (l@tl)
75 | l1 when l == l1 -> orig
77 | NCic.Prod (n,s,t) as orig ->
78 let s1 = f k s in let t1 = f (g (n,NCic.Decl s) k) t in
79 if t1 == t && s1 == s then orig else NCic.Prod (n,s1,t1)
80 | NCic.Lambda (n,s,t) as orig ->
81 let s1 = f k s in let t1 = f (g (n,NCic.Decl s) k) t in
82 if t1 == t && s1 == s then orig else NCic.Lambda (n,s1,t1)
83 | NCic.LetIn (n,ty,t,b) as orig ->
84 let ty1 = f k ty in let t1 = f k t in
85 let b1 = f (g (n,NCic.Def (t,ty)) k) b in
86 if ty1 == ty && t1 == t && b1 == b then orig else NCic.LetIn (n,ty1,t1,b1)
87 | NCic.Match (r,oty,t,pl) as orig ->
88 let oty1 = f k oty in let t1 = f k t in let pl1 = sharing_map (f k) pl in
89 if oty1 == oty && t1 == t && pl1 == pl then orig
90 else NCic.Match(r,oty1,t1,pl1)
97 let rec aux k _ = function
98 | NCic.Rel n when n > k -> raise NotClosed
99 | NCic.Meta (_, (s, NCic.Irl n)) when not (n+s <= k) -> raise NotClosed
100 | NCic.Meta (_, (s, NCic.Ctx l)) -> List.iter (aux (k+s) ()) l
101 | _ -> fold (fun _ k -> k + 1) k aux () t
104 with NotClosed -> false