source: src/common/Identifiers.ma @ 1055

Last change on this file since 1055 was 1055, checked in by mulligan, 9 years ago

changes to how identifiers are generated

File size: 4.3 KB
Line 
1
2include "basics/types.ma".
3include "ASM/String.ma".
4include "ASM/Arithmetic.ma".
5include "common/Errors.ma".
6
7(* identifiers and their generators are tagged to differentiate them, and to
8   provide extra type checking. *)
9
10(* in common/PreIdentifiers.ma, via Errors.ma.
11inductive identifier (tag:String) : Type[0] ≝
12  an_identifier : Word → identifier tag.
13*)
14
15record universe (tag:String) : Type[0] ≝
16{
17  next_identifier : Word;
18  counter_overflow: bool
19}.
20
21definition new_universe : ∀tag:String. universe tag ≝
22  λtag. mk_universe tag (zero ?) false.
23
24definition fresh ≝
25  λtag.
26  λuniv: universe tag.
27  let 〈gen, carries〉 ≝ add_with_carries ? (next_identifier ? univ) (zero ?) true in
28    if get_index_v … carries 0 ? then
29      〈an_identifier tag (next_identifier ? univ), mk_universe tag gen true〉
30    else
31      〈an_identifier tag (next_identifier ? univ), mk_universe tag gen false〉.
32  //
33qed.
34
35definition eq_identifier ≝
36  λt.
37  λl, r: identifier t.
38  match l with
39  [ an_identifier l' ⇒
40    match r with
41    [ an_identifier r' ⇒
42      eq_bv ? l' r'
43    ]
44  ].
45   
46definition word_of_identifier ≝
47  λt.
48  λl: identifier t.
49  match l with   
50  [ an_identifier l' ⇒ l'
51  ].
52
53definition identifier_eq : ∀tag:String. ∀x,y:identifier tag. (x=y) + (x≠y).
54#tag * #x * #y lapply (refl ? (eq_bv ? x y)) cases (eq_bv ? x y) in ⊢ (???% → %)
55#E [ % | %2 ]
56lapply E @eq_bv_elim
57[ #H #_ >H @refl | 2,3: #_ #H destruct | #H #_ % #H' destruct /2/ ]
58qed.
59
60definition identifier_of_nat : ∀tag:String. nat → identifier tag ≝
61  λtag,n. an_identifier tag (bitvector_of_nat ? n).
62
63
64(* Maps from identifiers to arbitrary types. *)
65
66include "ASM/BitVectorTrie.ma".
67
68inductive identifier_map (tag:String) (A:Type[0]) : Type[0] ≝
69  an_id_map : BitVectorTrie A 16 → identifier_map tag A.
70 
71definition empty_map : ∀tag:String. ∀A. identifier_map tag A ≝
72  λtag,A. an_id_map tag A (Stub A 16).
73
74definition lookup : ∀tag,A. identifier_map tag A → identifier tag → option A ≝
75  λtag,A,m,l. lookup_opt A 16 (match l with [ an_identifier l' ⇒ l' ])
76                              (match m with [ an_id_map m' ⇒ m' ]).
77
78(* Always adds the identifier to the map. *)
79definition add : ∀tag,A. identifier_map tag A → identifier tag → A → identifier_map tag A ≝
80λtag,A,m,l,a. an_id_map tag A (insert A 16 (match l with [ an_identifier l' ⇒ l' ]) a
81                                           (match m with [ an_id_map m' ⇒ m' ])).
82
83axiom MissingId : String.
84
85(* Only updates an existing entry; fails with an error otherwise. *)
86definition update : ∀tag,A. identifier_map tag A → identifier tag → A → res (identifier_map tag A) ≝
87λtag,A,m,l,a.
88  match update A 16 (match l with [ an_identifier l' ⇒ l' ]) a
89                    (match m with [ an_id_map m' ⇒ m' ]) with
90  [ None ⇒ Error ? ([MSG MissingId; CTX tag l]) (* missing identifier *)
91  | Some m' ⇒ OK ? (an_id_map tag A m')
92  ].
93
94(* Sets *)
95
96inductive identifier_set (tag:String) : Type[0] ≝
97  an_id_set : BitVectorTrie unit 16 → identifier_set tag.
98
99definition empty_set : ∀tag:String. identifier_set tag ≝
100λtag. an_id_set tag (Stub unit 16).
101
102definition add_set : ∀tag:String. identifier_set tag → identifier tag → identifier_set tag ≝
103λtag,s,i. an_id_set tag (insert unit 16 (match i with [ an_identifier i' ⇒ i' ])
104                                     it (match s with [ an_id_set s' ⇒ s' ])).
105
106definition singleton_set : ∀tag:String. identifier tag → identifier_set tag ≝
107λtag,i. add_set tag (empty_set tag) i.
108
109definition mem_set : ∀tag:String. identifier_set tag → identifier tag → bool ≝
110λtag,s,i. match lookup_opt ? 16 (match i with [ an_identifier i' ⇒ i' ])
111                                (match s with [ an_id_set s' ⇒ s' ]) with
112          [ None ⇒ false
113          | Some _ ⇒ true
114          ].
115
116definition union_set : ∀tag:String. identifier_set tag → identifier_set tag → identifier_set tag ≝
117λtag,s,s'. an_id_set tag (merge unit 16 (match s with [ an_id_set s0 ⇒ s0 ])
118                                        (match s' with [ an_id_set s1 ⇒ s1 ])).
119
120interpretation "identifier set union" 'union a b = (union_set ? a b).
121notation "∅" non associative with precedence 90 for @{ 'empty }.
122interpretation "empty identifier set" 'empty = (empty_set ?).
123interpretation "singleton identifier set" 'singl a = (add_set ? (empty_set ?) a).
Note: See TracBrowser for help on using the repository browser.