]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/basic_ag/bagCrg.ml
dcc495232f77181cea5ab6e05847bc435b891d96
[helm.git] / helm / software / lambda-delta / src / basic_ag / bagCrg.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 module C = Cps
13 module E = Entity
14 module J = Marks
15 module N = Level
16 module D = Crg
17 module Z = Bag
18
19 (* internal functions: crg to bag term **************************************)
20
21 let rec shift t = function
22    | _, []                    -> t
23    | (a, l, b) :: c, _ :: ns -> shift (Z.Bind (a, l, b, t)) (c, ns)
24    | _                        -> assert false
25
26 let rec xlate_term xlate_bind f c = function
27    | D.TSort (_, h)     -> f (Z.Sort h)
28    | D.TGRef (_, s)     -> f (Z.GRef s)
29    | D.TLRef (a, _, _)  -> 
30       let f i = 
31          let _, l, _ = List.nth c i in 
32          f (Z.LRef l)
33       in 
34       E.apix C.err f a
35    | D.TCast (_, u, t)  ->
36       let f tt uu = f (Z.Cast (uu, tt)) in
37       let f tt = xlate_term xlate_bind (f tt) c u in
38       xlate_term xlate_bind f c t
39    | D.TAppl (_, vs, t) ->
40       let map f v tt = let f vv = f (Z.Appl (vv, tt)) in xlate_term xlate_bind f c v in
41       let f tt = C.list_fold_right f map vs tt in
42       xlate_term xlate_bind f c t
43    | D.TProj (_, e, t)  ->
44       xlate_term xlate_bind f c (D.tshift e t)
45    | D.TBind (ab, D.Abst (n, ws), D.TCast (ac, u, t)) ->
46       xlate_term xlate_bind f c (D.TCast (ac, D.TBind (ab, D.Abst (N.pred n, ws), u), D.TBind (ab, D.Abst (n, ws), t)))
47    | D.TBind (a, b, t)  ->
48       let g _ ns = ns in
49       let ns = E.get_names g a in  
50       let cc = xlate_bind C.start c ns b in
51       let f tt = f (shift tt (cc, ns)) in
52       xlate_term xlate_bind f cc t
53
54 let rec xlate_bind f c ns = function
55    | D.Abst (_, ws) ->
56       let map f n w c =
57          let f ww = Z.push "xlate_bind" f c [n] (J.new_location ()) (Z.Abst ww) in 
58          xlate_term xlate_bind f c w
59       in
60       C.list_fold_right2 f map ns ws c
61    | D.Abbr vs      ->
62       let map f n v c = 
63          let f vv = Z.push "xlate_bind" f c [n] (J.new_location ()) (Z.Abbr vv) in
64          xlate_term xlate_bind f c v
65       in
66       C.list_fold_right2 f map ns vs c
67    | D.Void _       ->
68       let map f n c = Z.push "xlate_bind" f c [n] (J.new_location ()) Z.Void in
69       C.list_fold_right f map ns c
70
71 (* internal functions: bag to crg term **************************************)
72
73 let rec xlate_bk_term f c = function
74    | Z.Sort h            -> f (D.TSort ([], h))
75    | Z.GRef s            -> f (D.TGRef ([], s))
76    | Z.LRef l            -> 
77        let f i = f (D.TLRef ([], i, 0)) in
78        Z.nth C.err f l c
79    | Z.Cast (u, t)  ->
80       let f tt uu = f (D.TCast ([], uu, tt)) in
81       let f tt = xlate_bk_term (f tt) c u in
82       xlate_bk_term f c t 
83    | Z.Appl (u, t)       ->
84       let f tt uu = f (D.TAppl ([], [uu], tt)) in
85       let f tt = xlate_bk_term (f tt) c u in
86       xlate_bk_term f c t 
87    | Z.Bind (a, l, b, t) ->
88       let f tt bb = f (D.TBind (a, bb, tt)) in      
89       let f tt = xlate_bk_bind (f tt) c b in
90       let cc = Z.push "xlate_bk_term" C.start c a l b in
91       xlate_bk_term f cc t
92
93 and xlate_bk_bind f c = function
94    | Z.Abst t ->
95       let f tt = f (D.Abst (N.infinite, [tt])) in
96       xlate_bk_term f c t
97    | Z.Abbr t ->
98       let f tt = f (D.Abbr [tt]) in
99       xlate_bk_term f c t
100    | Z.Void   -> f (D.Void 1)
101
102 (* interface functions ******************************************************)
103
104 let bag_of_crg f t =
105    xlate_term xlate_bind f Z.empty_lenv t
106
107 let crg_of_bag f t = xlate_bk_term f Z.empty_lenv t