Skip to content

Commit

Permalink
fix box implementation, add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
javra committed Dec 1, 2023
1 parent 4332150 commit 81be12c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Aegis/Libfuncs/Box.lean
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace Sierra.FuncData

variable (metadataRef : FVarId)

def box_into (t : SierraType) : FuncData where
def into_box (t : SierraType) : FuncData where
inputTypes := [t]
branches := [{ outputTypes := [.Box t]
condition := fun a (ρ : Q(Nat)) =>
let m : Q(Metadata) := .fvar metadataRef
let lhs : Q(Option ($(t).toType)) := q($(m).boxHeap $t $ρ)
let rhs : Expr := mkAppN (mkConst `Option.some) #[q(Option ($(t).toType)), a]
let rhs := mkAppN (mkConst `Option.some [levelZero]) #[t.toQuote, a]
Expr.mkEq q(Option ($(t).toType)) lhs rhs }]

def unbox (t : SierraType) : FuncData where
Expand All @@ -21,13 +21,13 @@ def unbox (t : SierraType) : FuncData where
condition := fun (a : Q(Nat)) (ρ : Expr) =>
let m : Q(Metadata) := .fvar metadataRef
let rhs : Q(Option ($(t).toType)) := q($(m).boxHeap $t $a)
let lhs : Expr := mkAppN (mkConst `Option.some) #[q(Option ($(t).toType)), ρ]
let lhs := mkAppN (mkConst `Option.some [levelZero]) #[t.toQuote, ρ]
Expr.mkEq q(Option ($(t).toType)) lhs rhs }]

def boxLibfuncs (typeRefs : HashMap Identifier SierraType) : Identifier → Option FuncData
| .name "box_into" [.identifier ident] _ =>
| .name "into_box" [.identifier ident] _ =>
match typeRefs.find? ident with
| .some t => box_into metadataRef t
| .some t => into_box metadataRef t
| _ => .none
| .name "unbox" [.identifier ident] _ =>
match typeRefs.find? ident with
Expand Down
37 changes: 37 additions & 0 deletions Aegis/Tests/Commands.lean
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,43 @@ aegis_spec "test::u128_const" := fun _ _ => True

aegis_prove "test::u128_const" := fun _ _ _ => True.intro

aegis_load_string "type felt252 = felt252 [storable: true, drop: true, dup: true, zero_sized: false];
type Box<felt252> = Box<felt252> [storable: true, drop: true, dup: true, zero_sized: false];
libfunc into_box<felt252> = into_box<felt252>;
into_box<felt252>([0]) -> ([1]); // 0
return([1]); // 1
test::into_box@0([0]: felt252) -> (Box<felt252>);"

aegis_spec "test::into_box" := fun m a ρ =>
m.boxHeap .Felt252 ρ = .some a

aegis_prove "test::into_box" := fun _ _ _ _ => by
aesop

aegis_load_string "type Box<felt252> = Box<felt252> [storable: true, drop: true, dup: true, zero_sized: false];
type felt252 = felt252 [storable: true, drop: true, dup: true, zero_sized: false];
libfunc unbox<felt252> = unbox<felt252>;
libfunc store_temp<felt252> = store_temp<felt252>;
unbox<felt252>([0]) -> ([1]); // 0
store_temp<felt252>([1]) -> ([1]); // 1
return([1]); // 2
test::unbox@0([0]: Box<felt252>) -> (felt252);"

aegis_spec "test::unbox" := fun m a ρ =>
m.boxHeap .Felt252 a = .some ρ

aegis_prove "test::unbox" := fun m a b => by
unfold «spec_test::unbox»
rintro ⟨_,h,rfl⟩
symm
assumption

aegis_load_string "type Unit = Struct<ut@Tuple>;
type core::bool = Enum<ut@core::bool, Unit, Unit>;
Expand Down

0 comments on commit 81be12c

Please sign in to comment.