Rank Polymorphism Viewed as a Constraint Problem
Justin Slepak Panagiotis Manolios Olin Shivers
jrslepak@ccs.neu.edu pete@ccs.neu.edu shivers@ccs.neu.edu
Northeastern University
1
Rank Polymorphism Viewed as a Constraint Problem Justin Slepak - - PowerPoint PPT Presentation
Rank Polymorphism Viewed as a Constraint Problem Justin Slepak Panagiotis Manolios Olin Shivers jrslepak@ccs.neu.edu pete@ccs.neu.edu shivers@ccs.neu.edu Northeastern University 1 100 175 0.6 ( [(lo 0) (hi 0) ( 0)] (+ (* hi )
Justin Slepak Panagiotis Manolios Olin Shivers
jrslepak@ccs.neu.edu pete@ccs.neu.edu shivers@ccs.neu.edu
Northeastern University
1
100 175 0.6 (λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α))))
2
100 175 0.6 ((λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α)))) 100 175 0.6)
3
(λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α))))
4
((λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α)))) rgb1 ; 3 channels rgb2 ; 3 channels 0.6) ; scalar
5
Credit: Wikimedia user Sadalsuud
(λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α))))
6
Credit: Wikimedia user Sadalsuud
((λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α)))) sky ; row × col × chan labels ; row × col × chan img-mask) ; row × col × chan
7
(λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α))))
8
((λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α)))) film ; time × row × col × chan audience ; time × row × col × chan vid-mask) ; time × row × col × chan
9
Credit: Wikimedia user Thetawave
(λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α))))
1
Credit: Wikimedia user Thetawave
((λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α)))) scene1 ; time × row × col × chan scene2 ; time × row × col × chan [0.0 ... 1.0]) ; time
11
(λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α))))
12
(λ [(lo 0) (hi 0) (α 0)] (+ (* hi α) (* lo (- 1 α)))) Polymorphic in dimensionality
13
14
1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 0 1 1 1
3,7
15
1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 0 1 1 1
3,7
0 1 2 3 4 5 6 7
2,2,2
16
1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 0 1 1 1
3,7
0 1 2 3 4 5 6 7
2,2,2
1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 0 1 1 1
3,7
0 1 2 3 4 5 6 7
2,2,2
18
1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 0 1 1 1
3,7
0 1 2 3 4 5 6 7
2,2,2
shape: size in each dimension
19
1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 0 1 1 1
3,7
2 0 1 2 3 4 5 6 7
2,2,2
3
atoms: non-aggregate elements shape: size in each dimension rank: length of shape i.e., number of dimensions
2
dot 1 4 2 3
2,2
3 5
2
21
dot 1 4 2 3
2,2
3 5
2
cell: basic unit function operates on
22
dot 1 4 2 3
2,2
3 5
2
cell: basic unit function operates on
23
dot 1 4 2 3
2,2
3 5
2
cell: basic unit function operates on frame: structure around cells
24
dot 1 4 2 3
2,2
3 5
2
cell: basic unit function operates on frame: structure around cells function applied to each cell
25
dot 1 4 2 3
2,2
3 5
2
cell: basic unit function operates on frame: structure around cells function applied to each cell results reassembled in frame
26
Rank n array can split n+1 ways
27
Rank n array can split n+1 ways 0 1 2 3 1 2 3 4 2 3 4 5
3,4
28
Rank n array can split n+1 ways 0 1 2 3 1 2 3 4 2 3 4 5
3,4
0 1 2 3 1 2 3 4 2 3 4 5
3,4
29
Rank n array can split n+1 ways 0 1 2 3 1 2 3 4 2 3 4 5
3,4
0 1 2 3 1 2 3 4 2 3 4 5
3,4
0 1 2 3 1 2 3 4 2 3 4 5
3,4
3
dot 1 4 2 3
2,2
3 5
2
31
dot 1 4 2 3
2,2
3 5
2
+ 1 4 2 3
2,2
3 5
2
32
dot 1 4 2 3
2,2
3 5
2
+ 1 4 2 3
2,2
3 5
2
poly 1 4 2 3
2,2
3 5
2
33
dot 1 4 2 3
2,2
3 5
2
+ 1 4 2 3
2,2
3 5
2
poly 1 4 2 3
2,2
3 5
2
Principal frame: maximum under prefix ordering
34
dot 1 4 2 3
2,2
3 5
2
= 23 21
2
+ 1 4 2 3
2,2
3 5
2
= 4 7 7 8
2,2
poly 1 4 2 3
2,2
3 5
2
= 13 17
2
Principal frame: maximum under prefix ordering
35
Trying different shapes
36
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5
2
37
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5
2
+ 1 4 2 3 7 8
3,2
3 5
2
38
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5
2
+ 1 4 2 3 7 8
3,2
3 5
2
poly 1 4 2 3 7 8
3,2
3 5
2
39
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5
2
= 23 21 61
3
+ 1 4 2 3 7 8
3,2
3 5
2
poly 1 4 2 3 7 8
3,2
3 5
2
4
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5
2
= 23 21 61
3
+ 1 4 2 3 7 8
3,2
3 5
2
→ Shape error: no principal frame poly 1 4 2 3 7 8
3,2
3 5
2
41
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5
2
= 23 21 61
3
+ 1 4 2 3 7 8
3,2
3 5
2
→ Shape error: no principal frame poly 1 4 2 3 7 8
3,2
3 5
2
→ Shape error: no principal frame
42
Trying different shapes
43
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5 1
3
44
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5 1
3
+ 1 4 2 3 7 8
3,2
3 5 1
3
45
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5 1
3
+ 1 4 2 3 7 8
3,2
3 5 1
3
poly 1 4 2 3 7 8
3,2
3 5 1
3
46
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5 1
3
→ Domain error: dot requires equal length args + 1 4 2 3 7 8
3,2
3 5 1
3
poly 1 4 2 3 7 8
3,2
3 5 1
3
47
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5 1
3
→ Domain error: dot requires equal length args + 1 4 2 3 7 8
3,2
3 5 1
3
= 4 7 7 8 8 9
3,2
poly 1 4 2 3 7 8
3,2
3 5 1
3
48
Trying different shapes dot 1 4 2 3 7 8
3,2
3 5 1
3
→ Domain error: dot requires equal length args + 1 4 2 3 7 8
3,2
3 5 1
3
= 4 7 7 8 8 9
3,2
poly 1 4 2 3 7 8
3,2
3 5 1
3
→ 13 17 15
3
49
5
1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 0 1 1 1
3,7
51
[[1 0 0 1 1 0 1] [0 1 0 1 0 1 1] [0 0 1 0 1 1 1]]
52
[[1 0 0 1 1 0 1] [0 1 0 1 0 1 1] [0 0 1 0 1 1 1]] : (Arr (Shp 3 7) Int)
53
[[1 0 0 1 1 0 1] [0 1 0 1 0 1 1] [0 0 1 0 1 1 1]] : (Arr (Shp 3 7) Int) 0 1 2 3 4 5 6 7
2,2,2
54
[[1 0 0 1 1 0 1] [0 1 0 1 0 1 1] [0 0 1 0 1 1 1]] : (Arr (Shp 3 7) Int) [[[0 1] [2 3]] [[4 5] [6 7]]]
55
[[1 0 0 1 1 0 1] [0 1 0 1 0 1 1] [0 0 1 0 1 1 1]] : (Arr (Shp 3 7) Int) [[[0 1] [2 3]] [[4 5] [6 7]]] : (Arr (Shp 2 2 2) Int)
56
[[1 0 0 1 1 0 1] [0 1 0 1 0 1 1] [0 0 1 0 1 1 1]] : (Arr (Shp 3 7) Int) [[[0 1] [2 3]] [[4 5] [6 7]]] : (Arr (Shp 2 2 2) Int)
[[1 0 0 1 1 0 1] [0 1 0 1 0 1 1] [0 0 1 0 1 1 1]] : (Arr (Shp 3 7) Int) [[[0 1] [2 3]] [[4 5] [6 7]]] : (Arr (Shp 2 2 2) Int)
58
[[1 0 0 1 1 0 1] [0 1 0 1 0 1 1] [0 0 1 0 1 1 1]] : (Arr (Shp 3 7) Int) [[[0 1] [2 3]] [[4 5] [6 7]]] : (Arr (Shp 2 2 2) Int) 0 : (Arr (Shp) Int)
59
+ dot poly
6
+ : (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int)) dot poly
61
+ : (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int)) dot : (Π ((l Dim)) (-> ((Arr (Shp l) Int) (Arr (Shp l) Int)) (Arr (Shp) Int))) poly
62
+ : (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int)) dot : (Π ((l Dim)) (-> ((Arr (Shp l) Int) (Arr (Shp l) Int)) (Arr (Shp) Int))) poly : (Π ((l Dim)) (-> ((Arr (Shp l) Int) (Arr (Shp) Int)) (Arr (Shp) Int)))
63
+ : (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int)) dot : (Π ((l Dim)) (-> ((Arr (Shp l) Int) (Arr (Shp l) Int)) (Arr (Shp) Int))) poly : (Π ((l Dim)) (-> ((Arr (Shp l) Int) (Arr (Shp) Int)) (Arr (Shp) Int))) append
64
+ : (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int)) dot : (Π ((l Dim)) (-> ((Arr (Shp l) Int) (Arr (Shp l) Int)) (Arr (Shp) Int))) poly : (Π ((l Dim)) (-> ((Arr (Shp l) Int) (Arr (Shp) Int)) (Arr (Shp) Int))) append : (∀ ((T Atom)) (Π ((l1 Dim) (l2 Dim) (c Shp)) (-> ((Arr (++ (Shp l1) c) T) (Arr (++ (Shp l2) c) T)) (Arr (++ (Shp (+ l1 l2)) c) T))))
65
(+ [[1 4] [2 3] [7 8]] [3 5 1])
66
(+ [[1 4] [2 3] [7 8]] [3 5 1]) + : (Arr (Shp) (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int)))
67
(+ [[1 4] [2 3] [7 8]] [3 5 1]) + : (Arr (Shp) (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int))) [[1 4] [2 3] [7 8]] : (Arr (Shp 3 2) Int)
68
(+ [[1 4] [2 3] [7 8]] [3 5 1]) + : (Arr (Shp) (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int))) [[1 4] [2 3] [7 8]] : (Arr (Shp 3 2) Int) [3 5 1] : (Arr (Shp 3) Int)
69
(+ [[1 4] [2 3] [7 8]] [3 5 1]) + : (Arr (Shp) (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int))) [[1 4] [2 3] [7 8]] : (Arr (Shp 3 2) Int) [3 5 1] : (Arr (Shp 3) Int) (Shp) ⊑ (Shp 3) ⊑ (Shp 3 2)
7
(+ [[1 4] [2 3] [7 8]] [3 5 1]) + : (Arr (Shp) (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int))) [[1 4] [2 3] [7 8]] : (Arr (Shp 3 2) Int) [3 5 1] : (Arr (Shp 3) Int) (Shp) ⊑ (Shp 3) ⊑ (Shp 3 2) (Arr (Shp 3 2) Int)
71
(+ [[1 4] [2 3] [7 8]] [3 5 1]) [[+ +] [+ +] [+ +]] : (Arr (Shp 2 3) (-> ((Arr (Shp) Int) (Arr (Shp) Int)) (Arr (Shp) Int))) [[1 4] [2 3] [7 8]] : (Arr (Shp 3 2) Int) [[3 3] [5 5] [1 1]] : (Arr (Shp 3 2) Int) (Shp) ⊑ (Shp 3) ⊑ (Shp 3 2) (Arr (Shp 3 2) Int)
72
(+ [[1 4] [2 3] [7 8]] [3 5])
73
(+ [[1 4] [2 3] [7 8]] [3 5]) [[1 4] [2 3] [7 8]] : (Arr (Shp 3 2) Int) [3 5] : (Arr (Shp 2) Int)
74
(+ [[1 4] [2 3] [7 8]] [3 5]) [[1 4] [2 3] [7 8]] : (Arr (Shp 3 2) Int) [3 5] : (Arr (Shp 2) Int) (Shp 2) ⊑ / (Shp 3 2)
75
(+ [[1 4] [2 3] [7 8]] [3 5]) [[1 4] [2 3] [7 8]] : (Arr (Shp 3 2) Int) [3 5] : (Arr (Shp 2) Int) (Shp 2) ⊑ / (Shp 3 2) (Shp 3 2) ⊑ / (Shp 2)
76
(+ [[1 4] [2 3] [7 8]] [3 5]) [[1 4] [2 3] [7 8]] : (Arr (Shp 3 2) Int) [3 5] : (Arr (Shp 2) Int) (Shp 2) ⊑ / (Shp 3 2) (Shp 3 2) ⊑ / (Shp 2)
77
78
79
8
81
82
associativity identity
83
associativity identity nothing equal unless required
84
associativity identity nothing equal unless required
85
86
87
88
Redundant but will be useful later
89
Validate index arguments supplied by programmer
9
Validate index arguments supplied by programmer Cell types?
91
Validate index arguments supplied by programmer Cell types?
92
Validate index arguments supplied by programmer Cell types? Principal frame?
93
Validate index arguments supplied by programmer Cell types? Principal frame?
94
95
96
97
98
99
Write in terms of
1
Write in terms of Decidability issues
11
12
Canonicalize symbolic shapes
13
Canonicalize symbolic shapes
14
Canonicalize symbolic shapes
15
Canonicalize symbolic shapes
16
In universal fragment, easy equality/prefix/suffix — including witnesses
17
In universal fragment, easy equality/prefix/suffix — including witnesses
18
In universal fragment, easy equality/prefix/suffix — including witnesses
19
In universal fragment, easy equality/prefix/suffix — including witnesses Fold argument frame shapes (suffix witnesses) together
11
In universal fragment, easy equality/prefix/suffix — including witnesses Fold argument frame shapes (suffix witnesses) together
111
In universal fragment, easy equality/prefix/suffix — including witnesses Fold argument frame shapes (suffix witnesses) together
112
In universal fragment, easy equality/prefix/suffix — including witnesses Fold argument frame shapes (suffix witnesses) together Type checking works entirely within universal fragment
113
Identify index arguments elided by programmer
114
Identify index arguments elided by programmer Function type (Pi (e ...) (-> ((Arr ι σ) ...) τ))
115
Identify index arguments elided by programmer Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
116
Identify index arguments elided by programmer Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
117
Identify index arguments elided by programmer Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
118
Identify index arguments elided by programmer Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
119
Identify index arguments elided by programmer Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
12
Identify index arguments elided by programmer Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
121
Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
122
Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
123
Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
124
Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ...
125
Function type Argument types (Pi (e ...) (-> ((Arr ι σ) ...) τ)) (Arr κ σ) ... Pretend are additional generators
126
Why can we treat like additional generators?
127
Why can we treat like additional generators? Algorithm for existential fragment (Makanin, 1977)
128
Why can we treat like additional generators? Algorithm for existential fragment (Makanin, 1977) "How might subsequences align with each other?"
129
Why can we treat like additional generators? Algorithm for existential fragment (Makanin, 1977) "How might subsequences align with each other?" Equations about smaller sequences
13
Why can we treat like additional generators? Algorithm for existential fragment (Makanin, 1977) "How might subsequences align with each other?" Equations about smaller sequences No boundaries inside a generator
131
Why can we treat like additional generators? Algorithm for existential fragment (Makanin, 1977) "How might subsequences align with each other?" Equations about smaller sequences No boundaries inside a generator ... or a universal variable
132
Why can we treat like additional generators? Algorithm for existential fragment (Makanin, 1977) "How might subsequences align with each other?" Equations about smaller sequences No boundaries inside a generator ... or a universal variable Caveat: only works in conjunctive fragment
133
134
(append [[1 2] [3 4]] [[5 6] [7 8]])
135
(append [[1 2] [3 4]] [[5 6] [7 8]])
(Pi ((l1 Dim) (l2 Dim) (c Shape)) (Arr (Shp) (-> ((Arr (++ (Shp l1) c) Int) (Arr (++ (Shp l2) c) Int)) (Arr (++ (Shp (+ l1 l2)) c) Int))))
136
(append [[1 2] [3 4]] [[5 6] [7 8]])
(Pi ((l1 Dim) (l2 Dim) (c Shape)) (Arr (Shp) (-> ((Arr (++ (Shp l1) c) Int) (Arr (++ (Shp l2) c) Int)) (Arr (++ (Shp (+ l1 l2)) c) Int))))
c = (Shp 2) c = (Shp)
137
(append [[1 2] [3 4]] [[5 6] [7 8]])
(Pi ((l1 Dim) (l2 Dim) (c Shape)) (Arr (Shp) (-> ((Arr (++ (Shp l1) c) Int) (Arr (++ (Shp l2) c) Int)) (Arr (++ (Shp (+ l1 l2)) c) Int))))
c = (Shp 2) Major axis append c = (Shp)
138
(append [[1 2] [3 4]] [[5 6] [7 8]])
(Pi ((l1 Dim) (l2 Dim) (c Shape)) (Arr (Shp) (-> ((Arr (++ (Shp l1) c) Int) (Arr (++ (Shp l2) c) Int)) (Arr (++ (Shp (+ l1 l2)) c) Int))))
c = (Shp 2) Major axis append c = (Shp) Lifted row-wise append
139
(append [[1 2] [3 4]] [[5 6] [7 8]])
(Pi ((l1 Dim) (l2 Dim) (c Shape)) (Arr (Shp) (-> ((Arr (++ (Shp l1) c) Int) (Arr (++ (Shp l2) c) Int)) (Arr (++ (Shp (+ l1 l2)) c) Int))))
c = (Shp 2) Major axis append c = (Shp) Lifted row-wise append Established convention: Operate along major axis
14
(append [[1 2] [3 4]] [[5 6] [7 8]])
(Pi ((l1 Dim) (l2 Dim) (c Shape)) (Arr (Shp) (-> ((Arr (++ (Shp l1) c) Int) (Arr (++ (Shp l2) c) Int)) (Arr (++ (Shp (+ l1 l2)) c) Int))))
c = (Shp 2) Major axis append c = (Shp) Lifted row-wise append Established convention: Operate along major axis Require scalar frame shape
141
142
143
Generating
144
Generating Solving
145
Solving Largely done (not by me — Makanin, 1977; Karhumäki, et al, 2000) ILP modulo theories (Manolios & Papavasileiou, 2013)
146
Generating In progress
147
Justin Slepak Panagiotis Manolios Olin Shivers
jrslepak@ccs.neu.edu pete@ccs.neu.edu shivers@ccs.neu.edu
Northeastern University
148