- Author and Context: Robert Schenck wrote this on June 17, 2024. He's a PhD student at DIKU and has worked on Futhark. AUTOMAP isn't in Futhark's
master
branch yet but a paper is in review and he'll give a talk at ARRAY24 on June 25th. He's looking for postdoc positions in type systems/functional programming/compilers. - Motivation for AUTOMAP: To make mathematical code involving higher-dimensional arguments look closer to handwritten code by avoiding clutter from "administrative" maps. For example, to be able to write
[1,2,3] + [4,5,6]
instead ofmap2 (+) [1,2,3] [4,5,6]
. Examples of AUTOMAP:
- Subtracting matrices:
map2 (map2 (-)) [[1,2,3], [4,5,6], [7,8,9]] [[9,8,7], [6,5,4], [3,2,1]]
ormap (map (-)) [[1,2,3], [4,5,6], [7,8,9]] [[9,8,7], [6,5,4], [3,2,1]]
. - Adding a matrix and a vector:
map (map (+)) [[1,2,3], [4,5,6], [7,8,9]] (rep [10,20,30])
. - Applying a function to arguments of different ranks:
map ((map f xss) (rep xss)) xss
for a functionf : []int -> [][]int -> int -> int
andxss : [][]int
.
- Subtracting matrices:
Challenges of Rank Polymorphism in Futhark:
- Usually done dynamically in languages like NumPy and APL, but harder in statically-typed languages like Futhark as it needs to be decided during type checking without runtime information.
- Futhark supports parametric polymorphism which complicates reasoning about how to insert
map
s andrep
s. - There are often many ways to insert
map
s andrep
s to make an application rank-correct, leading to ambiguity.
Strategy for Handling Ambiguity:
- Rule 1: An application can be
map
ped orrep
ped but never both. - Rule 2: Minimize the number of inserted
map
s andrep
s. - When there are multiple minimal elaborations, the compiler signals an error and the programmer can choose or insert
map
s orrep
s manually.
- Rule 1: An application can be
- Finding Minimal Rank-Correct Elaborations: Traverse the program, generate constraints for each application to obtain a rank-correct program. For example, for
f x
, generate constraints like|a| = |c|
(relaxed to rank equality), and handle rank differences withR
andM
variables. Accumulate these constraints during type checking and solve them to find the minimal assignment of rank variables. - Solving the Constraints: Linearize the constraints using binary variables to enforce Rule 1. Transform the problem into an integer linear program (ILP) and solve it using an existing solver or an ILP algorithm.
- How AUTOMAP Works: For each top-level definition in a program: generate rank equality and Rule 1 constraints, transform into an ILP and solve. The solution tells the compiler how to insert the minimal number of
map
s andrep
s. Type check the transformed program and continue compilation.
Footnotes:
- This is about ad-hoc polymorphism and adding a definition for vector addition.
- Typed Remora demonstrates shape polymorphism at the type-level.
- Delaying
map
andrep
decisions until runtime has multiple drawbacks in a statically-typed language.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。