source: src/common/Identifiers.ma @ 1053

Last change on this file since 1053 was 1053, checked in by mulligan, 8 years ago

changes

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