Skip to content

Latest commit

 

History

History
237 lines (193 loc) · 11.3 KB

README.md

File metadata and controls

237 lines (193 loc) · 11.3 KB

Lutz Hühnken's Flix Examples

Flix project Directory lutz-examples\ contains Flix code examples presented in Lutz Hühnken's article "Flix for Java programmers" (June 2022).

🔎 Visit the Codeberg repository 2022-06-24-flix-for-java-programmers to get the original Flix code examples.

We made the following changes resp. additions in comparison to the original Flix code examples :

  • The source files have been updated 1 to successfully compile with the latest version of the Flix compiler.
  • Each example has the same directory layout as the Flix code examples presented in document examples\README.md.
  • Each example includes a basic test suite (directory src\test\).
  • We provide several build scripts to build, run and test the Flix code examples.

Currently we provide four ways to build, run and test the Flix code examples :

Build tool Build file Parent file Environment(s)
cmd.exe build.bat   MS Windows
sh.exe build.sh   Any a)
gradle.exe build.gradle common.gradle Any
make.exe Makefile Makefile.inc Any
a) Here "Any" means "tested on MS Windows / Cygwin / MSYS2 / Unix".
 

channels Example

Command build -verbose run generates the target file channels.jar from the Flix source file src\main\channels.flix and executes the target file :

> build -verbose run
Initialize Flix project directory "target\channels"
Copy 1 Flix source file to directory "target\channels\src\"
Compile 1 Flix source file into directory "target\channels\build\"
Create archive file "target\channels\channels.jar"
Extract Flix runtime from archive file "%FLIX_HOME%\flix.jar"
Add Flix runtime to archive file "target\channels\channels.jar"
Execute Flix program "target\channels\channels.jar"
29
39
[...]
72
35
done

Command build -verbose test generates the target file target\channels\channels.jar from the Flix sources files src\main\channels.flix and src\test\channelsTest.flix and runs the test suite :

> build -verbose test
Copy 1 Flix source file to directory "target\channels\src\"
Copy 1 Flix test source file to directory "target\channels\test\"
Compile 1 Flix source file and 1 Flix test source file
Create archive file "target\channels\channels.jar"
Extract Flix runtime from archive file "C:\opt\flix-0.43.0\flix.jar"
Add Flix runtime to archive file "target\channels\channels.jar"
Execute tests for Flix program "target\channels\channels.jar"
Running 1 tests...

   PASS  test01 384,1us

Passed: 1, Failed: 0. Skipped: 0. Elapsed: 6,9ms.

🔎 Command build.sh is the counterpart of the above batch file build.bat for Unix-like environments; for instance :  

> bash -c "./build.sh -verbose run"
Initialize directory "target/channels"
Copy Flix source files to directory "target/channels/src"
Copy Flix test source files to directory "target/channels/test"
Compile 1 Flix source files to directory "target/channels/build"
Create archive file "target/channels/channels.jar"
Extract class files from archive file "C:/opt/flix-0.43.0/flix.jar"
Update archive file "target/channels/channels.jar"
Execute the JAR file "target/channels/channels.jar"
20
...
7
done
 
> bash --version | findstr version
GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu)
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

datalog Example

Command make run (Makefile/Makefile.inc) generates the target file target\datalog\datalog.jar from the Flix source file src\main\datalog.flix and executes the target file :

> make run
[ -d "target/datalog" ] || "%GIT_HOME%/usr/bin/mkdir.exe" -p "target/datalog"
cd "target/datalog"; \
        [ -d "build" ] || "%JAVA_HOME%/bin/java.exe" -jar "%FLIX_HOME%/flix.jar" init && \
        "%GIT_HOME%/usr/bin/rm.exe" -f "src/Main.flix" && \
        "%GIT_HOME%/usr/bin/cp.exe" -r "F:/lutz-examples/datalog/src/main/." src && \
        "%JAVA_HOME%/bin/java.exe" -jar "%FLIX_HOME%/flix.jar" build && \
        "%JAVA_HOME%/bin/java.exe" -jar "%FLIX_HOME%/flix.jar" build-jar
"%JAVA_HOME%/bin/java.exe"  -jar "target/datalog/datalog.jar"
Children of Zeus = Apollo :: Ares :: Nil
Ancestors of Apollo = Kronos :: Leto :: Rhea :: Zeus :: Nil
0

effects Example

Command gradle -q run (build.gradle/ common.gradle) generates the target file target\effects\effects.jar from the Flix source file src\main\effects.flix and executes the target file :

> gradle -q run
2

1
2
3
4

2
4
6
8
0

interop Example

Command build run generates the target file target\interop\interop.jar from the Flix source file src\main\interop.flix and executes the target file :

> build run
false
0

mutualrecursion Example

Command make -s run (Makefile/Makefile.inc) generates the target file target\mutualrecursion\mutualrecursion.jar from the Flix source file src\main\mutualrecursion.flix and executes the target file :

> make -s run
true
0

🔎 Command build features option -java to compile/run the Java version of this code example.

> build -java -verbose run
Compile Java source files to directory "target\classes"
Execute Java program "MutualRecursion"
true

random Example

Command gradle -q run (build.gradle/common.gradle) generates the target file target\random\random.jar from the Flix source file src\main\random.flix and executes the target file :

> gradle -q run
109
169
[...]
192
124
0

tailcalls Example

Command build run generates the target file target\tailcalls\tailcalls.jar from the Flix source file src\main\tailcalls.flix and executes the target file :

> build run
312512500
0

The same example written in Java (src\main\tailcalls.java) fails with a runtime error :

> build -java run
Exception in thread "main" java.lang.StackOverflowError
        at Tailcalls.tailsum(tailcalls.java:12)
        at Tailcalls.tailsum(tailcalls.java:12)
        [...]
        at Tailcalls.tailsum(tailcalls.java:12)
        at Tailcalls.tailsum(tailcalls.java:12)
Error: Failed to execute Java program "Tailcalls"

Footnotes

[1] Updated source files

Here are our modifications of the original Flix code examples from Lutz Hühnken's article "Flix for Java programmers" (June 2022) :
Source file(s) Source code change
*.flix Old syntax: & Impure, new syntax: \ IO.
channels.flix
random.flix
Changed return type Unit & Impure to Unit \ NonDet for functions doSomething and main (see definition of Random.nextInt64).

mics/May 2024