laziness by need · 2020. 9. 4. · laziness by need stephen chang northeastern university...
TRANSCRIPT
![Page 1: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/1.jpg)
Laziness By Need
Stephen ChangNortheastern University
3/19/2013
ESOP 2013, Rome, Italy
![Page 2: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/2.jpg)
Laziness is great.
“pragmatically important because it enables the producer-consumer programming style”
[HM76]
“the most powerful tool for modularization … the key to successful programming”
[Hughes90]
... Valid?
![Page 3: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/3.jpg)
Or is it?
“in a lazy language, it’s much more difficult to predict the order of evaluation”
[PJ11]
“lazy programs can exhibit astonishing poor space behavior”
[HHPJW07]
“monumentally difficult to reason about time”
[Harper11]
![Page 4: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/4.jpg)
I want the good without the bad.
![Page 5: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/5.jpg)
Solution: strict + lazy(when needed)
via static analysis
![Page 6: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/6.jpg)
“languages should support both strict and lazy”
[PJ2011]
“The question is:What’s the default?
How easy is it to get the other?How do you mix them together?”
Combining lazy and strict has been done?
![Page 7: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/7.jpg)
Previous Approaches
• Lenient evaluation: Id, pH [Nikhil91, NAH+95]
• Eager Haskell [Maessen02]
• Optimistic Evaluation [EPJ03]
• Strictness analysis [Mycroft1981, BHA86, CPJ85]
• Cheap Eagerness [Faxen00]
All Adds strictness to lazy languages.
![Page 8: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/8.jpg)
How do real-world lazy programmers add
strictness?
![Page 9: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/9.jpg)
seq
![Page 10: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/10.jpg)
What about adding laziness to strict languages?
“most thunks are unnecessary”[EPJ03]
“both before and after optimization, most thunks are evaluated”
[Faxen00]
“most Id90 programs require neither functional nor conditionalnon-strictness”
[SG95]
“in our corpus of R programs … the average evaluation rate of promises is
90%”[MHOV12]
![Page 11: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/11.jpg)
strictlanguages
lazylanguages
lazy+
strictnessanalysis
lazy+
optimisticevaluation
strict+
lazinessby need
more laziness (placements not exact)
lenientevaluation
![Page 12: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/12.jpg)
Strict languages already have laziness
![Page 13: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/13.jpg)
So what’s the problem?
• Lazy data structures are not enough.
• Lazy annotations are hard to get right.
• Laziness is a global property!
![Page 14: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/14.jpg)
Same FringeTwo binary trees have the same fringe if they have exactly the same leaves, reading from left to right.
samefringe tree1 tree2 =
(flatten tree1) == (flatten tree2)
1
2
5,000,001...
0
1
5,000,000...
![Page 15: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/15.jpg)
Same Fringe
flat (Leaf x) acc = x::acc
flat (Node t1 t2) acc = flat t1 (flat t2 acc)
flatten t = flat t []
A (Tree X) is either a:- Leaf X- Node (Tree X) (Tree X)
![Page 16: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/16.jpg)
Same Fringe (eager)
1
2
5,000,001...
0
1
5,000,000...
let tree1 = let tree2 =
samefringe tree1 tree2 => false
0m13.363s
![Page 17: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/17.jpg)
Same Fringe (with streams)
A (Stream X) is either a:- Nil- Lcons X $(Stream X)
![Page 18: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/18.jpg)
Same Fringe (with streams)
flatten t = flat t Nil
flat (Leaf x) acc = Lcons x $acc
flat (Node t1 t2) acc = flat t1 (flat t2 acc)
![Page 19: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/19.jpg)
Same Fringe (with streams)
streameq $Nil $Nil = true
streameq $(Lcons x1 xs1) $(Lcons x2 xs2)=
x1==x2 && streameq xs1 xs2
streameq _ _ = false
![Page 20: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/20.jpg)
Same Fringe (with streams)
samefringe tree1 tree2 => false
0m17.277s
samefringe tree1 tree2 =
streameq $(flatten tree1) $(flatten tree2)
(with lazy trees)0m36.905s
![Page 21: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/21.jpg)
Same Fringe (naïvely lazy)
flatten t = flat t Nil
flat (Leaf x) acc = Lcons x $acc
flat (Node t1 t2) acc = flat t1 (flat t2 acc)
![Page 22: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/22.jpg)
Same Fringe (properly lazy)
flatten t = flat t Nil
flat (Leaf x) acc = Lcons x $acc
flat (Node t1 t2) acc = flat t1 $(flat t2 acc)
![Page 23: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/23.jpg)
Same Fringe (properly lazy)
samefringe tree1 tree2 => false
0m0.002s
![Page 24: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/24.jpg)
Takeaway
• Using lazy data structures is not enough.
• Additional annotations are needed but can be tricky.
• If only there was a tool that could help with the process . . .
![Page 25: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/25.jpg)
lcons x y
≡
cons x $y
30s 5s
![Page 26: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/26.jpg)
Same Fringe (naïvely lazy)
flatten t = flat t Nil
flat (Leaf x) acc = Lcons x $acc
flat (Node t1 t2) acc = flat t1 (flat t2 acc)
![Page 27: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/27.jpg)
control flow analysis
+
laziness flow analysis
![Page 28: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/28.jpg)
control flow analysis
+
laziness flow analysis
![Page 29: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/29.jpg)
arguments that reach a lazy construct
arguments that reach a strict context
expressions to force
![Page 30: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/30.jpg)
Transformation
• Delay all
• Force all
![Page 31: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/31.jpg)
Abstract value
tracks flow of functions arguments.
![Page 32: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/32.jpg)
Read: Sets
if and only if constraints
approximate expression
Analysis specified with rules:
hold.
![Page 33: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/33.jpg)
![Page 34: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/34.jpg)
![Page 35: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/35.jpg)
![Page 36: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/36.jpg)
examples:
– arguments to primitives
– if test expression
– function position in an application
strict contextscontexts where a thunk should not appear
![Page 37: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/37.jpg)
![Page 38: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/38.jpg)
We used our tool …
… and found some bugs.
![Page 39: Laziness By Need · 2020. 9. 4. · Laziness By Need Stephen Chang Northeastern University 3/19/2013 ESOP 2013, Rome, Italy](https://reader036.vdocuments.net/reader036/viewer/2022071113/5fea0875c9ca744793145b5d/html5/thumbnails/39.jpg)
Conclusions
• Get the benefits of laziness by starting strict and adding laziness by need.
• A flow-analysis-based tool can help in adding laziness to strict programs.
Thanks.