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