Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade examples to Purity Inference #223

Merged
merged 38 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fecc220
upgrade Arithmetic example
lukewilliamboswell Dec 20, 2024
35cfc03
upgrade CommandLineArgs example
lukewilliamboswell Dec 20, 2024
f2fe587
upgrade CommandLineArgsFile example
lukewilliamboswell Dec 20, 2024
3aa1587
format upgraded examples
lukewilliamboswell Dec 20, 2024
d43f0aa
upgrade examples/CustomInspect/OpaqueTypes.roc
lukewilliamboswell Dec 20, 2024
b6c980f
upgrade DesugaringAwait example
lukewilliamboswell Dec 20, 2024
8101c25
upgrade DesugaringTry example
lukewilliamboswell Dec 20, 2024
0eb8315
upgrade EncodeDecode example
lukewilliamboswell Dec 20, 2024
d1cb621
upgrade FizzBuzz example
lukewilliamboswell Dec 20, 2024
bd1ef11
upgrade GraphTraversal example
lukewilliamboswell Dec 20, 2024
5725d45
upgrade HelloWeb example
lukewilliamboswell Dec 20, 2024
b04d603
upgrade HelloWorld example
lukewilliamboswell Dec 20, 2024
59eba26
upgrade ImportFromDirectory example
lukewilliamboswell Dec 20, 2024
e919121
upgrade ImportPackageFromModule example
lukewilliamboswell Dec 20, 2024
378fbc1
upgrade IngestFiles example
lukewilliamboswell Dec 20, 2024
98e5817
upgrade Json example
lukewilliamboswell Dec 20, 2024
cab513c
upgrade LeastSquares example
lukewilliamboswell Dec 20, 2024
88a681d
upgrade MultipleRocFiles example
lukewilliamboswell Dec 20, 2024
822ba32
upgrade Parser example
lukewilliamboswell Dec 20, 2024
a480ed7
update examples/MultipleRocFiles/Hello.roc
lukewilliamboswell Dec 20, 2024
07c9575
upgrade RandomNumbers example
lukewilliamboswell Dec 20, 2024
924d2c4
upgrade RecordBuilder example
lukewilliamboswell Dec 20, 2024
b47ffbc
upgrade Results example
lukewilliamboswell Dec 20, 2024
8dde089
upgrade SafeMath example
lukewilliamboswell Dec 20, 2024
732cbb1
upgrade TaskLoop example
lukewilliamboswell Dec 20, 2024
fa4a5a9
upgrade TowersOfHanoi example
lukewilliamboswell Dec 20, 2024
34b4b05
upgrade Tuples example
lukewilliamboswell Dec 20, 2024
de3ef07
upgrade Tasks example
lukewilliamboswell Dec 20, 2024
1c09333
fixup expect scripts
lukewilliamboswell Dec 20, 2024
ffb35dc
update to basic-cli 0.18
Anton-4 Dec 28, 2024
3a64426
use basic-ws 11
Anton-4 Dec 28, 2024
af13086
format + re-enable
Anton-4 Dec 28, 2024
609df1e
test on macos too
Anton-4 Dec 31, 2024
14fb71d
forgot some lines
Anton-4 Dec 31, 2024
8c10ec0
finishing up
Anton-4 Jan 3, 2025
0173aa7
typo
Anton-4 Jan 3, 2025
33fb988
update md title
Anton-4 Jan 3, 2025
113ce46
mention issue
Anton-4 Jan 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@ on:
pull_request:
workflow_dispatch:

name: Test examples on Ubuntu
name: Test examples without nix

jobs:
test-examples:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-20.04, ubuntu-24.04]
operating-system: [ubuntu-20.04, ubuntu-24.04, macos-13, macos-14]
timeout-minutes: 90
steps:
- uses: actions/checkout@v3

- name: Set OS-specific variables
id: vars
run: |
if [[ "${{ matrix.operating-system }}" =~ ^ubuntu- ]]; then
echo "os_pattern=linux_x86_64" >> $GITHUB_OUTPUT
elif [ "${{ matrix.operating-system }}" = "macos-13" ]; then
echo "os_pattern=macos_x86_64" >> $GITHUB_OUTPUT
else
echo "os_pattern=macos_apple_silicon" >> $GITHUB_OUTPUT
fi

- id: try_fetching_testing_release
continue-on-error: true
run: |
curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_x86_64-TESTING.tar.gz
curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-${{ steps.vars.outputs.os_pattern }}-TESTING.tar.gz

- name: There are no TESTING releases, checking regular releases instead
if: steps.try_fetching_testing_release.outcome == 'failure'
run: |
curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_x86_64-latest.tar.gz
curl -fOL https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-${{ steps.vars.outputs.os_pattern }}-latest.tar.gz

- name: rename nightly tar
run: mv $(ls | grep "roc_nightly.*tar\.gz") roc_nightly.tar.gz
Expand All @@ -38,16 +49,28 @@ jobs:
- name: get short commit SHA
run: echo "SHORT_COMMIT_SHA=$(./roc_nightly/roc version | grep -oP 'commit \K[a-f0-9]+' )" >> $GITHUB_ENV

- name: install expect # used to check if output is correct
- name: Install dependencies (Ubuntu)
if: startsWith(matrix.operating-system, 'ubuntu-')
run: sudo apt install -y expect

- name: print runner CPU
- name: Install dependencies (macOS)
if: startsWith(matrix.operating-system, 'macos-')
run: |
brew install expect
brew install z3

- name: print runner CPU (Ubuntu)
if: startsWith(matrix.operating-system, 'ubuntu-')
run: lscpu

- name: print runner CPU (macOS)
if: startsWith(matrix.operating-system, 'macos-')
run: sysctl -n machdep.cpu.brand_string

- name: check if roc files are properly formatted
run: ROC=./roc_nightly/roc ./ci_scripts/check_format.sh

- run: ROC=./roc_nightly/roc ./ci_scripts/all_tests.sh

- name: Checks if every folder in examples is mentioned in ./examples/index.md
run: bash ./ci_scripts/check_index.sh
run: bash ./ci_scripts/check_index.sh
57 changes: 29 additions & 28 deletions ci_scripts/all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ expect ci_scripts/expect_scripts/CommandLineArgs.exp
$ROC build ./examples/CommandLineArgsFile/main.roc
expect ci_scripts/expect_scripts/CommandLineArgsFile.exp

$ROC build ./examples/DesugaringAwait/main.roc
expect ci_scripts/expect_scripts/DesugaringAwait.exp

$ROC build ./examples/DesugaringTry/main.roc
$ROC test ./examples/DesugaringTry/main.roc
expect ci_scripts/expect_scripts/DesugaringTry.exp
Expand All @@ -66,11 +63,11 @@ $ROC test ./examples/TowersOfHanoi/Hanoi.roc
$ROC build ./examples/Results/main.roc
expect ci_scripts/expect_scripts/Results.exp

$ROC build ./examples/Tasks/main.roc
expect ci_scripts/expect_scripts/Tasks.exp
$ROC build ./examples/ErrorHandling/main.roc
expect ci_scripts/expect_scripts/ErrorHandling.exp

$ROC build ./examples/TaskLoop/main.roc
expect ci_scripts/expect_scripts/TaskLoop.exp
$ROC build ./examples/LoopEffect/main.roc
expect ci_scripts/expect_scripts/LoopEffect.exp

$ROC test ./examples/RecordBuilder/DateParser.roc

Expand All @@ -95,26 +92,30 @@ expect ci_scripts/expect_scripts/HelloWeb.exp
$ROC build ./examples/ImportPackageFromModule/main.roc
expect ci_scripts/expect_scripts/ImportPackageFromModule.exp

$ROC build --lib ./examples/GoPlatform/main.roc --output examples/GoPlatform/platform/libapp.so
go build -C examples/GoPlatform/platform -buildmode=pie -o dynhost

$ROC preprocess-host ./examples/GoPlatform/platform/dynhost ./examples/GoPlatform/platform/main.roc ./examples/GoPlatform/platform/libapp.so
$ROC build ./examples/GoPlatform/main.roc

# temporarily allow failure of lsb_release in case it is not installed
set +e
os_info=$(lsb_release -a 2>/dev/null)
set -e

# Skip Go tests if os is Ubuntu and we're not inside nix. This avoids a segfault on CI. See https://github.com/roc-lang/examples/issues/164
if echo "$os_info" | grep -q "Ubuntu" && [ -z "${IN_NIX_SHELL}" ]; then
echo "Skipping Go test due to https://github.com/roc-lang/examples/issues/164"
else
echo "Running Go test..."
expect ci_scripts/expect_scripts/GoPlatform.exp
fi

$ROC test ./examples/CustomInspect/OpaqueTypes.roc

$ROC build ./examples/DotNetPlatform/main.roc --lib --output ./examples/DotNetPlatform/platform/interop
expect ci_scripts/expect_scripts/DotNetPlatform.exp
# Check if we're not on macOS, these examples don't work on macos yet
if [[ "$(uname)" != "Darwin" ]]; then
$ROC build --lib ./examples/GoPlatform/main.roc --output examples/GoPlatform/platform/libapp.so
go build -C examples/GoPlatform/platform -buildmode=pie -o dynhost

$ROC preprocess-host ./examples/GoPlatform/platform/dynhost ./examples/GoPlatform/platform/main.roc ./examples/GoPlatform/platform/libapp.so
$ROC build ./examples/GoPlatform/main.roc

# temporarily allow failure of lsb_release in case it is not installed
set +e
os_info=$(lsb_release -a 2>/dev/null)
set -e

# Skip Go tests if os is Ubuntu and we're not inside nix. This avoids a segfault on CI. See https://github.com/roc-lang/examples/issues/164
if echo "$os_info" | grep -q "Ubuntu" && [ -z "${IN_NIX_SHELL}" ]; then
echo "Skipping Go test due to https://github.com/roc-lang/examples/issues/164"
else
echo "Running Go test..."
expect ci_scripts/expect_scripts/GoPlatform.exp
fi


$ROC build ./examples/DotNetPlatform/main.roc --lib --output ./examples/DotNetPlatform/platform/interop
expect ci_scripts/expect_scripts/DotNetPlatform.exp
fi
26 changes: 0 additions & 26 deletions ci_scripts/expect_scripts/DesugaringAwait.exp

This file was deleted.

2 changes: 1 addition & 1 deletion ci_scripts/expect_scripts/DesugaringTry.exp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set timeout 7

spawn ./examples/DesugaringTry/main

expect -exact "(Ok {birthYear: 1990, name: \"Alice\"})\r\n(Ok {birthYear: 1990, name: \"Alice\"})\r\n" {
expect -exact "(Ok {birth_year: 1990, name: \"Alice\"})\r\n(Ok {birth_year: 1990, name: \"Alice\"})\r\n" {
expect eof
exit 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set timeout 7

spawn ./examples/Tasks/main "https://www.roc-lang.org" roc.html
spawn ./examples/ErrorHandling/main "https://www.roc-lang.org" roc.html

expect "Done\r\n" {
expect eof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set timeout 7

spawn ./examples/TaskLoop/main
spawn ./examples/LoopEffect/main

expect "Enter some numbers on different lines, then press Ctrl-D to sum them up.\r\n"

Expand All @@ -18,7 +18,7 @@ send "\004"

expect "Sum: 2\r\n" {
expect eof {
spawn ./examples/TaskLoop/main
spawn ./examples/LoopEffect/main

expect "Enter some numbers on different lines, then press Ctrl-D to sum them up.\r\n"

Expand Down
5 changes: 2 additions & 3 deletions ci_scripts/expect_scripts/RandomNumbers.exp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ set timeout 7

spawn ./examples/RandomNumbers/main


expect "Random numbers are: 29,30,71,64,48,33,55,68,53,28\r\n" {
expect "52\r\n34\r\n26\r\n69\r\n34\r\n35\r\n51\r\n74\r\n70\r\n39\r\n" {
expect eof
exit 0
}

puts stderr "\nError: output was different from expected value."
exit 1
exit 1
4 changes: 2 additions & 2 deletions ci_scripts/expect_scripts/Tuples.exp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ set timeout 7

spawn ./examples/Tuples/main

expect "First is: A String,\r\nSecond is: true, \r\nThird is: 15000000.\r\nYou also have some pears.\r\n" {
expect "First is: A String,\r\nSecond is: true,\r\nThird is: 15000000.\r\nYou also have some pears.\r\n" {
expect eof
exit 0
}

puts stderr "\nError: output was different from expected value."
exit 1
exit 1
72 changes: 35 additions & 37 deletions examples/Arithmetic/main.roc
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br" }
app [main!] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.18.0/0APbwVN1_p1mJ96tXjaoiUCr8NBGamr8G8Ac_DrXR-o.tar.br" }

import pf.Stdout
import pf.Arg
import pf.Arg exposing [Arg]

TaskErrors : [InvalidArg, InvalidNumStr]
main! : List Arg.Arg => Result {} _
main! = \raw_args ->

main =
task =
args = readArgs!
args : { a : I32, b : I32 }
args = try read_args raw_args

formatResult = \(operation, result) ->
resultStr = Num.toStr result
result =
[
("sum", args.a + args.b),
("difference", args.a - args.b),
("product", args.a * args.b),
("integer quotient", args.a // args.b),
("remainder", args.a % args.b),
("exponentiation", Num.powInt args.a args.b),
]
|> List.map \(operation, answer) ->
answer_str = Num.toStr answer

"$(operation): $(resultStr)"
"$(operation): $(answer_str)"
|> Str.joinWith "\n"

results =
[
("sum", args.a + args.b),
("difference", args.a - args.b),
("product", args.a * args.b),
("integer quotient", args.a // args.b),
("remainder", args.a % args.b),
("exponentiation", Num.powInt args.a args.b),
]
|> List.map formatResult
|> Str.joinWith "\n"

Task.ok results

when Task.result! task is
Ok result -> Stdout.line result
Err InvalidArg -> Task.err (Exit 1 "Error: Please provide two integers between -1000 and 1000 as arguments.")
Err InvalidNumStr -> Task.err (Exit 1 "Error: Invalid number format. Please provide integers between -1000 and 1000.")
Stdout.line! result

## Reads two command-line arguments, attempts to parse them as `I32` numbers,
## and returns a task containing a record with two fields, `a` and `b`, holding
Expand All @@ -41,21 +34,26 @@ main =
## as `I32` numbers, or if the parsed numbers are outside the expected range
## (-1000 to 1000), the function will return a task that fails with an
## error `InvalidArg` or `InvalidNumStr`.
readArgs : Task.Task { a : I32, b : I32 } TaskErrors
readArgs =
read_args : List Arg -> Result { a : I32, b : I32 } [Exit I32 Str]
read_args = \raw_args ->

invalid_args = Exit 1 "Error: Please provide two integers between -1000 and 1000 as arguments."
invalid_num_str = Exit 1 "Error: Invalid number format. Please provide integers between -1000 and 1000."

args =
Arg.list {}
|> Task.mapErr! \_ -> InvalidArg
if List.len raw_args != 3 then
return Err invalid_args
else
List.map raw_args Arg.display

aResult = List.get args 1 |> Result.try Str.toI32
bResult = List.get args 2 |> Result.try Str.toI32
a_result = List.get args 1 |> Result.try Str.toI32
b_result = List.get args 2 |> Result.try Str.toI32

when (aResult, bResult) is
when (a_result, b_result) is
(Ok a, Ok b) ->
if a < -1000 || a > 1000 || b < -1000 || b > 1000 then
Task.err InvalidNumStr
Err invalid_num_str
else
Task.ok { a, b }
Ok { a, b }

_ -> Task.err InvalidNumStr
_ -> Err invalid_num_str
Loading
Loading