Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Inlining and Specialisation
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= What is specialisation? = Specialisation is the process of removing typeclass dictionary arguments by creating a new type-specialised definition for an overloaded function. Once specialised, dictionary methods can be easily inlined which usually creates more efficient code. For example, if we define the overloaded function <code>foo</code> <pre class="haskell">foo :: Show a => a -> a -> Bool foo x y = show x == show y</pre> the following core definition will be produced: <pre class="haskell">foo = \ @a $dShow x y -> eqString (show $dShow x) (show $dShow y)</pre> There are now 4 parameters to <code>foo</code>, the first argument is a type (denoted by <code>@</code>), the second argument is the dictionary for <code>Show</code> (denoted by the <code>$d</code> prefix) and the last two are the arguments <code>x</code> and <code>y</code> of the original definition. The class constraint is translated into a dictionary. Each time a class method is used, we must dynamically lookup which definition to use in the supplied class dictionary. If we know which type <code>a</code> is instantiated with, we can specialise the definition of <code>foo</code> and produce much better code. <pre class="haskell">qux :: Bool -> Bool -> Bool qux = foo @Bool</pre> Using <code>foo</code> at a specific type produces a new definition <code>foo_$sfoo</code> which is defined as: <pre class="haskell">foo_$sfoo :: Bool -> Bool -> Bool foo_$sfoo = foo @Bool $fShowBool</pre> Further optimisations then inline <code>foo</code> and then the dictionary selector <code>show</code> which produces the following more direct program. <pre class="haskell">foo_$sfoo = \ x y -> case x of { False -> case y of { False -> foo4; True -> foo3 }; True -> case y of _ { False -> foo2; True -> foo1 } }</pre> == When does specialisation occur? == Specialisation occurs when an overloaded function is called at a specific type. The specialised definition is placed in the module where the call happens but also exported so that it can be reused if there is another upstream call-site where specialisation would take place. By default, functions are not specialised across modules. There are two ways to make functions specialise across modules: # Marking a function as <code>INLINABLE</code> or <code>INLINE</code>. # Using the flag <code>-fspecialise-aggressively</code> when compiling the client module. An unfolding must still be available to perform specialisation. Further to this, observe that for specialisation to occur across modules, the unfolding must be made available in interface files. Notice this subtle point, the <code>INLINABLE</code> pragma guarantees the precise conditions for a function to be specialised across modules.
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width