]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_ag/brgType.ml
we are changing the kernel version from basic_rg to basic_ag
[helm.git] / helm / software / lambda-delta / basic_ag / brgType.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 S = Share
14 module L = Log
15 module H = Hierarchy
16 module I = AutItem
17 module B = Brg
18 module O = BrgOutput
19 module E = BrgEnvironment
20 module R = BrgReduction
21
22 exception TypeError of B.message
23
24 (* Internal functions *******************************************************)
25
26 let level = 4
27
28 let error1 s c t =
29    raise (TypeError (L.ct_items1 s c t))
30
31 let error2 s1 c1 t1 s2 c2 t2 =
32    raise (TypeError (L.ct_items2 s1 c1 t1 s2 c2 t2))
33
34 let mk_gref u l =
35    let map t v = B.Appl (v, t) in
36    List.fold_left map (B.GRef u) l
37
38 (* Interface functions ******************************************************)
39
40 let rec b_type_of f g c x = 
41    L.log O.specs level (L.ct_items1 "Now checking" c x);
42    match x with
43    | B.Sort h                 ->
44       let f h = f x (B.Sort h) in H.apply f g h
45    | B.LRef i                 ->
46       let f = function
47          | Some (_, B.Abst w)               -> f x w
48          | Some (_, B.Abbr (B.Cast (w, v))) -> f x w
49          | Some (_, B.Abbr _)               -> assert false
50          | Some (_, B.Void)                 -> 
51             error1 "reference to excluded variable" c x
52          | None                             ->
53             error1 "variable not found" c x      
54       in
55       B.get f c i
56    | B.GRef uri               ->
57       let f = function
58          | _, _, B.Abst w               -> f x w
59          | _, _, B.Abbr (B.Cast (w, v)) -> f x w
60          | _, _, B.Abbr _               -> assert false
61          | _, _, B.Void                 ->
62             error1 "reference to excluded object" c x
63       in
64       E.get_obj f uri   
65    | B.Bind (id, B.Abbr v, t) ->
66       let f xv xt tt =
67          f (S.sh2 v xv t xt x (B.bind_abbr id)) (B.bind_abbr id xv tt)
68       in
69       let f xv cc = b_type_of (f xv) g cc t in
70       let f xv = B.push (f xv) c id (B.Abbr xv) in
71       let f xv vv = match xv with 
72          | B.Cast _ -> f xv
73          | _        -> f (B.Cast (vv, xv))
74       in
75       type_of f g c v
76    | B.Bind (id, B.Abst u, t) ->
77       let f xu xt tt =
78          f (S.sh2 u xu t xt x (B.bind_abst id)) (B.bind_abst id xu tt)
79       in
80       let f xu cc = b_type_of (f xu) g cc t in
81       let f xu _ = B.push (f xu) c id (B.Abst xu) in
82       type_of f g c u
83    | B.Bind (id, B.Void, t)   ->
84       let f xt tt = 
85          f (S.sh1 t xt x (B.bind id B.Void)) (B.bind id B.Void tt)
86       in
87       let f cc = b_type_of f g cc t in
88       B.push f c id B.Void   
89    | B.Appl (v, t)            ->
90       let h xv vv xt tt cc = function
91          | R.Sort _ -> error1 "not a function" c xt
92          | R.Abst w -> 
93             L.log O.specs (succ level) (L.ct_items1 "Just scanned" cc w);
94             let f b =
95                if b then f (S.sh2 v xv t xt x B.appl) (B.appl xv tt) else
96                error2 "the term" cc xv "must be of type" cc w
97             in
98             R.are_convertible f cc w vv     
99       in
100       let f xv vv xt = function
101 (* inserting a missing "modus ponens" *)      
102          | B.Appl (y2, B.Appl (y1, B.GRef u)) when U.eq u I.imp ->
103             b_type_of f g c (mk_gref I.mp [y1; y2; xv; xt])
104          | tt -> R.ho_whd (h xv vv xt tt) c tt
105       in
106       let f xv vv = b_type_of (f xv vv) g c t in
107       type_of f g c v
108    | B.Cast (u, t)            ->
109       let f xu xt b = 
110          if b then f (S.sh2 u xu t xt x B.cast) xu else
111          error2 "the term" c xt "must be of type" c xu
112       in
113       let f xu xt tt = R.are_convertible (f xu xt) c xu tt in
114       let f xu _ = b_type_of (f xu) g c t in
115       type_of f g c u
116
117 and type_of f g c x =
118    let f t u = L.unbox (); f t u in
119    L.box (); b_type_of f g c x