]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_ag/bagType.ml
new message reporting system improves performance significatively
[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 H  = Hierarchy
17 module E  = Entity
18 module G  = Options
19 module S  = Status
20 module Z  = Bag
21 module ZO = BagOutput
22 module ZE = BagEnvironment
23 module ZR = BagReduction
24
25 (* Internal functions *******************************************************)
26
27 let level = 3
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 err st c t =
34    let sc = "In the envireonment" in
35    err (L.et_items1 sc c st t)
36
37 let error3 err 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    err (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 let rec b_type_of err f st c x = 
48 (*   L.warn "Entering T.b_type_of"; *)
49    if !G.trace >= level then log1 "Now checking" c x;
50    match x with
51    | Z.Sort h                    ->
52       let h = H.apply h in f x (Z.Sort h) 
53    | Z.LRef i                    ->
54       let err0 () = error1 err "variable not found" c x in
55       let f _ = function
56          | Z.Abst w               -> f x w
57          | Z.Abbr (Z.Cast (w, v)) -> f x w
58          | Z.Abbr _               -> assert false
59          | Z.Void                 -> error1 err "reference to excluded variable" c x     
60       in
61       Z.get err0 f c i
62    | Z.GRef uri                  ->
63       let f = function
64          | _, _, E.Abst (_, w)          -> f x w
65          | _, _, E.Abbr (Z.Cast (w, v)) -> f x w
66          | _, _, E.Abbr _               -> assert false
67          | _, _, E.Void                 -> assert false
68       in
69       ZE.get_entity f uri   
70    | Z.Bind (a, l, Z.Abbr v, t) ->
71       let f xv xt tt =
72          f (W.sh2 v xv t xt x (Z.bind_abbr a l)) (Z.bind_abbr a l xv tt)
73       in
74       let f xv cc = b_type_of err (f xv) st cc t in
75       let f xv = Z.push "type abbr" (f xv) c a l (Z.Abbr xv) in
76       let f xv vv = match xv with 
77          | Z.Cast _ -> f xv
78          | _        -> f (Z.Cast (vv, xv))
79       in
80       type_of err f st c v
81    | Z.Bind (a, l, Z.Abst u, t) ->
82       let f xu xt tt =
83          f (W.sh2 u xu t xt x (Z.bind_abst a l)) (Z.bind_abst a l xu tt)
84       in
85       let f xu cc = b_type_of err (f xu) st cc t in
86       let f xu _ = Z.push "type abst" (f xu) c a l (Z.Abst xu) in
87       type_of err f st c u
88    | Z.Bind (a, l, Z.Void, t)   ->
89       let f xt tt = 
90          f (W.sh1 t xt x (Z.bind a l Z.Void)) (Z.bind a l Z.Void tt)
91       in
92       let f cc = b_type_of err f st cc t in
93       Z.push "type void" f c a l Z.Void   
94    | Z.Appl (v, t)            ->
95       let f xv vv xt tt = function
96          | ZR.Abst w                             -> 
97             if !G.trace > level then L.log ZO.specs (succ level) (L.t_items1 "Just scanned" c w);
98             let f a =                
99 (*             L.warn (Printf.sprintf "Convertible: %b" a); *)
100                if a then f (W.sh2 v xv t xt x Z.appl) (Z.appl xv tt)
101                else error3 err c xv vv w
102             in
103             ZR.are_convertible f st c w vv
104          | _                                    -> 
105             error1 err "not a function" c xt
106       in
107       let f xv vv xt tt = ZR.ho_whd (f xv vv xt tt) st c tt in
108       let f xv vv = b_type_of err (f xv vv) st c t in
109       type_of err f st c v
110    | Z.Cast (u, t)            ->
111       let f xu xt tt a =  
112          (* L.warn (Printf.sprintf "Convertible: %b" a); *)
113          if a then f (W.sh2 u xu t xt x Z.cast) xu else error3 err c xt tt xu
114       in
115       let f xu xt tt = ZR.are_convertible (f xu xt tt) st c xu tt in
116       let f xu _ = b_type_of err (f xu) st c t in
117       type_of err f st c u
118
119 (* Interface functions ******************************************************)
120
121 and type_of err f st c x = b_type_of err f st c x