]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_kernel/nCicUtils.ml
update in basuc_2
[helm.git] / matita / 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 head_beta_reduce = ref (fun _ ~upto:_ _ -> assert false);;
21 let set_head_beta_reduce = (:=) head_beta_reduce;;
22
23 let expand_local_context = function
24   | C.Irl len -> 
25       let rec aux acc = function 
26         | 0 -> acc
27         | n -> aux (C.Rel n::acc) (n-1)
28       in
29        aux [] len
30   | C.Ctx lctx -> lctx
31 ;;
32
33 let lookup_subst n subst =
34   try
35     List.assoc n subst
36   with Not_found -> raise (Subst_not_found n)
37 ;;
38
39 let lookup_meta n metasenv =
40   try
41     List.assoc n metasenv
42   with Not_found -> raise (Meta_not_found n)
43 ;;
44
45 let fold g k f acc = function
46  | C.Meta _ -> assert false
47  | C.Implicit _
48  | C.Sort _
49  | C.Const _
50  | C.Rel _ -> acc
51  | C.Appl [] | C.Appl [_] -> assert false
52  | C.Appl l -> List.fold_left (f k) acc l
53  | C.Prod (n,s,t)
54  | C.Lambda (n,s,t) -> f (g (n,C.Decl s) k) (f k acc s) t
55  | C.LetIn (n,ty,t,bo) -> f (g (n,C.Def (t,ty)) k) (f k (f k acc ty) t) bo
56  | C.Match (_,oty,t,pl) -> List.fold_left (f k) (f k (f k acc oty) t) pl
57 ;;
58
59 let map status g k f = function
60  | C.Meta _ -> assert false
61  | C.Implicit _
62  | C.Sort _
63  | C.Const _
64  | C.Rel _ as t -> t
65  | C.Appl [] | C.Appl [_] -> assert false
66  | C.Appl l as orig ->
67     let fire_beta, upto = 
68       match l with C.Meta _ :: _ -> true, List.length l - 1 | _ -> false, 0 
69     in
70     let l1 = HExtlib.sharing_map (f k) l in
71     if l == l1 then orig else
72     let t =
73       match l1 with
74       | C.Appl l :: tl -> C.Appl (l@tl)
75       | l1 -> C.Appl l1
76     in
77       if fire_beta then !head_beta_reduce (status :> NCic.status) ~upto t
78       else t
79  | C.Prod (n,s,t) as orig ->
80      let s1 = f k s in let t1 = f (g (n,C.Decl s) k) t in
81      if t1 == t && s1 == s then orig else C.Prod (n,s1,t1)
82  | C.Lambda (n,s,t) as orig -> 
83      let s1 = f k s in let t1 = f (g (n,C.Decl s) k) t in
84      if t1 == t && s1 == s then orig else C.Lambda (n,s1,t1)
85  | C.LetIn (n,ty,t,b) as orig -> 
86      let ty1 = f k ty in let t1 = f k t in
87      let b1 = f (g (n,C.Def (t,ty)) k) b in
88      if ty1 == ty && t1 == t && b1 == b then orig else C.LetIn (n,ty1,t1,b1)
89  | C.Match (r,oty,t,pl) as orig -> 
90      let oty1 = f k oty in let t1 = f k t in let pl1 = HExtlib.sharing_map (f k) pl in
91      if oty1 == oty && t1 == t && pl1 == pl then orig 
92      else C.Match(r,oty1,t1,pl1)
93 ;;