Skip to content

Commit 2bb9dda

Browse files
authored
use module system in Decoder.lean and Encoder.lean (#906)
Signed-off-by: Craig Disselkoen <cdiss@amazon.com>
1 parent 7264e73 commit 2bb9dda

File tree

17 files changed

+180
-122
lines changed

17 files changed

+180
-122
lines changed

cedar-lean/Cedar/SymCC.lean

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@
1414
limitations under the License.
1515
-/
1616

17-
import Cedar.SymCC.Extractor
18-
import Cedar.SymCC.Decoder
19-
import Cedar.SymCC.Encoder
20-
import Cedar.SymCC.SatUnsat
21-
import Cedar.SymCC.Solver
22-
import Cedar.SymCC.Verifier
23-
import Cedar.Validation.Validator
17+
module
18+
19+
-- TODO: once we remove the `@[expose]`s below, do all of these need to be public imports?
20+
public import Cedar.SymCC.Extractor
21+
public import Cedar.SymCC.Decoder
22+
public import Cedar.SymCC.Encoder
23+
public import Cedar.SymCC.SatUnsat
24+
public import Cedar.SymCC.Solver
25+
public import Cedar.SymCC.Verifier
26+
public import Cedar.Validation.Validator
2427

2528
/-! This file contains the top-level interface to SymCC. -/
2629

30+
@[expose] public section -- TODO: make the public interface more granular/intentional, instead of having everything public and exposed
31+
2732
namespace Cedar.SymCC
2833

2934
open Cedar.Spec

cedar-lean/Cedar/SymCC/Decoder.lean

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
limitations under the License.
1515
-/
1616

17+
module
18+
1719
import Cedar.Spec
18-
import Cedar.SymCC.Encoder
19-
import Cedar.SymCC.Interpretation
20+
public import Cedar.SymCC.Encoder
21+
public import Cedar.SymCC.Interpretation
2022
import Cedar.Validation
2123
import Std.Internal.Parsec.Basic
2224

@@ -34,7 +36,8 @@ See also Appendix B of https://smt-lib.org/papers/smt-lib-reference-v2.7-r2025-0
3436
namespace Cedar.SymCC.Decoder
3537

3638
open Std.Internal.Parsec String Batteries
37-
open Cedar.Spec Cedar.Validation Cedar.Data
39+
open Cedar.Validation Cedar.Data
40+
open Cedar.Spec hiding Result
3841

3942
----- Parsing functions for SMTLib syntax -----
4043

@@ -188,7 +191,7 @@ where
188191
let etyId ← enc.types.find? (.entity ety)
189192
.some (mems.mapIdx λ i eid => (Encoder.enumId etyId i, ⟨ety, eid⟩))
190193

191-
abbrev Result (α) := Except String α
194+
public abbrev Result (α) := Except String α
192195

193196
instance : Coe α (Result α) where
194197
coe := Except.ok
@@ -356,21 +359,21 @@ def defaultPrim (eidOf : EntityType → String) : TermPrimType → TermPrim
356359
| .entity ety => .entity ⟨ety, eidOf ety⟩
357360
| .ext xty => defaultExt xty
358361

359-
def defaultLit (eidOf : EntityType → String) : TermType → Term
362+
public def defaultLit (eidOf : EntityType → String) : TermType → Term
360363
| .prim pty => .prim (defaultPrim eidOf pty)
361364
| .option ty => .none ty
362365
| .set ty => .set Set.empty ty
363366
| .record tys => .record (tys.mapOnValues₂ λ ⟨ty, _⟩ => defaultLit eidOf ty)
364367

365-
def defaultUDF (eidOf : EntityType → String) (f : UUF) : UDF :=
368+
public def defaultUDF (eidOf : EntityType → String) (f : UUF) : UDF :=
366369
⟨f.arg, f.out, Map.empty, defaultLit eidOf f.out⟩
367370

368-
def eidOfForEntities (εs : SymEntities) (ety : EntityType) : String :=
371+
public def eidOfForEntities (εs : SymEntities) (ety : EntityType) : String :=
369372
match εs.find? ety with
370373
| .some ⟨_, _, .some (Set.mk (eid :: _)), _⟩ => eid
371374
| _ => ""
372375

373-
def defaultInterpretation (εs : SymEntities) : Interpretation :=
376+
public def defaultInterpretation (εs : SymEntities) : Interpretation :=
374377
let eidOf := (eidOfForEntities εs)
375378
{
376379
vars := λ v => defaultLit eidOf v.ty,
@@ -386,7 +389,7 @@ environment `εnv`. If `εnv` is well-formed, the terms `ts` are well-formed wit
386389
respect to `εnv.entities`, and CVC5 is sound, the the resulting Interpretation
387390
satisfies `ts` and is well-formed with respect to `εnv.entities`.
388391
-/
389-
def decode (model : String) (enc : EncoderState) : Result Interpretation := do
392+
public def decode (model : String) (enc : EncoderState) : Result Interpretation := do
390393
let x ← SExpr.parse |>.run model
391394
let ⟨vars, uufs⟩ ← x.decodeModel (IdMaps.ofEncoderState enc)
392395
let eidOf := λ ety =>

cedar-lean/Cedar/SymCC/Encoder.lean

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
limitations under the License.
1515
-/
1616

17-
import Cedar.SymCC.Env
17+
module
18+
19+
public import Cedar.SymCC.Env
1820
import Cedar.SymCC.Factory
19-
import Cedar.SymCC.Solver
20-
import Batteries.Data.RBMap
21+
public import Cedar.SymCC.Solver
22+
public import Batteries.Data.RBMap
2123

2224
/-!
2325
This file defines the Cedar encoder, which translates a list of boolean Terms
@@ -69,7 +71,7 @@ namespace Cedar.SymCC
6971
open Cedar.Spec Cedar.Validation
7072
open Solver Batteries
7173

72-
structure EncoderState where
74+
public structure EncoderState where
7375
terms : RBMap Term String (compareOfLessAndEq · ·)
7476
types : RBMap TermType String (compareOfLessAndEq · ·)
7577
uufs : RBMap UUF String (compareOfLessAndEq · ·)
@@ -84,12 +86,12 @@ abbrev EncoderM (α) := StateT EncoderState SolverM α
8486

8587
namespace Encoder
8688

87-
def termId (n : Nat) : String := s!"t{n}"
88-
def uufId (n : Nat) : String := s!"f{n}"
89-
def entityTypeId (n : Nat) : String := s!"E{n}"
90-
def enumId (E : String) (n : Nat) : String := s!"{E}_m{n}"
91-
def recordTypeId (n : Nat) : String := s!"R{n}"
92-
def recordAttrId (R : String) (a : Nat) : String := s!"{R}_a{a}"
89+
def termId (n : Nat) : String := s!"t{n}"
90+
def uufId (n : Nat) : String := s!"f{n}"
91+
def entityTypeId (n : Nat) : String := s!"E{n}"
92+
public def enumId (E : String) (n : Nat) : String := s!"{E}_m{n}"
93+
def recordTypeId (n : Nat) : String := s!"R{n}"
94+
def recordAttrId (R : String) (a : Nat) : String := s!"{R}_a{a}"
9395

9496
def typeNum : EncoderM Nat := do return (← get).types.size
9597
def termNum : EncoderM Nat := do return (← get).terms.size
@@ -146,7 +148,7 @@ The maximum Unicode code point supported in SMT-LIB 2.7.
146148
Also see `num_codes` in cvc5:
147149
https://github.com/cvc5/cvc5/blob/b78e7ed23348659db52a32765ad181ae0c26bbd5/src/util/string.h#L53
148150
-/
149-
def smtLibMaxCodePoint : Nat := 196607
151+
public def smtLibMaxCodePoint : Nat := 196607
150152

151153
/-
152154
This function needs to encode unicode strings with two levels of
@@ -335,7 +337,7 @@ Solver.lean.
335337
Note that `encode` itself first resets the solver in order to define datatypes
336338
etc.
337339
-/
338-
def encode (ts : List Term) (εnv : SymEnv) (produceModels : Bool := false) : SolverM EncoderState := do
340+
public def encode (ts : List Term) (εnv : SymEnv) (produceModels : Bool := false) : SolverM EncoderState := do
339341
Solver.reset
340342
Solver.setOptionProduceModels produceModels
341343
Solver.setLogic "ALL"

cedar-lean/Cedar/SymCC/Extractor.lean

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
limitations under the License.
1515
-/
1616

17-
import Cedar.SymCC.Enforcer
18-
import Cedar.SymCC.Concretizer
19-
import Cedar.SymCC.Interpretation
17+
module
2018

19+
-- TODO: once we remove the `@[expose]`s below, do all of these need to be public imports?
20+
public import Cedar.SymCC.Enforcer
21+
public import Cedar.SymCC.Concretizer
22+
public import Cedar.SymCC.Interpretation
2123

2224
/-!
2325
# Extracting strongly well-formed counterexamples to verification queries
@@ -41,6 +43,8 @@ See `Cedar.Thm.SymCC.Verification` for how this is used to prove that verificati
4143
queries based on Cedar's compiler and hierarchy enforcer are correct.
4244
-/
4345

46+
@[expose] public section -- TODO: make the public interface more granular/intentional, instead of having everything public and exposed
47+
4448
namespace Cedar.SymCC
4549

4650
open Data Factory Spec

cedar-lean/Cedar/SymCC/SatUnsat.lean

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
limitations under the License.
1515
-/
1616

17-
import Cedar.SymCC.Concretizer
18-
import Cedar.SymCC.Decoder
19-
import Cedar.SymCC.Extractor
20-
import Cedar.SymCC.Interpretation
21-
import Cedar.SymCC.Solver
22-
import Cedar.SymCC.Verifier
17+
module
18+
19+
-- TODO: once we remove the `@[expose]`s below, do all of these need to be public imports?
20+
public import Cedar.SymCC.Concretizer
21+
public import Cedar.SymCC.Decoder
22+
public import Cedar.SymCC.Extractor
23+
public import Cedar.SymCC.Interpretation
24+
public import Cedar.SymCC.Solver
25+
public import Cedar.SymCC.Verifier
26+
27+
@[expose] public section -- TODO: make the public interface more granular/intentional, instead of having everything public and exposed
2328

2429
namespace Cedar.SymCC
2530

cedar-lean/Cedar/SymCCOpt.lean

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@
1414
limitations under the License.
1515
-/
1616

17-
import Cedar.SymCC
18-
import Cedar.SymCCOpt.CompiledPolicies
19-
import Cedar.SymCCOpt.Extractor
20-
import Cedar.SymCCOpt.SatUnsat
21-
import Cedar.SymCCOpt.Verifier
22-
import Cedar.Validation.Validator
17+
module
18+
19+
-- TODO: once we remove the `@[expose]`s below, do all of these need to be public imports?
20+
public import Cedar.SymCC
21+
public import Cedar.SymCCOpt.CompiledPolicies
22+
public import Cedar.SymCCOpt.Extractor
23+
public import Cedar.SymCCOpt.SatUnsat
24+
public import Cedar.SymCCOpt.Verifier
25+
public import Cedar.Validation.Validator
2326

2427
/-! This file contains the top-level interface to optimized SymCC. -/
2528

29+
@[expose] public section -- TODO: make the public interface more granular/intentional, instead of having everything public and exposed
30+
2631
namespace Cedar.SymCC
2732

2833
open Cedar.Spec

cedar-lean/Cedar/SymCCOpt/Authorizer.lean

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,27 @@
1414
limitations under the License.
1515
-/
1616

17-
import Cedar.SymCCOpt.Compiler
17+
module
18+
19+
public import Cedar.SymCCOpt.Compiler
1820

1921
namespace Cedar.SymCC.Opt
2022

2123
open Factory Cedar.Spec
2224

23-
def compileWithEffect (effect : Effect) (policy : Policy) (εnv : SymEnv) : Result (Option CompileResult) :=
25+
public def compileWithEffect (effect : Effect) (policy : Policy) (εnv : SymEnv) : Result (Option CompileResult) :=
2426
if policy.effect == effect
2527
then Opt.compile policy.toExpr εnv
2628
else .ok .none
2729

28-
def satisfiedPolicies (effect : Effect) (policies : Policies) (εnv : SymEnv) : Result CompileResult := do
30+
public def satisfiedPolicies (effect : Effect) (policies : Policies) (εnv : SymEnv) : Result CompileResult := do
2931
let ress ← policies.filterMapM (compileWithEffect effect · εnv)
3032
.ok {
3133
term := anyTrue (eq · (someOf true)) (ress.map CompileResult.term)
3234
footprint := ress.mapUnion CompileResult.footprint
3335
}
3436

35-
def isAuthorized (policies : Policies) (εnv : SymEnv) : Result CompileResult := do
37+
public def isAuthorized (policies : Policies) (εnv : SymEnv) : Result CompileResult := do
3638
let forbids ← satisfiedPolicies .forbid policies εnv
3739
let permits ← satisfiedPolicies .permit policies εnv
3840
.ok {

cedar-lean/Cedar/SymCCOpt/CompiledPolicies.lean

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414
limitations under the License.
1515
-/
1616

17-
import Cedar.Spec
18-
import Cedar.SymCC
19-
import Cedar.SymCCOpt.Authorizer
20-
import Cedar.SymCCOpt.Compiler
21-
import Cedar.Validation.Validator
17+
module
18+
19+
-- TODO: once we remove the `@[expose]`s below, do all of these need to be public imports?
20+
public import Cedar.Spec
21+
public import Cedar.SymCC
22+
public import Cedar.SymCCOpt.Authorizer
23+
public import Cedar.SymCCOpt.Compiler
24+
public import Cedar.Validation.Validator
25+
26+
@[expose] public section -- TODO: make the public interface more granular/intentional, instead of having everything public and exposed
2227

2328
namespace Cedar.SymCC
2429

cedar-lean/Cedar/SymCCOpt/Compiler.lean

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
limitations under the License.
1515
-/
1616

17-
import Cedar.SymCC.Compiler
18-
import Cedar.SymCC.Env
19-
import Cedar.SymCC.ExtFun
20-
import Cedar.SymCC.Factory
17+
module
18+
19+
-- TODO: once we remove the `@[expose]`s below, do all of these need to be public imports?
20+
public import Cedar.SymCC.Compiler
21+
public import Cedar.SymCC.Env
22+
public import Cedar.SymCC.ExtFun
23+
public import Cedar.SymCC.Factory
2124

2225
/-!
2326
This file defines an optimized Cedar symbolic compiler, which computes the
@@ -30,6 +33,8 @@ For notes on the meaning of compile or footprint, see SymCC/Compiler.lean and
3033
SymCC/Enforcer.lean, which have clearer, unoptimized implementations.
3134
-/
3235

36+
@[expose] public section -- TODO: make the public interface more granular/intentional, instead of having everything public and exposed
37+
3338
namespace Cedar.SymCC.Opt
3439

3540
open Cedar.Data Cedar.Spec Cedar.Validation

cedar-lean/Cedar/SymCCOpt/Enforcer.lean

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
limitations under the License.
1515
-/
1616

17+
module
18+
1719
import Cedar.SymCC.Enforcer
18-
import Cedar.SymCCOpt.CompiledPolicies
20+
public import Cedar.SymCCOpt.CompiledPolicies
1921

2022
namespace Cedar.SymCC
2123

@@ -24,22 +26,22 @@ open Cedar.Data Cedar.Spec Factory
2426
/--
2527
Returns the ground acyclicity and transitivity assumptions for a single `CompiledPolicy`.
2628
-/
27-
def enforceCompiledPolicy (cp : CompiledPolicy) : Set Term :=
29+
public def enforceCompiledPolicy (cp : CompiledPolicy) : Set Term :=
2830
let tr := cp.footprint.elts.flatMap (λ t => cp.footprint.elts.map (transitivity t · cp.εnv.entities))
2931
Set.make (cp.acyclicity.elts ++ tr)
3032

3133
/--
3234
Returns the ground acyclicity and transitivity assumptions for a single `CompiledPolicySet`.
3335
-/
34-
def enforceCompiledPolicySet (cpset : CompiledPolicySet) : Set Term :=
36+
public def enforceCompiledPolicySet (cpset : CompiledPolicySet) : Set Term :=
3537
let tr := cpset.footprint.elts.flatMap (λ t => cpset.footprint.elts.map (transitivity t · cpset.εnv.entities))
3638
Set.make (cpset.acyclicity.elts ++ tr)
3739

3840
/--
3941
Returns the ground acyclicity and transitivity assumptions for a pair of `CompiledPolicy`.
4042
Caller guarantees that `cp₁` and `cp₂` were compiled for the same `εnv`.
4143
-/
42-
def enforcePairCompiledPolicy (cp₁ : CompiledPolicy) (cp₂ : CompiledPolicy) : Set Term :=
44+
public def enforcePairCompiledPolicy (cp₁ : CompiledPolicy) (cp₂ : CompiledPolicy) : Set Term :=
4345
assert! cp₁.εnv = cp₂.εnv
4446
let footprint := cp₁.footprint ++ cp₂.footprint
4547
let tr := footprint.elts.flatMap (λ t => footprint.elts.map (transitivity t · cp₁.εnv.entities))
@@ -49,7 +51,7 @@ def enforcePairCompiledPolicy (cp₁ : CompiledPolicy) (cp₂ : CompiledPolicy)
4951
Returns the ground acyclicity and transitivity assumptions for a pair of `CompiledPolicySet`.
5052
Caller guarantees that `cpset₁` and `cpset₂` were compiled for the same `εnv`.
5153
-/
52-
def enforcePairCompiledPolicySet (cpset₁ : CompiledPolicySet) (cpset₂ : CompiledPolicySet) : Set Term :=
54+
public def enforcePairCompiledPolicySet (cpset₁ : CompiledPolicySet) (cpset₂ : CompiledPolicySet) : Set Term :=
5355
assert! cpset₁.εnv = cpset₂.εnv
5456
let footprint := cpset₁.footprint ++ cpset₂.footprint
5557
let tr := footprint.elts.flatMap (λ t => footprint.elts.map (transitivity t · cpset₁.εnv.entities))

0 commit comments

Comments
 (0)