Homepage / Notes / Computer Science / Programming Languages / Niche Languages / Futhark
Futhark is a small programming language designed to be compiled to efficient parallel code. It is a statically typed, data-parallel, and purely functional array language in the ML family, and comes with a heavily optimising ahead-of-time compiler that presently generates either GPU code via CUDA and OpenCL, or multi-threaded CPU code. https://futhark-lang.org/
futhark
codefuthark c main.fut
./main
Run with arguments:
echo 9 | ./main
futhark
codefuthark opencl main.fut
futhark repl
https://futhark-lang.org/examples/values.html
def an_int : i32 = 9
def a_float : f64 = 9.0
def a_boolean = true
https://futhark-lang.org/examples/arrays.html
def arr = [1,2,3]
arr
[1i32, 2i32, 3i32]
Arrays of arrays have to be of the same size:
def marr = [[1,2,3], [4,5,6]]
marr
[[1i32, 2i32, 3i32], [4i32, 5i32, 6i32]]
This is illegal:
def marr = [[1,2,3], [4,5]]
Dimensions "3" and "2" do not match.
Arrays are indexed from zero:
arr[0]
1i32
Arrays can be sliced:
arr[1:]
[2i32, 3i32]
Strides are supported
arr[::-1]
[3i32, 2i32, 1i32]
iota 5
[0i64, 1i64, 2i64, 3i64, 4i64]
Three dots, not two
1...5
[1i32, 2i32, 3i32, 4i32, 5i32]
replicate 3 9
[9i32, 9i32, 9i32]
map (+1) [1, 2, 3]
[2i32, 3i32, 4i32]
reduce (+) 0 [1, 2, 3]
6i32
scan (+) 0 [1, 2, 3, 4]
[1i32, 3i32, 6i32, 10i32]
def a_tuple = (1, true)
a_tuple
(1i32, true)
a_tuple.0
1i32
def a_record = {foo = 1, bar = true}
a_record
{bar = true, foo = 1i32}
a_record.foo
1i32
https://futhark-lang.org/examples/functions.html
With type inference:
def plus1 x =
x + 1
With declared types:
def plus1 (x: i32) : i32 =
x + 1
def sum a = reduce (+) 0 a
sum [1, 2, 3]
6i32
def plus = (+)
2 `plus` 2
4i32
[1,2,3,4] |> scan (+) 0 |> reduce (+) 0
20i32
loop x = 1 for i < 5 do
x * (i + 1)
https://futhark-lang.org/examples/literate-basics.html
futhark literate {filename}
Convert a .fut
file to a .md
file. For specially formatted comments (directives), results are appended.
https://futhark-book.readthedocs.io/en/latest/