Flix Programming Language Documentation
repository·master·Indexed 25 days ago
https://github.com/flix/flixFlix is a statically typed, multi-paradigm programming language combining functional, imperative, and logic programming features. This documentation covers language syntax, naming conventions, and standard library patterns, as well as technical details on bytecode generation, the checkcast invariant, and effect system suspension. It also provides guides for building the compiler using Mill or IntelliJ IDEA and setting up the official VSCode extension.
What's inside Flix
- Flix is a statically typed programming language that supports functional, imperative, and logic programming paradigms. For more detailed information, visit the official website at flix.dev.
Understand the checkcast Invariant in Flix bytecode
masterThe Flix compiler maintains a specific invariant during bytecode generation:
compileExpr(e)must leave a value on the JVM stack whose verifier type is compatible withBackendType.toBackendType(e.tpe).Because Flix compiles enums, tuples, records, and structs to JVM classes with erased field types (using
java.lang.Object),CHECKCASTinstructions are required to narrow the JVM verifier type fromObjectto the actual runtime type when extracting values from these structures.Build and run the Flix compiler with IntelliJ IDEA
masterYou can build and run the Flix compiler and standard library using IntelliJ IDEA.
Project Setup
- Open IDEA and select "Get from VCS".
- Navigate to
Settings | Build, Execution, Deployment | Build Toolsand configure the following:- Set "Build and run using:" to
IntelliJ IDEA. - Set "Run tests using:" to
IntelliJ IDEA.
- Set "Build and run using:" to
Running the REPL
To start the Flix REPL:
- Navigate to
main/src/ca/uwaterloo/flix/Main.scala. - Right-click on
def mainand selectRun 'Main'.
Compiling and running a Flix file
To compile and run a specific Flix file containing a
mainfunction:- Right-click on the
def maininMain.scala. - Select "Modify Run Configuration".
- Enter the target file name in the "Program arguments" field.
1. Open IDEA and choose "Get from VCS" 2. In "Settings | Build, Execution, Deployment | Build Tools": 1. Set "Build and run using:" to "IntelliJ IDEA" 2. Set "Run tests using:" to "IntelliJ IDEA"Manage NewObject casts in Let bindings
masterWhile most
Letbindings do not perform casting,NewObjectexpressions (anonymous Java class construction) are an exception.Because the JVM verifier may fail to resolve the hierarchy of dynamically generated anonymous classes (e.g.,
Anon$42), it may fall back toObject. To ensure the verifier recognizes the correct superclass, theLetbinding for aNewObjectmust include a cast to the declared superclass type.Flix Naming Conventions
masterFollow these naming conventions for Flix code:
- Variables: Use short, typical one-letter names (e.g.,
oforOption,lforList). - Type Variables: Use
a,b,c. - Effect Variables: Use
ef,ef1,ef2, etc. - Trait Instances: Declare trait instances immediately below the type declaration, ordered as:
Eq,Order,ToString.
- Variables: Use short, typical one-letter names (e.g.,
Understand effect system suspension and frame management
masterWhen using algebraic effects, Flix generates an
applyFramemethod to manage function state during suspension and resumption.Suspension Process
- Save (
setPc): The current state is saved by cloning the closure, setting thepc(program counter) to the next resume address, and copying locals to erased fields (e.g.,frame.l0 = slot4). Since these fields areObject, no casting is needed during saving. - Return: The frame is attached to a
Suspensionand returned.
Resumption Process
- Restore: The handler calls
frame.applyFrame(value). The method usesloadFromFieldto load locals from the erased fields and casts them back to their real types. - Narrowing (
narrowLocals): At eachpcPointLabel(resume site), the JVM verifier might have merged types, broadening them toObject. ThenarrowLocalsoperation immediately re-casts non-primitive locals to their declared types to ensure type safety in the resumed code.
- Save (
Access Flix Online Resources
masterUse the following online tools for development and learning:
- Online Playground: https://play.flix.dev/ - Test code snippets in the browser.
- Online Book: https://doc.flix.dev/ - Comprehensive language documentation.
- API Documentation: https://api.flix.dev/ - Detailed reference for the standard library.
Language Syntax and Naming Conventions
masterFlix follows specific naming conventions for types and variables:
- Type variables: lowercase (e.g.,
a,b) - Types: Uppercase (e.g.,
List[a],Int32) - Local variables and functions: lowercase
- Enum constructors: Uppercase
Other syntax features include:
- String interpolation: Uses the
ToStringtrait, e.g.,"Hello ${name}". - Pipeline operator: Uses
|>for chaining operations. - Infix application: Use backticks for infix function applications.
- Program holes: Use
???or?nameto denote incomplete code. - Set and Map literals: Use
Set#{1, 2, 3}andMap#{1 => 2, 3 => 4}.
- Type variables: lowercase (e.g.,
Configure ScalaDoc formatting in IntelliJ
masterTo ensure correct ScalaDoc formatting, adjust your IntelliJ IDEA settings: Go to
Settings > Editor > Code Style > Scala > ScalaDocand enable the optionadd additional space for leading asterisk.Scala Naming Conventions
masterFollow these naming conventions for Scala code:
- Common Methods: Use patterns like
visitExp,visitExps,visitPat, etc. - Variables:
- Abbreviate long variable names (e.g.,
eff,tparam). - Name expressions sequentially (e.g.,
exp1,exp2,exp3) rather than using names likebeginExpwhich become outdated.
- Abbreviate long variable names (e.g.,
- Constructors: Generally do not abbreviate constructor names (e.g.,
Effect), with specific exceptions likeTypeParam,Sig, andDef.
- Common Methods: Use patterns like
Test the Flix VSCode extension
masterTo test the VSCode integration, you must first configure the path to your VSCode project and build the necessary JAR.
- Create a
.envfile in the project root with the following content:VSCODE_PATH=/path/to/vscode/project - Run the Mill command to build and copy the JAR:
./mill flix.vscode - Open the directory in VSCode using
File -> Open Folder ...to begin testing.
VSCODE_PATH=/path/to/vscode/project ./mill flix.vscode- Create a
Perform property-based testing with QuickCheck frameworks
masterFlix supports QuickCheck frameworks for property-based testing. Instead of defining individual test cases, you define properties that your code must satisfy. The framework then:
- Generates a large number of random inputs to test these properties.
- Explores edge cases to find potential bugs.
- Automatically simplifies failing inputs to find the simplest case that triggers the failure, aiding in efficient debugging.