Skip to content

Commit ddcec57

Browse files
authored
use Lean modules in more files (#912)
Signed-off-by: Craig Disselkoen <cdiss@amazon.com>
1 parent 4720ffd commit ddcec57

40 files changed

+599
-459
lines changed

cedar-lean/Cedar/Data/Int64.lean

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public def MAX : Int := 9223372036854775807
3232

3333
----- Definitions -----
3434

35+
@[expose]
3536
public def ofIntChecked (i : Int) (_ : MIN ≤ i ∧ i ≤ MAX) : Int64 :=
3637
Int64.ofInt i
3738

@@ -41,20 +42,15 @@ public def ofInt? (i : Int) : Option Int64 :=
4142
else .none
4243

4344
/-- We don't @[expose] the definition of `ofInt?`; but callers can use this
44-
theorem, which partially specifies its behavior -/
45+
theorem, which specifies its behavior -/
4546
public theorem ofInt?_some_iff {i : Int} :
46-
MIN ≤ i ∧ i ≤ MAX ↔ (ofInt? i).isSome
47-
:= by simp [ofInt?]
47+
MIN ≤ i ∧ i ≤ MAX ↔ (ofInt? i) = some (Int64.ofInt i)
48+
:= by simp [ofInt?, ofIntChecked]
4849

49-
/-- Corollary of the above -/
50+
/-- Inverse of the above -/
5051
public theorem ofInt?_none_iff {i : Int} :
5152
i < MIN ∨ i > MAX ↔ (ofInt? i) = none
52-
:= by
53-
have h := ofInt?_some_iff (i := i)
54-
simp_all only [Option.isSome, gt_iff_lt]
55-
split at h
56-
· simp_all
57-
· by_cases i < MIN <;> simp_all
53+
:= by grind [ofInt?, ofIntChecked]
5854

5955
public def add? (i₁ i₂ : Int64) : Option Int64 := ofInt? (i₁.toInt + i₂.toInt)
6056

cedar-lean/Cedar/Spec/Entities.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,34 @@ public structure EntityData where
3737

3838
public abbrev Entities := Map EntityUID EntityData
3939

40+
@[expose]
4041
public def Entities.ancestors (es : Entities) (uid : EntityUID) : Result (Set EntityUID) := do
4142
let d ← es.findOrErr uid .entityDoesNotExist
4243
.ok d.ancestors
4344

45+
@[expose]
4446
public def Entities.ancestorsOrEmpty (es : Entities) (uid : EntityUID) : Set EntityUID :=
4547
match es.find? uid with
4648
| some d => d.ancestors
4749
| none => Set.empty
4850

51+
@[expose]
4952
public def Entities.attrs (es : Entities) (uid : EntityUID) : Result (Map Attr Value) := do
5053
let d ← es.findOrErr uid .entityDoesNotExist
5154
.ok d.attrs
5255

56+
@[expose]
5357
public def Entities.attrsOrEmpty (es : Entities) (uid : EntityUID) : Map Attr Value :=
5458
match es.find? uid with
5559
| some d => d.attrs
5660
| none => Map.empty
5761

62+
@[expose]
5863
public def Entities.tags (es : Entities) (uid : EntityUID) : Result (Map Tag Value) := do
5964
let d ← es.findOrErr uid .entityDoesNotExist
6065
.ok d.tags
6166

67+
@[expose]
6268
public def Entities.tagsOrEmpty (es : Entities) (uid : EntityUID) : Map Tag Value :=
6369
match es.find? uid with
6470
| some d => d.tags

cedar-lean/Cedar/Spec/ExtFun.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public def res {α} [Coe α Ext] : Option α → Result Value
5656
| some v => .ok v
5757
| none => .error .extensionError
5858

59+
@[expose]
5960
public def call : ExtFun → List Value → Result Value
6061
| .decimal, [.prim (.string s)] => res (Decimal.decimal s)
6162
| .lessThan,

cedar-lean/Cedar/SymCC/Compiler.lean

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ def reducibleEq (ty₁ ty₂ : TermType) : Result Bool :=
8282
else .error .typeError
8383

8484
def compileInₑ (t₁ t₂ : Term) (ancs? : Option UnaryFunction) : Term :=
85-
let isEq := if t₁.typeOf = t₂.typeOf then eq t₁ t₂ else false
86-
let isIn := if let some ancs := ancs? then set.member t₂ (app ancs t₁) else false
8785
or isEq isIn
86+
where
87+
isEq := if t₁.typeOf = t₂.typeOf then eq t₁ t₂ else false
88+
isIn := if let some ancs := ancs? then set.member t₂ (app ancs t₁) else false
8889

8990
def compileInₛ (t ts : Term) (ancs? : Option UnaryFunction) : Term :=
90-
let isIn₁ := if ts.typeOf = .set t.typeOf then set.member t ts else false
91-
let isIn₂ := if let some ancs := ancs? then set.intersects ts (app ancs t) else false
9291
or isIn₁ isIn₂
92+
where
93+
isIn₁ := if ts.typeOf = .set t.typeOf then set.member t ts else false
94+
isIn₂ := if let some ancs := ancs? then set.intersects ts (app ancs t) else false
9395

9496
def compileHasTag (entity tag : Term) : Option (Option SymTags) → Result Term
9597
| .none => .error .noSuchEntityType

cedar-lean/Cedar/SymCC/ExtFun.lean

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,27 @@ open Factory Batteries
4747

4848
namespace Decimal
4949

50-
def lessThan (t₁ t₂ : Term) : Term :=
50+
public def lessThan (t₁ t₂ : Term) : Term :=
5151
bvslt (ext.decimal.val t₁) (ext.decimal.val t₂)
5252

53-
def lessThanOrEqual (t₁ t₂ : Term) : Term :=
53+
public def lessThanOrEqual (t₁ t₂ : Term) : Term :=
5454
bvsle (ext.decimal.val t₁) (ext.decimal.val t₂)
5555

56-
def greaterThan (t₁ t₂ : Term) : Term :=
56+
public def greaterThan (t₁ t₂ : Term) : Term :=
5757
lessThan t₂ t₁
5858

59-
def greaterThanOrEqual (t₁ t₂ : Term) : Term :=
59+
public def greaterThanOrEqual (t₁ t₂ : Term) : Term :=
6060
lessThanOrEqual t₂ t₁
6161

6262
end Decimal
6363

6464
namespace IPAddr
6565
open BitVec
6666

67-
def isIpv4 (t : Term) : Term :=
67+
public def isIpv4 (t : Term) : Term :=
6868
ext.ipaddr.isV4 t
6969

70-
def isIpv6 (t : Term) : Term :=
70+
public def isIpv6 (t : Term) : Term :=
7171
not (ext.ipaddr.isV4 t)
7272

7373
def subnetWidth (w : Nat) (ipPre : Term) : Term :=
@@ -100,7 +100,7 @@ def inRangeV (isIp : Term → Term) (range : Term → Term × Term) (t₁ t₂ :
100100
(and (isIp t₁) (isIp t₂))
101101
(inRange range t₁ t₂))
102102

103-
def isInRange (t₁ t₂ : Term) : Term :=
103+
public def isInRange (t₁ t₂ : Term) : Term :=
104104
(or
105105
(inRangeV isIpv4 rangeV4 t₁ t₂)
106106
(inRangeV isIpv6 rangeV6 t₁ t₂))
@@ -113,45 +113,45 @@ def inRangeLit (t : Term) (cidr₄ : Ext.IPAddr.CIDR Ext.IPAddr.V4_WIDTH) (cidr
113113
(inRange rangeV4 t (ipTerm (Ext.IPAddr.IPNet.V4 cidr₄)))
114114
(inRange rangeV6 t (ipTerm (Ext.IPAddr.IPNet.V6 cidr₆))))
115115

116-
def isLoopback (t : Term) : Term :=
116+
public def isLoopback (t : Term) : Term :=
117117
inRangeLit t Ext.IPAddr.LOOP_BACK_CIDR_V4 Ext.IPAddr.LOOP_BACK_CIDR_V6
118118

119-
def isMulticast (t : Term) : Term :=
119+
public def isMulticast (t : Term) : Term :=
120120
inRangeLit t Ext.IPAddr.MULTICAST_CIDR_V4 Ext.IPAddr.MULTICAST_CIDR_V6
121121

122122
end IPAddr
123123

124124
namespace Duration
125125

126-
def toMilliseconds (t : Term) : Term := ext.duration.val t
126+
public def toMilliseconds (t : Term) : Term := ext.duration.val t
127127

128-
def toSeconds (t : Term) : Term :=
128+
public def toSeconds (t : Term) : Term :=
129129
bvsdiv (toMilliseconds t) (.prim (.bitvec (Int64.toBitVec 1000)))
130130

131-
def toMinutes (t : Term) : Term :=
131+
public def toMinutes (t : Term) : Term :=
132132
bvsdiv (toSeconds t) (.prim (.bitvec (Int64.toBitVec 60)))
133133

134-
def toHours (t : Term) : Term :=
134+
public def toHours (t : Term) : Term :=
135135
bvsdiv (toMinutes t) (.prim (.bitvec (Int64.toBitVec 60)))
136136

137-
def toDays (t : Term) : Term :=
137+
public def toDays (t : Term) : Term :=
138138
bvsdiv (toHours t) (.prim (.bitvec (Int64.toBitVec 24)))
139139

140140
end Duration
141141

142142
namespace Datetime
143143

144-
def offset (dt dur : Term) : Term :=
144+
public def offset (dt dur : Term) : Term :=
145145
let dt_val := ext.datetime.val dt
146146
let dur_val := ext.duration.val dur
147147
ifFalse (bvsaddo dt_val dur_val) (ext.datetime.ofBitVec (bvadd dt_val dur_val))
148148

149-
def durationSince (dt₁ dt₂ : Term) : Term :=
149+
public def durationSince (dt₁ dt₂ : Term) : Term :=
150150
let dt₁_val := ext.datetime.val dt₁
151151
let dt₂_val := ext.datetime.val dt₂
152152
ifFalse (bvssubo dt₁_val dt₂_val) (ext.duration.ofBitVec (bvsub dt₁_val dt₂_val))
153153

154-
def toDate (dt : Term) : Term :=
154+
public def toDate (dt : Term) : Term :=
155155
let zero := .prim (.bitvec (Int64.toBitVec 0))
156156
let one := .prim (.bitvec (Int64.toBitVec 1))
157157
let ms_per_day := .prim (.bitvec (Int64.toBitVec 86400000))
@@ -166,7 +166,7 @@ def toDate (dt : Term) : Term :=
166166
)
167167
)
168168

169-
def toTime (dt : Term) : Term :=
169+
public def toTime (dt : Term) : Term :=
170170
let zero := .prim (.bitvec (Int64.toBitVec 0))
171171
let ms_per_day := .prim (.bitvec (Int64.toBitVec 86400000))
172172
let dt_val := ext.datetime.val dt

cedar-lean/Cedar/SymCC/Factory.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ namespace Factory
4747

4848
---------- Term constructors ----------
4949

50+
@[inline, expose]
5051
public def noneOf (ty : TermType) : Term := .none ty
5152

53+
@[inline, expose]
5254
public def someOf (t : Term) : Term := .some t
5355
prefix:0 "⊙" => someOf
5456

cedar-lean/Cedar/SymCC/Function.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public def UnaryFunction.outType : UnaryFunction → TermType
5959
| .uuf f => f.out
6060
| .udf f => f.out
6161

62-
@[inline]
62+
@[inline, expose]
6363
public def UnaryFunction.isUUF : UnaryFunction → Bool
6464
| .uuf _ => true
6565
| .udf _ => false
6666

67-
@[inline]
67+
@[inline, expose]
6868
public def UnaryFunction.isUDF : UnaryFunction → Bool
6969
| .udf _ => true
7070
| .uuf _ => false
@@ -73,6 +73,7 @@ public def UDF.isLiteral (f : UDF) : Bool :=
7373
f.default.isLiteral &&
7474
f.table.toList.all λ (tᵢ, tₒ) => tᵢ.isLiteral && tₒ.isLiteral
7575

76+
@[inline, expose]
7677
public def UnaryFunction.isLiteral : UnaryFunction → Bool
7778
| .udf f => f.isLiteral
7879
| .uuf _ => false

cedar-lean/Cedar/SymCC/Interpretation.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ decreasing_by
164164
omega
165165
· omega
166166

167+
@[expose]
167168
public def SymRequest.interpret (I : Interpretation) (req : SymRequest) : SymRequest :=
168169
{
169170
principal := req.principal.interpret I,
@@ -189,6 +190,7 @@ public def SymEntityData.interpret (I : Interpretation) (d : SymEntityData) : Sy
189190
public def SymEntities.interpret (I : Interpretation) (es : SymEntities) : SymEntities :=
190191
es.mapOnValues (SymEntityData.interpret I)
191192

193+
@[expose]
192194
public def SymEnv.interpret (I : Interpretation) (env : SymEnv) : SymEnv :=
193195
⟨env.request.interpret I, env.entities.interpret I⟩
194196

cedar-lean/Cedar/SymCC/TermType.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,32 @@ public abbrev TermType.ext (xty : ExtType) : TermType := .prim (.ext xty)
5454
public abbrev TermType.tagFor (ety : EntityType) : TermType :=
5555
.record (EntityTag.mk (.entity ety) .string)
5656

57-
@[inline]
57+
@[inline, expose]
5858
public def TermType.isPrimType : TermType → Bool
5959
| .prim _ => true
6060
| _ => false
6161

62-
@[inline]
62+
@[inline, expose]
6363
public def TermType.isEntityType : TermType → Bool
6464
| .prim (.entity _) => true
6565
| _ => false
6666

67-
@[inline]
67+
@[inline, expose]
6868
public def TermType.isBitVecType : TermType → Bool
6969
| .prim (.bitvec _) => true
7070
| _ => false
7171

72-
@[inline]
72+
@[inline, expose]
7373
public def TermType.isRecordType : TermType → Bool
7474
| .record _ => true
7575
| _ => false
7676

77-
@[inline]
77+
@[inline, expose]
7878
public def TermType.isOptionType : TermType → Bool
7979
| .option _ => true
8080
| _ => false
8181

82-
@[inline]
82+
@[inline, expose]
8383
public def TermType.isOptionEntityType : TermType → Bool
8484
| .option (.entity _) => true
8585
| _ => false

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public theorem wf_empty {α β} [LT α] [DecidableLT α] :
7979
(Map.empty : Map α β).WellFormed
8080
:= by simp [Map.WellFormed, Map.make, Map.empty, List.canonicalize_nil, Map.toList]
8181

82+
public theorem wf_singleton [LT α] [DecidableLT α] (a : α) (b : β) :
83+
(Map.mk [(a, b)]).WellFormed
84+
:= by
85+
simp [WellFormed, make, toList, List.canonicalize_singleton]
86+
8287
/--
8388
In well-formed maps, if there are two pairs with the same key, then they have
8489
the same value

0 commit comments

Comments
 (0)