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