Skip to content

Commit aa311f7

Browse files
authored
switch to higher-level functions in a few places (#901)
Signed-off-by: Craig Disselkoen <cdiss@amazon.com>
1 parent 7612a6d commit aa311f7

File tree

24 files changed

+270
-235
lines changed

24 files changed

+270
-235
lines changed

cedar-lean/Cedar/Data/Set.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ public instance [DecidableEq α] : Inter (Set α) := ⟨Set.intersect⟩
157157
-- enables ∪
158158
public instance [LT α] [DecidableLT α] : Union (Set α) := ⟨Set.union⟩
159159

160+
----- Termination-friendly iterators -----
161+
162+
public def all₁ {α} (s : Set α) (f : {a : α // a ∈ s} → Bool) : Bool :=
163+
s.elts.attach.all f
164+
165+
public def any₁ {α} (s : Set α) (f : {a : α // a ∈ s} → Bool) : Bool :=
166+
s.elts.attach.any f
167+
160168
end Set
161169

162170
end Cedar.Data

cedar-lean/Cedar/Data/SizeOf.lean

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ public theorem sizeOf_lt_of_toList [SizeOf α] [SizeOf β] (m : Map α β) :
9090
generalize sizeOf m.1 = s
9191
omega
9292

93+
public theorem sizeOf_lt_of_values [SizeOf α] [SizeOf β] {m : Map α β}
94+
(h : b ∈ m.values) :
95+
sizeOf b < sizeOf m
96+
:= by
97+
simp only [values, List.mem_map, Prod.exists, exists_eq_right] at h
98+
replace ⟨a, h⟩ := h
99+
exact List.sizeOf_snd_lt_sizeOf_list h
100+
93101
public theorem sizeOf_lt_of_tl [SizeOf α] [SizeOf β] {m : Map α β} {tl : List (α × β)}
94102
(h : m.toList = (k, v) :: tl) :
95103
1 + sizeOf tl < sizeOf m
@@ -117,7 +125,7 @@ public theorem sizeOf_lt_of_mem [SizeOf α] {s : Set α}
117125
omega
118126
omega
119127

120-
public theorem sizeOf_lt_of_elts [SizeOf α] {s : Set α} :
128+
public theorem sizeOf_lt_of_elts [SizeOf α] (s : Set α) :
121129
sizeOf s.elts < sizeOf s
122130
:= by
123131
simp only [elts]

cedar-lean/Cedar/SymCC/Decoder.lean

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,10 @@ def defaultPrim (eidOf : EntityType → String) : TermPrimType → TermPrim
357357
| .ext xty => defaultExt xty
358358

359359
def defaultLit (eidOf : EntityType → String) : TermType → Term
360-
| .prim pty => .prim (defaultPrim eidOf pty)
361-
| .option ty => .none ty
362-
| .set ty => .set Set.empty ty
363-
| .record (Map.mk tys) =>
364-
let ts := tys.map₂ λ ⟨(a, ty), _⟩ => (a, defaultLit eidOf ty)
365-
.record (Map.mk ts)
366-
termination_by ty => sizeOf ty
367-
decreasing_by simp_wf ; simp at * ; omega
360+
| .prim pty => .prim (defaultPrim eidOf pty)
361+
| .option ty => .none ty
362+
| .set ty => .set Set.empty ty
363+
| .record tys => .record (tys.mapOnValues₂ λ ⟨ty, _⟩ => defaultLit eidOf ty)
368364

369365
def defaultUDF (eidOf : EntityType → String) (f : UUF) : UDF :=
370366
⟨f.arg, f.out, Map.empty, defaultLit eidOf f.out⟩

cedar-lean/Cedar/SymCC/Factory.lean

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,10 @@ public def set.inter (ts₁ ts₂ : Term) : Term :=
256256
| _, _ => .app Op.set.inter [ts₁, ts₂] ts₁.typeOf
257257

258258
public def set.isEmpty : Term → Term
259-
| .set (Set.mk []) _ => true
260-
| .set (Set.mk (_::_)) _ => false
259+
| .set s _ => s.isEmpty
261260
| ts =>
262261
match ts.typeOf with
263-
| .set ty => eq ts (.set (Set.mk []) ty)
262+
| .set ty => eq ts (.set Set.empty ty)
264263
| _ => false
265264

266265
public def set.intersects (ts₁ ts₂ : Term) : Term :=

cedar-lean/Cedar/SymCC/Term.lean

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
module
1818

19+
import Cedar.Data.SizeOf
1920
import Cedar.Spec
2021
public import Cedar.SymCC.Data
2122
public import Cedar.SymCC.Op
@@ -89,11 +90,21 @@ public def Term.typeOf : Term → TermType
8990

9091
public def Term.isLiteral : Term → Bool
9192
| .prim _
92-
| .none _ => true
93-
| .some t => t.isLiteral
94-
| .set (Set.mk ts) _ => ts.attach.all (λ ⟨t, _⟩ => t.isLiteral)
95-
| .record (Map.mk atrs) => atrs.attach₃.all (λ ⟨(_, t), _⟩ => t.isLiteral)
96-
| _ => false
93+
| .none _ => true
94+
| .some t => t.isLiteral
95+
| .set ts _ => ts.all₁ λ ⟨t, _⟩ => t.isLiteral
96+
| .record atrs => atrs.toList.attach₂.all λ ⟨(_, t), _⟩ => t.isLiteral
97+
| _ => false
98+
decreasing_by
99+
all_goals simp_wf
100+
· rename_i h
101+
have := Set.sizeOf_lt_of_elts ts
102+
have := List.sizeOf_lt_of_mem h
103+
omega
104+
· rename_i h
105+
have := Map.sizeOf_lt_of_toList atrs
106+
simp only at *
107+
omega
97108

98109
public instance : Coe Bool Term where
99110
coe b := .prim (.bool b)

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public theorem all_pmap_subtype
6464
:= by
6565
induction as <;> simp [*]
6666

67-
/-! ### map and map₁ -/
67+
/-! ### map, map₁, map₂, map₃, and `attach` variants -/
6868

6969
/--
7070
Copied from Mathlib. We can delete this if it gets added to Batteries.
@@ -128,6 +128,28 @@ public theorem attach_def {as : List α} :
128128
:= by
129129
simp [attach, attachWith]
130130

131+
public theorem mem_attach₂ [SizeOf α] [SizeOf β] {as : List (α × β)} {pair : {x : α × β // sizeOf x.snd < 1 + sizeOf as}} :
132+
pair ∈ as.attach₂ → (pair.val.fst, pair.val.snd) ∈ as
133+
:= by
134+
simp only [attach₂, mem_pmap, Prod.exists]
135+
intro h
136+
replace ⟨a, b, h, _⟩ := h ; subst pair
137+
simp [h]
138+
139+
@[simp]
140+
public theorem all_attach₂ [SizeOf α] [SizeOf β] {as : List (α × β)} {f : (α × β) → Bool} :
141+
as.attach₂.all (λ ⟨pair, _⟩ => f pair) = as.all f
142+
:= by
143+
unfold attach₂
144+
rw [all_pmap_subtype]
145+
146+
@[simp]
147+
public theorem all_attach₂_snd [SizeOf α] [SizeOf β] {as : List (α × β)} {f : β → Bool} :
148+
as.attach₂.all (λ ⟨(_, b), _⟩ => f b) = as.all λ (_, b) => f b
149+
:= by
150+
unfold attach₂
151+
simp [all_pmap_subtype λ ((_, b) : α × β) => f b]
152+
131153
public theorem map₁_eq_map (f : α → β) (as : List α) :
132154
as.map₁ (λ x : {x // x ∈ as} => f x.val) =
133155
as.map f

cedar-lean/Cedar/Thm/Data/Map.lean

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,23 @@ public theorem values_mapOnValues [LT α] [StrictLT α] [DecidableLT α] [Decida
847847
unfold mapOnValues values
848848
induction m.1 <;> simp
849849

850+
@[simp]
851+
public theorem mapOnValues_mapOnValues [LT α] [StrictLT α] [DecidableLT α] [DecidableEq α] {f : β → γ} {g : γ → φ} {m : Map α β} :
852+
(m.mapOnValues f).mapOnValues g = m.mapOnValues (g ∘ f)
853+
:= by simp [mapOnValues]
854+
855+
public theorem mapOnValues_restricted_id {f : β → β} {m : Map α β} :
856+
(∀ b ∈ m.values, f b = b) →
857+
m.mapOnValues f = m
858+
:= by
859+
simp only [mapOnValues]
860+
cases m ; simp only [toList_mk_id, mk.injEq]
861+
intro h
862+
apply List.map_restricted_id
863+
intro pair hpair
864+
simp [h pair.snd (Map.in_list_in_values hpair)]
865+
866+
@[simp]
850867
public theorem mapOnValues₂_eq_mapOnValues {α β γ} [SizeOf α] [SizeOf β] (m : Map α β) (f : β → γ) :
851868
m.mapOnValues₂ (λ x : { x : β // sizeOf x < sizeOf m } => f x.1) = m.mapOnValues f
852869
:= by
@@ -988,40 +1005,31 @@ public theorem find?_none_iff_findorErr_errors [LT α] [DecidableLT α] [Decidab
9881005
exact findOrErr_err_iff_find?_none.symm
9891006

9901007
/--
991-
Converse of `in_toList_in_mapOnValues`; requires the extra preconditions that `m`
992-
is `WellFormed` and `f` is injective
1008+
Converse of `in_toList_in_mapOnValues`; requires the extra precondition that `f` is injective
9931009
-/
994-
public theorem in_mapOnValues_in_toList [LT α] [DecidableLT α] [StrictLT α] [DecidableEq α] {f : β → γ} {m : Map α β} {k : α} {v : β}
995-
(wf : m.WellFormed) :
1010+
public theorem in_mapOnValues_in_toList [LT α] [DecidableLT α] [StrictLT α] [DecidableEq α] {f : β → γ} {m : Map α β} {k : α} {v : β} :
9961011
(k, f v) ∈ (m.mapOnValues f).toList →
9971012
(∀ v', f v = f v' → v = v') → -- require f to be injective
9981013
(k, v) ∈ m.toList
9991014
:= by
1000-
rw [mapOnValues_eq_make_map f wf]
1001-
unfold toList
1002-
intro h₁ h_inj
1003-
replace h₁ := make_mem_list_mem h₁
1004-
replace ⟨(k', v'), h₁, h₂⟩ := List.mem_map.mp h₁
1005-
simp only [Prod.mk.injEq] at h₂ ; replace ⟨h₂', h₂⟩ := h₂ ; subst k'
1006-
specialize h_inj v' h₂.symm
1015+
simp only [toList, mapOnValues, List.mem_map, Prod.mk.injEq, Prod.exists, forall_exists_index,
1016+
and_imp]
1017+
intro a b h₁ _ h₂ h_inj ; subst a
1018+
specialize h_inj b h₂.symm
10071019
subst h_inj
10081020
exact h₁
10091021

10101022
/--
10111023
Slightly different formulation of `in_mapOnValues_in_toList`
10121024
-/
1013-
public theorem in_mapOnValues_in_toList' [LT α] [DecidableLT α] [StrictLT α] [DecidableEq α] {f : β → γ} {m : Map α β} {k : α} {v' : γ}
1014-
(wf : m.WellFormed) :
1025+
public theorem in_mapOnValues_in_toList' [LT α] [DecidableLT α] [StrictLT α] [DecidableEq α] {f : β → γ} {m : Map α β} {k : α} {v' : γ} :
10151026
(k, v') ∈ (m.mapOnValues f).toList →
10161027
∃ v, f v = v' ∧ (k, v) ∈ m.toList
10171028
:= by
1018-
rw [mapOnValues_eq_make_map f wf]
1019-
unfold toList
1020-
intro h₁
1021-
replace h₁ := make_mem_list_mem h₁
1022-
replace ⟨(k', v'), h₁, h₂⟩ := List.mem_map.mp h₁
1023-
simp only [Prod.mk.injEq] at h₂ ; replace ⟨h₂', h₂⟩ := h₂ ; subst k' h₂
1024-
exists v'
1029+
simp only [mapOnValues, toList, List.mem_map, Prod.mk.injEq, Prod.exists,
1030+
forall_exists_index, and_imp]
1031+
intro a b h₁ _ _ ; subst a v'
1032+
exists b
10251033

10261034
/-! ### mapMOnValues -/
10271035

cedar-lean/Cedar/Thm/Data/Set.lean

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ public theorem map_empty [LT β] [DecidableLT β] (f : α → β) :
156156
:= by
157157
simp [Set.map, empty_eq_mk_nil, Set.elts, Set.make, List.canonicalize_nil]
158158

159+
@[simp]
160+
public theorem all_empty (f : α → Bool) :
161+
Set.empty.all f = true
162+
:= by simp [all]
163+
164+
@[simp]
165+
public theorem any_empty (f : α → Bool) :
166+
Set.empty.any f = false
167+
:= by simp [any]
168+
159169
/-! ### isEmpty -/
160170

161171
@[simp]
@@ -757,4 +767,16 @@ public theorem difference_subset [LT α] [DecidableLT α] [StrictLT α] [Decidab
757767
rw [mem_difference]
758768
exact And.left
759769

770+
/-! ### all₁ and any₁ -/
771+
772+
@[simp]
773+
public theorem all₁_eq_all {s : Set α} {f : α → Bool} :
774+
(s.all₁ λ ⟨elt, _⟩ => f elt) = s.all f
775+
:= by simp [all₁, all]
776+
777+
@[simp]
778+
public theorem any₁_eq_any {s : Set α} {f : α → Bool} :
779+
(s.any₁ λ ⟨elt, _⟩ => f elt) = s.any f
780+
:= by simp [any₁, any]
781+
760782
end Cedar.Data.Set

cedar-lean/Cedar/Thm/SymCC/Compiler/WellTyped.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,6 @@ theorem compile_well_typed_unaryApp
631631
]
632632
split
633633
· simp [typeOf_bool]
634-
· simp [typeOf_bool]
635634
· simp [hty_get_comp_expr]
636635
apply (wf_eq ?_ ?_ ?_).right
637636
any_goals assumption

cedar-lean/Cedar/Thm/SymCC/Concretizer/Util.lean

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ theorem wf_δ_implies_wf_app_tags_keys {uid : EntityUID} {δ : SymEntityData} {
112112
theorem lit_tagOf (uid : EntityUID) (tag : Tag) :
113113
(tagOf (Term.entity uid) (Term.string tag)).isLiteral
114114
:= by
115-
simp only [tagOf, Term.isLiteral, List.cons.sizeOf_spec, Prod.mk.sizeOf_spec,
116-
Term.prim.sizeOf_spec, TermPrim.entity.sizeOf_spec, TermPrim.string.sizeOf_spec,
117-
List.nil.sizeOf_spec, List.attach₃, List.pmap, List.all_cons, List.all_nil, Bool.and_self]
115+
simp [tagOf, Term.isLiteral]
118116

119117
end Cedar.Thm

0 commit comments

Comments
 (0)