Skip to content

Commit 92d80e9

Browse files
authored
Make Map.kvs private and Map.toList opaque (#880)
Signed-off-by: Craig Disselkoen <cdiss@amazon.com>
1 parent 1707103 commit 92d80e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+558
-628
lines changed

cedar-lean/Cedar/Data/Map.lean

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ deriving Repr, DecidableEq, Inhabited
3737

3838
namespace Map
3939

40-
public abbrev kvs {α : Type u} {β : Type v} (m : Map α β) := m.1
40+
private abbrev kvs {α : Type u} {β : Type v} (m : Map α β) := m.1
4141

4242
----- Definitions -----
4343

@@ -51,18 +51,17 @@ public def empty {α β} : Map α β := .mk []
5151
/-- Returns an ordered and duplicate free list provided the given map is well-formed. -/
5252
public def toList {α : Type u} {β : Type v} (m : Map α β) : List (Prod α β) := m.kvs
5353

54-
5554
/-- Returns the keys of `m` as a set. -/
5655
public def keys {α β} (m : Map α β) : Set α :=
57-
Set.mk (m.kvs.map Prod.fst) -- well-formed by construction
56+
Set.mk (m.toList.map Prod.fst) -- well-formed by construction
5857

5958
/-- Returns the values of `m` as a list. -/
6059
public def values {α β} (m : Map α β) : List β :=
61-
m.kvs.map Prod.snd
60+
m.toList.map Prod.snd
6261

6362
/-- Returns the binding for `k` in `m`, if any. -/
6463
public def find? {α β} [BEq α] (m : Map α β) (k : α) : Option β :=
65-
match m.kvs.find? (λ ⟨k', _⟩ => k' == k) with
64+
match m.toList.find? (λ ⟨k', _⟩ => k' == k) with
6665
| some (_, v) => some v
6766
| _ => none
6867

@@ -84,23 +83,26 @@ public def find! {α β} [Repr α] [BEq α] [Inhabited β] (m : Map α β) (k :
8483

8584
/-- Filters `m` using `f`. -/
8685
public def filter {α β} (f : α → β → Bool) (m : Map α β) : Map α β :=
87-
Map.mk (m.kvs.filter (λ ⟨k, v⟩ => f k v))
86+
Map.mk (m.toList.filter (λ ⟨k, v⟩ => f k v))
8887

8988
public def size {α β} (m : Map α β) : Nat :=
90-
m.kvs.length
89+
m.toList.length
9190

9291
public def mapOnValues {α β γ} (f : β → γ) (m : Map α β) : Map α γ :=
93-
Map.mk (m.kvs.map (λ (k, v) => (k, f v)))
92+
Map.mk (m.toList.map (λ (k, v) => (k, f v)))
93+
94+
public def mapOnValues₃ {α β γ} (f : β → γ) (m : Map α β) : Map α γ :=
95+
Map.mk (m.toList.map₃ (λ ⟨(k, v), _⟩ => (k, f v)))
9496

9597
public def mapOnKeys {α β γ} [LT γ] [DecidableLT γ] (f : α → γ) (m : Map α β) : Map γ β :=
96-
Map.make (m.kvs.map (λ (k, v) => (f k, v)))
98+
Map.make (m.toList.map (λ (k, v) => (f k, v)))
9799

98100
public def mapMOnValues {α β γ} [LT α] [DecidableLT α] [Monad m] (f : β → m γ) (map : Map α β) : m (Map α γ) := do
99-
let kvs ← map.kvs.mapM (λ (k, v) => f v >>= λ v' => pure (k, v'))
101+
let kvs ← map.toList.mapM (λ (k, v) => f v >>= λ v' => pure (k, v'))
100102
pure (Map.mk kvs)
101103

102104
public def mapMOnKeys {α β γ} [LT γ] [DecidableLT γ] [Monad m] (f : α → m γ) (map : Map α β) : m (Map γ β) := do
103-
let kvs ← map.kvs.mapM (λ (k, v) => f k >>= λ k' => pure (k', v))
105+
let kvs ← map.toList.mapM (λ (k, v) => f k >>= λ k' => pure (k', v))
104106
pure (Map.make kvs)
105107

106108
public def wellFormed {α β} [LT α] [DecidableLT α] (m : Map α β) : Bool :=
@@ -109,17 +111,17 @@ public def wellFormed {α β} [LT α] [DecidableLT α] (m : Map α β) : Bool :=
109111
----- Instances -----
110112

111113
public instance [LT (Prod α β)] : LT (Map α β) where
112-
lt a b := a.kvs < b.kvs
114+
lt a b := a.1 < b.1
113115

114116
public instance decLt [LT (Prod α β)] [DecidableEq (Prod α β)] [DecidableLT (Prod α β)] : (n m : Map α β) → Decidable (n < m)
115117
| .mk nkvs, .mk mkvs => List.decidableLT nkvs mkvs
116118

117119
-- enables ∈ notation for map keys
118120
public instance : Membership α (Map α β) where
119-
mem m a := List.Mem a (m.kvs.map Prod.fst)
121+
mem m a := List.Mem a (m.toList.map Prod.fst)
120122

121123
public instance [LT α] [DecidableLT α] : HAppend (Map α β) (Map α β) (Map α β) where
122-
hAppend a b := Map.make (a.kvs ++ b.kvs)
124+
hAppend a b := Map.make (a.toList ++ b.toList)
123125

124126
end Map
125127

cedar-lean/Cedar/Data/SizeOf.lean

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
module
1818

1919
public import Cedar.Data.Map
20+
import all Cedar.Data.Map -- allow accessing internals of Cedar.Data.Map, necessary for sizeOf calculations
2021
public import Cedar.Data.Set
2122

2223
/-!
@@ -44,21 +45,21 @@ public theorem sizeOf_lt_of_value [SizeOf α] [SizeOf β] {m : Map α β} {k :
4445
omega
4546
omega
4647

47-
public theorem sizeOf_lt_of_kvs [SizeOf α] [SizeOf β] (m : Map α β) :
48-
sizeOf m.kvs < sizeOf m
48+
public theorem sizeOf_lt_of_toList [SizeOf α] [SizeOf β] (m : Map α β) :
49+
sizeOf m.toList < sizeOf m
4950
:= by
50-
unfold kvs
51+
unfold toList kvs
5152
conv => rhs ; unfold sizeOf _sizeOf_inst _sizeOf_1 ; simp only
5253
generalize sizeOf m.1 = s
5354
omega
5455

5556
public theorem sizeOf_lt_of_tl [SizeOf α] [SizeOf β] {m : Map α β} {tl : List (α × β)}
56-
(h : m.kvs = (k, v) :: tl) :
57+
(h : m.toList = (k, v) :: tl) :
5758
1 + sizeOf tl < sizeOf m
5859
:= by
5960
conv => rhs ; unfold sizeOf _sizeOf_inst _sizeOf_1
6061
simp only
61-
unfold kvs at h
62+
unfold toList at h
6263
simp only [h, List.cons.sizeOf_spec, Prod.mk.sizeOf_spec]
6364
omega
6465

cedar-lean/Cedar/SymCC/Concretizer.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def Prim.entityUIDs : Prim → Set EntityUID
3939
def Value.entityUIDs : Value → Set EntityUID
4040
| .prim p => p.entityUIDs
4141
| .set (Set.mk vs) => vs.mapUnion₁ (λ ⟨v, _⟩ => v.entityUIDs)
42-
| .record (Map.mk avs) => avs.mapUnion₃ (λ ⟨(_, v), _⟩ => v.entityUIDs)
42+
| .record avs => avs.toList.mapUnion₃ (λ ⟨(_, v), _⟩ => v.entityUIDs)
4343
| .ext _ => Set.empty
4444

4545
def Value.entityUID? : Value → Option EntityUID
@@ -60,7 +60,7 @@ def EntityData.entityUIDs (d : EntityData) : Set EntityUID :=
6060
d.ancestors ∪ (Value.record d.attrs).entityUIDs ∪ (Value.record d.tags).entityUIDs
6161

6262
def Entities.entityUIDs (es : Entities) : Set EntityUID :=
63-
es.kvs.mapUnion (λ (uid, d) => (Set.singleton uid) ∪ d.entityUIDs)
63+
es.toList.mapUnion (λ (uid, d) => (Set.singleton uid) ∪ d.entityUIDs)
6464

6565
structure Env where
6666
request : Request
@@ -171,7 +171,7 @@ def SymRequest.entityUIDs (ρ : SymRequest) : Set EntityUID :=
171171

172172
def UDF.entityUIDs (f : UDF) : Set EntityUID :=
173173
f.default.entityUIDs ∪
174-
f.table.kvs.mapUnion (λ (tᵢ, tₒ) => tᵢ.entityUIDs ∪ tₒ.entityUIDs)
174+
f.table.toList.mapUnion (λ (tᵢ, tₒ) => tᵢ.entityUIDs ∪ tₒ.entityUIDs)
175175

176176
def UnaryFunction.entityUIDs : UnaryFunction → Set EntityUID
177177
| .uuf _ => Set.empty
@@ -184,13 +184,13 @@ where
184184
| .none => Set.empty
185185
| .some eids => Set.make (eids.elts.map (EntityUID.mk ety))
186186
attrs := δ.attrs.entityUIDs
187-
ancs := δ.ancestors.kvs.mapUnion (λ (_, f) => f.entityUIDs)
187+
ancs := δ.ancestors.toList.mapUnion (λ (_, f) => f.entityUIDs)
188188
tags := match δ.tags with
189189
| .none => Set.empty
190190
| .some τs => τs.vals.entityUIDs
191191

192192
def SymEntities.entityUIDs (εs : SymEntities) : Set EntityUID :=
193-
εs.kvs.mapUnion λ (ety, δ) => δ.entityUIDs ety
193+
εs.toList.mapUnion λ (ety, δ) => δ.entityUIDs ety
194194

195195
def SymEnv.entityUIDs (εnv : SymEnv) : Set EntityUID :=
196196
εnv.request.entityUIDs ∪ εnv.entities.entityUIDs
@@ -206,7 +206,7 @@ def SymRequest.concretize? (ρ : SymRequest) : Option Request := do
206206
def SymEntityData.concretize? (uid : EntityUID) (δ : SymEntityData) : Option EntityData := do
207207
let tuid ← if isValidEntityUID then .some (Term.entity uid) else .none
208208
let attrs ← (app δ.attrs tuid).recordValue?
209-
let ancs ← δ.ancestors.kvs.mapM (λ (_, f) => (app f tuid).setOfEntityUIDs?)
209+
let ancs ← δ.ancestors.toList.mapM (λ (_, f) => (app f tuid).setOfEntityUIDs?)
210210
.some ⟨attrs, ancs.mapUnion id, ← (tags tuid)⟩
211211
where
212212
isValidEntityUID :=

cedar-lean/Cedar/SymCC/Enforcer.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ where
123123
| .some f₁₃ => set.subset (app f₂₃ t₂') (app f₁₃ t₁') -- f₂₃ t₂' ⊆ f₁₃ t₁'
124124
| .none => set.isEmpty (app f₂₃ t₂') -- f₁₃ t₁' = ∅
125125
areAncestors t₂' (anc₂ : Map EntityType UnaryFunction) t₁' ety₁ :=
126-
anc₂.kvs.foldl (λ acc ⟨ety₃, f₂₃⟩ => and acc (areAncestorsOfType t₂' f₂₃ ety₃ t₁' ety₁)) (.prim (.bool true))
126+
anc₂.toList.foldl (λ acc ⟨ety₃, f₂₃⟩ => and acc (areAncestorsOfType t₂' f₂₃ ety₃ t₁' ety₁)) (.prim (.bool true))
127127

128128
/--
129129
Returns the ground acyclicity and transitivity assumptions for xs and env.

cedar-lean/Cedar/SymCC/Extractor.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ def UnaryFunction.uuf? : UnaryFunction → Option UUF
5050
| _ => .none
5151

5252
def SymEntityData.uufAncestors (δ : SymEntityData) : Set UUF :=
53-
Set.make (δ.ancestors.kvs.filterMap (UnaryFunction.uuf? ∘ Prod.snd))
53+
Set.make (δ.ancestors.toList.filterMap (UnaryFunction.uuf? ∘ Prod.snd))
5454

5555
def SymEntities.uufAncestors (εs : SymEntities) : Set UUF :=
56-
εs.kvs.mapUnion (SymEntityData.uufAncestors ∘ Prod.snd)
56+
εs.toList.mapUnion (SymEntityData.uufAncestors ∘ Prod.snd)
5757

5858
def UUF.repairAncestors (fts : Set EntityUID) (I : Interpretation) (f : UUF) : UDF :=
5959
let udf := I.funs f

cedar-lean/Cedar/SymCC/Symbolizer.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where
7070
have := Map.find?_mem_toList _h
7171
have := List.sizeOf_lt_of_mem this
7272
cases rec
73-
simp [Map.toList, Map.kvs] at this ⊢
73+
simp at this ⊢
7474
omega
7575

7676
/--

cedar-lean/Cedar/TPE/Input.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ where
150150
else
151151
.error .entitiesDoNotMatch
152152
entitiesMatch :=
153-
es₂.kvs.all λ (a₂, e₂) => match es₁.find? a₂ with
153+
es₂.toList.all λ (a₂, e₂) => match es₁.find? a₂ with
154154
| .some e₁ =>
155155
let ⟨attrs₁, ancestors₁, tags₁⟩ := e₁
156156
partialIsValid e₂.attrs (· = attrs₁) &&

cedar-lean/Cedar/Thm/Authorization/Evaluator.lean

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import Cedar.Spec
1818
import Cedar.Thm.Data.Control
1919
import Cedar.Thm.Data.List
20+
import Cedar.Thm.Data.Map
2021

2122
/-!
2223
This file contains useful lemmas about the `Evaluator` functions.
@@ -240,6 +241,7 @@ theorem record_value_contains_evaluated_attrs {rxs : List (Attr × Expr)} {rvs :
240241
simp only [evaluate] at he
241242
cases he₁ : rxs.mapM₂ fun x => bindAttr x.1.fst (evaluate x.1.snd request entities) <;>
242243
simp only [he₁, Except.bind_err, reduceCtorEq, Except.bind_ok, Except.ok.injEq, Value.record.injEq] at he
244+
subst rvs
243245
rename_i rvs'
244246
replace he₁ : List.Forallᵥ (λ x y => evaluate x request entities = Except.ok y) rxs rvs' := by
245247
simp only [List.forallᵥ_def]
@@ -257,11 +259,11 @@ theorem record_value_contains_evaluated_attrs {rxs : List (Attr × Expr)} {rvs :
257259
split at hfv <;> simp only [Option.some.injEq, reduceCtorEq] at hfv
258260
subst hfv
259261
rename_i a' _ hfv
260-
rw [←he, (by simpa using List.find?_some hfv : a' = a)] at hfv
262+
rw [(by simpa using List.find?_some hfv : a' = a)] at hfv
261263
exact hfv
262-
have ⟨(_, x), he₂, he₃, he₄⟩ := List.forall₂_implies_all_right he₁ (a, av) (List.mem_of_find?_eq_some hfv)
264+
have ⟨(a', x), he₂, he₃, he₄⟩ := List.forall₂_implies_all_right he₁ (a, av) (List.mem_of_find?_eq_some hfv)
263265
subst he₃
264266
exists x
265-
simp only [List.mem_of_sortedBy_implies_find? he₂ (List.canonicalize_sortedBy _ _), he₄, Map.make, Map.find?, and_self]
267+
simp [List.mem_of_sortedBy_implies_find? he₂ (List.canonicalize_sortedBy _ _), he₄, Map.make, Map.find?, and_self]
266268

267269
end Cedar.Thm

cedar-lean/Cedar/Thm/BatchedEvaluator.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ theorem as_partial_request_refines {req : Request} :
4747

4848
theorem any_refines_empty_entities :
4949
EntitiesRefine es Data.Map.empty := by
50-
simp only [EntitiesRefine, Data.Map.empty, Data.Map.find?, Map.kvs]
50+
simp only [EntitiesRefine, Data.Map.empty, Data.Map.find?, Map.toList]
5151
intro a e₂ h₁
5252
contradiction
5353

cedar-lean/Cedar/Thm/Data/List/Basic.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@ public theorem isSorted_correct {α} [LT α] [DecidableLT α] {l : List α} :
560560

561561
/-! ### Forallᵥ -/
562562

563-
@[expose]
564563
public def Forallᵥ {α β γ} (p : β → γ → Prop) (kvs₁ : List (α × β)) (kvs₂ : List (α × γ)) : Prop :=
565564
List.Forall₂ (λ kv₁ kv₂ => kv₁.fst = kv₂.fst ∧ p kv₁.snd kv₂.snd) kvs₁ kvs₂
566565

0 commit comments

Comments
 (0)