]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/assembly/compiler/environment.ma
AST to ASTFE completed up to a few computational (!!!) axioms.
[helm.git] / helm / software / matita / contribs / assembly / compiler / environment.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 (* ********************************************************************** *)
16 (*                                                                        *)
17 (* Sviluppato da:                                                         *)
18 (*   Cosimo Oliboni, oliboni@cs.unibo.it                                  *)
19 (*                                                                        *)
20 (* ********************************************************************** *)
21
22 include "string/string.ma".
23 include "freescale/word32.ma".
24 include "compiler/ast_type.ma".
25
26 (* ***************** *)
27 (* GESTIONE AMBIENTE *)
28 (* ***************** *)
29
30 (* descrittore: const + type *)
31 inductive desc_elem : Type ≝
32 DESC_ELEM: bool → ast_type → desc_elem.
33
34 (* elemento: name + desc *)
35 inductive var_elem : Type ≝
36 VAR_ELEM: aux_str_type → desc_elem → var_elem.
37
38 (* ambiente globale: (ambiente base + ambienti annidati) *)
39 definition aux_env_type ≝ ne_list (list var_elem).
40
41 (* getter *)
42 definition get_const_desc ≝ λd:desc_elem.match d with [ DESC_ELEM c _ ⇒ c ].
43 definition get_type_desc ≝ λd:desc_elem.match d with [ DESC_ELEM _ t ⇒ t ].
44
45 definition get_name_var ≝ λv:var_elem.match v with [ VAR_ELEM n _ ⇒ n ].
46 definition get_desc_var ≝ λv:var_elem.match v with [ VAR_ELEM _ d ⇒ d ].
47
48 (* ambiente vuoto *)
49 definition empty_env ≝ ne_nil ? (nil var_elem ).
50
51 (* confronto *)
52 let rec eq_aux_env_type_aux (subE,subE':list var_elem) on subE ≝
53  match subE with
54   [ nil ⇒ match subE' with
55    [ nil ⇒ true | cons _ _ ⇒ false ]
56   | cons h tl ⇒ match subE' with
57    [ nil ⇒ false | cons h' tl' ⇒ (strCmp_str (get_name_var h) (get_name_var h'))⊗
58                                  (eq_bool (get_const_desc (get_desc_var h)) (get_const_desc (get_desc_var h')))⊗
59                                  (eq_ast_type (get_type_desc (get_desc_var h)) (get_type_desc (get_desc_var h')))⊗   
60                                  (eq_aux_env_type_aux tl tl') ]
61   ].
62
63 axiom eqbauxenvtypeaux_to_eq : ∀e,e'.eq_aux_env_type_aux e e' = true → e = e'.
64
65 let rec eq_aux_env_type (e,e':aux_env_type) on e ≝
66  match e with
67   [ ne_nil h ⇒ match e' with
68    [ ne_nil h' ⇒ eq_aux_env_type_aux h h' | ne_cons _ _ ⇒ false ]
69   | ne_cons h tl ⇒ match e' with
70    [ ne_nil h' ⇒ false | ne_cons h' tl' ⇒ (eq_aux_env_type_aux h h')⊗(eq_aux_env_type tl tl') ]
71   ].
72
73 axiom eqbauxenvtype_to_eq : ∀e,e':aux_env_type.eq_aux_env_type e e' = true → e = e'.
74
75 (* setter *)
76 definition enter_env ≝ λe:aux_env_type.empty_env&e.
77 definition leave_env ≝ λe:aux_env_type.cut_first_neList ? e.
78
79 (* recupera descrittore da ambiente: il primo trovato, ma e' anche l'unico *)
80 let rec get_desc_from_lev_env (env:list var_elem ) (str:aux_str_type) on env ≝
81 match env with
82  [ nil ⇒ None ?
83  | cons h t ⇒ match strCmp_str str (get_name_var h) with
84   [ true ⇒ Some ? (get_desc_var h)
85   | false ⇒ get_desc_from_lev_env t str ]].
86
87 (* recupera descrittore da ambiente globale: il primo trovato *)
88 let rec get_desc_env_aux (env:aux_env_type) (str:aux_str_type) on env ≝
89  match env with
90   [ ne_nil h ⇒ get_desc_from_lev_env h str 
91   | ne_cons h t ⇒  match get_desc_from_lev_env h str with 
92    [ None ⇒ get_desc_env_aux t str | Some res' ⇒ Some ? res' ]
93   ].
94
95 definition check_desc_env ≝ λe:aux_env_type.λstr:aux_str_type.
96  match get_desc_env_aux e str with [ None ⇒ False | Some _ ⇒ True ].
97
98 definition checkb_desc_env ≝ λe:aux_env_type.λstr:aux_str_type.
99  match get_desc_env_aux e str with [ None ⇒ false | Some _ ⇒ true ].
100
101 lemma checkbdescenv_to_checkdescenv : ∀e,str.checkb_desc_env e str = true → check_desc_env e str.
102  unfold checkb_desc_env;
103  unfold check_desc_env;
104  intros;
105  letin K ≝ (get_desc_env_aux e str);
106  elim K;
107  [ normalize; autobatch |
108    normalize; autobatch ]
109 qed.
110
111 definition get_desc_env ≝ λe:aux_env_type.λstr:aux_str_type.
112  match get_desc_env_aux e str with
113   [ None ⇒ DESC_ELEM true (AST_TYPE_BASE AST_BASE_TYPE_BYTE8) | Some x ⇒ x ].
114
115 definition check_not_already_def_env ≝ λe:aux_env_type.λstr:aux_str_type.
116  match get_desc_from_lev_env (get_first_neList ? e) str with [ None ⇒ True | Some _ ⇒ False ].
117
118 definition checkb_not_already_def_env ≝ λe:aux_env_type.λstr:aux_str_type.
119  match get_desc_from_lev_env (get_first_neList ? e) str with [ None ⇒ true | Some _ ⇒ false ].
120
121 lemma checkbnotalreadydefenv_to_checknotalreadydefenv : ∀e,str.checkb_not_already_def_env e str = true → check_not_already_def_env e str.
122  unfold checkb_not_already_def_env;
123  unfold check_not_already_def_env;
124  intros;
125  letin K ≝ (get_desc_from_lev_env (get_first_neList ? e) str);
126  elim K;
127  [ normalize; autobatch |
128    normalize; autobatch ]
129 qed.
130
131 (* aggiungi descrittore ad ambiente globale: in testa al primo ambiente *)
132 definition add_desc_env ≝
133 λe:aux_env_type.λstr:aux_str_type.λc:bool.λdesc:ast_type.
134 (*let var ≝ VAR_ELEM str (DESC_ELEM c desc) in*)
135  match e with
136   [ ne_nil h ⇒ ne_nil ? ((VAR_ELEM str (DESC_ELEM c desc))::h)
137   | ne_cons h tl ⇒ «((VAR_ELEM str (DESC_ELEM c desc))::h) £ »&tl
138   ].