Revert "Avoid defining unsized platform Tasks"

This reverts commit d3732b4440.
This commit is contained in:
Anton-4 2024-08-28 16:37:01 +02:00
parent d3732b4440
commit ca7d1cb3fa
No known key found for this signature in database
GPG key ID: 0971D718C0A9B937
18 changed files with 105 additions and 109 deletions

View file

@ -5,9 +5,7 @@ import pf.PlatformTasks
# adapted from https://github.com/koka-lang/koka/blob/master/test/bench/haskell/cfold.hs
main : Task {} []
main =
{ value, isError } =
PlatformTasks.getInt
|> Task.mapErr! \_ -> crash "unreachable"
{ value, isError } = PlatformTasks.getInt!
inputResult =
if isError then
Err GetIntError
@ -21,15 +19,13 @@ main =
optimized = eval (constFolding (reassoc e))
unoptimized
|> Num.toStr
|> Str.concat " & "
|> Str.concat (Num.toStr optimized)
|> PlatformTasks.putLine
|> Task.mapErr! \_ -> crash "unreachable"
|> Num.toStr
|> Str.concat " & "
|> Str.concat (Num.toStr optimized)
|> PlatformTasks.putLine
Err GetIntError ->
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|> Task.mapErr! \_ -> crash "unreachable"
Expr : [
Add Expr Expr,

View file

@ -7,9 +7,7 @@ IO a : Task a []
main : Task {} []
main =
{ value, isError } =
PlatformTasks.getInt
|> Task.mapErr! \_ -> crash "unreachable"
{ value, isError } = PlatformTasks.getInt!
inputResult =
if isError then
Err GetIntError
@ -29,7 +27,6 @@ main =
Err GetIntError ->
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|> Task.mapErr! \_ -> crash "unreachable"
nestHelp : I64, (I64, Expr -> IO Expr), I64, Expr -> IO Expr
nestHelp = \s, f, m, x ->
@ -170,7 +167,5 @@ deriv = \i, f ->
Num.toStr (i + 1)
|> Str.concat " count: "
|> Str.concat (Num.toStr (count fprime))
PlatformTasks.putLine line
|> Task.mapErr! \_ -> crash "unreachable"
PlatformTasks.putLine! line
Task.ok fprime

View file

@ -11,4 +11,3 @@ main =
Issue2279Help.asText 42
PlatformTasks.putLine text
|> Task.mapErr! \_ -> crash "unreachable"

View file

@ -4,9 +4,7 @@ import pf.PlatformTasks
main : Task {} []
main =
{ value, isError } =
PlatformTasks.getInt
|> Task.mapErr! \_ -> crash "unreachable"
{ value, isError } = PlatformTasks.getInt!
inputResult =
if isError then
Err GetIntError
@ -16,13 +14,11 @@ main =
when inputResult is
Ok n ->
queens n # original koka 13
|> Num.toStr
|> PlatformTasks.putLine
|> Task.mapErr! \_ -> crash "unreachable"
|> Num.toStr
|> PlatformTasks.putLine
Err GetIntError ->
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|> Task.mapErr! \_ -> crash "unreachable"
ConsList a : [Nil, Cons a (ConsList a)]

View file

@ -2,8 +2,8 @@ hosted PlatformTasks
exposes [putLine, putInt, getInt]
imports []
putLine : Str -> Task {} {}
putLine : Str -> Task {} *
putInt : I64 -> Task {} {}
putInt : I64 -> Task {} *
getInt : Task { value : I64, isError : Bool } {}
getInt : Task { value : I64, isError : Bool } *

File diff suppressed because one or more lines are too long

View file

@ -39,9 +39,7 @@ fold = \f, tree, b ->
main : Task {} []
main =
{ value, isError } =
PlatformTasks.getInt
|> Task.mapErr! \_ -> crash "unreachable"
{ value, isError } = PlatformTasks.getInt!
inputResult =
if isError then
Err GetIntError
@ -59,17 +57,14 @@ main =
val = fold (\_, v, r -> if v then r + 1 else r) head 0
val
|> Num.toStr
|> PlatformTasks.putLine
|> Task.mapErr! \_ -> crash "unreachable"
|> Num.toStr
|> PlatformTasks.putLine
Nil ->
PlatformTasks.putLine "fail"
|> Task.mapErr! \_ -> crash "unreachable"
Err GetIntError ->
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|> Task.mapErr! \_ -> crash "unreachable"
insert : Tree (Num k) v, Num k, v -> Tree (Num k) v
insert = \t, k, v -> if isRed t then setBlack (ins t k v) else ins t k v

View file

@ -12,9 +12,7 @@ ConsList a : [Nil, Cons a (ConsList a)]
main : Task {} []
main =
{ value, isError } =
PlatformTasks.getInt
|> Task.mapErr! \_ -> crash "unreachable"
{ value, isError } = PlatformTasks.getInt!
inputResult =
if isError then
Err GetIntError
@ -27,13 +25,11 @@ main =
val = fold (\_, v, r -> if v then r + 1 else r) m 0
val
|> Num.toStr
|> PlatformTasks.putLine
|> Task.mapErr! \_ -> crash "unreachable"
|> Num.toStr
|> PlatformTasks.putLine
Err GetIntError ->
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|> Task.mapErr! \_ -> crash "unreachable"
boom : Str -> a
boom = \_ -> boom ""

View file

@ -8,9 +8,8 @@ main =
tree = insert 0 {} Empty
tree
|> show
|> PlatformTasks.putLine
|> Task.mapErr! \_ -> crash "unreachable"
|> show
|> PlatformTasks.putLine
show : RedBlackTree I64 {} -> Str
show = \tree -> showRBTree tree Num.toStr (\{} -> "{}")

View file

@ -5,8 +5,7 @@ import AStar
main : Task {} []
main =
PlatformTasks.putLine (showBool test1)
|> Task.mapErr! \_ -> crash "unreachable"
PlatformTasks.putLine! (showBool test1)
showBool : Bool -> Str
showBool = \b ->

View file

@ -8,19 +8,10 @@ IO a : Task a []
main : IO {}
main =
when Base64.fromBytes (Str.toUtf8 "Hello World") is
Err _ ->
PlatformTasks.putLine "sadness"
|> Task.mapErr! \_ -> crash "unreachable"
Err _ -> PlatformTasks.putLine "sadness"
Ok encoded ->
PlatformTasks.putLine (Str.concat "encoded: " encoded)
|> Task.mapErr! \_ -> crash "unreachable"
PlatformTasks.putLine! (Str.concat "encoded: " encoded)
when Base64.toStr encoded is
Ok decoded ->
PlatformTasks.putLine (Str.concat "decoded: " decoded)
|> Task.mapErr! \_ -> crash "unreachable"
Err _ ->
PlatformTasks.putLine "sadness"
|> Task.mapErr! \_ -> crash "unreachable"
Ok decoded -> PlatformTasks.putLine (Str.concat "decoded: " decoded)
Err _ -> PlatformTasks.putLine "sadness"

View file

@ -2,6 +2,6 @@ hosted PlatformTasks
exposes [putLine, getLine]
imports []
putLine : Str -> Task {} {}
putLine : Str -> Task {} *
getLine : Task Str {}
getLine : Task Str *

View file

@ -4,12 +4,8 @@ import pf.PlatformTasks
main : Task {} []
main =
line =
PlatformTasks.getLine
|> Task.mapErr! \_ -> crash "unreachable"
PlatformTasks.putLine "You entered: $(line)"
|> Task.mapErr! \_ -> crash "unreachable"
PlatformTasks.putLine "It is known"
|> Task.mapErr! \_ -> crash "unreachable"
line = PlatformTasks.getLine!
PlatformTasks.putLine! "You entered: $(line)"
PlatformTasks.putLine! "It is known"
Task.ok {}

View file

@ -5,25 +5,18 @@ import pf.PlatformTasks
Handle := U64
line : Handle -> Task Str *
line = \@Handle handle ->
PlatformTasks.getFileLine handle
|> Task.mapErr \_ -> crash "unreachable"
line = \@Handle handle -> PlatformTasks.getFileLine handle
chunk : Handle -> Task (List U8) *
chunk = \@Handle handle ->
PlatformTasks.getFileBytes handle
|> Task.mapErr \_ -> crash "unreachable"
chunk = \@Handle handle -> PlatformTasks.getFileBytes handle
open : Str -> Task Handle *
open = \path ->
PlatformTasks.openFile path
|> Task.map @Handle
|> Task.mapErr \_ -> crash "unreachable"
close : Handle -> Task.Task {} *
close = \@Handle handle ->
PlatformTasks.closeFile handle
|> Task.mapErr \_ -> crash "unreachable"
close = \@Handle handle -> PlatformTasks.closeFile handle
withOpen : Str, (Handle -> Task {} a) -> Task {} a
withOpen = \path, callback ->

View file

@ -2,20 +2,20 @@ hosted PlatformTasks
exposes [openFile, closeFile, withFileOpen, getFileLine, getFileBytes, putLine, putRaw, getLine, getChar]
imports []
openFile : Str -> Task U64 {}
openFile : Str -> Task U64 *
closeFile : U64 -> Task {} {}
closeFile : U64 -> Task {} *
withFileOpen : Str, (U64 -> Task ok err) -> Task {} {}
withFileOpen : Str, (U64 -> Task ok err) -> Task {} *
getFileLine : U64 -> Task Str {}
getFileLine : U64 -> Task Str *
getFileBytes : U64 -> Task (List U8) {}
getFileBytes : U64 -> Task (List U8) *
putLine : Str -> Task {} {}
putLine : Str -> Task {} *
putRaw : Str -> Task {} {}
putRaw : Str -> Task {} *
getLine : Task Str {}
getLine : Task Str *
getChar : Task U8 {}
getChar : Task U8 *

View file

@ -5,12 +5,8 @@ module [
import pf.PlatformTasks
line : {} -> Task Str *
line = \{} ->
PlatformTasks.getLine
|> Task.mapErr \_ -> crash "unreachable"
line : Task Str *
line = PlatformTasks.getLine
char : {} -> Task U8 *
char = \{} ->
PlatformTasks.getChar
|> Task.mapErr \_ -> crash "unreachable"
char : Task U8 *
char = PlatformTasks.getChar

View file

@ -3,11 +3,7 @@ module [line, raw]
import pf.PlatformTasks
line : Str -> Task {} *
line = \text ->
PlatformTasks.putLine text
|> Task.mapErr \_ -> crash "unreachable"
line = PlatformTasks.putLine
raw : Str -> Task {} *
raw = \text ->
PlatformTasks.putRaw text
|> Task.mapErr \_ -> crash "unreachable"
raw = PlatformTasks.putRaw

53
sort.roc Normal file
View file

@ -0,0 +1,53 @@
app [main] { cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.13.0/nW9yMRtZuCYf1Oa9vbE5XoirMwzLbtoSgv7NGhUlqYA.tar.br" }
import cli.Stdout
import cli.Task
import cli.Arg
import cli.File
import cli.Utc
import cli.Task exposing [Task]
dataPath : U64 -> Str
dataPath = \day ->
"data/day$(if day < 10 then "0" else "")$(Num.toStr day).txt"
loadData : U64 -> Task Str _
loadData = \day ->
day
|> dataPath
|> File.readUtf8
|> Task.mapErr UnableToReadFile
solutions : List (Str -> Str, Str -> Str)
solutions = [
(\i -> i,\i -> i),
]
runSolution : (Str -> Str), U64, Str -> Task {} []_
runSolution = \solution, index, input ->
Stdout.line! "Part $(Num.toStr index):"
startTime = Utc.now!
result = solution input
endTime = Utc.now!
delta = Utc.deltaAsMillis startTime endTime |> Num.toStr
Stdout.line! "$(result) ($(delta)ms)"
checkDay : U64 -> Task {} [InvalidDay Str]_
checkDay = \day ->
if day < 1 || day > 25 then Task.err (InvalidDay "Must be between 1 and 25") else Task.ok {}
runDay : Str -> Task {} []_
runDay = \dayArg ->
day = Str.toU64 dayArg |> Task.fromResult!
checkDay! day
Stdout.line! "Day $(Num.toStr day)"
input = loadData! day
(part1, part2) = List.get solutions (day - 1) |> Task.fromResult!
runSolution! part1 1 input
runSolution! part2 2 input
main =
args = Arg.list! {}
daysStr = if List.len args < 2 then List.range { start: At 1, end: At 25 } |> List.map Num.toStr else args |> List.dropFirst 1
_ = daysStr |> List.map runDay |> List.reverse |> Task.sequence!
Task.ok {}