Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rely on impls more #556

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions cedar-lean/CedarProto/EntityDecl.lean
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ deriving Repr, Inhabited

namespace EntityDecl

@[inline]
def mergeOption [Field α] (x1 x2 : Option α) : Option α :=
match x1, x2 with
| some s1, some s2 => some (Field.merge s1 s2)
| none, some x => some x
| some x, none => some x
| none, none => none

@[inline]
def mergeName (result : EntityDecl) (x : Spec.Name) : EntityDecl :=
{result with
Expand All @@ -67,7 +59,7 @@ def mergeAttributes (result : EntityDecl) (x : Proto.Map String (Qualified Proto
@[inline]
def mergeTags (result : EntityDecl) (x : ProtoType) : EntityDecl :=
{result with
tags := mergeOption result.tags (some x)
tags := Field.merge result.tags (some x)
}

@[inline]
Expand All @@ -82,7 +74,7 @@ def merge (x y : EntityDecl) : EntityDecl :=
name := Field.merge x.name y.name
descendants := y.descendants ++ x.descendants
attrs := x.attrs ++ y.attrs
tags := mergeOption x.tags y.tags
tags := Field.merge x.tags y.tags
enums := x.enums ++ y.enums
}

Expand Down
14 changes: 9 additions & 5 deletions cedar-lean/Protobuf/Field.lean
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ namespace Merge
@[inline]
def override (_ : α) (x : α) : α := x

/-- Concatenation semantics, combines two arrays -/
@[inline]
def concatenate (x1 : Array α) (x2 : Array α) : Array α :=
x1.append x2

end Merge

@[inline]
Expand All @@ -58,6 +53,15 @@ def guardWireType {α : Type} [Field α] (wt : WireType) : BParsec Unit := do
if foundWt ≠ wt then
throw s!"WireType mismatch: found {repr foundWt}, expected {repr wt}"

instance [Field α] : Field (Option α) where
parse := do
let a : α ← Field.parse
pure (some a)
merge
| some a1, some a2 => some (Field.merge a1 a2)
| _, a2 => a2
expectedWireType := Field.expectedWireType α

@[inline]
def fromInterField {α β : Type} [Inhabited α] [Field α] (convert : α → β) (merge : β → β → β) : Field β := {
parse := do
Expand Down
2 changes: 1 addition & 1 deletion cedar-lean/Protobuf/Map.lean
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def parse [Inhabited KeyT] [Inhabited ValueT] [Field KeyT] [Field ValueT] : BPar
instance {α β : Type} [Inhabited α] [Inhabited β] [Field α] [Field β] : Field (Map α β) := {
parse := parse
expectedWireType := WireType.LEN
merge := Field.Merge.concatenate
merge := (· ++ ·)
}
end Map
end Proto
20 changes: 16 additions & 4 deletions cedar-lean/Protobuf/Packed.lean
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,22 @@ instance [Repr α] [Field α] : Repr (Repeated α) := by
unfold Repeated
apply inferInstance

instance [Field α] : HAppend (Repeated α) (Repeated α) (Repeated α) where
hAppend a b :=
let a : Array α := a
let b : Array α := b
a ++ b

/-- Parses one value from a record -/
@[inline]
def parse (α : Type) [Field α] : BParsec (Array α) := do
let element ← Field.parse
pure #[element]

instance [Field α] : Field (Repeated α) := {
parse := (parse α)
parse := parse α
expectedWireType := Field.expectedWireType α
merge := Field.Merge.concatenate
merge := (· ++ ·)
}

end Repeated
Expand All @@ -76,6 +82,12 @@ instance [Repr α] [Field α] : Repr (Packed α) := by
unfold Packed
apply inferInstance

instance [Field α] : HAppend (Packed α) (Packed α) (Packed α) where
hAppend a b :=
let a : Array α := a
let b : Array α := b
a ++ b

@[inline]
def parse (α : Type) [Field α] : BParsec (Array α) := do
let len_size ← Len.parseSize
Expand All @@ -86,9 +98,9 @@ def parse (α : Type) [Field α] : BParsec (Array α) := do
#[]

instance [Field α] : Field (Packed α) := {
parse := (parse α)
parse := parse α
expectedWireType := WireType.LEN
merge := Field.Merge.concatenate
merge := (· ++ ·)
}

end Packed
Expand Down