Skip to content

Commit 7ca8e8f

Browse files
authored
Cherry-pick #260 to 3.1.x: update for cedar#702 (RFC 57) (#261)
Signed-off-by: Craig Disselkoen <[email protected]>
1 parent afc9cf1 commit 7ca8e8f

File tree

8 files changed

+37
-85
lines changed

8 files changed

+37
-85
lines changed

cedar

Submodule cedar updated 36 files

cedar-lean/Cedar/Spec/Evaluator.lean

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ def intOrErr : Option Int64 → Result Value
3333
def apply₁ : UnaryOp → Value → Result Value
3434
| .not, .prim (.bool b) => ok !b
3535
| .neg, .prim (.int i) => intOrErr i.neg?
36-
| .mulBy c, .prim (.int i) => intOrErr (c.mul? i)
37-
| .like p, .prim (.string s) => ok (wildcardMatch s p)
38-
| .is ety, .prim (.entityUID uid) => ok (ety == uid.ty)
39-
| _, _ => error .typeError
36+
| .like p, .prim (.string s) => .ok (wildcardMatch s p)
37+
| .is ety, .prim (.entityUID uid) => .ok (ety == uid.ty)
38+
| _, _ => .error .typeError
4039

4140
def inₑ (uid₁ uid₂ : EntityUID) (es : Entities) : Bool :=
4241
uid₁ == uid₂ || (es.ancestorsOrEmpty uid₁).contains uid₂
@@ -52,10 +51,11 @@ def apply₂ (op₂ : BinaryOp) (v₁ v₂ : Value) (es : Entities) : Result Val
5251
| .lessEq, .prim (.int i), .prim (.int j) => ok ((i ≤ j): Bool)
5352
| .add, .prim (.int i), .prim (.int j) => intOrErr (i.add? j)
5453
| .sub, .prim (.int i), .prim (.int j) => intOrErr (i.sub? j)
55-
| .contains, .set vs₁, _ => ok (vs₁.contains v₂)
56-
| .containsAll, .set vs₁, .set vs₂ => ok (vs₂.subset vs₁)
57-
| .containsAny, .set vs₁, .set vs₂ => ok (vs₁.intersects vs₂)
58-
| .mem, .prim (.entityUID uid₁), .prim (.entityUID uid₂) => ok (inₑ uid₁ uid₂ es)
54+
| .mul, .prim (.int i), .prim (.int j) => intOrErr (i.mul? j)
55+
| .contains, .set vs₁, _ => .ok (vs₁.contains v₂)
56+
| .containsAll, .set vs₁, .set vs₂ => .ok (vs₂.subset vs₁)
57+
| .containsAny, .set vs₁, .set vs₂ => .ok (vs₁.intersects vs₂)
58+
| .mem, .prim (.entityUID uid₁), .prim (.entityUID uid₂) => .ok (inₑ uid₁ uid₂ es)
5959
| .mem, .prim (.entityUID uid₁), .set (vs) => inₛ uid₁ vs es
6060
| _, _, _ => error .typeError
6161

cedar-lean/Cedar/Spec/Expr.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ inductive Var where
3535
inductive UnaryOp where
3636
| not
3737
| neg
38-
| mulBy (i : Int64)
3938
| like (p : Pattern)
4039
| is (ety : EntityType)
4140

@@ -46,6 +45,7 @@ inductive BinaryOp where
4645
| lessEq
4746
| add
4847
| sub
48+
| mul
4949
| contains
5050
| containsAll
5151
| containsAny

cedar-lean/Cedar/Thm/Validation/Typechecker/BinaryApp.lean

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ theorem type_of_int_cmp_is_sound {op₂ : BinaryOp} {x₁ x₂ : Expr} {c₁ c
214214
}
215215

216216
theorem type_of_int_arith_inversion {op₂ : BinaryOp} {x₁ x₂ : Expr} {c c' : Capabilities} {env : Environment} {ty : CedarType}
217-
(h₁ : op₂ = .add ∨ op₂ = .sub)
217+
(h₁ : op₂ = .add ∨ op₂ = .sub ∨ op₂ = .mul)
218218
(h₂ : typeOf (Expr.binaryApp op₂ x₁ x₂) c env = Except.ok (ty, c')) :
219219
c' = ∅ ∧
220220
ty = .int ∧
@@ -224,7 +224,7 @@ theorem type_of_int_arith_inversion {op₂ : BinaryOp} {x₁ x₂ : Expr} {c c'
224224
simp [typeOf] at *
225225
cases h₃ : typeOf x₁ c env <;> simp [h₃] at h₂
226226
cases h₄ : typeOf x₂ c env <;> simp [h₄] at h₂
227-
rcases h₁ with h₁ | h₁
227+
rcases h₁ with h₁ | h₁ | h₁
228228
all_goals {
229229
subst h₁
230230
simp [typeOfBinaryApp, err, ok] at h₂
@@ -238,7 +238,7 @@ theorem type_of_int_arith_inversion {op₂ : BinaryOp} {x₁ x₂ : Expr} {c c'
238238
}
239239

240240
theorem type_of_int_arith_is_sound {op₂ : BinaryOp} {x₁ x₂ : Expr} {c₁ c₂ : Capabilities} {env : Environment} {ty : CedarType} {request : Request} {entities : Entities}
241-
(h₀ : op₂ = .add ∨ op₂ = .sub)
241+
(h₀ : op₂ = .add ∨ op₂ = .sub ∨ op₂ = .mul)
242242
(h₁ : CapabilitiesInvariant c₁ request entities)
243243
(h₂ : RequestAndEntitiesMatchEnvironment env request entities)
244244
(h₃ : typeOf (Expr.binaryApp op₂ x₁ x₂) c₁ env = Except.ok (ty, c₂))
@@ -266,15 +266,19 @@ theorem type_of_int_arith_is_sound {op₂ : BinaryOp} {x₁ x₂ : Expr} {c₁ c
266266
have ⟨i₁, ih₁⟩ := instance_of_int_is_int ih₃
267267
have ⟨i₂, ih₂⟩ := instance_of_int_is_int ih₄
268268
subst ih₁ ih₂
269-
rcases h₀ with h₀ | h₀ <;> subst h₀ <;> simp [apply₂, intOrErr]
269+
rcases h₀ with h₀ | h₀ | h₀ <;> subst h₀ <;> simp [apply₂, intOrErr]
270270
case inl =>
271271
cases h₄ : Int64.add? i₁ i₂ <;> simp [h₄]
272272
case none => exact type_is_inhabited CedarType.int
273273
case some => simp [InstanceOfType.instance_of_int]
274-
case inr =>
274+
case inr.inl =>
275275
cases h₄ : Int64.sub? i₁ i₂ <;> simp [h₄]
276276
case none => exact type_is_inhabited CedarType.int
277277
case some => simp [InstanceOfType.instance_of_int]
278+
case inr.inr =>
279+
cases h₄ : Int64.mul? i₁ i₂ <;> simp [h₄]
280+
case none => exact type_is_inhabited CedarType.int
281+
case some => simp [InstanceOfType.instance_of_int]
278282

279283
theorem type_of_contains_inversion {x₁ x₂ : Expr} {c c' : Capabilities} {env : Environment} {ty : CedarType}
280284
(h₁ : typeOf (Expr.binaryApp .contains x₁ x₂) c env = Except.ok (ty, c')) :
@@ -838,7 +842,8 @@ theorem type_of_binaryApp_is_sound {op₂ : BinaryOp} {x₁ x₂ : Expr} {c₁ c
838842
| .less
839843
| .lessEq => exact type_of_int_cmp_is_sound (by simp) h₁ h₂ h₃ ih₁ ih₂
840844
| .add
841-
| .sub => exact type_of_int_arith_is_sound (by simp) h₁ h₂ h₃ ih₁ ih₂
845+
| .sub
846+
| .mul => exact type_of_int_arith_is_sound (by simp) h₁ h₂ h₃ ih₁ ih₂
842847
| .contains => exact type_of_contains_is_sound h₁ h₂ h₃ ih₁ ih₂
843848
| .containsAll
844849
| .containsAny => exact type_of_containsA_is_sound (by simp) h₁ h₂ h₃ ih₁ ih₂

cedar-lean/Cedar/Thm/Validation/Typechecker/UnaryApp.lean

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -133,53 +133,6 @@ theorem type_of_neg_is_sound {x₁ : Expr} {c₁ c₂ : Capabilities} {env : Env
133133
exact type_is_inhabited CedarType.int
134134
}
135135

136-
theorem type_of_mulBy_inversion {x₁ : Expr} {k : Int64} {c₁ c₂ : Capabilities} {env : Environment} {ty : CedarType}
137-
(h₁ : typeOf (Expr.unaryApp (.mulBy k) x₁) c₁ env = Except.ok (ty, c₂)) :
138-
c₂ = ∅ ∧
139-
ty = .int ∧
140-
∃ c₁', typeOf x₁ c₁ env = Except.ok (.int, c₁')
141-
:= by
142-
simp [typeOf] at h₁
143-
cases h₂ : typeOf x₁ c₁ env <;> simp [h₂] at h₁
144-
case ok res =>
145-
have ⟨ty₁, c₁'⟩ := res
146-
simp [typeOfUnaryApp] at h₁
147-
split at h₁ <;> try contradiction
148-
simp [ok] at h₁
149-
simp [h₁]
150-
151-
theorem type_of_mulBy_is_sound {x₁ : Expr} {k : Int64} {c₁ c₂ : Capabilities} {env : Environment} {ty : CedarType} {request : Request} {entities : Entities}
152-
(h₁ : CapabilitiesInvariant c₁ request entities)
153-
(h₂ : RequestAndEntitiesMatchEnvironment env request entities)
154-
(h₃ : typeOf (Expr.unaryApp (.mulBy k) x₁) c₁ env = Except.ok (ty, c₂))
155-
(ih : TypeOfIsSound x₁) :
156-
GuardedCapabilitiesInvariant (Expr.unaryApp (.mulBy k) x₁) c₂ request entities ∧
157-
∃ v, EvaluatesTo (Expr.unaryApp (.mulBy k) x₁) request entities v ∧ InstanceOfType v ty
158-
:= by
159-
have ⟨h₅, h₆, c₁', h₄⟩ := type_of_mulBy_inversion h₃
160-
subst h₅; subst h₆
161-
apply And.intro
162-
case left => exact empty_guarded_capabilities_invariant
163-
case right =>
164-
have ⟨_, v₁, h₆, h₇⟩ := ih h₁ h₂ h₄ -- IH
165-
simp [EvaluatesTo] at h₆
166-
simp [EvaluatesTo, evaluate]
167-
rcases h₆ with h₆ | h₆ | h₆ | h₆ <;> simp [h₆]
168-
case inr.inr.inr =>
169-
have ⟨i, h₈⟩ := instance_of_int_is_int h₇
170-
subst h₈
171-
simp [apply₁, intOrErr]
172-
cases h₉ : k.mul? i
173-
case none =>
174-
simp only [or_false, or_true, true_and]
175-
exact type_is_inhabited CedarType.int
176-
case some i' =>
177-
simp only [Except.ok.injEq, false_or, exists_eq_left']
178-
apply InstanceOfType.instance_of_int
179-
all_goals {
180-
exact type_is_inhabited CedarType.int
181-
}
182-
183136
theorem type_of_like_inversion {x₁ : Expr} {p : Pattern} {c₁ c₂ : Capabilities} {env : Environment} {ty : CedarType}
184137
(h₁ : typeOf (Expr.unaryApp (.like p) x₁) c₁ env = Except.ok (ty, c₂)) :
185138
c₂ = ∅ ∧
@@ -234,7 +187,7 @@ theorem type_of_is_inversion {x₁ : Expr} {ety : EntityType} {c₁ c₂ : Capab
234187
have ⟨ty₁, c₁'⟩ := res
235188
simp [typeOfUnaryApp] at h₁
236189
split at h₁ <;> try contradiction
237-
case h_5 _ _ ety' h₃ =>
190+
case h_4 _ _ ety' h₃ =>
238191
simp only [UnaryOp.is.injEq] at h₃
239192
subst h₃
240193
simp [ok] at h₁
@@ -284,7 +237,6 @@ theorem type_of_unaryApp_is_sound {op₁ : UnaryOp} {x₁ : Expr} {c₁ c₂ : C
284237
match op₁ with
285238
| .not => exact type_of_not_is_sound h₁ h₂ h₃ ih
286239
| .neg => exact type_of_neg_is_sound h₁ h₂ h₃ ih
287-
| .mulBy k => exact type_of_mulBy_is_sound h₁ h₂ h₃ ih
288240
| .like p => exact type_of_like_is_sound h₁ h₂ h₃ ih
289241
| .is ety => exact type_of_is_is_sound h₁ h₂ h₃ ih
290242

cedar-lean/Cedar/Validation/Typechecker.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ def typeOfUnaryApp (op : UnaryOp) (ty : CedarType) : ResultType :=
106106
match op, ty with
107107
| .not, .bool x => ok (.bool x.not)
108108
| .neg, .int => ok .int
109-
| .mulBy _, .int => ok .int
110109
| .like _, .string => ok (.bool .anyBool)
111110
| .is ety₁, .entity ety₂ => ok (.bool (if ety₁ = ety₂ then .tt else .ff))
112111
| _, _ => err (.unexpectedType ty)
@@ -172,6 +171,7 @@ def typeOfBinaryApp (op₂ : BinaryOp) (ty₁ ty₂ : CedarType) (x₁ x₂ : Ex
172171
| .lessEq, .int, .int => ok (.bool .anyBool)
173172
| .add, .int, .int => ok .int
174173
| .sub, .int, .int => ok .int
174+
| .mul, .int, .int => ok .int
175175
| .contains, .set ty₃, _ => ifLubThenBool ty₂ ty₃
176176
| .containsAll, .set ty₃, .set ty₄ => ifLubThenBool ty₃ ty₄
177177
| .containsAny, .set ty₃, .set ty₄ => ifLubThenBool ty₃ ty₄

cedar-lean/DiffTest/Parser.lean

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def jsonToBinaryOp (json : Lean.Json) : ParseResult BinaryOp := do
117117
| "LessEq" => .ok .lessEq
118118
| "Add" => .ok .add
119119
| "Sub" => .ok .sub
120+
| "Mul" => .ok .mul
120121
| "Contains" => .ok .contains
121122
| "ContainsAll" => .ok .containsAll
122123
| "ContainsAny" => .ok .containsAny
@@ -181,10 +182,6 @@ partial def jsonToExpr (json : Lean.Json) : ParseResult Expr := do
181182
let op ← getJsonField body "op" >>= jsonToUnaryOp
182183
let arg ← getJsonField body "arg" >>= jsonToExpr
183184
.ok (.unaryApp op arg)
184-
| "MulByConst" => do
185-
let c ← getJsonField body "constant" >>= jsonToInt64
186-
let arg ← getJsonField body "arg" >>= jsonToExpr
187-
.ok (.unaryApp (.mulBy c) arg)
188185
| "Like" => do
189186
let pat ← getJsonField body "pattern" >>= jsonToPattern
190187
let expr ← getJsonField body "expr" >>= jsonToExpr

cedar-policy-generators/src/expr.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,10 @@ impl<'a> ExprGenerator<'a> {
108108
self.generate_expr(max_depth - 1, u)?,
109109
self.generate_expr(max_depth - 1, u)?,
110110
)),
111-
1 => {
112-
// arbitrary expression, which may be a constant
113-
let expr = self.generate_expr(max_depth - 1, u)?;
114-
// arbitrary constant integer
115-
let c = self.constant_pool.arbitrary_int_constant(u)?;
116-
Ok(ast::Expr::mul(expr, c))
117-
},
111+
1 => Ok(ast::Expr::mul(
112+
self.generate_expr(max_depth - 1, u)?,
113+
self.generate_expr(max_depth - 1, u)?,
114+
)),
118115
1 => {
119116
// negation expression
120117
Ok(ast::Expr::neg(self.generate_expr(max_depth - 1, u)?))
@@ -641,17 +638,18 @@ impl<'a> ExprGenerator<'a> {
641638
)?,
642639
)),
643640
// * expression
644-
1 => {
645-
// arbitrary expression, which may be a constant
646-
let expr = self.generate_expr_for_type(
641+
1 => Ok(ast::Expr::mul(
642+
self.generate_expr_for_type(
647643
&Type::long(),
648644
max_depth - 1,
649645
u,
650-
)?;
651-
// arbitrary integer constant
652-
let c = self.constant_pool.arbitrary_int_constant(u)?;
653-
Ok(ast::Expr::mul(expr, c))
654-
},
646+
)?,
647+
self.generate_expr_for_type(
648+
&Type::long(),
649+
max_depth - 1,
650+
u,
651+
)?,
652+
)),
655653
// negation expression
656654
1 => Ok(ast::Expr::neg(self.generate_expr_for_type(
657655
&Type::long(),

0 commit comments

Comments
 (0)