]> matita.cs.unibo.it Git - helm.git/blob - components/ng_kernel/nCicUtils.ml
matita 0.5.1 tagged
[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$ *)
13
14 module C = NCic
15 module Ref = NReference
16
17 exception Subst_not_found of int
18 exception Meta_not_found of int
19
20 let expand_local_context = function
21   | C.Irl len -> 
22       let rec aux acc = function 
23         | 0 -> acc
24         | n -> aux (C.Rel n::acc) (n-1)
25       in
26        aux [] len
27   | C.Ctx lctx -> lctx
28 ;;
29
30 let lookup_subst n subst =
31   try
32     List.assoc n subst
33   with Not_found -> raise (Subst_not_found n)
34 ;;
35
36 let lookup_meta n metasenv =
37   try
38     List.assoc n metasenv
39   with Not_found -> raise (Meta_not_found n)
40 ;;
41
42 let fold g k f acc = function
43  | C.Meta _ -> assert false
44  | C.Implicit _
45  | C.Sort _
46  | C.Const _
47  | C.Rel _ -> acc
48  | C.Appl [] | C.Appl [_] -> assert false
49  | C.Appl l -> List.fold_left (f k) acc l
50  | C.Prod (n,s,t)
51  | C.Lambda (n,s,t) -> f (g (n,C.Decl s) k) (f k acc s) t
52  | C.LetIn (n,ty,t,bo) -> f (g (n,C.Def (t,ty)) k) (f k (f k acc ty) t) bo
53  | C.Match (_,oty,t,pl) -> List.fold_left (f k) (f k (f k acc oty) t) pl
54 ;;
55
56 let map g k f = function
57  | C.Meta _ -> assert false
58  | C.Implicit _
59  | C.Sort _
60  | C.Const _
61  | C.Rel _ as t -> t
62  | C.Appl [] | C.Appl [_] -> assert false
63  | C.Appl l as orig ->
64     (match HExtlib.sharing_map (f k) l with
65       | C.Appl l :: tl -> C.Appl (l@tl)
66       | l1 when l == l1 -> orig
67       | l1 -> C.Appl l1)
68  | C.Prod (n,s,t) as orig ->
69      let s1 = f k s in let t1 = f (g (n,C.Decl s) k) t in
70      if t1 == t && s1 == s then orig else C.Prod (n,s1,t1)
71  | C.Lambda (n,s,t) as orig -> 
72      let s1 = f k s in let t1 = f (g (n,C.Decl s) k) t in
73      if t1 == t && s1 == s then orig else C.Lambda (n,s1,t1)
74  | C.LetIn (n,ty,t,b) as orig -> 
75      let ty1 = f k ty in let t1 = f k t in
76      let b1 = f (g (n,C.Def (t,ty)) k) b in
77      if ty1 == ty && t1 == t && b1 == b then orig else C.LetIn (n,ty1,t1,b1)
78  | C.Match (r,oty,t,pl) as orig -> 
79      let oty1 = f k oty in let t1 = f k t in let pl1 = HExtlib.sharing_map (f k) pl in
80      if oty1 == oty && t1 == t && pl1 == pl then orig 
81      else C.Match(r,oty1,t1,pl1)
82 ;;