haskell.net the art of avoiding work oliver hunt & nigel perry university of canterbury, new...
TRANSCRIPT
![Page 1: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/1.jpg)
Haskell.NET
The Art of Avoiding Work
Oliver Hunt & Nigel PerryUniversity of Canterbury, New Zealand
![Page 2: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/2.jpg)
Summary
Introduction Background Prior Art Challenges & Solutions Results The Future Questions
![Page 3: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/3.jpg)
Introduction: What? Implementation of Haskell 98 on
Rotor/.NET Haskell is one of the worlds most popular
functional programming languages. How to compile Haskell for .NET is still a
research problem• Can it be done “reasonably”?• Would IL changes provide “worthwhile”
benefit:cost?
![Page 4: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/4.jpg)
Introduction: Haskell Well known Used in industry and academia Non-strict Glasgow Haskell Compiler (GHC)
Provides intermediate-level output to support new backends
Extends Haskell 98 – providing future avenues of research
![Page 5: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/5.jpg)
Introduction: Why?
Different language families are suited to different tasks This adds a non-strict functional language to
the languages available on .NET To test the extent to which .NET can run
many languages. Primarily used by object oriented imperative
languages.
![Page 6: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/6.jpg)
Prior Art: Bridges
The functional language runs natively on the real machine
A software bridge is provided to the VM Examples:
Lambada (Haskell for JVM) Hugs.Net (Haskell for .NET)
![Page 7: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/7.jpg)
Prior Art: New Languages Designed to work on the VMs
Reduced features Mixed compile/interpretive approaches More OO-like type systems
Examples: Pizza (JVM)
• Strict• Introduced parametric polymorphism to JVM
Mondrian (.NET)• OO-like type system• Used a mixed compiled/interpretive approach• Targeted at scripting applications
![Page 8: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/8.jpg)
Haskell 98 on .NET: Challenges
Non-strict evaluation Functions as values Partial evaluation/“currying” Type switches
![Page 9: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/9.jpg)
Challenge: Non-strict Evaluation
Mondrian: “External” non-strictness Client must know
• Manual evaluation Interpretive-like
JIT Objects: “Internal” non-strictness Non-strictness hidden from client
• Automatic evaluation Doesn’t support disjoint union types well
• Disjoint union types central to Haskell 98…
![Page 10: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/10.jpg)
Haskell.NET: Non-strict Evaluation
Use “Internal” non-strictness Best for interworking
Primitive types Follow JIT Objects Optimise to use single class, rather than
type/subtype combination Auto conversion to/from values & boxed values
Function types Use hidden nested subtype
![Page 11: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/11.jpg)
Non-strict Evaluation (cont) Disjoint union types
Type: abstract class Alternatives: sub-classes Discrimination:
• use tag fields Resolution:
• “asX” methods rather than casting Non-strictness
• Hidden sub-class• Internal: evaluation hidden by tag/asX
Issues: Casting only works for evaluated values Not totally transparent
• But disjoint unions not “standard” OO
![Page 12: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/12.jpg)
Non-strict types data List a = Cons a (List a) : Nil
![Page 13: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/13.jpg)
Challenge: Functions as values .NET provides delegates, which are “OO function
pointers” Unfortunately:
Relatively inefficient given the high usage in Haskell Difficult to combine with non-strictness
Replace using a parametric function type Provide conversions to/from delegates for inter-language
working Extends to support partial application Might extend to support higher-rank types (Glasgow extension)
![Page 14: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/14.jpg)
Challenge: Partial Evaluation
Calling a function omitting arguments, to create a new function, e.g. Inc x = x+ Calling (inc 3) returns a function that adds 3
to its argument We extend our previous function type
Instance fields used to store pre-supplied arguments
![Page 15: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/15.jpg)
Challenge: Type Switches
Very slow in .NET Must use a series of type checks/casts These checks take account of subtypes
Addressed by the addition of an explicit tag enumerand to each type. I.e: Effectively duplicate the hidden VM tag Do exact, as opposed to subtype, matching
![Page 16: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/16.jpg)
Status Compiler functional
But incomplete…• Large %age of Haskell 98 language• Smaller %age of Haskell 98 libraries• Largely engineering, not research, left
Performance? Primes Sieve (first 1000 primes)
• GHC Native: 0.9s• GHC .NET: 1.5s
![Page 17: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/17.jpg)
Future Work: Glasgow Haskell Higher Rank Types
Passing generic functions as values Partly supported now:
• Wrap inside interface
Higher Kind Types E.g. allow M<X> where both M & X are variable How to do reasonably efficiently in .NET?
• Fallback is reflection/runtime code generation…
Currently by-passed (e.g. hardwire Monad to IO)
![Page 18: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/18.jpg)
Future Work: IL Changes? Compared to “native” implementation:
More classes – adds VM load Some extra indirections – a runtime cost Virtual dispatch for tag checking Etc.
Investigate if IL changes would: Provide Haskell a good benefit:cost Benefit other languages
![Page 19: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/19.jpg)
Demo
It really does work…
![Page 20: Haskell.NET The Art of Avoiding Work Oliver Hunt & Nigel Perry University of Canterbury, New Zealand](https://reader036.vdocuments.net/reader036/viewer/2022082805/551475a7550346284e8b628e/html5/thumbnails/20.jpg)
Q & (maybe) A