From 1018aa1af83e639a6b41b5650bf3b717e7f8de68 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 12 Jun 2024 14:46:52 +0200 Subject: Deactivate the coercion from Nat to Scalar --- backends/lean/Base/Primitives/Scalar.lean | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'backends/lean/Base/Primitives') diff --git a/backends/lean/Base/Primitives/Scalar.lean b/backends/lean/Base/Primitives/Scalar.lean index 8fb067e1..157ade2c 100644 --- a/backends/lean/Base/Primitives/Scalar.lean +++ b/backends/lean/Base/Primitives/Scalar.lean @@ -351,10 +351,17 @@ instance [Decide (Scalar.cMin ty ≤ v ∧ v ≤ Scalar.cMax ty)] : InBounds ty @[simp] abbrev Scalar.check_bounds (ty : ScalarTy) (x : Int) : Bool := (Scalar.cMin ty ≤ x || Scalar.min ty ≤ x) ∧ (x ≤ Scalar.cMax ty || x ≤ Scalar.max ty) +/- Discussion: + This coercion can be slightly annoying at times, because if we write + something like `u = 3` (where `u` is, for instance, as `U32`), then instead of + coercing `u` to `Int`, Lean will lift `3` to `U32`). + For now we deactivate it. + -- TODO(raitobezarius): the inbounds constraint is a bit ugly as we can pretty trivially -- discharge the lhs on ≥ 0. instance {ty: ScalarTy} [InBounds ty (Int.ofNat n)]: OfNat (Scalar ty) (n: ℕ) where ofNat := Scalar.ofInt n +-/ theorem Scalar.check_bounds_imp_in_bounds {ty : ScalarTy} {x : Int} (h: Scalar.check_bounds ty x) : -- cgit v1.2.3 From c8272aeea205ca9cb36e22757473ca2a931a4933 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 12 Jun 2024 14:52:34 +0200 Subject: Update the scalar notation for the Lean backend --- backends/lean/Base/Primitives/Scalar.lean | 50 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'backends/lean/Base/Primitives') diff --git a/backends/lean/Base/Primitives/Scalar.lean b/backends/lean/Base/Primitives/Scalar.lean index 157ade2c..5f14a14f 100644 --- a/backends/lean/Base/Primitives/Scalar.lean +++ b/backends/lean/Base/Primitives/Scalar.lean @@ -313,13 +313,13 @@ theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : apply And.intro <;> have hmin := Scalar.cMin_bound ty <;> have hmax := Scalar.cMax_bound ty <;> linarith /- [match_pattern] attribute: allows to us `Scalar.ofIntCore` inside of patterns. - This is particularly useful once we introduce notations like `#u32` (which + This is particularly useful once we introduce notations like `u32` (which desugards to `Scalar.ofIntCore`) as it allows to write expressions like this: Example: ``` match x with - | 0#u32 => ... - | 1#u32 => ... + | 0u32 => ... + | 1u32 => ... | ... ``` -/ @@ -328,7 +328,7 @@ theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : { val := x, hmin := h.left, hmax := h.right } -- The definitions below are used later to introduce nice syntax for constants, --- like `1#u32`. We are reusing the technique described here: https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Different.20elaboration.20inside.2Foutside.20of.20match.20patterns/near/425455284 +-- like `1u32`. We are reusing the technique described here: https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Different.20elaboration.20inside.2Foutside.20of.20match.20patterns/near/425455284 class InBounds (ty : ScalarTy) (x : Int) := hInBounds : Scalar.cMin ty ≤ x ∧ x ≤ Scalar.cMax ty @@ -1281,38 +1281,38 @@ def U128.ofIntCore := @Scalar.ofIntCore .U128 @[match_pattern] abbrev U64.ofInt := @Scalar.ofInt .U64 @[match_pattern] abbrev U128.ofInt := @Scalar.ofInt .U128 -postfix:max "#isize" => Isize.ofInt -postfix:max "#i8" => I8.ofInt -postfix:max "#i16" => I16.ofInt -postfix:max "#i32" => I32.ofInt -postfix:max "#i64" => I64.ofInt -postfix:max "#i128" => I128.ofInt -postfix:max "#usize" => Usize.ofInt -postfix:max "#u8" => U8.ofInt -postfix:max "#u16" => U16.ofInt -postfix:max "#u32" => U32.ofInt -postfix:max "#u64" => U64.ofInt -postfix:max "#u128" => U128.ofInt +postfix:max "isize" => Isize.ofInt +postfix:max "i8" => I8.ofInt +postfix:max "i16" => I16.ofInt +postfix:max "i32" => I32.ofInt +postfix:max "i64" => I64.ofInt +postfix:max "i128" => I128.ofInt +postfix:max "usize" => Usize.ofInt +postfix:max "u8" => U8.ofInt +postfix:max "u16" => U16.ofInt +postfix:max "u32" => U32.ofInt +postfix:max "u64" => U64.ofInt +postfix:max "u128" => U128.ofInt /- Testing the notations -/ -example := 0#u32 -example := 1#u32 -example := 1#i32 -example := 0#isize -example := (-1)#isize +example := 0u32 +example := 1u32 +example := 1i32 +example := 0isize +example := (-1)isize example (x : U32) : Bool := match x with - | 0#u32 => true + | 0u32 => true | _ => false example (x : U32) : Bool := match x with - | 1#u32 => true + | 1u32 => true | _ => false example (x : I32) : Bool := match x with - | (-1)#i32 => true + | (-1)i32 => true | _ => false -- Notation for pattern matching @@ -1334,7 +1334,7 @@ example {ty} (x : Scalar ty) : Bool := | _ => false -- Testing the notations -example : Result Usize := 0#usize + 1#usize +example : Result Usize := 0usize + 1usize -- TODO: factor those lemmas out @[simp] theorem Scalar.ofInt_val_eq {ty} (h : Scalar.min ty ≤ x ∧ x ≤ Scalar.max ty) : (Scalar.ofIntCore x h).val = x := by -- cgit v1.2.3 From fe59d3afe131e9f83cbdf73b1c8089ad090d92fb Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 12 Jun 2024 16:18:05 +0200 Subject: Update CoreConvertNum.lean --- backends/lean/Base/Primitives/CoreConvertNum.lean | 44 +++++++++++------------ 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'backends/lean/Base/Primitives') diff --git a/backends/lean/Base/Primitives/CoreConvertNum.lean b/backends/lean/Base/Primitives/CoreConvertNum.lean index eb456a96..bc3db8b2 100644 --- a/backends/lean/Base/Primitives/CoreConvertNum.lean +++ b/backends/lean/Base/Primitives/CoreConvertNum.lean @@ -18,40 +18,40 @@ namespace num -- core.convert.num -- Conversions def FromUsizeBool.from (b : Bool) : Usize := - if b then 1#usize else 0#usize + if b then 1usize else 0usize def FromU8Bool.from (b : Bool) : U8 := - if b then 1#u8 else 0#u8 + if b then 1u8 else 0u8 def FromU16Bool.from (b : Bool) : U16 := - if b then 1#u16 else 0#u16 + if b then 1u16 else 0u16 def FromU32Bool.from (b : Bool) : U32 := - if b then 1#u32 else 0#u32 + if b then 1u32 else 0u32 def FromU64Bool.from (b : Bool) : U64 := - if b then 1#u64 else 0#u64 + if b then 1u64 else 0u64 def FromU128Bool.from (b : Bool) : U128 := - if b then 1#u128 else 0#u128 + if b then 1u128 else 0u128 def FromIsizeBool.from (b : Bool) : Isize := - if b then 1#isize else 0#isize + if b then 1isize else 0isize def FromI8Bool.from (b : Bool) : I8 := - if b then 1#i8 else 0#i8 + if b then 1i8 else 0i8 def FromI16Bool.from (b : Bool) : I16 := - if b then 1#i16 else 0#i16 + if b then 1i16 else 0i16 def FromI32Bool.from (b : Bool) : I32 := - if b then 1#i32 else 0#i32 + if b then 1i32 else 0i32 def FromI64Bool.from (b : Bool) : I64 := - if b then 1#i64 else 0#i64 + if b then 1i64 else 0i64 def FromI128Bool.from (b : Bool) : I128 := - if b then 1#i128 else 0#i128 + if b then 1i128 else 0i128 def FromUsizeU8.from (x : U8) : Usize := ⟨ x.val, by scalar_tac, by scalar_tac ⟩ @@ -259,18 +259,18 @@ def FromI128I128 : core.convert.From I128 I128 := { } -- to_le_bytes -def core.num.U8.to_le_bytes (x : U8) : Array U8 1#usize := sorry -def core.num.U16.to_le_bytes (x : U16) : Array U8 2#usize := sorry -def core.num.U32.to_le_bytes (x : U32) : Array U8 4#usize := sorry -def core.num.U64.to_le_bytes (x : U64) : Array U8 8#usize := sorry -def core.num.U128.to_le_bytes (x : U128) : Array U8 128#usize := sorry +def core.num.U8.to_le_bytes (x : U8) : Array U8 1usize := sorry +def core.num.U16.to_le_bytes (x : U16) : Array U8 2usize := sorry +def core.num.U32.to_le_bytes (x : U32) : Array U8 4usize := sorry +def core.num.U64.to_le_bytes (x : U64) : Array U8 8usize := sorry +def core.num.U128.to_le_bytes (x : U128) : Array U8 128usize := sorry -- to_be_bytes -def core.num.U8.to_be_bytes (x : U8) : Array U8 1#usize := sorry -def core.num.U16.to_be_bytes (x : U16) : Array U8 2#usize := sorry -def core.num.U32.to_be_bytes (x : U32) : Array U8 4#usize := sorry -def core.num.U64.to_be_bytes (x : U64) : Array U8 8#usize := sorry -def core.num.U128.to_be_bytes (x : U128) : Array U8 128#usize := sorry +def core.num.U8.to_be_bytes (x : U8) : Array U8 1usize := sorry +def core.num.U16.to_be_bytes (x : U16) : Array U8 2usize := sorry +def core.num.U32.to_be_bytes (x : U32) : Array U8 4usize := sorry +def core.num.U64.to_be_bytes (x : U64) : Array U8 8usize := sorry +def core.num.U128.to_be_bytes (x : U128) : Array U8 128usize := sorry end core.convert -- cgit v1.2.3 From dd2b973a86680308807d7f26aff81d3310550f84 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 12 Jun 2024 18:20:23 +0200 Subject: Revert "Update CoreConvertNum.lean" This reverts commit fe59d3afe131e9f83cbdf73b1c8089ad090d92fb. --- backends/lean/Base/Primitives/CoreConvertNum.lean | 44 +++++++++++------------ 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'backends/lean/Base/Primitives') diff --git a/backends/lean/Base/Primitives/CoreConvertNum.lean b/backends/lean/Base/Primitives/CoreConvertNum.lean index bc3db8b2..eb456a96 100644 --- a/backends/lean/Base/Primitives/CoreConvertNum.lean +++ b/backends/lean/Base/Primitives/CoreConvertNum.lean @@ -18,40 +18,40 @@ namespace num -- core.convert.num -- Conversions def FromUsizeBool.from (b : Bool) : Usize := - if b then 1usize else 0usize + if b then 1#usize else 0#usize def FromU8Bool.from (b : Bool) : U8 := - if b then 1u8 else 0u8 + if b then 1#u8 else 0#u8 def FromU16Bool.from (b : Bool) : U16 := - if b then 1u16 else 0u16 + if b then 1#u16 else 0#u16 def FromU32Bool.from (b : Bool) : U32 := - if b then 1u32 else 0u32 + if b then 1#u32 else 0#u32 def FromU64Bool.from (b : Bool) : U64 := - if b then 1u64 else 0u64 + if b then 1#u64 else 0#u64 def FromU128Bool.from (b : Bool) : U128 := - if b then 1u128 else 0u128 + if b then 1#u128 else 0#u128 def FromIsizeBool.from (b : Bool) : Isize := - if b then 1isize else 0isize + if b then 1#isize else 0#isize def FromI8Bool.from (b : Bool) : I8 := - if b then 1i8 else 0i8 + if b then 1#i8 else 0#i8 def FromI16Bool.from (b : Bool) : I16 := - if b then 1i16 else 0i16 + if b then 1#i16 else 0#i16 def FromI32Bool.from (b : Bool) : I32 := - if b then 1i32 else 0i32 + if b then 1#i32 else 0#i32 def FromI64Bool.from (b : Bool) : I64 := - if b then 1i64 else 0i64 + if b then 1#i64 else 0#i64 def FromI128Bool.from (b : Bool) : I128 := - if b then 1i128 else 0i128 + if b then 1#i128 else 0#i128 def FromUsizeU8.from (x : U8) : Usize := ⟨ x.val, by scalar_tac, by scalar_tac ⟩ @@ -259,18 +259,18 @@ def FromI128I128 : core.convert.From I128 I128 := { } -- to_le_bytes -def core.num.U8.to_le_bytes (x : U8) : Array U8 1usize := sorry -def core.num.U16.to_le_bytes (x : U16) : Array U8 2usize := sorry -def core.num.U32.to_le_bytes (x : U32) : Array U8 4usize := sorry -def core.num.U64.to_le_bytes (x : U64) : Array U8 8usize := sorry -def core.num.U128.to_le_bytes (x : U128) : Array U8 128usize := sorry +def core.num.U8.to_le_bytes (x : U8) : Array U8 1#usize := sorry +def core.num.U16.to_le_bytes (x : U16) : Array U8 2#usize := sorry +def core.num.U32.to_le_bytes (x : U32) : Array U8 4#usize := sorry +def core.num.U64.to_le_bytes (x : U64) : Array U8 8#usize := sorry +def core.num.U128.to_le_bytes (x : U128) : Array U8 128#usize := sorry -- to_be_bytes -def core.num.U8.to_be_bytes (x : U8) : Array U8 1usize := sorry -def core.num.U16.to_be_bytes (x : U16) : Array U8 2usize := sorry -def core.num.U32.to_be_bytes (x : U32) : Array U8 4usize := sorry -def core.num.U64.to_be_bytes (x : U64) : Array U8 8usize := sorry -def core.num.U128.to_be_bytes (x : U128) : Array U8 128usize := sorry +def core.num.U8.to_be_bytes (x : U8) : Array U8 1#usize := sorry +def core.num.U16.to_be_bytes (x : U16) : Array U8 2#usize := sorry +def core.num.U32.to_be_bytes (x : U32) : Array U8 4#usize := sorry +def core.num.U64.to_be_bytes (x : U64) : Array U8 8#usize := sorry +def core.num.U128.to_be_bytes (x : U128) : Array U8 128#usize := sorry end core.convert -- cgit v1.2.3 From d36736fa4e7eb9f42f35303b8080d17ddbee92d2 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 12 Jun 2024 18:20:52 +0200 Subject: Revert "Update the scalar notation for the Lean backend" This reverts commit c8272aeea205ca9cb36e22757473ca2a931a4933. --- backends/lean/Base/Primitives/Scalar.lean | 50 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'backends/lean/Base/Primitives') diff --git a/backends/lean/Base/Primitives/Scalar.lean b/backends/lean/Base/Primitives/Scalar.lean index 5f14a14f..157ade2c 100644 --- a/backends/lean/Base/Primitives/Scalar.lean +++ b/backends/lean/Base/Primitives/Scalar.lean @@ -313,13 +313,13 @@ theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : apply And.intro <;> have hmin := Scalar.cMin_bound ty <;> have hmax := Scalar.cMax_bound ty <;> linarith /- [match_pattern] attribute: allows to us `Scalar.ofIntCore` inside of patterns. - This is particularly useful once we introduce notations like `u32` (which + This is particularly useful once we introduce notations like `#u32` (which desugards to `Scalar.ofIntCore`) as it allows to write expressions like this: Example: ``` match x with - | 0u32 => ... - | 1u32 => ... + | 0#u32 => ... + | 1#u32 => ... | ... ``` -/ @@ -328,7 +328,7 @@ theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : { val := x, hmin := h.left, hmax := h.right } -- The definitions below are used later to introduce nice syntax for constants, --- like `1u32`. We are reusing the technique described here: https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Different.20elaboration.20inside.2Foutside.20of.20match.20patterns/near/425455284 +-- like `1#u32`. We are reusing the technique described here: https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Different.20elaboration.20inside.2Foutside.20of.20match.20patterns/near/425455284 class InBounds (ty : ScalarTy) (x : Int) := hInBounds : Scalar.cMin ty ≤ x ∧ x ≤ Scalar.cMax ty @@ -1281,38 +1281,38 @@ def U128.ofIntCore := @Scalar.ofIntCore .U128 @[match_pattern] abbrev U64.ofInt := @Scalar.ofInt .U64 @[match_pattern] abbrev U128.ofInt := @Scalar.ofInt .U128 -postfix:max "isize" => Isize.ofInt -postfix:max "i8" => I8.ofInt -postfix:max "i16" => I16.ofInt -postfix:max "i32" => I32.ofInt -postfix:max "i64" => I64.ofInt -postfix:max "i128" => I128.ofInt -postfix:max "usize" => Usize.ofInt -postfix:max "u8" => U8.ofInt -postfix:max "u16" => U16.ofInt -postfix:max "u32" => U32.ofInt -postfix:max "u64" => U64.ofInt -postfix:max "u128" => U128.ofInt +postfix:max "#isize" => Isize.ofInt +postfix:max "#i8" => I8.ofInt +postfix:max "#i16" => I16.ofInt +postfix:max "#i32" => I32.ofInt +postfix:max "#i64" => I64.ofInt +postfix:max "#i128" => I128.ofInt +postfix:max "#usize" => Usize.ofInt +postfix:max "#u8" => U8.ofInt +postfix:max "#u16" => U16.ofInt +postfix:max "#u32" => U32.ofInt +postfix:max "#u64" => U64.ofInt +postfix:max "#u128" => U128.ofInt /- Testing the notations -/ -example := 0u32 -example := 1u32 -example := 1i32 -example := 0isize -example := (-1)isize +example := 0#u32 +example := 1#u32 +example := 1#i32 +example := 0#isize +example := (-1)#isize example (x : U32) : Bool := match x with - | 0u32 => true + | 0#u32 => true | _ => false example (x : U32) : Bool := match x with - | 1u32 => true + | 1#u32 => true | _ => false example (x : I32) : Bool := match x with - | (-1)i32 => true + | (-1)#i32 => true | _ => false -- Notation for pattern matching @@ -1334,7 +1334,7 @@ example {ty} (x : Scalar ty) : Bool := | _ => false -- Testing the notations -example : Result Usize := 0usize + 1usize +example : Result Usize := 0#usize + 1#usize -- TODO: factor those lemmas out @[simp] theorem Scalar.ofInt_val_eq {ty} (h : Scalar.min ty ≤ x ∧ x ≤ Scalar.max ty) : (Scalar.ofIntCore x h).val = x := by -- cgit v1.2.3