]> matita.cs.unibo.it Git - helm.git/blob - components/ng_kernel/nCicUtils.ml
tagged 0.5.0-rc1
[helm.git] / components / ng_kernel / nCicUtils.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: nCicSubstitution.ml 8135 2008-02-13 15:35:43Z tassi $ *)
13
14 exception Subst_not_found of int
15 exception Meta_not_found of int
16
17 let expand_local_context = function
18   | NCic.Irl len -> 
19       let rec aux acc = function 
20         | 0 -> acc
21         | n -> aux (NCic.Rel n::acc) (n-1)
22       in
23        aux [] len
24   | NCic.Ctx lctx -> lctx
25 ;;
26
27 let lookup_subst n subst =
28   try
29     List.assoc n subst
30   with Not_found -> raise (Subst_not_found n)
31 ;;
32
33 let lookup_meta index metasenv =
34   try
35     List.find (fun (index', _, _, _) -> index = index') metasenv
36   with Not_found -> raise (Meta_not_found index)
37 ;;
38
39 let fold g k f acc = function
40  | NCic.Meta _ -> assert false
41  | NCic.Implicit _
42  | NCic.Sort _
43  | NCic.Const _
44  | NCic.Rel _ -> acc
45  | NCic.Appl [] | NCic.Appl [_] -> assert false
46  | NCic.Appl l -> List.fold_left (f k) acc l
47  | NCic.Prod (n,s,t)
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
51 ;;
52
53 let sharing_map f l =
54   let unchanged = ref true in
55   let rec aux b = function
56     | [] as t -> unchanged := b; t
57     | he::tl ->
58         let he1 = f he in
59         he1 :: aux (b && he1 == he) tl
60   in
61   let l1 = aux true l in
62   if !unchanged then l else l1
63 ;;
64         
65 let map g k f = function
66  | NCic.Meta _ -> assert false
67  | NCic.Implicit _
68  | NCic.Sort _
69  | NCic.Const _
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
76       | l1 -> NCic.Appl l1)
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)
91 ;;
92
93 exception NotClosed
94
95 let is_closed t = 
96   try
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
102     in 
103       aux 0 () t; true
104   with NotClosed -> false
105 ;;
106