]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgValidity.ml
- we are moving from old (patched) management of sort inclusion
[helm.git] / helm / software / helena / src / basic_rg / brgValidity.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 L  = Log
13 module G  = Options
14 module E  = Entity
15 module B  = Brg
16 module BE = BrgEnvironment
17 module BR = BrgReduction
18
19 (* Internal functions *******************************************************)
20
21 let level = 3
22
23 let warn s = L.warn level s
24
25 let message1 st1 m t1 =
26    L.et_items1 "In the environment" m st1 t1
27
28 let log1 st s m t =
29    let s =  s ^ " the term" in
30    L.log st BR.specs level (message1 s m t) 
31
32 let error1 err s m t =
33    err (message1 s m t)
34
35 let message2 m1 t1 m2 t2 =    
36    let sm2, st2 = "In the environment", "the term" in
37    let sm1, st1 = "is valid, but in the environment", "it must be of type" in
38    L.et_items2 sm2 m2 st2 t2 ~sc2:sm1 ~c2:m1 st1 t1
39
40 let error2 err m1 t1 m2 t2 =
41    err (message2 m1 t1 m2 t2)
42
43 let zero = Some 0
44
45 let one = Some 1
46
47 let assert_convertibility err f st m u t =
48    if !G.trace >= level then warn "Asserting convertibility for cast";
49    if BR.are_convertible st m zero u m one t then f () else
50    error2 err m u m t
51
52 let assert_applicability err f st m v t =
53    if !G.trace >= level then warn "Asserting applicability";
54    match BR.xwhd st m None t with 
55       | _, B.Sort _                      ->
56          error1 err "not a function" m t
57       | mw, B.Bind (_, B.Abst (_, w), _) ->
58          if !G.trace >= level then warn "Asserting convertibility for application";
59          if BR.are_convertible st mw zero w m one v then f () else
60          error2 err mw w m v
61       | _                                -> assert false (**)
62
63 let rec b_validate err f st m x =
64    if !G.trace >= level then log1 st "Now checking" m x;
65    match x with
66    | B.Sort _         -> f ()
67    | B.LRef (_, i)    ->
68       begin match BR.get m i with
69          | B.Abst _
70          | B.Abbr _ -> f ()
71          | B.Void   -> 
72             error1 err "reference to excluded variable" m x
73       end
74    | B.GRef (_, uri)  ->
75       begin match BE.get_entity uri with
76          | _, _, _, E.Abst _
77          | _, _, _, E.Abbr _ -> f ()
78          | _, _, _, E.Void   ->
79             error1 err "reference to unknown entry" m x
80       end
81    | B.Bind (a, b, t) ->
82       let f () = b_validate err f st (BR.push m a b) t in
83       begin match b with 
84          | B.Abst (n, u) -> validate err f st m u
85          | B.Abbr v      -> validate err f st m v
86          | B.Void        -> f ()
87       end
88    | B.Appl (_, v, t)        ->
89       let f () = assert_applicability err f st m v t in
90       let f () = b_validate err f st m t in
91       validate err f st m v
92    | B.Cast (_, u, t)        ->
93       let f () = assert_convertibility err f st m u t in
94       let f () = b_validate err f st m t in
95       validate err f st m u
96
97 (* Interface functions ******************************************************)
98
99 and validate err f st m x = b_validate err f st m x