W is a statically-typed functional programming language with a syntax inspired by Wolfram Language that transpiles to Rust. The core principles are:
- Functional Paradigm: Every computation is an expression
- Strong Static Typing: All types determined at compile-time
- Type Inference: Reduce verbosity while maintaining type safety
- Transpiles to Rust: Leverages Rust's performance, safety, and ecosystem
- Rust-Level Performance: Generated Rust code compiles to efficient native binaries
The language uses a function-call-based syntax where every operation is a function call:
Print["Hello, World!"]
Add[1, 2, 3] # Returns 6
Subtract[10, 5] # Returns 5
Multiply[2, 3, 4] # Returns 24
Divide[10, 2] # Returns 5
Power[2, 3] # Returns 8
(* Or use infix operators *)
1 + 2 + 3 # Returns 6
x * x # Squaring
(* Without type annotations *)
f[x, y] := Power[x, y]
(* With type annotations *)
Square[x: Int32] := x * x
(* Using the function *)
Print[Square[5]] # Outputs: 25
(* Multiple parameters with types *)
Add[x: Int32, y: Int32] := x + y
(* Cond expression - similar to LISP's cond *)
Cond[
[condition1, statements1],
[condition2, statements2],
[default_statements]
]
(* Lists - transpiles to Vec<T> in Rust *)
[1, 2, 3] # List of integers
List[1, 2, 3] # Equivalent
ProcessList[items: List[Int32]] := items
(* Arrays - fixed size *)
Array[Int32, 5] # Fixed-size array of 5 Int32s
(* Slices - borrowed views *)
Slice[Int32] # Slice of Int32s
(* HashSets - unique elements *)
UniqueItems[items: HashSet[String]] := items
(* Maps *)
Map[String, Int32] # HashMap in Rust
BTreeMap[String, Int32] # Ordered map
BTreeSet[Int32] # Ordered set
(* Tuples - heterogeneous, fixed-size composite types *)
(1, "hello") # Two-element tuple
(42, "answer", true) # Three-element tuple with different types
() # Empty tuple (unit type)
(42,) # Single-element tuple (note trailing comma)
(* Nested tuples *)
((1, 2), (3, 4)) # Tuple of tuples
(* Explicit constructor syntax *)
Tuple[10, "test"] # Alternative syntax
(* In function signatures *)
MakePair[x: Int32, y: String] := (x, y)
GetFirst[pair: Tuple[Int32, String]] := pair
(* Option types - for nullable values *)
Some[42] # Some value
Some["Hello, World!"]
Some[Some[100]] # Nested options
None # Empty option
(* Result types - for error handling *)
Ok[value] # Success case
Err[error] # Error case
(* Match expression - destructure and match values *)
Match[value,
[pattern1, result1],
[pattern2, result2],
[pattern3, result3]
]
(* Wildcard pattern - matches anything *)
Match[x, [_, "default"]]
(* Literal patterns *)
Match[5,
[1, "one"],
[2, "two"],
[_, "other"]
]
(* Variable binding *)
Match[100, [x, x]] # Binds value to x
(* Option patterns *)
Match[Some[42],
[Some[x], x],
[None, 0]
]
(* Tuple patterns *)
Match[(1, 2),
[(x, y), x] # Destructure tuple
]
(* Nested patterns *)
Match[Some[(42, "answer")],
[Some[(num, str)], num],
[None, 0]
]
(* Lambda/Anonymous function syntax *)
Function[{x}, x * 2] # Single parameter
Function[{x, y}, x + y] # Multiple parameters
Function[{x: Int32}, x * x] # With type annotation
(* Map - transform each element *)
Map[Function[{x}, x * 2], [1, 2, 3]]
(* Result: [2, 4, 6] *)
(* Filter - select elements *)
Filter[Function[{x}, x > 5], [1, 10, 3, 8]]
(* Result: [10, 8] *)
(* Fold - reduce to single value *)
Fold[Function[{acc, x}, acc + x], 0, [1, 2, 3, 4, 5]]
(* Result: 15 *)
(* Nested operations *)
Map[
Function[{x}, x * 2],
Filter[Function[{x}, x > 2], [1, 2, 3, 4, 5]]
]
(* Result: [6, 8, 10] *)
W supports a comprehensive type system that maps directly to Rust types:
Int8,Int16,Int32,Int64,Int128Int(platform-dependent, equivalent to Rust'sisize)
UInt8,UInt16,UInt32,UInt64,UInt128UInt(platform-dependent, equivalent to Rust'susize)
Float32(f32 in Rust)Float64(f64 in Rust)
BoolCharString
Tuple[T1, T2, ...]- Heterogeneous, fixed-size tuple ((T1, T2, ...) in Rust)- Can contain different types
- Supports nesting:
Tuple[Int32, Tuple[String, Bool]] - Empty tuple
()represents the unit type
List[T]- Dynamic array (Vec in Rust)Array[T, N]- Fixed-size array ([T; N] in Rust)Slice[T]- Borrowed view into a sequence (&[T] in Rust)Map[K, V]- Hash map (HashMap<K, V> in Rust)HashSet[T]- Set of unique values (HashSet in Rust)BTreeMap[K, V]- Ordered map (BTreeMap<K, V> in Rust)BTreeSet[T]- Ordered set (BTreeSet in Rust)
Option[T]- Optional values (Option in Rust)Some[value]- Present valueNone- Absent value
Result[T, E]- Result of operations that can fail (Result<T, E> in Rust)Ok[value]- Success caseErr[error]- Error case
Function[arg_types..., return_type]- Function signatures
- Compile-time type checking: All type errors caught during transpilation
- Zero runtime overhead: Direct mapping to Rust types with no abstraction penalty
- Idiomatic Rust generation: Produce clean, readable Rust code
- Leverage Rust ecosystem: Access to Rust's safety guarantees and performance
- Minimal runtime dependencies: Generated code relies only on Rust's standard library
- Parse: W source code is parsed into an Abstract Syntax Tree (AST)
- Type Check: Static type analysis ensures type safety
- Transpile: AST is transformed into equivalent Rust code
- Compile: Generated Rust code is compiled by
rustcinto a native binary
This is an experimental transpiler written in Rust, exploring functional language design and Rust code generation. The project demonstrates how a high-level functional syntax can compile down to efficient, safe Rust code.