From 85c9d319504275a70367f78f166e7173297507c8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 1 Feb 2024 23:47:59 +0100 Subject: [PATCH] Add crash implementation to document compiler bug --- crash-me.roc | 23 +++++++++++++++++++++++ higher-order.roc | 21 ++++++++++++++++----- 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 crash-me.roc diff --git a/crash-me.roc b/crash-me.roc new file mode 100644 index 0000000..a54861d --- /dev/null +++ b/crash-me.roc @@ -0,0 +1,23 @@ +app "crash-me" + packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.1/Icc3xJoIixF3hCcfXrDwLCu4wQHtNdPyoJkEbkgIElA.tar.br" } + imports [pf.Stdout] + provides [main] to pf + +main = + fn1 = bind Num.sub 2 + fn2 = compose fn1 fn1 + + results = + [fn1, fn2] + |> List.map \f -> f 2 + |> List.map Num.toStr + |> Str.joinWith "\n" + Stdout.line "$(results)" + +bind : (a, b -> c), a -> (b -> c) +bind = \fn, a -> + \b -> fn a b + +compose : (a -> b), (b -> c) -> (a -> c) +compose = \fn1, fn2 -> + \a -> fn1 a |> fn2 diff --git a/higher-order.roc b/higher-order.roc index f69ecd8..d87c5b6 100644 --- a/higher-order.roc +++ b/higher-order.roc @@ -5,13 +5,24 @@ app "higher-order" main : Task.Task {} I32 main = - mySum = bind anySum 2 - Stdout.line "$(Num.toStr (mySum 2))" + fn1 = bind Num.sub 3 + fn2 = bindL Num.sub 3 + fn3 = compose fn2 fn1 + results = + [fn1 2, fn2 2, fn3 2] + |> List.map Num.toStr + |> List.mapWithIndex \e, idx -> "fn$(Num.toStr idx) -> $(e)" + |> Str.joinWith "\n" + Stdout.line "$(results)" bind : (a, b -> c), a -> (b -> c) bind = \fn, a -> \b -> fn a b -anySum : Num a, Num a -> Num a -anySum = \a, b -> - a + b + 2 +bindL : (a, b -> c), b -> (a -> c) +bindL = \fn, b -> + \a -> fn a b + +compose : (a -> b), (b -> c) -> (a -> c) +compose = \fn1, fn2 -> + \a -> fn1 a |> fn2