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