]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicUtils.ml
nuri_of_ouri, ouri_of_nuri, reference_of_ouri, ouri_of_reference moved
[helm.git] / helm / software / 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$ *)
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 n metasenv =
34   try
35     List.assoc n metasenv
36   with Not_found -> raise (Meta_not_found n)
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 map g k f = function
54  | NCic.Meta _ -> assert false
55  | NCic.Implicit _
56  | NCic.Sort _
57  | NCic.Const _
58  | NCic.Rel _ as t -> t
59  | NCic.Appl [] | NCic.Appl [_] -> assert false
60  | NCic.Appl l as orig ->
61     (match HExtlib.sharing_map (f k) l with
62       | NCic.Appl l :: tl -> NCic.Appl (l@tl)
63       | l1 when l == l1 -> orig
64       | l1 -> NCic.Appl l1)
65  | NCic.Prod (n,s,t) as orig ->
66      let s1 = f k s in let t1 = f (g (n,NCic.Decl s) k) t in
67      if t1 == t && s1 == s then orig else NCic.Prod (n,s1,t1)
68  | NCic.Lambda (n,s,t) as orig -> 
69      let s1 = f k s in let t1 = f (g (n,NCic.Decl s) k) t in
70      if t1 == t && s1 == s then orig else NCic.Lambda (n,s1,t1)
71  | NCic.LetIn (n,ty,t,b) as orig -> 
72      let ty1 = f k ty in let t1 = f k t in
73      let b1 = f (g (n,NCic.Def (t,ty)) k) b in
74      if ty1 == ty && t1 == t && b1 == b then orig else NCic.LetIn (n,ty1,t1,b1)
75  | NCic.Match (r,oty,t,pl) as orig -> 
76      let oty1 = f k oty in let t1 = f k t in let pl1 = HExtlib.sharing_map (f k) pl in
77      if oty1 == oty && t1 == t && pl1 == pl then orig 
78      else NCic.Match(r,oty1,t1,pl1)
79 ;;