]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_ag/bagType.ml
refactoring ...
[helm.git] / helm / software / helena / src / basic_ag / bagType.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 U  = NUri
13 module C  = Cps
14 module W  = Share
15 module L  = Log
16 module E  = Entity
17 module H  = Hierarchy
18 module S  = Status
19 module Z  = Bag
20 module ZO = BagOutput
21 module ZE = BagEnvironment
22 module ZR = BagReduction
23
24 exception TypeError of Z.message
25
26 (* Internal functions *******************************************************)
27
28 let level = 4
29
30 let log1 s c t =
31    let sc, st = s ^ " in the envireonment", "the term" in
32    L.log ZO.specs level (L.et_items1 sc c st t)
33
34 let error1 st c t =
35    let sc = "In the envireonment" in
36    raise (TypeError (L.et_items1 sc c st t))
37
38 let error3 c t1 t2 t3 =
39    let sc, st1, st2, st3 = 
40       "In the envireonment", "the term", "is of type", "but must be of type"
41    in
42    raise (TypeError (L.et_items3 sc c st1 t1 st2 t2 st3 t3))
43
44 let mk_gref u l =
45    let map t v = Z.Appl (v, t) in
46    List.fold_left map (Z.GRef u) l
47
48 (* Interface functions ******************************************************)
49
50 let rec b_type_of f st c x = 
51 (*   L.warn "Entering T.b_type_of"; *)
52    log1 "Now checking" c x;
53    match x with
54    | Z.Sort h                    ->
55       let h = H.apply h in f x (Z.Sort h) 
56    | Z.LRef i                    ->
57       let err () = error1 "variable not found" c x in
58       let f _ = function
59          | Z.Abst w               -> f x w
60          | Z.Abbr (Z.Cast (w, v)) -> f x w
61          | Z.Abbr _               -> assert false
62          | Z.Void                 -> error1 "reference to excluded variable" c x         
63       in
64       Z.get err f c i
65    | Z.GRef uri                  ->
66       let f = function
67          | _, _, E.Abst (_, w)          -> f x w
68          | _, _, E.Abbr (Z.Cast (w, v)) -> f x w
69          | _, _, E.Abbr _               -> assert false
70          | _, _, E.Void                 -> assert false
71       in
72       ZE.get_entity f uri   
73    | Z.Bind (a, l, Z.Abbr v, t) ->
74       let f xv xt tt =
75          f (W.sh2 v xv t xt x (Z.bind_abbr a l)) (Z.bind_abbr a l xv tt)
76       in
77       let f xv cc = b_type_of (f xv) st cc t in
78       let f xv = Z.push "type abbr" (f xv) c a l (Z.Abbr xv) in
79       let f xv vv = match xv with 
80          | Z.Cast _ -> f xv
81          | _        -> f (Z.Cast (vv, xv))
82       in
83       type_of f st c v
84    | Z.Bind (a, l, Z.Abst u, t) ->
85       let f xu xt tt =
86          f (W.sh2 u xu t xt x (Z.bind_abst a l)) (Z.bind_abst a l xu tt)
87       in
88       let f xu cc = b_type_of (f xu) st cc t in
89       let f xu _ = Z.push "type abst" (f xu) c a l (Z.Abst xu) in
90       type_of f st c u
91    | Z.Bind (a, l, Z.Void, t)   ->
92       let f xt tt = 
93          f (W.sh1 t xt x (Z.bind a l Z.Void)) (Z.bind a l Z.Void tt)
94       in
95       let f cc = b_type_of f st cc t in
96       Z.push "type void" f c a l Z.Void   
97    | Z.Appl (v, t)            ->
98       let f xv vv xt tt = function
99          | ZR.Abst w                             -> 
100             L.box (succ level);
101             L.log ZO.specs (succ level) (L.t_items1 "Just scanned" c w);
102             L.unbox (succ level);
103             let f a =                
104 (*             L.warn (Printf.sprintf "Convertible: %b" a); *)
105                if a then f (W.sh2 v xv t xt x Z.appl) (Z.appl xv tt)
106                else error3 c xv vv w
107             in
108             ZR.are_convertible f ~si:st.S.si c w vv
109          | _                                    -> 
110             error1 "not a function" c xt
111       in
112       let f xv vv xt tt = ZR.ho_whd (f xv vv xt tt) c tt in
113       let f xv vv = b_type_of (f xv vv) st c t in
114       type_of f st c v
115    | Z.Cast (u, t)            ->
116       let f xu xt tt a =  
117          (* L.warn (Printf.sprintf "Convertible: %b" a); *)
118          if a then f (W.sh2 u xu t xt x Z.cast) xu else error3 c xt tt xu
119       in
120       let f xu xt tt = ZR.are_convertible (f xu xt tt) ~si:st.S.si c xu tt in
121       let f xu _ = b_type_of (f xu) st c t in
122       type_of f st c u
123
124 and type_of f st c x =
125    let f t u = L.unbox level; f t u in
126    L.box level; b_type_of f st c x