Skip to content

Commit 7612a6d

Browse files
Move some complicated proof for tags/attrs into lemmas (#902)
1 parent c94f828 commit 7612a6d

File tree

3 files changed

+208
-167
lines changed

3 files changed

+208
-167
lines changed

cedar-lean/Cedar/Thm/TPE/WellTyped/Basic.lean

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,26 @@ theorem well_typed_bool { env : TypeEnv} {b : Bool}:
5050
apply Residual.WellTyped.val
5151
apply InstanceOfType.instance_of_bool
5252
simp [InstanceOfBoolType]
53+
54+
theorem entity_data_from_partial
55+
{env : TypeEnv} {req : Request} {es : Entities} {pes : PartialEntities}
56+
{uid : EntityUID} {pe : PartialEntityData} :
57+
InstanceOfWellFormedEnvironment req es env →
58+
EntitiesRefine es pes →
59+
pes.find? uid = some pe →
60+
∃ (edata : EntityData),
61+
es.find? uid = some edata ∧
62+
PartialIsValid (· = edata.attrs) pe.attrs ∧
63+
PartialIsValid (· = edata.ancestors) pe.ancestors ∧
64+
PartialIsValid (· = edata.tags) pe.tags ∧
65+
InstanceOfSchemaEntry uid edata env
66+
:= by
67+
intros h_wf h_eref h_find
68+
specialize h_eref uid pe h_find
69+
rcases h_eref with ⟨edata, h_es, h_attrs, h_anc, h_tags⟩
70+
exists edata
71+
refine ⟨h_es, h_attrs, h_anc, h_tags, ?_⟩
72+
unfold InstanceOfWellFormedEnvironment at h_wf
73+
rcases h_wf with ⟨_, _, h_schema⟩
74+
unfold InstanceOfSchema at h_schema
75+
exact h_schema.1 uid edata h_es

cedar-lean/Cedar/Thm/TPE/WellTyped/Binary.lean

Lines changed: 92 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,72 @@ open Cedar.Spec
3131
open Cedar.Validation
3232
open Cedar.TPE
3333

34+
theorem tags_if_partial_tags
35+
{env : TypeEnv} {req : Request} {es : Entities} {pes : PartialEntities}
36+
{uid : EntityUID} {tags : Map Tag Value}
37+
(h_wf : InstanceOfWellFormedEnvironment req es env)
38+
(h_eref : EntitiesRefine es pes)
39+
(h_tags : PartialEntities.tags pes uid = some tags) :
40+
∃ (edata : EntityData),
41+
edata.tags = tags ∧
42+
InstanceOfSchemaEntry uid edata env
43+
:= by
44+
unfold PartialEntities.tags PartialEntities.get at h_tags
45+
cases h₁ : (Map.find? pes uid) <;> rw [h₁] at h_tags
46+
. simp at h_tags
47+
. rename_i pe
48+
have ⟨edata, h_es, _, _, h_pt, h_entry⟩ :=
49+
entity_data_from_partial h_wf h_eref h₁
50+
simp only [Option.bind] at h_tags
51+
cases h_pe : pe.tags <;> rw [h_pe] at h_tags
52+
. simp at h_tags
53+
. simp only [Option.some.injEq] at h_tags
54+
rw [h_pe] at h_pt
55+
cases h_pt
56+
rename_i h_eq
57+
exact ⟨edata, by rw [← h_tags, h_eq], h_entry⟩
58+
59+
theorem entity_tag_well_typed
60+
{env : TypeEnv} {req : Request} {es : Entities} {pes : PartialEntities}
61+
{uid : EntityUID} {ety : EntityType}
62+
{tags : Map Tag Value} {v : Value} {ty : CedarType} :
63+
InstanceOfWellFormedEnvironment req es env →
64+
EntitiesRefine es pes →
65+
InstanceOfEntityType uid ety env →
66+
PartialEntities.tags pes uid = some tags →
67+
v ∈ tags.values →
68+
env.ets.tags? ety = some (some ty) →
69+
InstanceOfType env v ty.liftBoolTypes
70+
:= by
71+
intros h_wf h_eref h_ent h_tags h_mem h_schema
72+
have ⟨edata, h_eq, h_entry⟩ := tags_if_partial_tags h_wf h_eref h_tags
73+
rw [← h_eq] at h_mem
74+
unfold InstanceOfSchemaEntry at h_entry
75+
cases h_entry
76+
. rename_i h_ent_entry
77+
unfold InstanceOfEntitySchemaEntry at h_ent_entry
78+
rcases h_ent_entry with ⟨entry, h_ets, _, _, _, h_inst_tags⟩
79+
unfold InstanceOfEntityType at h_ent
80+
rcases h_ent with ⟨h_ent_ty, _⟩
81+
rw [← h_ent_ty] at h_ets
82+
unfold InstanceOfEntityTags at h_inst_tags
83+
cases h₂ : entry.tags? <;> rw [h₂] at h_inst_tags <;> simp only at h_inst_tags
84+
. rw [h_inst_tags] at h_mem
85+
simp [Map.empty, Map.values] at h_mem
86+
. specialize h_inst_tags v h_mem
87+
unfold EntitySchema.tags? at h_schema
88+
rw [h_ets] at h_schema
89+
simp only [Option.map_some, Option.some.injEq] at h_schema
90+
rw [h₂] at h_schema
91+
simp only [Option.some.injEq] at h_schema
92+
rw [h_schema] at h_inst_tags
93+
exact type_lifting_preserves_instance_of_type h_inst_tags
94+
. rename_i h_act_entry
95+
unfold InstanceOfActionSchemaEntry at h_act_entry
96+
rcases h_act_entry with ⟨_, h_empty, _⟩
97+
rw [h_empty] at h_mem
98+
simp [Map.empty, Map.values] at h_mem
99+
34100
theorem partial_eval_well_typed_app₂_values_hasTag :
35101
Residual.WellTyped env (TPE.evaluate expr1 preq pes) →
36102
Residual.WellTyped env (TPE.evaluate expr2 preq pes) →
@@ -123,7 +189,6 @@ theorem partial_eval_well_typed_app₂_values_getTag :
123189
cases h_wt with
124190
| binaryApp h_expr1 h_expr2 h_op =>
125191

126-
127192
unfold TPE.getTag
128193
split
129194
. unfold someOrError
@@ -133,101 +198,33 @@ theorem partial_eval_well_typed_app₂_values_getTag :
133198
rename_i tags heq x₁ x₂ x₃ v h₃
134199
cases h_op
135200
rename_i ety ty h₄ h₅ h₆
136-
unfold EntitiesRefine at h_eref
137201
unfold Data.Map.find? at h₃
138-
split at h₃
139-
case h_2 => contradiction
140-
dsimp only [PartialEntities.tags, PartialEntities.get] at heq
141-
rename Value => v₂
142-
cases h₇: (Data.Map.find? pes id₁)
143-
case h_1.none =>
144-
rw [h₇] at heq
145-
simp at heq
146-
147-
rename Value => v₃
148-
rename PartialEntityData => pv
149-
specialize h_eref id₁ pv h₇
150-
rw [h₇] at heq
151-
simp only [Option.bind_some] at heq
152-
cases h_eref
153-
rename_i h₈
154-
rcases h₈ with ⟨h₈, h₉, h₁₀, h₁₁⟩
155-
rw [heq] at h₁₁
156-
cases h₁₁
157-
rename_i h₁₂
158-
rename_i e
159-
subst h₁₂
160-
let h_wf₂ := h_wf
161-
unfold InstanceOfWellFormedEnvironment at h_wf₂
162-
rcases h_wf₂ with ⟨h₁₄, _, h₁₆⟩
163-
unfold InstanceOfSchema at h₁₆
164-
rcases h₁₆ with ⟨h₁₆, h₁₇⟩
165-
specialize h₁₆ id₁ e h₈
166-
unfold InstanceOfSchemaEntry at h₁₆
167-
cases h₁₆
168-
. rename_i h₁₃ _ h₁₆
169-
unfold InstanceOfEntitySchemaEntry at h₁₆
170-
rcases h₁₆ with ⟨_, _, _, _, _, h₁₇⟩
171-
unfold InstanceOfEntityTags at h₁₇
172-
rename EntitySchemaEntry => w
173-
cases h₁₈: w.tags? <;> rw [h₁₈] at h₁₇ <;> simp only at h₁₇
174-
. rw [h₁₇] at h₁₃
175-
simp [Data.Map.empty] at h₁₃
176-
. have h₁₈ : v₃ ∈ e.tags.values := by {
177-
have h₁₉ := List.mem_of_find?_eq_some h₁₃
178-
have h₂₀ := Map.in_list_in_values h₁₉
179-
exact h₂₀
180-
}
181-
specialize h₁₇ v₃ h₁₈
182-
rename CedarType => ty
183-
rename_i h₁₉
184-
rename CedarType => ty₂
185-
injection h₃
186-
rename_i h₃
187-
rw [← h₃]
188-
rename Data.Map.find? env.ets id₁.ty = some w => h₂₁
189-
unfold EntitySchema.tags? at h₄
190-
have h_ety_eq : ety = id₁.ty := by {
191-
have h₂₁ := partial_eval_preserves_typeof _ h_expr1 preq pes
192-
rw [← h₂₁] at h₅
193-
unfold Residual.asValue at h₁
194-
cases h₂₂: TPE.evaluate expr1 preq pes
195-
. rw [h₂₂] at h₁
196-
rename Value => v₄
197-
simp only [Option.some.injEq] at h₁
198-
rw [h₁] at h₂₂
199-
rw [h₂₂] at h₅
200-
rw [h₂₂] at h₂₁
201-
rename expr1.typeOf = CedarType.entity ety => h₂₃
202-
rw [h₂₃] at h₂₁
203-
simp only [Residual.typeOf] at h₂₁
204-
rename CedarType => ty₃
205-
rw [h₂₂] at ih₁
206-
cases ih₁
207-
rename_i h₂₃
208-
rw [h₂₁] at h₂₃
209-
cases h₂₃
210-
rename_i h₂₄
211-
unfold InstanceOfEntityType at h₂₄
212-
rcases h₂₄ with ⟨h₂₄, _⟩
213-
exact h₂₄
214-
all_goals {
215-
rw [h₂₂] at h₁
216-
simp at h₁
217-
}
218-
}
219-
rw [h_ety_eq] at h₄
220-
rw [h₂₁] at h₄
221-
simp only [Option.map_some, Option.some.injEq] at h₄
222-
rw [h₁₉] at h₄
223-
simp only [Option.some.injEq] at h₄
224-
rw [h₄] at h₁₇
225-
exact type_lifting_preserves_instance_of_type h₁₇
226-
. rename_i h₁₃ _ h₁₆
227-
unfold InstanceOfActionSchemaEntry at h₁₆
228-
rcases h₁₆ with ⟨_, h₁₇, _, _, _⟩
229-
rw [h₁₇] at h₁₃
230-
simp at h₁₃
202+
split at h₃ <;> try contradiction
203+
rename_i v₂ v₃ _
204+
injection h₃; rename_i h₃; rw [← h₃]
205+
have h_v_mem : v₃ ∈ tags.values := by
206+
have h₁₉ := List.mem_of_find?_eq_some (by assumption)
207+
exact Map.in_list_in_values h₁₉
208+
have h_ent : InstanceOfEntityType id₁ ety env := by
209+
have h₂₁ := partial_eval_preserves_typeof _ h_expr1 preq pes
210+
unfold Residual.asValue at h₁
211+
cases h₂₂: TPE.evaluate expr1 preq pes
212+
case val v ty₃ =>
213+
replace h₁ : v = .prim (.entityUID id₁) := by
214+
simpa [h₂₂] using h₁
215+
rw [h₁] at h₂₂
216+
rw [h₂₂] at ih₁
217+
replace h₂₁ : ty₃ = CedarType.entity ety := by
218+
rw [h₅, h₂₂] at h₂₁
219+
simpa [Residual.typeOf] using h₂₁
220+
cases ih₁; rename_i h₂₃
221+
rw [h₂₁] at h₂₃
222+
cases h₂₃; rename_i h₂₄
223+
exact h₂₄
224+
all_goals
225+
rw [h₂₂] at h₁
226+
simp at h₁
227+
exact entity_tag_well_typed h_wf h_eref h_ent heq h_v_mem h₄
231228
. apply Residual.WellTyped.error
232229
. apply Residual.WellTyped.binaryApp
233230
. unfold Residual.asValue at h₁

0 commit comments

Comments
 (0)