Skip to content

Commit

Permalink
Merge branch 'main' into auto-snake-case
Browse files Browse the repository at this point in the history
  • Loading branch information
smores56 committed Jan 5, 2025
2 parents 0edbf16 + 8b641dd commit 865a399
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 211 deletions.
36 changes: 18 additions & 18 deletions crates/cli/tests/benchmarks/Base64/Decode.roc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ loop_help = \{ remaining, string } ->
if remaining >= 3 then
Bytes.Decode.map3(Bytes.Decode.u8, Bytes.Decode.u8, Bytes.Decode.u8, \x, y, z ->
a : U32
a = Num.int_cast(x)
a = Num.intCast(x)
b : U32
b = Num.int_cast(y)
b = Num.intCast(y)
c : U32
c = Num.int_cast(z)
combined = Num.bitwise_or(Num.bitwise_or(Num.shift_left_by(a, 16), Num.shift_left_by(b, 8)), c)
c = Num.intCast(z)
combined = Num.bitwiseOr(Num.bitwiseOr(Num.shiftLeftBy(a, 16), Num.shiftLeftBy(b, 8)), c)

Loop({
remaining: remaining - 3,
Expand All @@ -31,24 +31,24 @@ loop_help = \{ remaining, string } ->
Bytes.Decode.map2(Bytes.Decode.u8, Bytes.Decode.u8, \x, y ->

a : U32
a = Num.int_cast(x)
a = Num.intCast(x)
b : U32
b = Num.int_cast(y)
combined = Num.bitwise_or(Num.shift_left_by(a, 16), Num.shift_left_by(b, 8))
b = Num.intCast(y)
combined = Num.bitwiseOr(Num.shiftLeftBy(a, 16), Num.shiftLeftBy(b, 8))

Done(Str.concat(string, bits_to_chars(combined, 1))))
else
# remaining = 1
Bytes.Decode.map(Bytes.Decode.u8, \x ->

a : U32
a = Num.int_cast(x)
a = Num.intCast(x)

Done(Str.concat(string, bits_to_chars(Num.shift_left_by(a, 16), 2))))
Done(Str.concat(string, bits_to_chars(Num.shiftLeftBy(a, 16), 2))))

bits_to_chars : U32, Int * -> Str
bits_to_chars = \bits, missing ->
when Str.from_utf8(bits_to_chars_help(bits, missing)) is
when Str.fromUtf8(bits_to_chars_help(bits, missing)) is
Ok(str) -> str
Err(_) -> ""

Expand All @@ -63,23 +63,23 @@ bits_to_chars_help = \bits, missing ->
# with `0b111111` (which is 2^6 - 1 or 63) (so, 6 1s) to remove unwanted bits on the left.
# any 6-bit number is a valid base64 digit, so this is actually safe
p =
Num.shift_right_zf_by(bits, 18)
|> Num.int_cast
Num.shiftRightZfBy(bits, 18)
|> Num.intCast
|> unsafe_to_char

q =
Num.bitwise_and(Num.shift_right_zf_by(bits, 12), lowest6_bits_mask)
|> Num.int_cast
Num.bitwiseAnd(Num.shiftRightZfBy(bits, 12), lowest6_bits_mask)
|> Num.intCast
|> unsafe_to_char

r =
Num.bitwise_and(Num.shift_right_zf_by(bits, 6), lowest6_bits_mask)
|> Num.int_cast
Num.bitwiseAnd(Num.shiftRightZfBy(bits, 6), lowest6_bits_mask)
|> Num.intCast
|> unsafe_to_char

s =
Num.bitwise_and(bits, lowest6_bits_mask)
|> Num.int_cast
Num.bitwiseAnd(bits, lowest6_bits_mask)
|> Num.intCast
|> unsafe_to_char

equals : U8
Expand Down
30 changes: 15 additions & 15 deletions crates/cli/tests/benchmarks/Base64/Encode.roc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ InvalidChar : U8
to_bytes : Str -> List U8
to_bytes = \str ->
str
|> Str.to_utf8
|> Str.toUtf8
|> encode_chunks
|> Bytes.Encode.sequence
|> Bytes.Encode.encode
Expand Down Expand Up @@ -73,18 +73,18 @@ encode_characters = \a, b, c, d ->
n2 = unsafe_convert_char(b)

x : U32
x = Num.int_cast(n1)
x = Num.intCast(n1)

y : U32
y = Num.int_cast(n2)
y = Num.intCast(n2)

if d == equals then
if c == equals then
n = Num.bitwise_or(Num.shift_left_by(x, 18), Num.shift_left_by(y, 12))
n = Num.bitwiseOr(Num.shiftLeftBy(x, 18), Num.shiftLeftBy(y, 12))

# masking higher bits is not needed, Encode.unsignedInt8 ignores higher bits
b1 : U8
b1 = Num.int_cast(Num.shift_right_by(n, 16))
b1 = Num.intCast(Num.shiftRightBy(n, 16))

Ok(Bytes.Encode.u8(b1))
else if !(is_valid_char(c)) then
Expand All @@ -93,12 +93,12 @@ encode_characters = \a, b, c, d ->
n3 = unsafe_convert_char(c)

z : U32
z = Num.int_cast(n3)
z = Num.intCast(n3)

n = Num.bitwise_or(Num.bitwise_or(Num.shift_left_by(x, 18), Num.shift_left_by(y, 12)), Num.shift_left_by(z, 6))
n = Num.bitwiseOr(Num.bitwiseOr(Num.shiftLeftBy(x, 18), Num.shiftLeftBy(y, 12)), Num.shiftLeftBy(z, 6))

combined : U16
combined = Num.int_cast(Num.shift_right_by(n, 8))
combined = Num.intCast(Num.shiftRightBy(n, 8))

Ok(Bytes.Encode.u16(BE, combined))
else if !(is_valid_char(d)) then
Expand All @@ -108,22 +108,22 @@ encode_characters = \a, b, c, d ->
n4 = unsafe_convert_char(d)

z : U32
z = Num.int_cast(n3)
z = Num.intCast(n3)

w : U32
w = Num.int_cast(n4)
w = Num.intCast(n4)

n =
Num.bitwise_or(
Num.bitwise_or(Num.shift_left_by(x, 18), Num.shift_left_by(y, 12)),
Num.bitwise_or(Num.shift_left_by(z, 6), w),
Num.bitwiseOr(
Num.bitwiseOr(Num.shiftLeftBy(x, 18), Num.shiftLeftBy(y, 12)),
Num.bitwiseOr(Num.shiftLeftBy(z, 6), w),
)

b3 : U8
b3 = Num.int_cast(n)
b3 = Num.intCast(n)

combined : U16
combined = Num.int_cast(Num.shift_right_by(n, 8))
combined = Num.intCast(Num.shiftRightBy(n, 8))

Ok(Bytes.Encode.sequence([Bytes.Encode.u16(BE, combined), Bytes.Encode.u8(b3)]))

Expand Down
14 changes: 7 additions & 7 deletions crates/cli/tests/benchmarks/cFold.roc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
app [main] { pf: platform "platform/main.roc" }
app [main!] { pf: platform "platform/main.roc" }

import pf.PlatformTasks
import pf.Host

# adapted from https://github.com/koka-lang/koka/blob/master/test/bench/haskell/cfold.hs
main : Task {} []
main =
{ value, is_error } = PlatformTasks.get_int!
main! : {} => {}
main! = \{} ->
{ value, is_error } = Host.get_int!({})
input_result =
if is_error then
Err(GetIntError)
Expand All @@ -22,10 +22,10 @@ main =
|> Num.to_str
|> Str.concat(" & ")
|> Str.concat(Num.to_str(optimized))
|> PlatformTasks.put_line
|> Host.put_line!
Err(GetIntError) ->
PlatformTasks.put_line("Error: Failed to get Integer from stdin.")
Host.put_line!("Error: Failed to get Integer from stdin.")
Expr : [
Add Expr Expr,
Expand Down
42 changes: 22 additions & 20 deletions crates/cli/tests/benchmarks/closure.roc
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
app [main] { pf: platform "platform/main.roc" }
app [main!] { pf: platform "platform/main.roc" }

main : Task {} []
main =
main! : {} => {}
main! = \{} ->
closure1({})
|> Task.await(\_ -> closure2({}))
|> Task.await(\_ -> closure3({}))
|> Task.await(\_ -> closure4({}))
|> Result.try(closure2)
|> Result.try(closure3)
|> Result.try(closure4)
|> Result.with_default({})

# ---
closure1 : {} -> Task {} []
closure1 : {} -> Result {} []
closure1 = \_ ->
Task.ok(foo(to_unit_borrowed, "a long string such that it's malloced"))
|> Task.map(\_ -> {})
Ok(foo(to_unit_borrowed, "a long string such that it's malloced"))
|> Result.map(\_ -> {})

to_unit_borrowed = \x -> Str.count_utf8_bytes(x)

foo = \f, x -> f(x)

# ---
closure2 : {} -> Task {} []
closure2 : {} -> Result {} []
closure2 = \_ ->
x : Str
x = "a long string such that it's malloced"

Task.ok({})
|> Task.map(\_ -> x)
|> Task.map(to_unit)
Ok({})
|> Result.map(\_ -> x)
|> Result.map(to_unit)

to_unit = \_ -> {}

# # ---
closure3 : {} -> Task {} []
closure3 : {} -> Result {} []
closure3 = \_ ->
x : Str
x = "a long string such that it's malloced"

Task.ok({})
|> Task.await(\_ -> Task.ok(x) |> Task.map(\_ -> {}))
Ok({})
|> Result.try(\_ -> Ok(x) |> Result.map(\_ -> {}))

# # ---
closure4 : {} -> Task {} []
closure4 : {} -> Result {} []
closure4 = \_ ->
x : Str
x = "a long string such that it's malloced"

Task.ok({})
|> Task.await(\_ -> Task.ok(x))
|> Task.map(\_ -> {})
Ok({})
|> Result.try(\_ -> Ok(x))
|> Result.map(\_ -> {})
38 changes: 18 additions & 20 deletions crates/cli/tests/benchmarks/deriv.roc
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
app [main] { pf: platform "platform/main.roc" }
app [main!] { pf: platform "platform/main.roc" }

import pf.PlatformTasks
import pf.Host

# based on: https://github.com/koka-lang/koka/blob/master/test/bench/haskell/deriv.hs
IO a : Task a []

main : Task {} []
main =
{ value, is_error } = PlatformTasks.get_int!
main! : {} => {}
main! = \{} ->
{ value, is_error } = Host.get_int!({})
input_result =
if is_error then
Err(GetIntError)
Expand All @@ -22,22 +20,22 @@ main =
f : Expr
f = pow(x, x)

nest(deriv, n, f) # original koka n = 10
|> Task.map(\_ -> {})
_ = nest!(deriv!, n, f) # original koka n = 10
{}

Err(GetIntError) ->
PlatformTasks.put_line("Error: Failed to get Integer from stdin.")
Host.put_line!("Error: Failed to get Integer from stdin.")
nest_help : I64, (I64, Expr -> IO Expr), I64, Expr -> IO Expr
nest_help = \s, f, m, x ->
nest_help! : I64, (I64, Expr => Expr), I64, Expr => Expr
nest_help! = \s, f!, m, x ->
when m is
0 -> Task.ok(x)
0 -> x
_ ->
w = f!((s - m), x)
nest_help(s, f, (m - 1), w)
nest_help!(s, f!, (m - 1), w)
nest : (I64, Expr -> IO Expr), I64, Expr -> IO Expr
nest = \f, n, e -> nest_help(n, f, n, e)
nest! : (I64, Expr => Expr), I64, Expr => Expr
nest! = \f!, n, e -> nest_help!(n, f!, n, e)
Expr : [Val I64, Var Str, Add Expr Expr, Mul Expr Expr, Pow Expr Expr, Ln Expr]
Expand Down Expand Up @@ -160,12 +158,12 @@ count = \expr ->
Pow(f, g) -> count(f) + count(g)
Ln(f) -> count(f)
deriv : I64, Expr -> IO Expr
deriv = \i, f ->
deriv! : I64, Expr => Expr
deriv! = \i, f ->
fprime = d("x", f)
line =
Num.to_str((i + 1))
|> Str.concat(" count: ")
|> Str.concat(Num.to_str(count(fprime)))
PlatformTasks.put_line!(line)
Task.ok(fprime)
Host.put_line!(line)
fprime
8 changes: 4 additions & 4 deletions crates/cli/tests/benchmarks/issue2279.roc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
app [main] { pf: platform "platform/main.roc" }
app [main!] { pf: platform "platform/main.roc" }

import Issue2279Help
import pf.PlatformTasks
import pf.Host

main =
main! = \{} ->
text =
if Bool.true then
Issue2279Help.text
else
Issue2279Help.as_text(42)

PlatformTasks.put_line(text)
Host.put_line!(text)
14 changes: 7 additions & 7 deletions crates/cli/tests/benchmarks/nQueens.roc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
app [main] { pf: platform "platform/main.roc" }
app [main!] { pf: platform "platform/main.roc" }

import pf.PlatformTasks
import pf.Host

main : Task {} []
main =
{ value, is_error } = PlatformTasks.get_int!
main! : {} => {}
main! = \{} ->
{ value, is_error } = Host.get_int!({})
input_result =
if is_error then
Err(GetIntError)
Expand All @@ -15,10 +15,10 @@ main =
Ok(n) ->
queens(n) # original koka 13
|> Num.to_str
|> PlatformTasks.put_line
|> Host.put_line!

Err(GetIntError) ->
PlatformTasks.put_line("Error: Failed to get Integer from stdin.")
Host.put_line!("Error: Failed to get Integer from stdin.")
ConsList a : [Nil, Cons a (ConsList a)]
Expand Down
9 changes: 9 additions & 0 deletions crates/cli/tests/benchmarks/platform/Host.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
hosted Host
exposes [put_line!, put_int!, get_int!]
imports []

put_line! : Str => {}

put_int! : I64 => {}

get_int! : {} => { value : I64, is_error : Bool }
5 changes: 3 additions & 2 deletions crates/cli/tests/benchmarks/platform/app.roc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
app [main] { pf: platform "main.roc" }
app [main!] { pf: platform "main.roc" }

main = Task.ok({})
main! : {} => {}
main! = \{} -> {}
Loading

0 comments on commit 865a399

Please sign in to comment.