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