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