https://wiki.haskell.org/api.php?action=feedcontributions&user=Beerdude26&feedformat=atomHaskellWiki - User contributions [en]2020-02-22T13:42:26ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Applications_and_libraries/Interfacing_other_languages&diff=60655Applications and libraries/Interfacing other languages2016-03-27T07:09:10Z<p>Beerdude26: Added inline-c</p>
<hr />
<div>{{unknown copyright}}<br />
{{LibrariesPage}}<br />
<br />
== Tools for interfacing with other languages ==<br />
<br />
Haskell builtin features for interfacing to other languages are formally defined in the<br />
[http://www.cse.unsw.edu.au/~chak/haskell/ffi/ Foreign Function Interface (FFI)]. You may learn it by example at the <br />
[http://haskell.org/haskellwiki/IO_inside#Interfacing_with_C.2FC.2B.2B_and_foreign_libraries_.28under_development.29 IO inside] guide.<br />
<br />
See also [[RPC | Web services and RPC]].<br />
<br />
The following tools and libraries either simplify interfacing to C or allow interfacing to other languages and environments (COM, JVM, Python, Tcl, Lua...).<br />
<br />
=== C ===<br />
<br />
;[http://hackage.haskell.org/package/inline-c inline-c]<br />
:inline-c uses quasiquotation to seamlessly call C libraries and embed high-performance inline C code in Haskell modules.<br />
<br />
;[http://www.cse.unsw.edu.au/~chak/haskell/c2hs/ C-&gt;Haskell]<br />
:A lightweight tool for implementing access to C libraries from Haskell.<br />
<br />
;[http://hackage.haskell.org/package/c2hsc/ c2hsc]<br />
:A tool to create Bindings-DSL based interface files for C libraries.<br />
<br />
;[[HSFFIG]]<br />
:Haskell FFI Binding Modules Generator (HSFFIG) is a tool that takes a C library include file (.h) and generates Haskell Foreign Functions Interface import declarations for items (functions, structures, etc.) the header defines.<br />
<br />
;[http://www.astercity.net/~khaliff/haskell/kdirect/index.html KDirect]<br />
:A tool to simplify the process of interfacing C libraries to Haskell. It is less powerful than HaskellDirect, but easier to use and more portable.<br />
<br />
;[http://hackage.haskell.org/cgi-bin/hackage-scripts/package/libffi libffi]<br />
:A binding to libffi, allowing C functions of types only known at runtime to be called from Haskell.<br />
<br />
=== Java ===<br />
<br />
See [http://www.haskell.org/pipermail/haskell-cafe/2013-May/108299.html the discussion on Haskell Cafe].<br />
<br />
=== Python ===<br />
<br />
;[https://hackage.haskell.org/package/cpython cpython]<br />
:Bindings that allow Haskell code to call CPython code. <br />
<br />
;[http://quux.org/devel/missingpy MissingPy]<br />
:MissingPy is really two libraries in one. At its lowest level, MissingPy is a library designed to make it easy to call into Python from Haskell. It provides full support for interpreting arbitrary Python code, interfacing with a good part of the Python/C API, and handling Python objects. It also provides tools for converting between Python objects and their Haskell equivalents. Memory management is handled for you, and Python exceptions get mapped to Haskell Dynamic exceptions. At a higher level, MissingPy contains Haskell interfaces to some Python modules.<br />
<br />
=== Others ===<br />
<br />
; [https://github.com/wavewave/fficxx fficxx]<br />
: A Haskell-C++ Foreign Function Interface Generator<br />
<br />
;[http://web.archive.org/web/20010306205042/http://www.numeric-quest.com/haskell/smartest.html Smarty]<br />
:An interface between Haskell and Squeak, a freely available Smalltalk language and environment.<br />
<br />
;[ftp://ftp.cs.york.ac.uk/pub/haskell/contrib/lp2fp.tar.gz]<br />
:lp2fp is a program-translator from Prolog to Haskell.<br />
<br />
;[http://haskell.org/haskellscript/ HaskellScript]<br />
:HaskellScript is the collective name for all Haskell components, both tools and libararies, that allow interaction with the COM/ActiveX framework.<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/haskell-1990-2000/msg04613.html TclHaskell]<br />
:TclHaskell is a haskell binding to Tcl-Tk that lets you build GUIs in Haskell. It is Haskell98 compliant. It has been tested under hugs98 and ghc-4.04.<br />
<br />
;[[CPlusPlusFromHaskell]]<br />
:A hackish method calling C++ from Haskell.<br />
<br />
;[http://www.mail-archive.com/haskell@haskell.org/msg20614.html Re: &#91;Haskell&#93; C++ bindings]. <br />
:An e-mail describing how to link to C++ <br />
<br />
;[[HsLua]]<br />
:Haskell interface to Lua scripting language (tested with GHC 6.6/6.8).<br />
<br />
;[http://hackage.haskell.org/package/hR hR]<br />
:Haskell interface to R<br />
<br />
;[[Salsa]]<br />
:An experimental .NET Bridge for Haskell.<br />
<br />
;[[/Erlang|Erlang]]<br />
:Impersonates an Erlang node on the network.<br />
<br />
=== Obsolete ===<br />
<br />
;[http://web.archive.org/web/20060411002808/http://www.reid-consulting-uk.ltd.uk/docs/ffi.html Guide to Haskell's Foreign Function Interface] <br />
:Comparision of the different tools.<br />
<br />
;[http://web.archive.org/web/20100905111116/http://www.haskell.org/greencard/ Green Card]<br />
: Green Card is a foreign function interface preprocessor for Haskell, simplifying the task of interfacing Haskell programs to external libraries (which are normally exposed via C interfaces). Green Card is currently able to generate code compatible with [[Implementations#The_Haskell_Interpreter_Hugs| Hugs]] and [[Implementations#nhc98 | nhc]]. Green Card is compatible with the [[Implementations#GHC_the_Glasgow_Haskell_Compiler | Glasgow Haskell Compiler]] (GHC), versions prior to 6.2. <br />
:Green Card is not compatible with GHC versions 6.2 and later because Green Card uses a deprecated form of Foreign Function Interface (FFI) binding, _casm_: such old-style bindings were removed in GHC version 6.2. See [http://www.mail-archive.com/haskell@haskell.org/msg14004.html ANNOUNCE: GHC version 6.2].<br />
<br />
;[http://web.archive.org/web/20100524024455/http://www.haskell.org/hdirect/ HaskellDirect]<br />
:HaskellDirect is an Interface Definition Language (IDL) compiler for Haskell, which helps interfacing Haskell code to libraries or components written in other languages (C). An IDL specification specifies the type signatures and types expected by a set of external functions. One important use of this language neutral specification of interfaces is to specify COM (Microsoft's Component Object Model) interfaces, and HaskellDirect offers special support for both using COM objects from Haskell and creating Haskell COM objects. HaskellDirect groks both the OSF DCE dialect of IDL (including the various extensions introduced by the Microsoft IDL compiler) and the OMG IIOP/CORBA dialect. '''Not compatible with recent versions of GHC (6.6.1)'''<br />
<br />
<br />
==== Java ====<br />
<br />
;[http://labs.businessobjects.com/cal/ CAL]<br />
:A hybrid language of Java and Haskell.<br />
<br />
;[http://www.haskell.org/gcjni GCJNI]<br />
:A Java Native Interface for Haskell. Allows Haskell to invoke Java code. Includes a tool to generate Haskell bindings for a Java library. Works for hugs and ghc under both Linux and Windows. (Based on Greencard, see above.)<br />
<br />
;[http://sourceforge.net/projects/jvm-bridge/ Haskell/Java VM Bridge]<br />
:A bridge to the Java virtual machine via JNI for Haskell.<br />
<br />
;[http://research.microsoft.com/en-us/um/people/emeijer/papers/lambada.pdf Lambada] (PDF)<br />
:A Haskell <=> Java interoperation bridge. Interoperation between Haskell and Java is provided by Lambada via the Java Native Interface (JNI), giving you a set of Haskell abstractions that lets you both call out to Java from Haskell *and* wrap up Haskell code behind a Java-callable veneer. '''Not compatible with recent versions of GHC (6.6.1)'''<br />
<br />
;[http://wiki.brianweb.net/LambdaVM/ LambdaVM]<br />
:A set of patches and additions to GHC that let GHC compile from Core to JVM bytecode. Also has a FFI for calling native Java libraries.<br />
<br />
<br />
[[Category:FFI]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Google_summer_of_code&diff=60129Google summer of code2015-09-17T18:37:06Z<p>Beerdude26: /* Accepted GSOC2015 projects */</p>
<hr />
<div>The [https://developers.google.com/open-source/soc/?csw=1 Google Summer of Code (GSoC)] is an annual program funded by Google to allow hundreds of students around the world to participate in free or open source software.<br />
<br />
GSoC is open to students using all kinds of technologies and each year we like to see many projects written in/for the Haskell programming language.<br />
<br />
Some possibly useful links:<br />
<br />
* haskell.org: [http://hackage.haskell.org/trac/summer-of-code/report/1 summer of code trac]<br />
<br />
* reddit: [http://www.reddit.com/r/haskell_proposals haskell proposals]<br />
<br />
* Johan Tibell: [http://blog.johantibell.com/2011/03/writing-good-google-summer-of-code.html writing good gsoc], [http://blog.johantibell.com/2011/03/summer-of-code-project-suggestions.html gsoc suggestions]<br />
<br />
* Gwern: [http://www.gwern.net/Haskell%20Summer%20of%20Code Retrospectives 2006-2013]<br />
<br />
== Accepted GSOC2015 projects ==<br />
<br />
* A Strict language pragma for GHC - Adam Sandberg Eriksson [https://github.com/adamse Github account]<br />
* [https://gist.github.com/Alllex/439480b7e80303f19ddc STM Data Structures Implementation] - Alex Semin <br />
* [https://gist.github.com/AjayRamanathan/c84a4641836700a2547b Implementation of Layered Gramamar of Graphics] - chinu [https://github.com/AjayRamanathan/Plot Github repository] <br />
* Implementing Version Comparison for Cabal Packages - Craig Roche [https://github.com/cdxr Github account]<br />
* Improving Hackage Discoverability - D. Zack Garza [https://github.com/dzackgarza Github repository], [http://dzackgarza.com/mockup/main.html Mockup]<br />
* [http://darcs.net/GSoC/2015-Darcsden Darcsden improvements] - Daniil Frumin [https://github.com/co-dan Github account]<br />
* [https://gist.github.com/hdgarrood/0a389937149453c69e03 Pursuit enhancements] - Harry Garrood [https://github.com/hdgarrood Github account]<br />
* Improvements For HBLAS And Adding LAPACK Bindings. - JuejiYang<br />
* [https://gist.github.com/Jubobs/7a9298eeaf02bcefbc35 A standalone functional parser for CommonMark] - Julien Cretel [https://github.com/Jubobs Github account]<br />
* [http://mpickering.github.io/gsoc2015.html Refactor program with HLint suggestions] - Matthew Pickering [https://github.com/mpickering Github account], [https://camo.githubusercontent.com/a928338441f119fa911151c0db0ceaa72c51e0c4/687474703a2f2f692e696d6775722e636f6d2f3759586f5666742e676966 Demo]<br />
* Replication back-end for acid-state - Max Voit<br />
* [https://gist.github.com/spinda/b261167303515cc8a1d9 Native Haskell Type Encoding for LiquidHaskell] - Michael Smith [https://github.com/spinda Github account]<br />
* Exhaustiveness Checker for PureScript - Nicolas Del Piano [https://github.com/nicodelpiano Github account]<br />
* [https://gist.github.com/nkartashov/e46fd146b1df2d79aaf3 Fast splittable pseudorandom number generator for System.Random] - Nikita Kartashov [https://github.com/nkartashov Github account]<br />
* [https://github.com/sumitsahrawat/gsoc/blob/master/2015/ihaskell.pdf Interactive widgets in IHaskell] - Sumit Sahrawat [https://github.com/sumitsahrawat Github account]<br />
* Improvements to yesod-devel - urbanslug [https://github.com/urbanslug Github account]<br />
* [https://gist.github.com/fugyk/37510958b52589737274 Implement nix-like package management features in cabal] - Vishal Agrawal [https://github.com/fugyk Github account]<br />
* Haddock improvements - Łukasz Hanuszczak [https://github.com/mrhania Github account]<br />
<br />
=== Achieved Goals ===<br />
<br />
==== Refactor program with HLint suggestions ====<br />
<br />
My project was refactoring programs with hlint suggestions. I'm<br />
counting it as a success but I am waiting for a new release of HSE and<br />
hlint before making an announcement. We can apply almost all<br />
suggestions from hlint.<br />
<br />
Here is a demo of it working -<br />
https://camo.githubusercontent.com/a928338441f119fa911151c0db0ceaa72c51e0c4/687474703a2f2f692e696d6775722e636f6d2f3759586f5666742e676966<br />
<br />
==== Interactive widgets in IHaskell ====<br />
<br />
My project "Interactive widgets in IHaskell" was completed successfully. We'll be having a public announcement with an online demo on try.jupyter.org soon.<br />
<br />
==== A Strict language pragma for GHC ====<br />
<br />
Johan Tibell writes: I'm very happy with Adam's work strict Haskell. StrictData is already in HEAD and will be part of 8.0. Adam is working (as his studies permit) of finish the rest. Perhaps if we're really lucky we'll have the Strict pragma as well in 8.0.<br />
<br />
=== Did Not Achieve Goals ===<br />
<br />
==== Implementation of Layered Gramamar of Graphics ====<br />
<br />
Last commit was made on 30 June 2015 and the README says<br />
<br />
Working on a implementation of layered grammar of graphics. Work in progress,<br />
Nothing to see here, Move on.<br />
<br />
It may be that this work has been subsumed here https://github.com/cchalmers/plots but I am not sure how to confirm this.<br />
<br />
== Accepted GSOC2013 projects ==<br />
From [http://www.google-melange.com/gsoc/org/google/gsoc2013/haskell haskell.org]:<br />
<br />
* [http://blog.jlneder.com.ar/search/label/darcs Better record command for darcs]<br />
* [http://gsoc2013cwithmobiledevices.blogspot.com.ar/ Communicating with mobile devices]<br />
* [http://bsrkaditya.blogspot.com/ Enhancing Darcsden]<br />
* Extending GHC to support building modules in parallel<br />
* Improve Haddock Markup and Capabilities<br />
* [http://ofan.me/cat/gsoc.html Haskell Qt Binding Generator and semi-automated C++ ffi wrapper] (blog not live yet)<br />
* [http://cabal-summer.blogspot.com Improve the feedback of the cabal-install dependency solver]<br />
* [http://parenz.wordpress.com/ interactive-diagrams and a paste site with the ability for dynamic rendering of diagrams]<br />
* [http://hackage.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/Plan Overloaded record fields for GHC]<br />
* Parallelise 'cabal build'<br />
* [https://github.com/jbracker/haskell-chart Port Charts to use Diagrams]<br />
<br />
== Accepted GSOC2011 projects ==<br />
From [http://www.google-melange.com/gsoc/org/google/gsoc2011/haskell haskell.org]:<br />
<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/serras/50001 Alejandro Serrano : Improve EclipseFP]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/pastorn/14001 Alexander Göransson: Simplified OpenGL bindings]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/anklesaria/60001 Anklesaria: Interpreter Support for the Cabal-Install Build Tool]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/jaspervdj/15001 Jasper Van der Jeugt: Convert the text package to use UTF–8 internally]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/refold/31001 Mikhail Glushenkov: Build multiple Cabal packages in parallel]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/owst/18001 Owen Stephens: Darcs Bridge]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/mornfall/26001 Petr Ročkai: Darcs: primitive patches version 3]<br />
<br />
Projects from other organizations related to haskell:<br />
<br />
*[http://socghop.appspot.com/gsoc/project/google/gsoc2011/lazard/8001 David Lazar: Formal Executable Semantics of Haskell]</div>Beerdude26https://wiki.haskell.org/index.php?title=Google_summer_of_code&diff=60128Google summer of code2015-09-17T18:36:21Z<p>Beerdude26: /* Accepted GSOC2015 projects */</p>
<hr />
<div>The [https://developers.google.com/open-source/soc/?csw=1 Google Summer of Code (GSoC)] is an annual program funded by Google to allow hundreds of students around the world to participate in free or open source software.<br />
<br />
GSoC is open to students using all kinds of technologies and each year we like to see many projects written in/for the Haskell programming language.<br />
<br />
Some possibly useful links:<br />
<br />
* haskell.org: [http://hackage.haskell.org/trac/summer-of-code/report/1 summer of code trac]<br />
<br />
* reddit: [http://www.reddit.com/r/haskell_proposals haskell proposals]<br />
<br />
* Johan Tibell: [http://blog.johantibell.com/2011/03/writing-good-google-summer-of-code.html writing good gsoc], [http://blog.johantibell.com/2011/03/summer-of-code-project-suggestions.html gsoc suggestions]<br />
<br />
* Gwern: [http://www.gwern.net/Haskell%20Summer%20of%20Code Retrospectives 2006-2013]<br />
<br />
== Accepted GSOC2015 projects ==<br />
<br />
* A Strict language pragma for GHC - Adam Sandberg Eriksson [https://github.com/adamse Github account]<br />
* [STM Data Structures Implementation https://gist.github.com/Alllex/439480b7e80303f19ddc] - Alex Semin <br />
* [https://gist.github.com/AjayRamanathan/c84a4641836700a2547b Implementation of Layered Gramamar of Graphics] - chinu [https://github.com/AjayRamanathan/Plot Github repository] <br />
* Implementing Version Comparison for Cabal Packages - Craig Roche [https://github.com/cdxr Github account]<br />
* Improving Hackage Discoverability - D. Zack Garza [https://github.com/dzackgarza Github repository], [http://dzackgarza.com/mockup/main.html Mockup]<br />
* [http://darcs.net/GSoC/2015-Darcsden Darcsden improvements] - Daniil Frumin [https://github.com/co-dan Github account]<br />
* [https://gist.github.com/hdgarrood/0a389937149453c69e03 Pursuit enhancements] - Harry Garrood [https://github.com/hdgarrood Github account]<br />
* Improvements For HBLAS And Adding LAPACK Bindings. - JuejiYang<br />
* [https://gist.github.com/Jubobs/7a9298eeaf02bcefbc35 A standalone functional parser for CommonMark] - Julien Cretel [https://github.com/Jubobs Github account]<br />
* [http://mpickering.github.io/gsoc2015.html Refactor program with HLint suggestions] - Matthew Pickering [https://github.com/mpickering Github account], [https://camo.githubusercontent.com/a928338441f119fa911151c0db0ceaa72c51e0c4/687474703a2f2f692e696d6775722e636f6d2f3759586f5666742e676966 Demo]<br />
* Replication back-end for acid-state - Max Voit<br />
* [https://gist.github.com/spinda/b261167303515cc8a1d9 Native Haskell Type Encoding for LiquidHaskell] - Michael Smith [https://github.com/spinda Github account]<br />
* Exhaustiveness Checker for PureScript - Nicolas Del Piano [https://github.com/nicodelpiano Github account]<br />
* [https://gist.github.com/nkartashov/e46fd146b1df2d79aaf3 Fast splittable pseudorandom number generator for System.Random] - Nikita Kartashov [https://github.com/nkartashov Github account]<br />
* [https://github.com/sumitsahrawat/gsoc/blob/master/2015/ihaskell.pdf Interactive widgets in IHaskell] - Sumit Sahrawat [https://github.com/sumitsahrawat Github account]<br />
* Improvements to yesod-devel - urbanslug [https://github.com/urbanslug Github account]<br />
* [https://gist.github.com/fugyk/37510958b52589737274 Implement nix-like package management features in cabal] - Vishal Agrawal [https://github.com/fugyk Github account]<br />
* Haddock improvements - Łukasz Hanuszczak [https://github.com/mrhania Github account]<br />
<br />
=== Achieved Goals ===<br />
<br />
==== Refactor program with HLint suggestions ====<br />
<br />
My project was refactoring programs with hlint suggestions. I'm<br />
counting it as a success but I am waiting for a new release of HSE and<br />
hlint before making an announcement. We can apply almost all<br />
suggestions from hlint.<br />
<br />
Here is a demo of it working -<br />
https://camo.githubusercontent.com/a928338441f119fa911151c0db0ceaa72c51e0c4/687474703a2f2f692e696d6775722e636f6d2f3759586f5666742e676966<br />
<br />
==== Interactive widgets in IHaskell ====<br />
<br />
My project "Interactive widgets in IHaskell" was completed successfully. We'll be having a public announcement with an online demo on try.jupyter.org soon.<br />
<br />
==== A Strict language pragma for GHC ====<br />
<br />
Johan Tibell writes: I'm very happy with Adam's work strict Haskell. StrictData is already in HEAD and will be part of 8.0. Adam is working (as his studies permit) of finish the rest. Perhaps if we're really lucky we'll have the Strict pragma as well in 8.0.<br />
<br />
=== Did Not Achieve Goals ===<br />
<br />
==== Implementation of Layered Gramamar of Graphics ====<br />
<br />
Last commit was made on 30 June 2015 and the README says<br />
<br />
Working on a implementation of layered grammar of graphics. Work in progress,<br />
Nothing to see here, Move on.<br />
<br />
It may be that this work has been subsumed here https://github.com/cchalmers/plots but I am not sure how to confirm this.<br />
<br />
== Accepted GSOC2013 projects ==<br />
From [http://www.google-melange.com/gsoc/org/google/gsoc2013/haskell haskell.org]:<br />
<br />
* [http://blog.jlneder.com.ar/search/label/darcs Better record command for darcs]<br />
* [http://gsoc2013cwithmobiledevices.blogspot.com.ar/ Communicating with mobile devices]<br />
* [http://bsrkaditya.blogspot.com/ Enhancing Darcsden]<br />
* Extending GHC to support building modules in parallel<br />
* Improve Haddock Markup and Capabilities<br />
* [http://ofan.me/cat/gsoc.html Haskell Qt Binding Generator and semi-automated C++ ffi wrapper] (blog not live yet)<br />
* [http://cabal-summer.blogspot.com Improve the feedback of the cabal-install dependency solver]<br />
* [http://parenz.wordpress.com/ interactive-diagrams and a paste site with the ability for dynamic rendering of diagrams]<br />
* [http://hackage.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/Plan Overloaded record fields for GHC]<br />
* Parallelise 'cabal build'<br />
* [https://github.com/jbracker/haskell-chart Port Charts to use Diagrams]<br />
<br />
== Accepted GSOC2011 projects ==<br />
From [http://www.google-melange.com/gsoc/org/google/gsoc2011/haskell haskell.org]:<br />
<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/serras/50001 Alejandro Serrano : Improve EclipseFP]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/pastorn/14001 Alexander Göransson: Simplified OpenGL bindings]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/anklesaria/60001 Anklesaria: Interpreter Support for the Cabal-Install Build Tool]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/jaspervdj/15001 Jasper Van der Jeugt: Convert the text package to use UTF–8 internally]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/refold/31001 Mikhail Glushenkov: Build multiple Cabal packages in parallel]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/owst/18001 Owen Stephens: Darcs Bridge]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/mornfall/26001 Petr Ročkai: Darcs: primitive patches version 3]<br />
<br />
Projects from other organizations related to haskell:<br />
<br />
*[http://socghop.appspot.com/gsoc/project/google/gsoc2011/lazard/8001 David Lazar: Formal Executable Semantics of Haskell]</div>Beerdude26https://wiki.haskell.org/index.php?title=Google_summer_of_code&diff=60127Google summer of code2015-09-17T18:35:18Z<p>Beerdude26: Cleaned up the GSoC 2015 overview.</p>
<hr />
<div>The [https://developers.google.com/open-source/soc/?csw=1 Google Summer of Code (GSoC)] is an annual program funded by Google to allow hundreds of students around the world to participate in free or open source software.<br />
<br />
GSoC is open to students using all kinds of technologies and each year we like to see many projects written in/for the Haskell programming language.<br />
<br />
Some possibly useful links:<br />
<br />
* haskell.org: [http://hackage.haskell.org/trac/summer-of-code/report/1 summer of code trac]<br />
<br />
* reddit: [http://www.reddit.com/r/haskell_proposals haskell proposals]<br />
<br />
* Johan Tibell: [http://blog.johantibell.com/2011/03/writing-good-google-summer-of-code.html writing good gsoc], [http://blog.johantibell.com/2011/03/summer-of-code-project-suggestions.html gsoc suggestions]<br />
<br />
* Gwern: [http://www.gwern.net/Haskell%20Summer%20of%20Code Retrospectives 2006-2013]<br />
<br />
== Accepted GSOC2015 projects ==<br />
<br />
* A Strict language pragma for GHC - Adam Sandberg Eriksson [https://github.com/adamse Github account]<br />
* [STM Data Structures Implementation https://gist.github.com/Alllex/439480b7e80303f19ddc] - Alex Semin <br />
* [https://gist.github.com/AjayRamanathan/c84a4641836700a2547b Implementation of Layered Gramamar of Graphics] - chinu [https://github.com/AjayRamanathan/Plot Github repository] <br />
* Implementing Version Comparison for Cabal Packages - Craig Roche [https://github.com/cdxr Github account]<br />
* Improving Hackage Discoverability - D. Zack Garza [https://github.com/dzackgarza Github repository], [http://dzackgarza.com/mockup/main.html Mockup]<br />
* [http://darcs.net/GSoC/2015-Darcsden Darcsden improvements] - Daniil Frumin [https://github.com/co-dan Github account]<br />
* [https://gist.github.com/hdgarrood/0a389937149453c69e03 Pursuit enhancements] - Harry Garrood [https://github.com/hdgarrood Github account]<br />
* Improvements For HBLAS And Adding LAPACK Bindings. - JuejiYang<br />
* [https://gist.github.com/Jubobs/7a9298eeaf02bcefbc35 A standalone functional parser for CommonMark] - Julien Cretel [https://github.com/Jubobs Github account]<br />
* [http://mpickering.github.io/gsoc2015.html Refactor program with HLint suggestions] - Matthew Pickering [https://github.com/mpickering Github account], [https://camo.githubusercontent.com/a928338441f119fa911151c0db0ceaa72c51e0c4/687474703a2f2f692e696d6775722e636f6d2f3759586f5666742e676966 Demo]<br />
* Replication back-end for acid-state - Max Voit<br />
* [https://gist.github.com/spinda/b261167303515cc8a1d9 Native Haskell Type Encoding for LiquidHaskell] - Michael Smith [https://github.com/spinda Github account]<br />
* Exhaustiveness Checker for PureScript - Nicolas Del Piano [https://github.com/nicodelpiano Github account]<br />
* [https://gist.github.com/nkartashov/e46fd146b1df2d79aaf3 Fast splittable pseudorandom number generator for System.Random] - Nikita Kartashov [https://github.com/nkartashov Github account]<br />
* [https://github.com/sumitsahrawat/gsoc/blob/master/2015/ihaskell.pdf Interactive widgets in IHaskell] - Sumit Sahrawat [https://github.com/sumitsahrawat Github account]<br />
* Improvements to yesod-devel - urbanslug [https://github.com/urbanslug Github account]<br />
* Implement nix-like package management features in cabal <https://gist.github.com/fugyk/37510958b52589737274> - Vishal Agrawal [https://github.com/fugyk Github account]<br />
* Haddock improvements - Łukasz Hanuszczak [https://github.com/mrhania Github account]<br />
<br />
=== Achieved Goals ===<br />
<br />
==== Refactor program with HLint suggestions ====<br />
<br />
My project was refactoring programs with hlint suggestions. I'm<br />
counting it as a success but I am waiting for a new release of HSE and<br />
hlint before making an announcement. We can apply almost all<br />
suggestions from hlint.<br />
<br />
Here is a demo of it working -<br />
https://camo.githubusercontent.com/a928338441f119fa911151c0db0ceaa72c51e0c4/687474703a2f2f692e696d6775722e636f6d2f3759586f5666742e676966<br />
<br />
==== Interactive widgets in IHaskell ====<br />
<br />
My project "Interactive widgets in IHaskell" was completed successfully. We'll be having a public announcement with an online demo on try.jupyter.org soon.<br />
<br />
==== A Strict language pragma for GHC ====<br />
<br />
Johan Tibell writes: I'm very happy with Adam's work strict Haskell. StrictData is already in HEAD and will be part of 8.0. Adam is working (as his studies permit) of finish the rest. Perhaps if we're really lucky we'll have the Strict pragma as well in 8.0.<br />
<br />
=== Did Not Achieve Goals ===<br />
<br />
==== Implementation of Layered Gramamar of Graphics ====<br />
<br />
Last commit was made on 30 June 2015 and the README says<br />
<br />
Working on a implementation of layered grammar of graphics. Work in progress,<br />
Nothing to see here, Move on.<br />
<br />
It may be that this work has been subsumed here https://github.com/cchalmers/plots but I am not sure how to confirm this.<br />
<br />
== Accepted GSOC2013 projects ==<br />
From [http://www.google-melange.com/gsoc/org/google/gsoc2013/haskell haskell.org]:<br />
<br />
* [http://blog.jlneder.com.ar/search/label/darcs Better record command for darcs]<br />
* [http://gsoc2013cwithmobiledevices.blogspot.com.ar/ Communicating with mobile devices]<br />
* [http://bsrkaditya.blogspot.com/ Enhancing Darcsden]<br />
* Extending GHC to support building modules in parallel<br />
* Improve Haddock Markup and Capabilities<br />
* [http://ofan.me/cat/gsoc.html Haskell Qt Binding Generator and semi-automated C++ ffi wrapper] (blog not live yet)<br />
* [http://cabal-summer.blogspot.com Improve the feedback of the cabal-install dependency solver]<br />
* [http://parenz.wordpress.com/ interactive-diagrams and a paste site with the ability for dynamic rendering of diagrams]<br />
* [http://hackage.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/Plan Overloaded record fields for GHC]<br />
* Parallelise 'cabal build'<br />
* [https://github.com/jbracker/haskell-chart Port Charts to use Diagrams]<br />
<br />
== Accepted GSOC2011 projects ==<br />
From [http://www.google-melange.com/gsoc/org/google/gsoc2011/haskell haskell.org]:<br />
<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/serras/50001 Alejandro Serrano : Improve EclipseFP]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/pastorn/14001 Alexander Göransson: Simplified OpenGL bindings]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/anklesaria/60001 Anklesaria: Interpreter Support for the Cabal-Install Build Tool]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/jaspervdj/15001 Jasper Van der Jeugt: Convert the text package to use UTF–8 internally]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/refold/31001 Mikhail Glushenkov: Build multiple Cabal packages in parallel]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/owst/18001 Owen Stephens: Darcs Bridge]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/mornfall/26001 Petr Ročkai: Darcs: primitive patches version 3]<br />
<br />
Projects from other organizations related to haskell:<br />
<br />
*[http://socghop.appspot.com/gsoc/project/google/gsoc2011/lazard/8001 David Lazar: Formal Executable Semantics of Haskell]</div>Beerdude26https://wiki.haskell.org/index.php?title=Google_summer_of_code&diff=60126Google summer of code2015-09-17T18:25:27Z<p>Beerdude26: capitalize Haskell</p>
<hr />
<div>The [https://developers.google.com/open-source/soc/?csw=1 Google Summer of Code (GSoC)] is an annual program funded by Google to allow hundreds of students around the world to participate in free or open source software.<br />
<br />
GSoC is open to students using all kinds of technologies and each year we like to see many projects written in/for the Haskell programming language.<br />
<br />
Some possibly useful links:<br />
<br />
haskell.org: [http://hackage.haskell.org/trac/summer-of-code/report/1 summer of code trac]<br />
<br />
reddit: [http://www.reddit.com/r/haskell_proposals haskell proposals]<br />
<br />
Johan Tibell: [http://blog.johantibell.com/2011/03/writing-good-google-summer-of-code.html writing good gsoc], [http://blog.johantibell.com/2011/03/summer-of-code-project-suggestions.html gsoc suggestions]<br />
<br />
Gwern: [http://www.gwern.net/Haskell%20Summer%20of%20Code Retrospectives 2006-2013]<br />
<br />
== Accepted GSOC2015 projects ==<br />
<br />
* A Strict language pragma for GHC - Adam Sandberg Eriksson <https://github.com/adamse><br />
* STM Data Structures Implementation <https://gist.github.com/Alllex/439480b7e80303f19ddc> - Alex Semin<br />
* Implementation of Layered Gramamar of Graphics <https://gist.github.com/AjayRamanathan/c84a4641836700a2547b> - chinu <https://github.com/AjayRamanathan/Plot><br />
* Implementing Version Comparison for Cabal Packages - Craig Roche <https://github.com/cdxr><br />
* Improving Hackage Discoverability <http://dzackgarza.com/mockup/main.html> - D. Zack Garza <https://github.com/dzackgarza><br />
* Darcsden improvements <http://darcs.net/GSoC/2015-Darcsden> - Daniil Frumin <https://github.com/co-dan><br />
* Pursuit enhancements <https://gist.github.com/hdgarrood/0a389937149453c69e03> - Harry Garrood <https://github.com/hdgarrood><br />
* Improvements For HBLAS And Adding LAPACK Bindings. - JuejiYang<br />
* A standalone functional parser for CommonMark <https://gist.github.com/Jubobs/7a9298eeaf02bcefbc35> - Julien Cretel <https://github.com/Jubobs><br />
* Refactor program with HLint suggestions <http://mpickering.github.io/gsoc2015.html> - Matthew Pickering <https://github.com/mpickering><br />
* Replication back-end for acid-state - Max Voit<br />
* Native Haskell Type Encoding for LiquidHaskell <https://gist.github.com/spinda/b261167303515cc8a1d9> - Michael Smith <https://github.com/spinda><br />
* Exhaustiveness Checker for PureScript - Nicolas Del Piano <https://github.com/nicodelpiano><br />
* Fast splittable pseudorandom number generator for System.Random <https://gist.github.com/nkartashov/e46fd146b1df2d79aaf3> - Nikita Kartashov <https://github.com/nkartashov><br />
* Interactive widgets in IHaskell <https://github.com/sumitsahrawat/gsoc/blob/master/2015/ihaskell.pdf> - Sumit Sahrawat <https://github.com/sumitsahrawat><br />
* Improvements to yesod-devel - urbanslug <https://github.com/urbanslug><br />
* Implement nix-like package management features in cabal <https://gist.github.com/fugyk/37510958b52589737274> - Vishal Agrawal <https://github.com/fugyk><br />
* Haddock improvements - Łukasz Hanuszczak <https://github.com/mrhania><br />
<br />
=== Achieved Goals ===<br />
<br />
==== Refactor program with HLint suggestions ====<br />
<br />
My project was refactoring programs with hlint suggestions. I'm<br />
counting it as a success but I am waiting for a new release of HSE and<br />
hlint before making an announcement. We can apply almost all<br />
suggestions from hlint.<br />
<br />
Here is a demo of it working -<br />
https://camo.githubusercontent.com/a928338441f119fa911151c0db0ceaa72c51e0c4/687474703a2f2f692e696d6775722e636f6d2f3759586f5666742e676966<br />
<br />
==== Interactive widgets in IHaskell ====<br />
<br />
My project "Interactive widgets in IHaskell" was completed successfully. We'll be having a public announcement with an online demo on try.jupyter.org soon.<br />
<br />
==== A Strict language pragma for GHC ====<br />
<br />
Johan Tibell writes: I'm very happy with Adam's work strict Haskell. StrictData is already in HEAD and will be part of 8.0. Adam is working (as his studies permit) of finish the rest. Perhaps if we're really lucky we'll have the Strict pragma as well in 8.0.<br />
<br />
=== Did Not Achieve Goals ===<br />
<br />
==== Implementation of Layered Gramamar of Graphics ====<br />
<br />
Last commit was made on 30 June 2015 and the README says<br />
<br />
Working on a implementation of layered grammar of graphics. Work in progress,<br />
Nothing to see here, Move on.<br />
<br />
It may be that this work has been subsumed here https://github.com/cchalmers/plots but I am not sure how to confirm this.<br />
<br />
== Accepted GSOC2013 projects ==<br />
From [http://www.google-melange.com/gsoc/org/google/gsoc2013/haskell haskell.org]:<br />
<br />
* [http://blog.jlneder.com.ar/search/label/darcs Better record command for darcs]<br />
* [http://gsoc2013cwithmobiledevices.blogspot.com.ar/ Communicating with mobile devices]<br />
* [http://bsrkaditya.blogspot.com/ Enhancing Darcsden]<br />
* Extending GHC to support building modules in parallel<br />
* Improve Haddock Markup and Capabilities<br />
* [http://ofan.me/cat/gsoc.html Haskell Qt Binding Generator and semi-automated C++ ffi wrapper] (blog not live yet)<br />
* [http://cabal-summer.blogspot.com Improve the feedback of the cabal-install dependency solver]<br />
* [http://parenz.wordpress.com/ interactive-diagrams and a paste site with the ability for dynamic rendering of diagrams]<br />
* [http://hackage.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/Plan Overloaded record fields for GHC]<br />
* Parallelise 'cabal build'<br />
* [https://github.com/jbracker/haskell-chart Port Charts to use Diagrams]<br />
<br />
== Accepted GSOC2011 projects ==<br />
From [http://www.google-melange.com/gsoc/org/google/gsoc2011/haskell haskell.org]:<br />
<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/serras/50001 Alejandro Serrano : Improve EclipseFP]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/pastorn/14001 Alexander Göransson: Simplified OpenGL bindings]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/anklesaria/60001 Anklesaria: Interpreter Support for the Cabal-Install Build Tool]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/jaspervdj/15001 Jasper Van der Jeugt: Convert the text package to use UTF–8 internally]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/refold/31001 Mikhail Glushenkov: Build multiple Cabal packages in parallel]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/owst/18001 Owen Stephens: Darcs Bridge]<br />
*[http://www.google-melange.com/gsoc/project/google/gsoc2011/mornfall/26001 Petr Ročkai: Darcs: primitive patches version 3]<br />
<br />
Projects from other organizations related to haskell:<br />
<br />
*[http://socghop.appspot.com/gsoc/project/google/gsoc2011/lazard/8001 David Lazar: Formal Executable Semantics of Haskell]</div>Beerdude26https://wiki.haskell.org/index.php?title=Research_papers&diff=60063Research papers2015-09-04T11:42:25Z<p>Beerdude26: Removed link to http://www.catamorphism.net/, it's a picture gallery</p>
<hr />
<div>[[Category:Research]]<br />
<br />
__NOTOC__<br />
<br />
A lot of documentation exists about Haskell, and its foundations, in the form of research papers written by those investigating language design. An enormous research effort, by hundreds of researchers over the past 20 years, has gone into making Haskell such a great language. In general, if a feature is not well understood, it isn't going to become part of the language. <br />
<br />
Here is a selection of those papers, with the goal of making the wealth of material published on Haskell more available to the casual user, and not just researchers. Some of the papers are highly technical, others, not so. These papers are not suitable for those trying to learn the language from scratch, but more for those looking for a deeper understanding of the theoretical and practical aspects of Haskell.<br />
<br />
More links to papers can be found at [http://www.dohaskell.com/ dohaskell].<br />
<br />
There are E-reader-friendly versions of many PDFs available at [https://github.com/beerendlauwers/haskell-papers-ereader this Github repository].<br />
<br />
==Overview==<br />
<br />
;[http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf Why Functional Programming Matters] ∷ PDF<br />
:John Hughes. Comput. J. 32(2): 98-107 (1989)<br />
<br />
;[http://www.cs.kent.ac.uk/pubs/1997/224/index.html Higher-order + Polymorphic = Reusable]<br />
:Simon Thompson, 1997.<br />
<br />
;[http://research.microsoft.com/~simonpj/papers/history-of-haskell/index.htm The History of Haskell]<br />
:Simon Peyton Jones, Paul Hudak, John Hughes, and Philip Wadler, 2006<br />
<br />
==Categories==<br />
<br />
*[[/Runtime systems|Runtime systems]]<br />
*[[/Parallelism and concurrency|Parallelism and concurrency]]<br />
*[[/Compilation|Compilation]]<br />
*[[/Type systems|Type systems]]<br />
*[[/Data structures|Data structures]]<br />
*[[/Monads and arrows|Monads and arrows]]<br />
*[[/Generics|Generic programming]]<br />
*[[/Testing and correctness|Proofs, verification and testing]]<br />
*[[/Program development|Software application development]]<br />
*[[/Domain specific languages|Domain specific languages]]<br />
*[[/Functional reactive programming|Functional reactive programming]]<br />
*[[/Functional pearls|Functional pearls: beautiful design]]<br />
<br />
== Authors ==<br />
[[/Authors|Authors Index]]<br />
<br />
== Top 10 ==<br />
<br />
[[/Top_10|Most cited]] Haskell papers</div>Beerdude26https://wiki.haskell.org/index.php?title=Research_papers&diff=60062Research papers2015-09-04T08:56:59Z<p>Beerdude26: Link to https://github.com/beerendlauwers/haskell-papers-ereader</p>
<hr />
<div>[[Category:Research]]<br />
<br />
__NOTOC__<br />
<br />
A lot of documentation exists about Haskell, and its foundations, in the form of research papers written by those investigating language design. An enormous research effort, by hundreds of researchers over the past 20 years, has gone into making Haskell such a great language. In general, if a feature is not well understood, it isn't going to become part of the language. <br />
<br />
Here is a selection of those papers, with the goal of making the wealth of material published on Haskell more available to the casual user, and not just researchers. Some of the papers are highly technical, others, not so. These papers are not suitable for those trying to learn the language from scratch, but more for those looking for a deeper understanding of the theoretical and practical aspects of Haskell.<br />
<br />
More links to papers can be found at [http://www.dohaskell.com/ dohaskell].<br />
<br />
There are E-reader-friendly versions of many PDFs available at [https://github.com/beerendlauwers/haskell-papers-ereader this Github repository].<br />
<br />
==Overview==<br />
<br />
;[http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf Why Functional Programming Matters] ∷ PDF<br />
:John Hughes. Comput. J. 32(2): 98-107 (1989)<br />
<br />
;[http://www.cs.kent.ac.uk/pubs/1997/224/index.html Higher-order + Polymorphic = Reusable]<br />
:Simon Thompson, 1997.<br />
<br />
;[http://research.microsoft.com/~simonpj/papers/history-of-haskell/index.htm The History of Haskell]<br />
:Simon Peyton Jones, Paul Hudak, John Hughes, and Philip Wadler, 2006<br />
<br />
==Categories==<br />
<br />
*[[/Runtime systems|Runtime systems]]<br />
*[[/Parallelism and concurrency|Parallelism and concurrency]]<br />
*[[/Compilation|Compilation]]<br />
*[[/Type systems|Type systems]]<br />
*[[/Data structures|Data structures]]<br />
*[[/Monads and arrows|Monads and arrows]]<br />
*[[/Generics|Generic programming]]<br />
*[[/Testing and correctness|Proofs, verification and testing]]<br />
*[[/Program development|Software application development]]<br />
*[[/Domain specific languages|Domain specific languages]]<br />
*[[/Functional reactive programming|Functional reactive programming]]<br />
*[[/Functional pearls|Functional pearls: beautiful design]]<br />
<br />
== Authors ==<br />
[[/Authors|Authors Index]]<br />
<br />
== Top 10 ==<br />
<br />
[[/Top_10|Most cited]] Haskell papers<br />
<br />
== Bibliography ==<br />
<br />
[http://www.catamorphism.net/ Functional Programming Bibliography]<br />
<br />
A searchable bibliographic database, concentrating on Haskell, with over 1,500 citations.</div>Beerdude26https://wiki.haskell.org/index.php?title=Catch&diff=59966Catch2015-08-06T08:17:04Z<p>Beerdude26: /* Related work: */</p>
<hr />
<div><br />
= Introduction =<br />
<br />
Do you sometimes encounter the dreaded "pattern match failure: head<br />
[]" message? Do you have incomplete patterns which sometimes fail? Do<br />
you have incomplete patterns which you know don't fail, but still get<br />
compiler warnings about them? Would you like to statically ensure the<br />
absence of all calls to error?<br />
<br />
==Location==<br />
For the last few years I've been working on a pattern-match checker<br />
for Haskell, named Catch. I'm now happy to make a release:<br />
<br />
* Hackage: {{HackagePackage|id=catch}}<br />
* Homepage: http://www-users.cs.york.ac.uk/~ndm/catch/<br />
* User manual: http://www.cs.york.ac.uk/fp/darcs/catch/catch.htm<br />
<br />
While a [[darcs]] version is available, I strongly recommend the use of<br />
the tarball on [[hackage]].<br />
<br />
==Features:==<br />
*Detects all incomplete patterns and calls to error<br />
*Full support for Haskell, through the use of Yhc as a front end<br />
*Automatic - no annotations are required<br />
*Fast - checking times of under 5 seconds (excluding Yhc compilation)are typical<br />
<br />
==Drawbacks:==<br />
* Requires [[Yhc]] to be installed, which doesn't accept most Haskell extensions. This will be fixed once [[GHC]].Core is available as a standalone library. The underlying tool is not limited to Haskell 98 in any way.<br />
<br />
==Related work:==<br />
<br />
* [https://wiki.haskell.org/Liquid_Haskell Liquid Haskell] - Annotate your functions with invariants that are checked at compile-time.<br />
<br />
* The [https://hackage.haskell.org/package/total total] package does not search for incomplete pattern matches, but it does allow you to write compiler-checked exhaustive pattern matches. [http://www.haskellforall.com/2015/01/total-100-exhaustive-pattern-matching.html Blog post here].<br />
<br />
* [https://hackage.haskell.org/package/exhaustive exhaustive] Does the same, but without lenses.<br />
<br />
==Success stories:==<br />
*The [[darcs]] version of [[HsColour]] has been proven free of pattern match errors, and [[Catch]] was able to find three previously unknown crashes which could be triggered by a malformed document.<br />
*System.[[FilePath]] has been checked, and is free of pattern-match errors.<br />
*[[Xmonad]]'s central StackSet.hs module has been checked repeatedly, as it has evolved. Unfortunately currently this module goes beyond Haskell 98, but this should be fixed shortly.<br />
<br />
If you have any experiences with Catch, I'd be very interested to hear them.<br />
<br />
Thanks<br />
Neil<br />
[[Category:Development tools]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Learning_Haskell&diff=59962Learning Haskell2015-08-04T06:49:01Z<p>Beerdude26: Forgot http for the subreddit</p>
<hr />
<div>[[Category:Tutorials]]<br />
<br />
This portal points to places where you can go if you want to learn Haskell. <br />
<br />
The [[Introduction|Introduction to Haskell]] on the Haskell website tells you what Haskell gives you: substantially increased programmer productivity, shorter, clearer, and more maintainable code, fewer errors, higher reliability, a smaller semantic gap between the programmer and the language, shorter lead times. There is an old but still relevant paper about [http://www.cse.chalmers.se/~rjmh/Papers/whyfp.html Why Functional Programming Matters] (PDF) by John Hughes. More recently, Sebastian Sylvan wrote an article about [[Why Haskell Matters]].<br />
<br />
Join the [http://www.reddit.com/r/haskell Haskell subreddit], where we do regular Q&A threads called [[Hask Anything]] (that's the archive).<br />
<br />
There is also a [http://www.haskell.org/haskellwiki/Comparison table comparing Haskell to other functional languages]. Many questions about functional programming are answered by the [http://www.cs.nott.ac.uk/~gmh//faq.html comp.lang.functional FAQ].<br />
<br />
You can ask questions to members of the Haskell community on mailing lists, IRC, or StackOverflow. We recommend installing the [http://www.haskell.org/platform/ Haskell Platform].<br />
<br />
== Training courses ==<br />
<br />
Short training courses aimed at existing programmers<br />
<br />
* [http://www.well-typed.com/services_training On-site and public training courses] by Well-Typed (2-day intro, 2-day advanced, custom on-site courses)<br />
* [http://www.nobleprog.co.uk/haskell/training Public training courses] by NobleProg and Nilcons<br />
* [http://www.cs.ox.ac.uk/softeng/subjects/FPR.html Software Engineering course on Functional Programming] at the University of Oxford (1-week course)<br />
* [http://www.cs.uu.nl/wiki/USCS Summerschool on Applied Functional Programming] at Utrecht University (2-week course)<br />
<br />
== Material for self-study ==<br />
<br />
Below there are links to certain introductory material. If you want to dig deeper, see [[Books and tutorials]].<br />
<br />
=== Textbooks ===<br />
<br />
* [http://www.cs.yale.edu/homes/hudak/SOE/ The Haskell School of Expression]<br />
* [http://www.haskellcraft.com/ Haskell: the Craft of Functional Programming]<br />
* [http://www.prenhall.com/allbooks/ptr_0134843460.html Introduction to Functional Programming using Haskell]<br />
* [http://www.cambridge.org/us/knowledge/isbn/item1129654/Introduction%20to%20Functional%20Programming%20Systems%20Using%20Haskell/?site_locale=en_US An Introduction to Functional Programming Systems Using Haskell]<br />
* [http://www.iro.umontreal.ca/~lapalme/Algorithms-functional.html Algorithms: A functional programming approach]<br />
* [http://homepages.cwi.nl/~jve/HR/ The Haskell Road to Logic, Maths, and Programming] (also freely [http://fldit-www.cs.uni-dortmund.de/~peter/PS07/HR.pdf available online]). <br />
* [http://www.cs.nott.ac.uk/~gmh/book.html Programming in Haskell]<br />
* [http://book.realworldhaskell.org/ Real World Haskell]<br />
* [http://nostarch.com/lyah.htm Learn You a Haskell for Great Good!]<br />
<br />
=== Online tutorials ===<br />
<br />
* [[Meta-tutorial]]<br />
* [http://pluralsight.com/training/Courses/Find?highlight=true&searchTerm=haskell Haskell Fundamentals - get started and learn key concepts] at Pluralsight (2-part, 5 hour online course)<br />
* [http://en.wikibooks.org/wiki/Haskell Haskell Wikibook] A thorough textbook with a step-by-step beginners track assuming no programming background. Also includes many advanced concepts, and adaptations of "Yet Another Haskell Tutorial", "Write Yourself a Scheme in 48 Hours", and "All about monads".<br />
* [http://pub.hal3.name/daume02yaht.pdf YAHT - Yet Another Haskell Tutorial] (good tutorial available online)<br />
* [http://www.cs.ou.edu/~rlpage/fpclassCurrent/textbook/haskell.shtml Two dozen short lessons]<br />
* [http://www.haskell.org/tutorial/ A Gentle Introduction to Haskell] - classic text, but not so gentle really :D<br />
* [ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf Haskell-Tutorial]<br />
* [http://lasche.codingcrew.de/kurse/haskell/hskurs_index.htm Online Haskell Course] (German)<br />
* [http://collection.openlibra.com.s3.amazonaws.com/pdf/haskell_tutorial_for_c_programmers_en.pdf Haskell tutorial for C Programmers]<br />
* [http://learnyouahaskell.com/ Learn You a Haskell for Great Good!] Beautiful, illustrated Haskell tutorial for programmers with less of a functional programming background.<br />
* [http://www.youtube.com/playlist?list=PL2672EBC57C1F5F9B Learning Haskell] Ongoing tutorial in the form of YouTube videos; updates slowly.<br />
*[https://stevekrouse.github.io/hs.js/ Pattern matching, first-class functions, and abstracting over recursion in Haskell], a simulation of the evaluation of map, foldr and foldl.<br />
* [https://www.fpcomplete.com/school?show=tutorials School of Haskell]<br />
<br />
=== Advanced tutorials ===<br />
<br />
* [[Hitchhikers guide to Haskell]]<br />
* [http://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours Write Yourself a Scheme in 48 Hours]<br />
* [http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/ Tackling the Awkward Squad] (on I/O, interfacing to C, concurrency and exceptions)<br />
<br />
=== Debugging/profiling/optimization ===<br />
<br />
=== Monads ===<br />
<br />
* [http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html You Could Have Invented Monads! (And Maybe You Already Have.)]<br />
* [http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf Monads for Functional Programming]<br />
* [http://www.haskell.org/haskellwiki/All_About_Monads All about monads]<br />
* [[IO inside|IO inside: down the Rabbit Hole]]<br />
<br />
=== Type classes ===<br />
<br />
* [http://homepages.inf.ed.ac.uk/wadler/papers/class/class.ps.gz The paper that for the first time introduced type classes and their implementation using dictionaries]<br />
* [[Research papers/Type systems#Type classes|More papers on the type classes]]<br />
<br />
=== Generic programming ===<br />
<br />
* [[Scrap your boilerplate]]<br />
<br />
=== Popular libraries ===<br />
<br />
* ByteStrings?<br />
* [http://legacy.cs.uu.nl/daan/download/parsec/parsec.html Parsec, a fast combinator parser]<br />
* [[Modern array libraries]]<br />
* [http://www.haskell.org/haskellwiki/Gtk2Hs/Tutorials Gtk2Hs, the GUI library]<br />
* [https://ocharles.org.uk/blog/ 24 Days of Hackage] (blog posts about many popular libraries)<br />
<br />
=== Reference ===<br />
<br />
* The official language definition: [[Language and library specification]]<br />
* [http://www.letu.edu/people/jaytevis/Programming-Languages/Haskell/tourofprelude.html Tour of the Haskell Prelude]<br />
* [http://zvon.org/other/haskell/Outputglobal/index.html Haskell Reference]<br />
* Haskell [[Reference card]]<br />
* [http://members.chello.nl/hjgtuyl/tourdemonad.html A tour of the Haskell Monad functions]<br />
* [http://www.cs.uu.nl/wiki/bin/view/Helium/ATourOfTheHeliumPrelude Tour of the Helium Prelude]<br />
* [http://www.cs.kent.ac.uk/people/staff/sjt/craft2e/errors/allErrors.html Some common Hugs error messages]<br />
* [http://cheatsheet.codeslower.com/ The Haskell Cheatsheet] - A reference card and mini-tutorial in one.<br />
* A [http://www.haskell.org/haskellwiki/Category:Glossary Glossary] of common terminology.<br />
<br />
=== Course material ===<br />
* [http://www.cse.chalmers.se/edu/course/TDA555/ Introduction to Functional Programming, Chalmers] (for beginners at programming)<br />
* [http://www.cse.chalmers.se/edu/course/TDA452/ Functional Programming, Chalmers]<br />
* [http://www.cse.chalmers.se/edu/course/afp/ Advanced Functional Programming, Chalmers]<br />
* [http://www.cse.chalmers.se/edu/course/pfp/ Parallel Functional Programming, Chalmers]<br />
* [http://www.shuklan.com/haskell Introduction to Haskell], University of Virginia CS 1501<br />
* [http://www.cs.caltech.edu/courses/cs11/material/haskell/index.html CS 11 Caltech]<br />
* [http://www.cs.uu.nl/docs/vakken/lfp/ Functional programming]: course notes ([http://www.staff.science.uu.nl/~fokke101/courses/fp-eng.pdf English], [http://www.staff.science.uu.nl/~fokke101/courses/fp-nl.pdf Dutch], [http://www.staff.science.uu.nl/~fokke101/courses/fp-sp.pdf Spanish]), slides in Dutch<br />
* [http://www.cse.unsw.edu.au/~cs1011/05s2/ CS1011]: Tutorials, lab exercises and solutions<br />
* Stanford - [http://www.scs.stanford.edu/11au-cs240h/ Functional Systems in Haskell]<br />
<br />
<br />
== Trying Haskell online ==<br />
<br />
There are several websites where you can enter a Haskell program and run it. They are (in no particular order):<br />
* [https://cloud.sagemath.com/ SageMathCloud]<br />
* [https://www.fpcomplete.com/school/using-fphc FP Haskell Center]<br />
* [http://tryhaskell.org/ Try Haskell]<br />
* [http://www.codeworld.info/ Codeworld]<br />
* [http://chrisuehlinger.com/LambdaBubblePop/ Bubble Pop!], the satisfaction of popping bubble wrap, combined with the satisfaction of really elegant functional programming!<br />
* [http://tryplayg.herokuapp.com/ Try Haste & HPlayground client-side framework]; the source code is on [https://github.com/agocorona/tryhplay GitHub]<br />
* [https://koding.com/ Koding] is a cloud based IDE which supports Haskell and several other languages. Free accounts allow one virtual machine.<br />
<br />
To create a browser based environment yourself:<br />
* [http://gibiansky.github.io/IHaskell/ IHaskell]</div>Beerdude26https://wiki.haskell.org/index.php?title=Learning_Haskell&diff=59961Learning Haskell2015-08-04T06:48:11Z<p>Beerdude26: Added Reddit subreddit and Hask Anything</p>
<hr />
<div>[[Category:Tutorials]]<br />
<br />
This portal points to places where you can go if you want to learn Haskell. <br />
<br />
The [[Introduction|Introduction to Haskell]] on the Haskell website tells you what Haskell gives you: substantially increased programmer productivity, shorter, clearer, and more maintainable code, fewer errors, higher reliability, a smaller semantic gap between the programmer and the language, shorter lead times. There is an old but still relevant paper about [http://www.cse.chalmers.se/~rjmh/Papers/whyfp.html Why Functional Programming Matters] (PDF) by John Hughes. More recently, Sebastian Sylvan wrote an article about [[Why Haskell Matters]].<br />
<br />
Join the [www.reddit.com/r/haskell Haskell subreddit], where we do regular Q&A threads called [[Hask Anything]] (that's the archive).<br />
<br />
There is also a [http://www.haskell.org/haskellwiki/Comparison table comparing Haskell to other functional languages]. Many questions about functional programming are answered by the [http://www.cs.nott.ac.uk/~gmh//faq.html comp.lang.functional FAQ].<br />
<br />
You can ask questions to members of the Haskell community on mailing lists, IRC, or StackOverflow. We recommend installing the [http://www.haskell.org/platform/ Haskell Platform].<br />
<br />
== Training courses ==<br />
<br />
Short training courses aimed at existing programmers<br />
<br />
* [http://www.well-typed.com/services_training On-site and public training courses] by Well-Typed (2-day intro, 2-day advanced, custom on-site courses)<br />
* [http://www.nobleprog.co.uk/haskell/training Public training courses] by NobleProg and Nilcons<br />
* [http://www.cs.ox.ac.uk/softeng/subjects/FPR.html Software Engineering course on Functional Programming] at the University of Oxford (1-week course)<br />
* [http://www.cs.uu.nl/wiki/USCS Summerschool on Applied Functional Programming] at Utrecht University (2-week course)<br />
<br />
== Material for self-study ==<br />
<br />
Below there are links to certain introductory material. If you want to dig deeper, see [[Books and tutorials]].<br />
<br />
=== Textbooks ===<br />
<br />
* [http://www.cs.yale.edu/homes/hudak/SOE/ The Haskell School of Expression]<br />
* [http://www.haskellcraft.com/ Haskell: the Craft of Functional Programming]<br />
* [http://www.prenhall.com/allbooks/ptr_0134843460.html Introduction to Functional Programming using Haskell]<br />
* [http://www.cambridge.org/us/knowledge/isbn/item1129654/Introduction%20to%20Functional%20Programming%20Systems%20Using%20Haskell/?site_locale=en_US An Introduction to Functional Programming Systems Using Haskell]<br />
* [http://www.iro.umontreal.ca/~lapalme/Algorithms-functional.html Algorithms: A functional programming approach]<br />
* [http://homepages.cwi.nl/~jve/HR/ The Haskell Road to Logic, Maths, and Programming] (also freely [http://fldit-www.cs.uni-dortmund.de/~peter/PS07/HR.pdf available online]). <br />
* [http://www.cs.nott.ac.uk/~gmh/book.html Programming in Haskell]<br />
* [http://book.realworldhaskell.org/ Real World Haskell]<br />
* [http://nostarch.com/lyah.htm Learn You a Haskell for Great Good!]<br />
<br />
=== Online tutorials ===<br />
<br />
* [[Meta-tutorial]]<br />
* [http://pluralsight.com/training/Courses/Find?highlight=true&searchTerm=haskell Haskell Fundamentals - get started and learn key concepts] at Pluralsight (2-part, 5 hour online course)<br />
* [http://en.wikibooks.org/wiki/Haskell Haskell Wikibook] A thorough textbook with a step-by-step beginners track assuming no programming background. Also includes many advanced concepts, and adaptations of "Yet Another Haskell Tutorial", "Write Yourself a Scheme in 48 Hours", and "All about monads".<br />
* [http://pub.hal3.name/daume02yaht.pdf YAHT - Yet Another Haskell Tutorial] (good tutorial available online)<br />
* [http://www.cs.ou.edu/~rlpage/fpclassCurrent/textbook/haskell.shtml Two dozen short lessons]<br />
* [http://www.haskell.org/tutorial/ A Gentle Introduction to Haskell] - classic text, but not so gentle really :D<br />
* [ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf Haskell-Tutorial]<br />
* [http://lasche.codingcrew.de/kurse/haskell/hskurs_index.htm Online Haskell Course] (German)<br />
* [http://collection.openlibra.com.s3.amazonaws.com/pdf/haskell_tutorial_for_c_programmers_en.pdf Haskell tutorial for C Programmers]<br />
* [http://learnyouahaskell.com/ Learn You a Haskell for Great Good!] Beautiful, illustrated Haskell tutorial for programmers with less of a functional programming background.<br />
* [http://www.youtube.com/playlist?list=PL2672EBC57C1F5F9B Learning Haskell] Ongoing tutorial in the form of YouTube videos; updates slowly.<br />
*[https://stevekrouse.github.io/hs.js/ Pattern matching, first-class functions, and abstracting over recursion in Haskell], a simulation of the evaluation of map, foldr and foldl.<br />
* [https://www.fpcomplete.com/school?show=tutorials School of Haskell]<br />
<br />
=== Advanced tutorials ===<br />
<br />
* [[Hitchhikers guide to Haskell]]<br />
* [http://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours Write Yourself a Scheme in 48 Hours]<br />
* [http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/ Tackling the Awkward Squad] (on I/O, interfacing to C, concurrency and exceptions)<br />
<br />
=== Debugging/profiling/optimization ===<br />
<br />
=== Monads ===<br />
<br />
* [http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html You Could Have Invented Monads! (And Maybe You Already Have.)]<br />
* [http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf Monads for Functional Programming]<br />
* [http://www.haskell.org/haskellwiki/All_About_Monads All about monads]<br />
* [[IO inside|IO inside: down the Rabbit Hole]]<br />
<br />
=== Type classes ===<br />
<br />
* [http://homepages.inf.ed.ac.uk/wadler/papers/class/class.ps.gz The paper that for the first time introduced type classes and their implementation using dictionaries]<br />
* [[Research papers/Type systems#Type classes|More papers on the type classes]]<br />
<br />
=== Generic programming ===<br />
<br />
* [[Scrap your boilerplate]]<br />
<br />
=== Popular libraries ===<br />
<br />
* ByteStrings?<br />
* [http://legacy.cs.uu.nl/daan/download/parsec/parsec.html Parsec, a fast combinator parser]<br />
* [[Modern array libraries]]<br />
* [http://www.haskell.org/haskellwiki/Gtk2Hs/Tutorials Gtk2Hs, the GUI library]<br />
* [https://ocharles.org.uk/blog/ 24 Days of Hackage] (blog posts about many popular libraries)<br />
<br />
=== Reference ===<br />
<br />
* The official language definition: [[Language and library specification]]<br />
* [http://www.letu.edu/people/jaytevis/Programming-Languages/Haskell/tourofprelude.html Tour of the Haskell Prelude]<br />
* [http://zvon.org/other/haskell/Outputglobal/index.html Haskell Reference]<br />
* Haskell [[Reference card]]<br />
* [http://members.chello.nl/hjgtuyl/tourdemonad.html A tour of the Haskell Monad functions]<br />
* [http://www.cs.uu.nl/wiki/bin/view/Helium/ATourOfTheHeliumPrelude Tour of the Helium Prelude]<br />
* [http://www.cs.kent.ac.uk/people/staff/sjt/craft2e/errors/allErrors.html Some common Hugs error messages]<br />
* [http://cheatsheet.codeslower.com/ The Haskell Cheatsheet] - A reference card and mini-tutorial in one.<br />
* A [http://www.haskell.org/haskellwiki/Category:Glossary Glossary] of common terminology.<br />
<br />
=== Course material ===<br />
* [http://www.cse.chalmers.se/edu/course/TDA555/ Introduction to Functional Programming, Chalmers] (for beginners at programming)<br />
* [http://www.cse.chalmers.se/edu/course/TDA452/ Functional Programming, Chalmers]<br />
* [http://www.cse.chalmers.se/edu/course/afp/ Advanced Functional Programming, Chalmers]<br />
* [http://www.cse.chalmers.se/edu/course/pfp/ Parallel Functional Programming, Chalmers]<br />
* [http://www.shuklan.com/haskell Introduction to Haskell], University of Virginia CS 1501<br />
* [http://www.cs.caltech.edu/courses/cs11/material/haskell/index.html CS 11 Caltech]<br />
* [http://www.cs.uu.nl/docs/vakken/lfp/ Functional programming]: course notes ([http://www.staff.science.uu.nl/~fokke101/courses/fp-eng.pdf English], [http://www.staff.science.uu.nl/~fokke101/courses/fp-nl.pdf Dutch], [http://www.staff.science.uu.nl/~fokke101/courses/fp-sp.pdf Spanish]), slides in Dutch<br />
* [http://www.cse.unsw.edu.au/~cs1011/05s2/ CS1011]: Tutorials, lab exercises and solutions<br />
* Stanford - [http://www.scs.stanford.edu/11au-cs240h/ Functional Systems in Haskell]<br />
<br />
<br />
== Trying Haskell online ==<br />
<br />
There are several websites where you can enter a Haskell program and run it. They are (in no particular order):<br />
* [https://cloud.sagemath.com/ SageMathCloud]<br />
* [https://www.fpcomplete.com/school/using-fphc FP Haskell Center]<br />
* [http://tryhaskell.org/ Try Haskell]<br />
* [http://www.codeworld.info/ Codeworld]<br />
* [http://chrisuehlinger.com/LambdaBubblePop/ Bubble Pop!], the satisfaction of popping bubble wrap, combined with the satisfaction of really elegant functional programming!<br />
* [http://tryplayg.herokuapp.com/ Try Haste & HPlayground client-side framework]; the source code is on [https://github.com/agocorona/tryhplay GitHub]<br />
* [https://koding.com/ Koding] is a cloud based IDE which supports Haskell and several other languages. Free accounts allow one virtual machine.<br />
<br />
To create a browser based environment yourself:<br />
* [http://gibiansky.github.io/IHaskell/ IHaskell]</div>Beerdude26https://wiki.haskell.org/index.php?title=Hask_Anything&diff=59960Hask Anything2015-08-04T06:44:44Z<p>Beerdude26: Created Page</p>
<hr />
<div>A recurring Q&A thread on Reddit.<br />
<br />
* [https://www.reddit.com/r/haskell/comments/3cfax3/new_haskellers_ask_anything/ July 2015]<br />
<br />
* [https://www.reddit.com/r/haskell/comments/3fnvsg/hask_anything_the_thread_where_you_ask_and_we_try/ August 2015]</div>Beerdude26https://wiki.haskell.org/index.php?title=Applications_and_libraries/Operating_system&diff=59959Applications and libraries/Operating system2015-08-04T06:36:46Z<p>Beerdude26: /* Environment */</p>
<hr />
<div>'''Operating Systems and Systems Programming'''<br />
<br />
See also [[Research papers/Program development#Operating systems|research papers on this topic]].<br />
<br />
__TOC__<br />
<br />
== Applications ==<br />
<br />
=== Standalone operating systems ===<br />
<br />
;[http://programatica.cs.pdx.edu/House/ House]<br />
:House is a platform for exploring various ideas relating to low-level and system-level programming in a high-level functional language, or in short for building operating systems in Haskell. [http://web.cecs.pdx.edu/~kennyg/house/ A more up to date, unofficial version.]<br />
<br />
;[http://www.ninj4.net/kinetic/ Kinetic]<br />
:Kinetic is an operating system where the expressiveness of Haskell's type system guides the design of operating system features and facilities.<br />
<br />
=== Filesystems ===<br />
<br />
;[http://www.haskell.org/halfs/ Halfs, the Haskell Filesystem]<br />
:Halfs is a filesystem implemented in Haskell. Halfs can be mounted and used like any other Linux filesystem, or used as a library.<br />
<br />
;[http://okmij.org/ftp/Computation/Continuations.html#zipper-fs ZipperFS] <br />
:Oleg Kiselyov's [[zipper]]-based file server/OS where threading and exceptions are all realized via delimited [[continuation]]s.<br />
<br />
;[http://article.gmane.org/gmane.comp.lang.haskell.general/13585 Debian From Scratch]<br />
:A single, full debian rescue CD. The tool that generates these ISO images (dfsbuild) is written in Haskell.<br />
<br />
=== Hardware emulators ===<br />
<br />
;[http://www.mutantlemon.com/omegagb/ OmegaGB]<br />
:OmegaGB is a Nintendo Game Boy emulator written in Haskell.<br />
<br />
;[http://naesten.dyndns.org:8080/repos/ZMachine/ ZMachine]<br />
:ZMachine is a Z-machine (Infocom's interactive fiction VM) interpreter which currently needs attention to its UI rather badly. This points to the darcs repository, as I suppose you can tell. It uses Gtk2Hs, but it just goes down hill from there. Help welcome! --[[User:SamB|SamB]] 03:40, 6 December 2006 (UTC) (the author)<br />
<br />
=== Window managers ===<br />
<br />
;[http://www.xmonad.org/ xmonad]<br />
:A lightweight X11 window manager.<br />
<br />
;[http://www.bluetile.org Bluetile]<br />
:A tiling window manager based on xmonad which focuses on making the tiling paradigm easily accessible to users coming from traditional window managers by drawing on known conventions and providing both mouse and keyboard access for all features. It also tries to be usable 'out of the box', requiring minimal to no configuration in most cases.<br />
<br />
;[http://article.gmane.org/gmane.comp.lang.haskell.cafe/30804 hswm]<br />
:A non-tiling window manager with a plugin architecture<br />
<br />
=== Shell utilities ===<br />
<br />
;[http://web.comlab.ox.ac.uk/oucl/work/ian.lynagh/haskell-ls/ haskell-ls]<br />
:A (near-)clone of the GNU ls utility.<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/h4sh.html h4sh]<br />
:h4sh provides a set of Haskell List functions as normal unix shell commands. This allows us to use Haskell in shell scripts transparently. Each program is generated from the corresponding Haskell function's type<br />
<br />
=== Package management ===<br />
<br />
;[http://www.haskell.org/haskellwiki/himerge Himerge]<br />
:Luis Araujo's Haskell gui frontend to Emerge<br />
<br />
== Libraries ==<br />
<br />
=== Filesystems ===<br />
<br />
;[http://abridgegame.org/repos/fuse_example Fuse] <br />
:David Roundy's combination of a nice DarcsIO-style filesystem interface on the Haskell side (called FuseIO) with an interface to libfuse (which is a library for creating filesystems from user space on linux).<br />
<br />
;[http://code.haskell.org/hfuse/ hfuse]<br />
:Jeremy Bobbio's fuse bindings.<br />
<br />
;[http://haskell.org/~kolmodin/code/hinotify/ hinotify]<br />
:A library binding to [http://www.kernel.org/pub/linux/kernel/people/rml/inotify/ inotify], providing file system event notification, by simply add a watcher to a file or directory and get an event when it is accessed or modified.<br />
<br />
=== Dynamic linking ===<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/hs-plugins hs-plugins] <br />
:Library support for dynamically loading Haskell modules, as well as compiling source or ''eval'' code fragments at runtime.<br />
<br />
=== Processes ===<br />
<br />
;[http://hackage.haskell.org/package/popenhs-1.0.0 popenhs]<br />
:An old small library, based on runProcess in the standardised posix library. It provides lazy output from subprocesses.<br />
<br />
;[http://freearc.narod.ru/ Process]<br />
:Process is a fun library for easing decomposition of algorithms to several processes, which transmit intermediate data via Unix-like pipes. Each sub-process is just a function started with forkIO/forkOS with one additional parameter-pipe.<br />
<br />
See also [[Libraries and tools/Concurrency and parallelism | concurrency and parallelism]]<br />
<br />
=== Environment ===<br />
<br />
;[http://repetae.net/john/recent/out/GetOptions.html GetOptions]<br />
:This module provides an advanced option parsing routine which can properly parse options depending on what types are infered for them as well as produce a pretty error message with usage info when an incorrect option is used.<br />
<br />
;[https://hackage.haskell.org/package/optparse-applicative Optparse-applicative]<br />
:[http://www.reddit.com/r/haskell/comments/3cfax3/new_haskellers_ask_anything/csv0q4r Discussion on Reddit]. Turtle (see elsewhere on this page) provides a beginner-friendly wrapper: [http://www.reddit.com/r/haskell/comments/3cfax3/new_haskellers_ask_anything/csv5cqe Reddit link]<br />
<br />
=== Time ===<br />
<br />
;[http://semantic.org/TimeLib/ TimeLib]<br />
:TimeLib is an attempt to redesign the current library for handling time (System.Time), balancing expressive functionality and intelligible simplicity. Now at version 0.2, TimeLib features representation of TAI, UTC and UT1, as well as Gregorian, ISO 8601 week, and "year and day" calendars, time-zones, and functions for strftime-style formatting. We'd like to collect some additional documentation on this wiki, too: [[Libraries and tools/Time library | Time library]]<br />
<br />
;[http://www.cs.chalmers.se/~bringert/darcs/parsedate/doc/ ParseDate]<br />
:ParseDate provides a function for parsing dates and times given a date format string.<br />
<br />
;[http://uebb.cs.tu-berlin.de/~magr/projects/rdtsc/doc/ rdtsc]<br />
:Provides the function 'rdtsc' for accessing the time stamp counter on modern IA-32 processors. This is a 64-bit counter which counts the number of ticks since the machine has been powered up. Using this instruction, you can make very precise time measurements.<br />
<br />
=== Terminal ===<br />
;[[Library/VTY]]<br />
: A simple terminal interface library. It provides: handling of suspend/resume, window resizes, minimizes repaint area, automatically decodes keyboard keys into (key,modifier) tuples, and more!<br />
<br />
=== Shell ===<br />
<br />
==== Haskell shell examples ====<br />
<br />
;[http://www.volker-wysk.de/hsshellscript HsShellScript]<br />
:A library for using Haskell for tasks which are usually done by shell scripts, e.g. command line parsing, analysing paths, etc. It can be used also for tasks usually done [http://haskell.org/ghc/docs/latest/html/libraries/base/System-Console-GetOpt.html GetOpt] (a module for GNU-/POSIX-like option handling of commandline arguments). But also for many other things.<br />
<br />
;[http://www.informatik.uni-bonn.de/~ralf/software/examples/Hsh.html Jim Mattson's Hsh Haskell shell]<br />
:on the [http://www.informatik.uni-bonn.de/~ralf/software.html software] page by [http://www.informatik.uni-bonn.de/~ralf/ Ralf Hinze]. Hsh seems to be written in Haskell 1.3.<br />
<br />
;[http://nellardo.com/lang/haskell/hash/ HaSh]<br />
:a nascent project page on a shell scripting system<br />
<br />
;[http://okmij.org/ftp/Computation/monadic-shell.html Monadic i/o and UNIX shell programming]<br />
:UNIX pipes as IO monads.<br />
<br />
;[http://directory.fsf.org/shell-haskell.html shell-haskell]<br />
:start an external shell command asynchronously, write data to its standard input and read results from its standard output. There is a [http://darcs.haskell.org/shell-pipe/ Darcs repository] with a cabalized version.<br />
<br />
;[http://haskell.org/hashell hashell]<br />
:shell with some scripting capabilities to use Haskell as a scripting language.<br />
<br />
;[http://www.haskell.org/pipermail/haskell/2006-June/018059.html Haskell Shell (HSH)]<br />
:HSH, the Haskell shell. Things are still very preliminary in many ways, but this version already lets you:<br />
* Run commands<br />
* Pipe things between commands<br />
* Pipe command input/output into and out of pure Haskell functions<br />
* Pure Haskell functions are as much a first-class citizen as is grep or cat<br />
<br />
;[http://hackage.haskell.org/package/shelly shelly]<br />
: a convenient library for shell scripting using modern (Text) and safe (sytem-filepath) techniques and featuring good error messages.<br />
<br />
;[http://hackage.haskell.org/package/shell-monad shell-monad]<br />
: Procedures written in this monad produce shell scripts.<br />
<br />
;[https://github.com/Gabriel439/Haskell-Turtle-Library Turtle]<br />
: Turtle is a reimplementation of the Unix command line environment in Haskell so that you can use Haskell as a scripting language or a shell.<br />
<br />
;[https://github.com/jekor/hesh Hesh]<br />
: Haskell Extensible Shell, makes writing scripts in Haskell easier. Uses Template Haskell.<br />
<br />
;[https://github.com/chrisdone/hell Hell]<br />
: A simple read-eval-print (REPL) loop for Haskell that has some simple awareness of the current directory and completion works.<br />
<br />
;[https://github.com/cpennington/h4sh h4sh]<br />
: Makes Haskell functions available as unix shell commands.<br />
<br />
==== Link collections on pure functional shells ====<br />
<br />
* [http://lambda-the-ultimate.org/classic/message9846.html on Lambda the Ultimate]<br />
* [http://www.cse.unsw.edu.au/~pls/thesis-topics/functionalshell.html Thesis Topic: The Design and Implementation of a Functional Shell]<br />
<br />
=== File utilities ===<br />
<br />
;[http://quux.org/devel/magic-haskell magic-haskell]<br />
:magic-haskell is a binding to the libmagic library. With magic-haskell, you can determine the type of a file by looking at its contents rather than its name. This library also can yield the MIME type of a file by looking at its contents.<br />
<br />
;[http://darcs.haskell.org/iff IFF parser]<br />
:Parse files in the [http://www.digitalpreservation.gov/formats/fdd/fdd000115.shtml#identification Interchange File Format] by Electronic Arts as used in AIFF, ILBM, 8SVX and so on. Creation of IFF files is planned.<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/code/icfp05/MkTemp.hs mkstemps]<br />
:A Haskell reimplementation of the C mktemp/mkstemp/mkstemps library from OpenBSD<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/haskell-1990-2000/msg04763.html Unix.hs]<br />
:Koen Claessen's binding to common unix functions.<br />
<br />
;[http://hackage.haskell.org/cgi-bin/hackage-scripts/package/FileManip-0.1 FileManip]<br />
:A Haskell library for working with files and directories. Includes code for pattern matching, finding files, modifying file contents, and more.<br />
<br />
=== Logging ===<br />
<br />
;[http://repetae.net/john/recent/out/ErrorLog.html ErrorLog]<br />
:Manages an error log with proper locking. has a number of useful routines for detecting and reporting erronious conditions.<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/code/icfp05/Logging.hs Logging]<br />
:Multiple debug levels and log file support<br />
<br />
{{LibrariesPage}}</div>Beerdude26https://wiki.haskell.org/index.php?title=Applications_and_libraries/Operating_system&diff=59958Applications and libraries/Operating system2015-08-04T06:33:21Z<p>Beerdude26: /* Environment */ optparse-applicative</p>
<hr />
<div>'''Operating Systems and Systems Programming'''<br />
<br />
See also [[Research papers/Program development#Operating systems|research papers on this topic]].<br />
<br />
__TOC__<br />
<br />
== Applications ==<br />
<br />
=== Standalone operating systems ===<br />
<br />
;[http://programatica.cs.pdx.edu/House/ House]<br />
:House is a platform for exploring various ideas relating to low-level and system-level programming in a high-level functional language, or in short for building operating systems in Haskell. [http://web.cecs.pdx.edu/~kennyg/house/ A more up to date, unofficial version.]<br />
<br />
;[http://www.ninj4.net/kinetic/ Kinetic]<br />
:Kinetic is an operating system where the expressiveness of Haskell's type system guides the design of operating system features and facilities.<br />
<br />
=== Filesystems ===<br />
<br />
;[http://www.haskell.org/halfs/ Halfs, the Haskell Filesystem]<br />
:Halfs is a filesystem implemented in Haskell. Halfs can be mounted and used like any other Linux filesystem, or used as a library.<br />
<br />
;[http://okmij.org/ftp/Computation/Continuations.html#zipper-fs ZipperFS] <br />
:Oleg Kiselyov's [[zipper]]-based file server/OS where threading and exceptions are all realized via delimited [[continuation]]s.<br />
<br />
;[http://article.gmane.org/gmane.comp.lang.haskell.general/13585 Debian From Scratch]<br />
:A single, full debian rescue CD. The tool that generates these ISO images (dfsbuild) is written in Haskell.<br />
<br />
=== Hardware emulators ===<br />
<br />
;[http://www.mutantlemon.com/omegagb/ OmegaGB]<br />
:OmegaGB is a Nintendo Game Boy emulator written in Haskell.<br />
<br />
;[http://naesten.dyndns.org:8080/repos/ZMachine/ ZMachine]<br />
:ZMachine is a Z-machine (Infocom's interactive fiction VM) interpreter which currently needs attention to its UI rather badly. This points to the darcs repository, as I suppose you can tell. It uses Gtk2Hs, but it just goes down hill from there. Help welcome! --[[User:SamB|SamB]] 03:40, 6 December 2006 (UTC) (the author)<br />
<br />
=== Window managers ===<br />
<br />
;[http://www.xmonad.org/ xmonad]<br />
:A lightweight X11 window manager.<br />
<br />
;[http://www.bluetile.org Bluetile]<br />
:A tiling window manager based on xmonad which focuses on making the tiling paradigm easily accessible to users coming from traditional window managers by drawing on known conventions and providing both mouse and keyboard access for all features. It also tries to be usable 'out of the box', requiring minimal to no configuration in most cases.<br />
<br />
;[http://article.gmane.org/gmane.comp.lang.haskell.cafe/30804 hswm]<br />
:A non-tiling window manager with a plugin architecture<br />
<br />
=== Shell utilities ===<br />
<br />
;[http://web.comlab.ox.ac.uk/oucl/work/ian.lynagh/haskell-ls/ haskell-ls]<br />
:A (near-)clone of the GNU ls utility.<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/h4sh.html h4sh]<br />
:h4sh provides a set of Haskell List functions as normal unix shell commands. This allows us to use Haskell in shell scripts transparently. Each program is generated from the corresponding Haskell function's type<br />
<br />
=== Package management ===<br />
<br />
;[http://www.haskell.org/haskellwiki/himerge Himerge]<br />
:Luis Araujo's Haskell gui frontend to Emerge<br />
<br />
== Libraries ==<br />
<br />
=== Filesystems ===<br />
<br />
;[http://abridgegame.org/repos/fuse_example Fuse] <br />
:David Roundy's combination of a nice DarcsIO-style filesystem interface on the Haskell side (called FuseIO) with an interface to libfuse (which is a library for creating filesystems from user space on linux).<br />
<br />
;[http://code.haskell.org/hfuse/ hfuse]<br />
:Jeremy Bobbio's fuse bindings.<br />
<br />
;[http://haskell.org/~kolmodin/code/hinotify/ hinotify]<br />
:A library binding to [http://www.kernel.org/pub/linux/kernel/people/rml/inotify/ inotify], providing file system event notification, by simply add a watcher to a file or directory and get an event when it is accessed or modified.<br />
<br />
=== Dynamic linking ===<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/hs-plugins hs-plugins] <br />
:Library support for dynamically loading Haskell modules, as well as compiling source or ''eval'' code fragments at runtime.<br />
<br />
=== Processes ===<br />
<br />
;[http://hackage.haskell.org/package/popenhs-1.0.0 popenhs]<br />
:An old small library, based on runProcess in the standardised posix library. It provides lazy output from subprocesses.<br />
<br />
;[http://freearc.narod.ru/ Process]<br />
:Process is a fun library for easing decomposition of algorithms to several processes, which transmit intermediate data via Unix-like pipes. Each sub-process is just a function started with forkIO/forkOS with one additional parameter-pipe.<br />
<br />
See also [[Libraries and tools/Concurrency and parallelism | concurrency and parallelism]]<br />
<br />
=== Environment ===<br />
<br />
;[http://repetae.net/john/recent/out/GetOptions.html GetOptions]<br />
:This module provides an advanced option parsing routine which can properly parse options depending on what types are infered for them as well as produce a pretty error message with usage info when an incorrect option is used.<br />
<br />
;[https://hackage.haskell.org/package/optparse-applicative Optparse-applicative]<br />
:[http://www.reddit.com/r/haskell/comments/3cfax3/new_haskellers_ask_anything/csv0q4r Discussion on Reddit]<br />
<br />
=== Time ===<br />
<br />
;[http://semantic.org/TimeLib/ TimeLib]<br />
:TimeLib is an attempt to redesign the current library for handling time (System.Time), balancing expressive functionality and intelligible simplicity. Now at version 0.2, TimeLib features representation of TAI, UTC and UT1, as well as Gregorian, ISO 8601 week, and "year and day" calendars, time-zones, and functions for strftime-style formatting. We'd like to collect some additional documentation on this wiki, too: [[Libraries and tools/Time library | Time library]]<br />
<br />
;[http://www.cs.chalmers.se/~bringert/darcs/parsedate/doc/ ParseDate]<br />
:ParseDate provides a function for parsing dates and times given a date format string.<br />
<br />
;[http://uebb.cs.tu-berlin.de/~magr/projects/rdtsc/doc/ rdtsc]<br />
:Provides the function 'rdtsc' for accessing the time stamp counter on modern IA-32 processors. This is a 64-bit counter which counts the number of ticks since the machine has been powered up. Using this instruction, you can make very precise time measurements.<br />
<br />
=== Terminal ===<br />
;[[Library/VTY]]<br />
: A simple terminal interface library. It provides: handling of suspend/resume, window resizes, minimizes repaint area, automatically decodes keyboard keys into (key,modifier) tuples, and more!<br />
<br />
=== Shell ===<br />
<br />
==== Haskell shell examples ====<br />
<br />
;[http://www.volker-wysk.de/hsshellscript HsShellScript]<br />
:A library for using Haskell for tasks which are usually done by shell scripts, e.g. command line parsing, analysing paths, etc. It can be used also for tasks usually done [http://haskell.org/ghc/docs/latest/html/libraries/base/System-Console-GetOpt.html GetOpt] (a module for GNU-/POSIX-like option handling of commandline arguments). But also for many other things.<br />
<br />
;[http://www.informatik.uni-bonn.de/~ralf/software/examples/Hsh.html Jim Mattson's Hsh Haskell shell]<br />
:on the [http://www.informatik.uni-bonn.de/~ralf/software.html software] page by [http://www.informatik.uni-bonn.de/~ralf/ Ralf Hinze]. Hsh seems to be written in Haskell 1.3.<br />
<br />
;[http://nellardo.com/lang/haskell/hash/ HaSh]<br />
:a nascent project page on a shell scripting system<br />
<br />
;[http://okmij.org/ftp/Computation/monadic-shell.html Monadic i/o and UNIX shell programming]<br />
:UNIX pipes as IO monads.<br />
<br />
;[http://directory.fsf.org/shell-haskell.html shell-haskell]<br />
:start an external shell command asynchronously, write data to its standard input and read results from its standard output. There is a [http://darcs.haskell.org/shell-pipe/ Darcs repository] with a cabalized version.<br />
<br />
;[http://haskell.org/hashell hashell]<br />
:shell with some scripting capabilities to use Haskell as a scripting language.<br />
<br />
;[http://www.haskell.org/pipermail/haskell/2006-June/018059.html Haskell Shell (HSH)]<br />
:HSH, the Haskell shell. Things are still very preliminary in many ways, but this version already lets you:<br />
* Run commands<br />
* Pipe things between commands<br />
* Pipe command input/output into and out of pure Haskell functions<br />
* Pure Haskell functions are as much a first-class citizen as is grep or cat<br />
<br />
;[http://hackage.haskell.org/package/shelly shelly]<br />
: a convenient library for shell scripting using modern (Text) and safe (sytem-filepath) techniques and featuring good error messages.<br />
<br />
;[http://hackage.haskell.org/package/shell-monad shell-monad]<br />
: Procedures written in this monad produce shell scripts.<br />
<br />
;[https://github.com/Gabriel439/Haskell-Turtle-Library Turtle]<br />
: Turtle is a reimplementation of the Unix command line environment in Haskell so that you can use Haskell as a scripting language or a shell.<br />
<br />
;[https://github.com/jekor/hesh Hesh]<br />
: Haskell Extensible Shell, makes writing scripts in Haskell easier. Uses Template Haskell.<br />
<br />
;[https://github.com/chrisdone/hell Hell]<br />
: A simple read-eval-print (REPL) loop for Haskell that has some simple awareness of the current directory and completion works.<br />
<br />
;[https://github.com/cpennington/h4sh h4sh]<br />
: Makes Haskell functions available as unix shell commands.<br />
<br />
==== Link collections on pure functional shells ====<br />
<br />
* [http://lambda-the-ultimate.org/classic/message9846.html on Lambda the Ultimate]<br />
* [http://www.cse.unsw.edu.au/~pls/thesis-topics/functionalshell.html Thesis Topic: The Design and Implementation of a Functional Shell]<br />
<br />
=== File utilities ===<br />
<br />
;[http://quux.org/devel/magic-haskell magic-haskell]<br />
:magic-haskell is a binding to the libmagic library. With magic-haskell, you can determine the type of a file by looking at its contents rather than its name. This library also can yield the MIME type of a file by looking at its contents.<br />
<br />
;[http://darcs.haskell.org/iff IFF parser]<br />
:Parse files in the [http://www.digitalpreservation.gov/formats/fdd/fdd000115.shtml#identification Interchange File Format] by Electronic Arts as used in AIFF, ILBM, 8SVX and so on. Creation of IFF files is planned.<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/code/icfp05/MkTemp.hs mkstemps]<br />
:A Haskell reimplementation of the C mktemp/mkstemp/mkstemps library from OpenBSD<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/haskell-1990-2000/msg04763.html Unix.hs]<br />
:Koen Claessen's binding to common unix functions.<br />
<br />
;[http://hackage.haskell.org/cgi-bin/hackage-scripts/package/FileManip-0.1 FileManip]<br />
:A Haskell library for working with files and directories. Includes code for pattern matching, finding files, modifying file contents, and more.<br />
<br />
=== Logging ===<br />
<br />
;[http://repetae.net/john/recent/out/ErrorLog.html ErrorLog]<br />
:Manages an error log with proper locking. has a number of useful routines for detecting and reporting erronious conditions.<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/code/icfp05/Logging.hs Logging]<br />
:Multiple debug levels and log file support<br />
<br />
{{LibrariesPage}}</div>Beerdude26https://wiki.haskell.org/index.php?title=Foreign_Function_Interface&diff=59957Foreign Function Interface2015-08-04T06:19:30Z<p>Beerdude26: /* References */ Added Ivory reference</p>
<hr />
<div>[[Category:FFI]]<br />
<br />
== Introduction ==<br />
<br />
The Foreign Function Interface (FFI) allows Haskell programs to cooperate with programs written with other languages. Haskell programs can call foreign functions and foreign functions can call Haskell code.<br />
<br />
Compared to many other languages, Haskell FFI is very easy to use: in the most common case, you only have to translate the prototype of the foreign function into the equivalent Haskell prototype and you're done. For instance, to call the exponential function ("exp") of the libc, you only have to translate its prototype:<br />
<br />
<source lang="c"><br />
double exp(double);<br />
</source><br />
<br />
into the following Haskell code<br />
<br />
<haskell><br />
foreign import ccall "exp" c_exp :: Double -> Double<br />
</haskell><br />
<br />
Now you can use the function "c_exp" just like any other Haskell function. When evaluated, it will call "exp".<br />
<br />
Similarly, to export the following Haskell function:<br />
<br />
<haskell><br />
triple :: Int -> Int<br />
triple x = 3*x<br />
</haskell><br />
<br />
so that it can be used in foreign codes, you only have to write:<br />
<br />
<haskell><br />
foreign export ccall triple :: Int -> Int<br />
</haskell><br />
<br />
It can get at little more complicated depending on what you want to do, the function parameters, the foreign code you target, etc. This page is here to explain all of this to you.<br />
<br />
== Generalities ==<br />
<br />
=== FFI extension ===<br />
<br />
The Foreign Function Interface (FFI) is an extension to the Haskell standard. To use it, you need to enable it with the following compiler pragma at the beginning of your source file:<br />
<br />
<haskell><br />
{-# LANGUAGE ForeignFunctionInterface #-}<br />
</haskell><br />
<br />
=== Calling conventions ===<br />
<br />
When a program (in any language) is compiled into machine code, functions and procedures become labels: a label is a symbol (a string) associated to a position into the machine code. Calling a function only consists in putting parameters at appropriate places into memory and registers and then branching at the label position. The caller needs to know where to store parameters and the callee needs to know where to retrieve parameters from: there is a '''calling convention'''.<br />
<br />
To interact with foreign code, you need to know the calling conventions that are used by the other language implementation on the given architecture. It can also depend on the operating system.<br />
<br />
GHC supports standard calling conventions with the FFI: it can generate code to convert between its internal (non-standard) convention and the foreign one. If we consider the previous example:<br />
<br />
<haskell><br />
foreign import ccall "exp" c_exp :: Double -> Double<br />
</haskell><br />
<br />
we see that the C calling convention ("ccall") is used. GHC will generate code to put (and to retrieve) parameters into memory and registers conforming to what is expected by a code generated with a C compiler (or any other compiler conforming to this convention).<br />
<br />
Other available conventions supported by GHC include "stdcall" (i.e. Pascal convention).<br />
<br />
=== Foreign types ===<br />
<br />
Calling conventions depend on parameter types. For instance, floating-point values (Double, Float) may be passed into floating-point registers. Several values can be combined into a single vector register. And so on. As an example, in http://www.x86-64.org/documentation/abi.pdf you can find the algorithm describing how to pass parameters to functions on Linux on a x86-64 architecture depending on the types of the parameters.<br />
<br />
Only some Haskell types can be directly used as parameters for foreign functions, because they correspond to basic types of low-level languages such as C and are used to define calling conventions.<br />
<br />
According to [https://hackage.haskell.org/package/base/docs/Foreign-Ptr.html#g:2], the type of a foreign function is a ''foreign type'', that is a function type with zero or more arguments where:<br />
* the argument types can be ''marshallable foreign types'', i.e. Char, Int, Double, Float, Bool, Int8, Int16, Int32, Int64, Word8, Word16, Word32, Word64, Ptr a, FunPtr a, StablePtr a or a renaming of any of these using newtype.<br />
* the return type is either a ''marshallable foreign type'' or has the form IO t where t is a marshallable foreign type or ().<br />
<br />
'''Warning''': GHC does not support passing structures as values yet.<br />
<br />
The [http://hackage.haskell.org/package/base/docs/Foreign-C-Types.html Foreign.C.Types] module contains renaming of some of these ''marshallable foreign types'' with names closer to those of C types (e.g. CLong, CShort).<br />
<br />
If the foreign function performs side-effects, you have to explicitly indicate it in its type (using IO). GHC has no other way to detect it.<br />
<br />
<haskell><br />
foreign import ccall "my_func" myFunc :: Int -> IO Double<br />
</haskell><br />
<br />
Data structures have to passed by reference (using pointers). We will see how to use them later in this document.<br />
<br />
=== Exported functions ===<br />
<br />
GHC can generate wrappers so that a foreign code can call Haskell code:<br />
<br />
<haskell><br />
triple :: Int -> Int<br />
triple x = 3*x<br />
<br />
foreign export ccall triple :: Int -> Int<br />
</haskell><br />
<br />
In the generated binary object, there will be a label "triple" that can be called from a language using the C convention.<br />
<br />
Note that to call a Haskell function, the runtime system must have been initialized with a call to "hs_init". It must be released with a call to "hs_exit" when it is no longer required.<br />
<br />
See the [https://www.haskell.org/ghc/docs/latest/html/users_guide/ffi.html user guide] for more details.<br />
<br />
== Function pointers ==<br />
<br />
Sometimes you want to manipulate foreign pointers to foreign functions: these are [https://hackage.haskell.org/package/base/docs/Foreign-Ptr.html#g:2 FunPtr] in Haskell.<br />
<br />
You can get a function pointer by using "&" before a foreign function symbol:<br />
<haskell><br />
foreign import ccall "&exp" a_exp :: FunPtr (Double -> Double)<br />
</haskell><br />
<br />
Some foreign functions can also return function pointers.<br />
<br />
To call a function pointer from Haskell, GHC needs to convert between its own calling convention and the one expected by the foreign code. To create a function doing this conversion, you must use "dynamic" wrappers:<br />
<br />
<haskell><br />
foreign import ccall "dynamic" mkFun :: FunPtr (Double -> Double) -> (Double -> Double)<br />
</haskell><br />
<br />
Then you can apply this wrapper to a FunPtr to get a Haskell function:<br />
<br />
<haskell><br />
c_exp :: Double -> Double<br />
c_exp = mkFun a_exp<br />
</haskell><br />
<br />
You can also perform the opposite operation to give to the foreign code a pointer to a Haskell function. You need a "wrapper" wrapper. GHC generates the callable code to execute the wrapped Haskell closure with the appropriate calling convention and returns a pointer (FunPtr) on it. You have to release the generated code explicitly with `freeHaskellFunPtr` to avoid memory leaks: GHC has no way to know if the function pointer is still referenced in some foreign code, hence it doesn't collect it.<br />
<br />
<haskell><br />
add :: Int -> Int<br />
add = x+y<br />
<br />
foreign import ccall "wrapper" createAddPtr :: (Int -> Int) -> IO (FunPtr (Int -> Int))<br />
<br />
main = do<br />
addPtr <- createAddPtr add<br />
-- you can use addPtr like any other FunPtr (e.g. give it to foreign code)<br />
...<br />
-- you MUST free the FunPtr, otherwise it won't be collected<br />
freeHaskellFunPtr addPtr<br />
</haskell><br />
<br />
== Marshalling data ==<br />
<br />
In Haskell we are accustomed to let the runtime system -- especially the garbage collector -- manage memory. When we use the FFI, however, we sometimes need to do some manual memory management to comply with the data representations of the foreign codes. Hopefully, Haskell makes it very easy to manipulate low-level objects such as pointers. Moreover, many useful Haskell tools have been designed to simplify conversions between data representations.<br />
<br />
=== Pointers ===<br />
<br />
A pointer is an offset in memory. In Haskell, it is represented with the [https://hackage.haskell.org/package/base/docs/Foreign-Ptr.html Ptr a] data type. Where "a" is a phantom type that can be used to differentiate two pointers. You can think of "Ptr Stuff" as being equivalent to a "Stuff *" type in C (i.e. a pointer to a "Stuff" data). This analogy may not hold if "a" is a Haskell type not representable in the foreign language. For instance, you can have a pointer with the type "Ptr (Stuff -> OtherStuff)" but it is not function pointer in the foreign language: it is just a pointer tagged with the "Stuff -> OtherStuff" type.<br />
<br />
You can easily cast between pointer types using `castPtr` or perform pointer arithmetic using `plusPtr`, `minusPtr` and `alignPtr`. NULL pointer is represented with `nullPtr`.<br />
<br />
=== Memory allocation ===<br />
<br />
There are basically two ways to allocate memory:<br />
* on the Haskell heap, using `alloca*` functions in [https://hackage.haskell.org/package/base/docs/Foreign-Marshal-Alloc.html Foreign.Marshal.Alloc]<br />
<br />
The allocation is ephemeral: it lasts the time of the execution of an IO action, as in the following example:<br />
<haskell><br />
do<br />
allocaBytes 128 $ \ptr -> do<br />
-- do stuff with the pointer ptr...<br />
-- ...<br />
-- do not return "ptr" in any way because it will become an invalid pointer<br />
-- here the 128 bytes have been released and should not be accessed<br />
</haskell><br />
<br />
* on the "low-level" heap (the same as the runtime system uses), using `malloc*` functions in [https://hackage.haskell.org/package/base/docs/Foreign-Marshal-Alloc.html Foreign.Marshal.Alloc]<br />
<br />
Allocations on the low-level heap are not managed by the Haskell implementation and must be freed explicitly with `free`.<br />
<br />
<haskell><br />
do<br />
ptr <- mallocBytes 128<br />
-- do stuff with the pointer ptr...<br />
-- ...<br />
free ptr<br />
-- here the 128 bytes have been released and should not be accessed<br />
</haskell><br />
<br />
=== Foreign Pointers ===<br />
<br />
An hybrid approach is to use [https://hackage.haskell.org/package/base/docs/Foreign-ForeignPtr.html#t:ForeignPtr ForeignPtr]. Foreign pointers are similar to Ptr except that they have finalizers (i.e. actions) attached to them. When the garbage collector detects that a ForeignPtr is no longer accessible, it executes its associated finalizers. A basic finalizer is `finalizerFree` [https://hackage.haskell.org/package/base/docs/Foreign-Marshal-Alloc.html] that calls `free` on the pointer.<br />
<br />
You can convert a Ptr into a ForeignPtr using `newForeignPtr`, add additional finalizers, etc. [https://hackage.haskell.org/package/base/docs/Foreign-ForeignPtr.html#t:ForeignPtr].<br />
<br />
In the following example, we use `mallocForeignPtrBytes`. It is equivalent to call `malloc` and then to associate the `finalizerFree` finalizer with `newForeignPtr`. GHC has optimized implementations for `mallocForeignPtr*` functions, hence they should be preferred.<br />
<br />
<haskell><br />
do<br />
ptr <- mallocForeignPtrBytes 128<br />
-- do stuff with the pointer ptr...<br />
-- ...<br />
--<br />
-- ptr is freed when it is collected<br />
</haskell><br />
<br />
=== Using pointers: Storable instances ===<br />
<br />
You often want to read or to write at the address a of pointer. Reading consists in obtaining a Haskell value from a pointer; writing consists in somehow writing a representation of the Haskell value at the pointed address. Writing and reading a value depends on the type of the value, hence these methods are encapsulated into the [https://hackage.haskell.org/package/base/docs/Foreign-Storable.html Storable] type class.<br />
<br />
For any type T such that it exists a Storable T instance:<br />
* you can read a value, using <haskell>peek :: Ptr T -> IO T</haskell><br />
* you can write a value, using <haskell>poke :: Ptr T -> T -> IO ()</haskell><br />
<br />
`Storable a` also defines a `sizeOf :: a -> Int` method that returns the size of the stored value in bytes.<br />
<br />
All the ''marshallable foreign types'' (i.e. basic types) have Storable instances. Hence we can use these to write new Storable instances for more involved data types. In the following example, we create a Storable instance for a Complex data type:<br />
<br />
<haskell><br />
data Complex = Complex Double Double<br />
<br />
instance Storable Complex where<br />
sizeOf _ = 2 * sizeOf (undefined :: Double) -- stored complex size = 2 * size of a stored Double<br />
peek ptr = do<br />
real <- peek ptr<br />
img <- peekByteOff ptr (sizeOf real) -- we skip the bytes containing the real part<br />
return $ Complex real img<br />
poke ptr (Complex real img) = do<br />
poke ptr real<br />
pokeByteOff ptr (sizeOf real) img<br />
...<br />
</haskell><br />
<br />
This is not very complicated but it can become very cumbersome if our data type has many fields. Several tools have been developed to automatically or semi-automatically create the Storable instances.<br />
<br />
==== Renaming and Storable instances ====<br />
<br />
It is very common to use type renaming (i.e. newtype) to wrap a data type as in the following example, where we declare a type Pair that contains a pair of Double values just like our Complex type.<br />
<br />
<haskell><br />
newtype Pair = Pair Complex<br />
</haskell><br />
<br />
If we want to store Pair values exactly like Complex values, we have several possibilities:<br />
* unwrap the Complex value each time we want to use its Storable instance<br />
* create a new Storable Pair instance that does the same thing<br />
* automatically derive the Storable Pair instance<br />
<br />
The last solution is obviously the simplest one. It requires an extension however:<br />
<br />
<haskell><br />
{-# LANGUAGE GeneralizedNewtypeDeriving #-}<br />
<br />
newtype Pair = Pair Complex deriving (Storable)<br />
</haskell><br />
<br />
=== Arrays ===<br />
<br />
It is very common to read and to write arrays of values. [http://hackage.haskell.org/package/base/docs/Foreign-Marshal-Array.html Foreign.Marshal.Array] provides many functions to deal with pointers to arrays. You can easily write an Haskell list of storable values as an array of values, and vice versa.<br />
<br />
=== Strings ===<br />
<br />
Strings in Haskell are lists of Char, where Char represents a unicode character. Many foreign codes use the C representation for strings ([https://hackage.haskell.org/package/base/docs/Foreign-C-String.htm CString] in Haskell): an array of bytes where each byte is a extended ASCII character terminated with a NUL character.<br />
<br />
In [https://hackage.haskell.org/package/base/docs/Foreign-C-String.html Foreign.C.String], you have many functions to convert between both representations. Be careful because Unicode characters outside of the ASCII range may not be representable with the C representation.<br />
<br />
<haskell><br />
foreign import ccall "echo" c_echo :: CString -> IO ()<br />
<br />
echo :: String -> IO ()<br />
echo str = withCString str $ \c_str -><br />
c_echo c_str<br />
</haskell><br />
<br />
=== Data structures ===<br />
<br />
Marshalling data structures of foreign languages is the most cumbersome task: you have to find out the offset of each field of the data structure (considering padding bytes, etc.). Hopefully, there are Haskell tools to help with this task.<br />
<br />
Suppose you have a C data structure like this:<br />
struct MyStruct {<br />
double d;<br />
char c;<br />
int32_t i;<br />
};<br />
<br />
And its Haskell counterpart:<br />
<haskell><br />
data MyStruct = MyStruct<br />
{ d :: Double<br />
, c :: Word8<br />
, i :: Int32<br />
}<br />
</haskell><br />
<br />
The following sub-sections present the different ways to write the Storable instance for MyStruct. <br />
<br />
The following header is implied:<br />
<haskell><br />
import Control.Applicative ((<$>), (<*>))<br />
import Foreign.Storable<br />
</haskell><br />
<br />
==== The explicit way ====<br />
<br />
<haskell><br />
instance Storable MyStruct where<br />
alignment _ = 8<br />
sizeOf _ = 16<br />
peek ptr = MyStruct<br />
<$> peekByteOff ptr 0<br />
<*> peekByteOff ptr 8<br />
<*> peekByteOff ptr 12 -- skip padding bytes after "c"<br />
poke ptr (MyStruct d c i) = do<br />
pokeByteOff ptr 0 d<br />
pokeByteOff ptr 8 c<br />
pokeByteOff ptr 12 i<br />
</haskell><br />
<br />
* The structure alignment is the least common multiple of the alignments of the structure fields. The alignment of primitive types is equal to their size in bytes (e.g. 8 for Double, 1 for Word8 and 4 for Word32). Hence the alignment for MyStruct is 8.<br />
<br />
* We indicate the offset of each field explicitly for peek and poke methods. We introduce padding bytes to align the "i" field (Word32) on 4 bytes. A C compiler does the same thing (except for packed structures).<br />
<br />
* The size of the structure is the total number of bytes, including padding bytes between fields.<br />
<br />
==== hsc2hc ====<br />
<br />
[https://www.haskell.org/ghc/docs/latest/html/users_guide/hsc2hs.html hsc2hs] is a tool that can help you compute field offsets by using C headers directly.<br />
<br />
Save your Haskell file with a .hsc extension to enable the support of hsc2hs.<br />
<br />
<haskell><br />
#include <myheader.h><br />
<br />
instance Storable MyStruct where<br />
peek ptr = MyStruct<br />
<$> (#peek MyStruct, d) ptr<br />
<*> (#peek MyStruct, c) ptr<br />
<*> (#peek MyStruct, i) ptr<br />
...<br />
</haskell><br />
<br />
==== c2hs ====<br />
<br />
[https://hackage.haskell.org/package/c2hs c2hs] is another tool that can help you doing the same thing as hsc2hs for data structure marshalling. [http://stackoverflow.com/questions/6009031/difference-between-hsc2hs-and-c2hs They have differences in other aspects though].<br />
<br />
<haskell><br />
#include <myheader.h><br />
<br />
instance Storable MyStruct where<br />
peek ptr = MyStruct<br />
<$> {#get MyStruct->d} ptr<br />
<*> {#get MyStruct->c} ptr<br />
<*> {#get MyStruct->i} ptr<br />
...<br />
</haskell><br />
<br />
<br />
==== c-storable-deriving ====<br />
<br />
You can also derive the Storable instances from the types of the fields and their order in the Haskell data type by using [https://hackage.haskell.org/package/c-storable-deriving c-storable-deriving package].<br />
<br />
<haskell><br />
{-# LANGUAGE DeriveGeneric #-}<br />
<br />
import GHC.Generics (Generic)<br />
import Foreign.CStorable<br />
<br />
data MyStruct = MyStruct {...} deriving (Generic)<br />
<br />
instance CStorable MyStruct<br />
<br />
instance Storable MyStruct where<br />
sizeOf = cSizeOf<br />
alignment = cAlignment<br />
poke = cPoke<br />
peek = cPeek<br />
</haskell><br />
<br />
The CStorable type-class is equivalent to the Storable type-class but has additional default implementations for its methods if the type has an instance of Generic.<br />
<br />
=== Pointers to Haskell data ===<br />
<br />
In some cases, you may want to give to the foreign code an opaque reference to a Haskell value that you will retrieve later on. You need to be sure that the value is not collected between the time you give it and the time you retrieve it. [https://hackage.haskell.org/package/base/docs/Foreign-StablePtr.html Stable pointers] have been created exactly to do this. You can wrap a value into a StablePtr and give it to the foreign code (StablePtr is one of the marshallable foreign types).<br />
<br />
You need to manually free stable pointers using `freeStablePtr` when they are not required anymore.<br />
<br />
== Tools ==<br />
<br />
There are several tools to help writing bindings using the FFI. In particular by using C headers.<br />
<br />
=== Using C headers ===<br />
* [https://www.haskell.org/ghc/docs/latest/html/users_guide/hsc2hs.html hsc2hs]<br />
* [https://hackage.haskell.org/package/c2hs c2hs]<br />
* [http://hackage.haskell.org/package/bindings-DSL-1.0.22 bindings-DSL]<br />
* [http://hackage.haskell.org/package/c2hsc c2hsc]<br />
<br />
=== Haskell ===<br />
* [https://hackage.haskell.org/package/c-storable-deriving c-storable-deriving]<br />
<br />
=== Dynamic function call ===<br />
* [https://hackage.haskell.org/package/libffi libffi]: allow to call a C function without knowing its type at compile time.<br />
<br />
== Linking ==<br />
<br />
There are several ways for GHC to find the foreign code to link with:<br />
* Static linking: the foreign code binary object is merged with the Haskell one to form the final executable<br />
* Dynamic linking: the generated binary uses libraries (e.g. .dll or .so) that are automatically linked when the binary is executed<br />
* Explicit dynamic linking: the Haskell code explicitly loads libraries, finds symbols in them and makes calls to them<br />
<br />
The first two modes are well described in GHC and Cabal manuals. For the last one, you need to use platform dependent methods:<br />
* on UNIX, you can use [http://hackage.haskell.org/package/unix/docs/System-Posix-DynamicLinker.html System.Posix.DynamicLinker]<br />
<br />
Explicit dynamic linking helps you obtaining [https://hackage.haskell.org/package/base/docs/Foreign-Ptr.html#g:2 function pointers (FunPtr)]. You need to write "dynamic" wrappers to call the functions from Haskell.<br />
<br />
=== Dynamic linker template ===<br />
<br />
[https://hackage.haskell.org/package/dynamic-linker-template dynamic-linker-template] is a package that uses template Haskell to automatically generate "dynamic" wrappers for explicit dynamic linking (only supporting Unix for now).<br />
<br />
The idea is that a library is like a record containing functions, hence it is easy to generate the code that load symbols from a library and store them into a Haskell record.<br />
<br />
In the following code, the record matching library symbols is the data type MyLib. The generated code will apply "myModifier" to each field name of the record to find corresponding symbols in the library. myModifier should often be "id" but it is sometimes useful when symbols are not pretty. Here in the foreign code "_v2" is appended at the end of each symbol to avoid symbol clashes with the first version of the library.<br />
<br />
The package supports optional symbols: functions that may or may not be present in the library. These optional functions are represented by encapsulating the function type into Maybe.<br />
<br />
The `libHandle` field is mandatory and contains a pointer to the loaded library. You can use it to unload the library.<br />
<br />
A function called `loadMyLib` is generated to load symbols from a library, wrap them using "dynamic" wrappers and store them into a MyLib value that is returned.<br />
<br />
<haskell><br />
{-# LANGUAGE TemplateHaskell, ForeignFunctionInterface #-}<br />
<br />
import System.Posix.DynamicLinker.Template<br />
<br />
data MyLib = MyLib {<br />
libHandle :: DL,<br />
thing1 :: Double -> IO Int, -- Mandatory symbol<br />
thing2 :: Maybe (Int -> Int -> Int) -- Optional symbol<br />
}<br />
<br />
myModifier :: String -> String<br />
myModifier = (++ "_v2")<br />
<br />
$(makeDynamicLinker ''MyLib CCall 'myModifier)<br />
<br />
-- Load your library with:<br />
-- loadMyLib :: FilePath -> [RTLDFlags] -> IO MyLib<br />
</haskell><br />
<br />
== Enhancing performance and advanced topics ==<br />
<br />
To enhance performance of a call to a foreign function, you first need to understand how GHC runtime system works. GHC uses user-space threads. It uses a set of system threads (called "Tasks"). Each system thread can execute a "capability" (i.e. a user-space thread manager). User-space threads are distributed on capabilities. Each capability executes its associated user-space threads, one at a time, using cooperative scheduling or preemption if necessary.<br />
<br />
All the capabilities have to synchronize to perform garbage collection.<br />
<br />
When a FFI call is made:<br />
* the user-space thread is suspended (indicating it is waiting for the result of a foreign call)<br />
* the current system thread executing the capability executing the user-space thread releases the capability<br />
** the capability can be picked up by another system thread<br />
** the user-space threads that are not suspended in the capability can be executed<br />
** garbage collection can occur<br />
* the system thread executes the FFI call<br />
* when the FFI call returns, the user-space thread is woken up<br />
<br />
If there are too many blocked system threads, the runtime system can spawn new ones.<br />
<br />
=== Unsafe calls ===<br />
<br />
All the capability management before and after a FFI call adds some overhead. It is possible to avoid it in some cases by adding the "unsafe" keyword as in the following example:<br />
<br />
<haskell><br />
foreign import ccall unsafe "exp" c_exp :: Double -> Double<br />
</haskell><br />
<br />
By doing this, the foreign code will be directly called but the capability won't be released by the system thread during the call. Here are the drawbacks of this approach:<br />
* if the foreign function blocks indefinitely, the other user-space threads of the capability won't be executed anymore (deadlock)<br />
* if the foreign code calls back into the Haskell code, a deadlock may occur<br />
** it may wait for a value produced by one of the locked user-space threads on the capability<br />
** there may not be enough capabilities to execute the code<br />
<br />
=== Foreign PrimOps ===<br />
<br />
If unsafe foreign calls are not fast enough for you, you can try the GHCForeignImportPrim extension.<br />
<br />
<haskell><br />
{-# LANGUAGE GHCForeignImportPrim,<br />
MagicHash,<br />
UnboxedTuples,<br />
UnliftedFFITypes #-}<br />
<br />
import GHC.Base<br />
import GHC.Int<br />
<br />
-- Primitive with type :: Int -> Int -> IO Int<br />
foreign import prim "my_primop_cmm"<br />
my_primop# :: Int# -> Int# -> State# RealWorld -> (# State# RealWorld, Int# #)<br />
<br />
my_primop :: Int64 -> Int64 -> IO Int64<br />
my_primop (I64# x) (I64# y) = IO $ \s -><br />
case (my_primop# x y s) of (# s1, r #) -> (# s1, I64# r #)<br />
</haskell><br />
<br />
Then you have to write you foreign function "my_primop_cmm" using [https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/CmmType C--] language used internally by GHC.<br />
<br />
As an alternative, if you know how C-- is compiled on your architecture, you can write code in other languages. For instance [https://github.com/hsyl20/ViperVM/blob/master/src/lib/ViperVM/Arch/X86_64/Linux/syscall.c directly in assembly] or [http://breaks.for.alienz.org/blog/2012/02/09/parsing-market-data-feeds-with-ragel/ using C and LLVM].<br />
<br />
[http://breaks.for.alienz.org/blog/2012/05/23/ffi-vs-primops/ Here] is a comparison of the different approaches on a specific case.<br />
<br />
=== Bound threads ===<br />
<br />
Some foreign codes use (system) thread-local storage. Some others are not thread-safe. In both case, you have to be sure that the same '''system thread''' executes the FFI calls. To control how user-space threads are scheduled on system threads, GHC provide [http://hackage.haskell.org/package/base/docs/Control-Concurrent.html#g:8 bound threads]. Bound threads are user-space threads (Haskell threads) that can only be executed by a single system thread.<br />
<br />
Note that bound threads are more expensive to schedule than normal threads. The first thread executing "main" is a bound thread.<br />
<br />
=== Inline FFI calls ===<br />
<br />
If you want to make a one-shot FFI call without the hassle of writing the foreign import, you can use the following technique (using Template Haskell).<br />
<br />
In AddTopDecls.hs:<br />
<haskell><br />
{-# LANGUAGE TemplateHaskell #-}<br />
<br />
module AddTopDecls where<br />
<br />
import Language.Haskell.TH<br />
import Language.Haskell.TH.Syntax<br />
<br />
importDoubleToDouble :: String -> ExpQ<br />
importDoubleToDouble fname = do<br />
n <- newName fname<br />
d <- forImpD CCall unsafe fname n [t|Double -> Double|]<br />
addTopDecls [d]<br />
[|$(varE n)|]<br />
</haskell><br />
<br />
In your module:<br />
<haskell><br />
{-# LANGUAGE TemplateHaskell #-}<br />
<br />
module Main where<br />
<br />
import Language.Haskell.TH<br />
import Language.Haskell.TH.Syntax<br />
<br />
import AddTopDecls<br />
<br />
main :: IO ()<br />
main = do<br />
print ($(importDoubleToDouble "sin") pi)<br />
print ($(importDoubleToDouble "cos") pi)<br />
</haskell><br />
<br />
== History ==<br />
<br />
=== Header inclusion ===<br />
<br />
In old versions of GHC (6.8.3 and earlier), the compiler was able to check the prototypes of the foreign imports by including C header files into the generated C code. For instance, you could write:<br />
<haskell><br />
{-# INCLUDE <math.h> #-}<br />
</haskell><br />
or <br />
<haskell><br />
foreign import ccall "math.h sin" c_sin :: Double -> Double<br />
</haskell><br />
to include the "math.h" header.<br />
<br />
This is deprecated in GHC. Nevertheless you may still find examples using this syntax so it is good to know that it has been used. Moreover, other compilers may still use this feature.<br />
<br />
* Justification of the deprecation from the [http://www.haskell.org/ghc/docs/6.10.1/html/users_guide/ffi-ghc.html#glasgow-foreign-headers GHC 6.10.1 manual]:<br />
<br />
"C functions are normally declared using prototypes in a C header file. Earlier versions of GHC (6.8.3 and earlier) #included the header file in the C source file generated from the Haskell code, and the C compiler could therefore check that the C function being called via the FFI was being called at the right type.<br />
<br />
GHC no longer includes external header files when compiling via C, so this checking is not performed. The change was made for compatibility with the native code backend (-fasm) and to comply strictly with the FFI specification, which requires that FFI calls are not subject to macro expansion and other CPP conversions that may be applied when using C header files. This approach also simplifies the inlining of foreign calls across module and package boundaries: there's no need for the header file to be available when compiling an inlined version of a foreign call, so the compiler is free to inline foreign calls in any context.<br />
<br />
The -#include option is now deprecated, and the include-files field in a Cabal package specification is ignored."<br />
<br />
* [http://www.haskell.org/ghc/docs/6.8.3/html/users_guide/ffi-ghc.html#glasgow-foreign-headers Documentation of this feature in the GHC 6.8.3 manual]<br />
<br />
<br />
<br />
== References ==<br />
<br />
* FFI addendum<br />
* The [http://www.haskell.org/onlinereport/haskell2010/haskellch8.html#x15-1490008 Foreign Function Interface section] of the Haskell 2010 report<br />
* [https://www.haskell.org/ghc/docs/latest/html/users_guide/ffi.html FFI chapter in the GHC user guide]<br />
* [http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/ "Tackling the awkward squad" paper]<br />
* [http://community.haskell.org/~simonmar/papers/conc-ffi.pdf "Extending the Haskell FFI with Concurrency" paper] (the number of capabilities is now greater than 1)<br />
* http://blog.melding-monads.com/2011/10/24/concurrency-and-foreign-functions-in-the-glasgow-haskell-compiler/<br />
<br />
<br />
=== Related links ===<br />
<br />
* [https://github.com/GaloisInc/ivory Ivory]: EDSL for writing safer low-level C.<br />
<br />
=== Old links ===<br />
<br />
Select one of the following links for more information:<br />
* [[FFI Introduction]]<br />
* GHC manual: [http://www.haskell.org/ghc/docs/latest/html/users_guide/hsc2hs.html Writing Haskell interfaces to C code: hsc2hs]<br />
* [https://github.com/ifesdjeen/haskell-ffi-tutorial haskell-ffi-tutorial] at GitHub<br />
* The official description: chapters 8 and 24 to 37 of [http://www.haskell.org/onlinereport/haskell2010/#QQ2-15-159 The Haskell 2010 Language Report] (a draft: [http://www.cse.unsw.edu.au/~chak/haskell/ffi/ The Haskell 98 Foreign Function Interface 1.0. An Addendum to the Haskell 98 Report])<br />
* [[FFI cook book]]<br />
* [[FFI complete examples]]<br />
* [[GHC/Using the FFI]]<br />
* [http://research.microsoft.com/~simonpj/papers/marktoberdorf/ Tackling the awkward squad]<br />
* [https://github.com/wavewave/fficxx fficxx], a Haskell-C++ Foreign Function Interface Generator<br />
* [[Applications and libraries/Interfacing other languages]]<br />
* [http://rosettacode.org/wiki/Use_another_language_to_call_a_function#Haskell Use another language to call a function; Haskell]<br />
* [https://code.google.com/p/tabi/ TABI] a typeful tagged cross-language calling convention<br />
<br />
=== Blog articles ===<br />
<br />
* [http://www.serpentine.com/blog/2010/09/04/dealing-with-fragile-c-libraries-e-g-mysql-from-haskell/ Dealing with fragile C libraries (e.g. MySQL) from Haskell] <br />
* [http://blog.danieroux.com/2007/01/01/simple-demonstration-of-haskell-ffi/ Simple demonstration of Haskell FFI]<br />
* [http://therning.org/magnus/posts/2006-12-08-238-c-and-haskell-sitting-in-a-tree.html C and Haskell sitting in a tree…]<br />
* [http://vis.renci.org/jeff/2009/07/10/c2hs-example-to-save-other-people-frustration/ C2HS example: To save other people frustration] <br />
* [[Cxx foreign function interface]]; how to link to a C++ library<br />
* [http://blog.ezyang.com/2010/07/safety-first-ffi-and-threading/ Safety first: FFI and threading]<br />
<br />
== TODO ==<br />
<br />
* Fix References section<br />
<br />
* Foreign language specific issues<br />
** C++ symbol mangling<br />
** Embedded Objective C<br />
<br />
* Precision<br />
<br />
<code>The Haskell report only guarantees that Int has 30 bits of signed precision, so converting CInt to Int is not safe! On the other hand, many classes have instances for Int and Integer but not CInt, so it's generally more convenient to convert from the C types. To convert, I suppose you could either write a 'checkedFromIntegral' function if you're sure it's small or just use Integer.</code><br />
<br />
* Fix [https://wiki.haskell.org/C2hs]<br />
** One page per tool?<br />
** Links to external tool specific tutorials<br />
<br />
* Linking<br />
** pkgconfig<br />
** cabal<br />
** explicit (ghc parameters)<br />
** cf http://stackoverflow.com/questions/4959802/how-to-specify-dependency-on-external-c-library-in-cabal<br />
<br />
[[Category:FFI]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Applications_and_libraries/Compilers_and_interpreters&diff=59956Applications and libraries/Compilers and interpreters2015-08-04T06:17:14Z<p>Beerdude26: Added Ivory link</p>
<hr />
<div>Haskell, with its support for pattern matching on data structures,<br />
generic structure traversals, and expressive type system, is popular for<br />
implementing compilers and interpreters. Here's a selection of compilers<br />
and interpreters implemented in Haskell.<br />
<br />
==Large languages==<br />
<br />
===Haskell===<br />
<br />
;[http://haskell.org/ghc GHC]<br />
:GHC, The Glasgow Haskell Compiler, is written in Haskell<br />
<br />
;[[Yhc]]<br />
:Yhc, The York Haskell Compiler, is written in Haskell<br />
<br />
;[http://repetae.net/computer/jhc/ Jhc]<br />
:Jhc is a Haskell compiler which aims to produce the most efficient programs possible via whole program analysis<br />
<br />
;[http://haskell.org/nhc98 nhc98]<br />
:A compiler for Haskell 98, written in Haskell<br />
<br />
;[http://www.cs.uu.nl/groups/ST/Ehc/WebHome Ehc]<br />
:The purpose of the EHC project is to provide a description of a Haskell compiler which is as understandable as possible so it can be used for education as well as research.<br />
<br />
;[http://www.cs.uu.nl/wiki/UHC UHC]<br />
:UHC is the Utrecht Haskell Compiler. UHC supports almost all Haskell 98 features plus experimental extensions. The compiler runs under Mac OS X, Windows (Cygwin), and various Unix flavors.<br />
<br />
;[http://www.csg.lcs.mit.edu/projects/languages/ph.shtml pH]<br />
:A parallel version of Haskell from MIT.<br />
<br />
<br />
====Helium====<br />
<br />
;[http://www.cs.uu.nl/helium/ Helium]<br />
:A Haskell subset for educational purposes<br />
<br />
<br />
====Generic Haskell====<br />
<br />
;[http://generic-haskell.org/ Generic Haskell]<br />
:An extension of Haskell that supports generic programming<br />
<br />
<br />
====Data Field Haskell====<br />
<br />
;[http://www.mrtc.mdh.se/projects/DFH/ Data Field Haskell]<br />
:A dialect of the functional programming language Haskell that provides an instance of data fields<br />
<br />
<br />
====Eden====<br />
<br />
;[http://www.mathematik.uni-marburg.de/~eden/ Eden]<br />
:A Haskell dialect for parallel programming<br />
<br />
<br />
====Chameleon====<br />
<br />
;[http://taichi.ddns.comp.nus.edu.sg/taichiwiki/ChameleonHomePage Chameleon]<br />
:A Haskell-style language which implements the ideas described in a ``A Theory of Overloading``<br />
<br />
<br />
====CHR (Constraint Handling Rules)====<br />
<br />
;[http://www.cs.mu.oz.au/~gjd/haskellchr/ Haskell CHR]<br />
:A concurrent committed-choice constraint logic programming language, implemented using GHC's software transactional memory. According to the site referenced by the above-mentioned link, "It also contains an implementation of WAM for Haskell, so Prolog-style terms with variables are now possible."<br />
<br />
;[http://www.cs.kuleuven.be/~dtai/projects/CHR/systems/stmchr-0.1.tar.gz STM-based CHR implementation, by Michael Stahl] (gzipped TAR file)<br />
<br />
;[http://taichi.ddns.comp.nus.edu.sg/taichiwiki/CCHR CCHR: STM-based CHR implementation by Lam and Sulzmann]<br />
:According to the site referenced by the above-mentioned link, "CCHR is an experimental concurrent implementation of Constraint Handling Rules, designed to exploit concurrency and parallelism explicitly. CCHR is implemented in Haskell, with software transactional memory to manage synchronization of multiple solver threads working on the same problem. Constraint Handling Rules (CHR) is a concurrent committed choice constraint logic programming language to describe transformations (rewritings) among multi-sets of constraints (atomic formulae). CHR naturally support concurrent programming. Conjunction of constraints can be regarded as interacting collections of multiple asynchronous agents or processes. Their interaction is specified via transformation rules which can be applied simultaneously if the transformation rules do not interfere. Hence, one would expect to run CHR faster by executing transformation rules in parallel on a multi-core machine architecture. CCHR exactly allows such concurrency while solving CHR problems and exhibits significant speed up in most problems when executed on multi-core systems."<br />
<br />
<br />
=== Elm ===<br />
<br />
;[http://elm-lang.org/ Elm]<br />
:The Elm programming language aims to make web development more pleasant. Elm is a type-safe, functional reactive language that compiles to HTML, CSS, and JavaScript. <br />
<br />
<br />
===BASIC===<br />
<br />
;[http://hackage.haskell.org/package/BASIC BASIC]<br />
:A simplified version of the original BASIC embedded in Haskell.<br />
<br />
<br />
===Liskell===<br />
<br />
;[http://web.archive.org/web/20120609122549/http://www.liskell.org/ Liskell]<br />
:Liskell is Haskell on the inside but looks like Lisp on the outside<br />
<br />
===Perl===<br />
<br />
;[http://pugscode.org Pugs]<br />
:Pugs is an implementation of Perl 6, written in Haskell. It aims to implement the full Perl6 specification.<br />
<br />
<br />
===Python===<br />
<br />
;[http://hackage.haskell.org/package/berp-0.0.1 Berp]<br />
:Berp is an implementation of Python 3 in Haskell.<br />
<br />
;[http://code.google.com/p/haspy/ haspy]<br />
:Haspy is an implementation of Python in Haskell<br />
<br />
<br />
===Ruby===<br />
<br />
;[http://raa.ruby-lang.org/project/rtype/ RType]<br />
:RType is a Ruby interpreter written in Haskell<br />
<br />
<br />
===Flapjax===<br />
;[http://www.flapjax-lang.org/ Flapjax]<br />
:Flapjax is a language for functional reactive programming of AJAX web applications, whose compiler ([http://www.flapjax-lang.org/download/flapjax-source.tar.gz source]) is written in Haskell.<br />
<br />
<br />
===Scheme===<br />
<br />
;[http://halogen.note.amherst.edu/~jdtang/scheme_in_48/tutorial/overview.html Write Yourself a Scheme in 48 Hours]<br />
:A small Scheme interpreter<br />
<br />
;[http://www.korgwal.com/haskeem/ A Scheme in Haskell]<br />
<br />
<br />
===Lisp===<br />
<br />
;[http://www.defmacro.org/ramblings/lisp-in-haskell.html A Lisp Interpreter In Haskell]<br />
:A small lisp interpreter written in Haskell<br />
<br />
<br />
=== Emacs Lisp ===<br />
<br />
;[http://www.codersbase.com/index.php/helisp Helisp]<br />
:The beginnings of an Emacs lisp compiler/interpreter.<br />
<br />
<br />
===Epigram===<br />
<br />
;[http://www.e-pig.org/ Epigram]<br />
:Epigram is a prototype dependently typed functional programming language<br />
<br />
<br />
===Curry===<br />
<br />
;[http://danae.uni-muenster.de/~lux/curry/ The M&uuml;nster Curry Compiler]<br />
:A native code compiler for the declarative multi-paradigm language Curry, written in Haskell<br />
<br />
<br />
===Bluespec===<br />
;[http://www.bluespec.com Bluespec]<br />
:A compiler for a hardware description language translating a Haskell-like (but with System Verilog syntax these days) language to Verilog.<br />
<br />
<br />
===Cayenne===<br />
<br />
;[http://www.augustsson.net/Darcs/Cayenne/html/ Cayenne]<br />
:A compiler for a Haskell-like language with dependent types.<br />
<br />
<br />
===Agda===<br />
<br />
;[http://wiki.portal.chalmers.se/agda/pmwiki.php Agda]<br />
:A Cayenne-like programming language and proof assistant.<br />
<br />
<br />
===PolyP===<br />
<br />
;[http://www.cs.chalmers.se/~patrikj/poly/polyp/ PolyP]<br />
:A polytypic programming language<br />
<br />
<br />
===Forth===<br />
<br />
;[http://feather.perl6.nl/~nothingmuch/harrorth/ Harrorth]<br />
:Harrorth, a Forth interpreter<br />
<br />
<br />
===Eiffel===<br />
<br />
;[http://eiffelsoftware.origo.ethz.ch/index.php/DynBindModelHaskell Dynamic binding in Eiffel]<br />
:A model of dynamic binding in ECMA Eiffel, in Haskell<br />
<br />
<br />
===Crouton===<br />
<br />
;[http://crouton.sourceforge.net/ Crouton]<br />
:Crouton is a small but fairly complete functional programming language for querying and transforming parsed manuscripts, such as the PPCME. It is intended as an alternative to Corpus Search, based on a different philosophy. It is written in (and largely based on) the very nice functional programming language Haskell using the Parsec library<br />
<br />
<br />
=== JavaScript ===<br />
<br />
;[http://www.haskell.org/haskellwiki/Libraries_and_tools/HJS HJS]<br />
:HJS is a JavaScript parser written in Haskell. Available from HackageDB.<br />
<br />
<br />
===TCL===<br />
<br />
;[http://code.google.com/p/hiccup/ Hiccup]<br />
:Hiccup is a minimalistic TCL interpreter. It tries to be relatively simple, relatively efficient, and mostly correct. <br />
<br />
<br />
===Smalltalk===<br />
<br />
;[http://code.google.com/p/hst/ hst]<br />
:HST is a Smalltalk implementation in Haskell. [http://lstephen.wordpress.com/2007/07/23/completing-the-spike/ See here for more information]<br />
<br />
<br />
=== Disciple ===<br />
<br />
;[[DDC]]<br />
:Disciple is an explicitly lazy dialect of Haskell which supports destructive update, computational effects, type directed field projections and some other useful things.<br />
<br />
<br />
=== Timber ===<br />
<br />
;[http://timber-lang.org/Timber Timber]<br />
:Timber is a modern language for building event-driven systems, based around the notion of reactive objects. It is also a purely functional language derived from Haskell, although with a strict evaluation semantics. The Timber compiler currently runs on Linux and MacOS X platforms, but uses gcc as its back-end so it should be easily portable to most POSIX-like environments.<br />
<br />
=== Ivory ===<br />
<br />
;[http://ivorylang.org/ Ivory]<br />
: The Ivory Language is an eDSL for safe systems programming. You can think of Ivory as a safer C, embedded in Haskell. [https://github.com/GaloisInc/ivory Github]<br />
<br />
==Small languages==<br />
<br />
===Baskell===<br />
;[http://www.cs.mu.oz.au/~bjpop/code.html Baskell]<br />
:An interpreter for a small functional programming language. Supports strict and non-strict evaluation, and type inference. Useful for teaching purposes.<br />
<br />
===LambdaPi===<br />
<br />
;[http://www.informatik.uni-bonn.de/~loeh/LambdaPi.html LambdaPi]<br />
:LambdaPi, An Implementation of a Dependently Typed Lambda Calculus<br />
<br />
===Unlambda===<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/code/lambdabot/scripts/Unlambda.hs Unlambda.hs]<br />
:An implementation of Unlambda in Haskell<br />
<br />
===BF===<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/code/lambdabot/scripts/BF.hs BF.hs]<br />
:An implementation of BF in Haskell<br />
<br />
<br />
===Lambda calculus===<br />
<br />
;[http://programming.reddit.com/goto?id=qnir 4 lambda calculus implementations]<br />
:[http://www.augustsson.net/Darcs/Lambda/ With code], by Lennart Augustsson.<br />
<br />
;[http://www.cse.unsw.edu.au/~dons/code/lambdabot/Plugin/Lambda/ LMEngine]<br />
:An implementation of the untyped lambda calculus<br />
<br />
===QML, a functional quantum programming language===<br />
<br />
;[http://sneezy.cs.nott.ac.uk/QML/ QML: A Functional Quantum Programming Language] project<br />
:It is implemented in Haskell.<br />
<br />
:For online material on quantum computing in general, see [http://www.theory.caltech.edu/people/preskill/ph229/ Quantum Computation] course held by [http://www.theory.caltech.edu/people/preskill/index.html John Preskill].<br />
<br />
===HQL - HHM's Quantified Lambda===<br />
<br />
;[http://www.archive.moraldo.com.ar/index.php/Articles/HQLlanguage Hernan's Quantified Lambda]<br />
:a small functional language, whose expressions can involve the use of quantifier operators<br />
<br />
===Atom===<br />
;[http://funhdl.org/wiki/doku.php/atom Atom]<br />
:Atom is a small HDL that compiles conditional term rewriting systems down to Verilog RTL.<br />
<br />
===Feldspar ===<br />
<br />
;[http://hackage.haskell.org/package/feldspar-language Feldspar]<br />
:Feldspar (Functional Embedded Language for DSP and PARallelism) is an embedded DSL for describing digital signal processing algorithms developed at Ericsson.<br />
<br />
===AL (Assignment Language)===<br />
<br />
:It is used for teaching purposes in at the Technical University of Vienna.<br />
:An interpreter implemented in Haskell is described in [http://www.logic.at/staff/robinson/ali.ps ALI - an AL Interpreter implemented in Haskell] written by Peter Robinson.<br />
<br />
=== Whitespace ===<br />
;[http://compsoc.dur.ac.uk/whitespace/index.php Whitespace]<br />
:A language that uses whitespace characters as language elements and ignores all non-whitespace characters<br />
<br />
=== LIPL ===<br />
;[http://www.lipl.googlepages.com/index.html LIPL]<br />
:An interpreter for a tiny functional programming language. It features Hindley-Milner style type inference.<br />
<br />
=== Ministg ===<br />
;[http://www.haskell.org/haskellwiki/Ministg Ministg]<br />
:An interpreter for a high-level, small-step, operational semantics for the STG machine. Features execution tracing, rendered in HTML. Useful for studying the behaviour of the STG machine and experimenting with extensions to the machine. Also useful for studying program language implementation.<br />
<br />
=== Constantinople ===<br />
;[http://zzo38computer.cjb.net/prog/Constantinople.zip Constantinople]<br />
:A compiler for the Constantinople esolang. There are two datatypes, the list, and the bit, which can either be 0 or 1. Lists are infinite and lazily evaluated.<br />
<br />
== Embedded languages ==<br />
<br />
=== ForSyDe ===<br />
The ForSyDe (Formal System Design) methodology has been developed with the objective to move system-on-chip design to a higher level of abstraction. ForSyDe is implemented as a Haskell-embedded behavioral DSL.<br />
<br />
== Debuggers ==<br />
<br />
;[http://www.dsic.upv.es/users/elp/debussy Debussy]<br />
:A declarative debugger for OBJ-like languages<br />
<br />
;See also [[Debugging]].<br />
<br />
{{LibrariesPage}}</div>Beerdude26https://wiki.haskell.org/index.php?title=Applications_and_libraries/Statistics&diff=59955Applications and libraries/Statistics2015-08-04T06:12:41Z<p>Beerdude26: Created page</p>
<hr />
<div>== Libraries ==<br />
<br />
* Dataframes: [http://acowley.github.io/Frames/ Frames]<br />
<br />
== Talks ==<br />
<br />
* http://m.youtube.com/watch?v=2-JFkv9-JOQ</div>Beerdude26https://wiki.haskell.org/index.php?title=Applications_and_libraries&diff=59954Applications and libraries2015-08-04T06:11:07Z<p>Beerdude26: /* Haskell applications and libraries */ Added statistics page</p>
<hr />
<div>[[Category:Libraries]] [[Category:Tools]]<br />
<br />
The number of Haskell packages is growing rapidly. The section 'Haskell library collections' gives an ordering of all these packages by relative importance. In the section 'Haskell applications and libraries' an ordering by category is given. Finally some guidelines for developers of new packages are presented.<br />
<br />
== Haskell library collections ==<br />
<br />
=== Haskell Prelude ===<br />
The most important Haskell library is called the [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html Prelude]. It is implicitly imported by default, and includes the most commonly used functions. Make sure you know what they do and how to use them effectively.<br />
<br />
=== Haskell 2010 libraries ===<br />
The Haskell 2010 [[Language and library specification]] defines a set of [http://www.haskell.org/onlinereport/haskell2010/haskellpa2.html libraries] with basic functionality which all Haskell implementations should support, including the Prelude. Changes to these libraries are handled by the [http://hackage.haskell.org/trac/haskell-prime/ Haskell'] process.<br />
<br />
Haskell modules that almost everybody uses are in this group, for example:<br />
[http://www.haskell.org/onlinereport/haskell2010/haskellch13.html Control.Monad], [http://www.haskell.org/onlinereport/haskell2010/haskellch20.html Data.List] <br />
and [http://www.haskell.org/onlinereport/haskell2010/haskellch41.html System.IO]. Within GHC, these are mostly grouped into the [http://hackage.haskell.org/package/base base] package, but for example [http://www.haskell.org/onlinereport/haskell2010/haskellch14.html Data.Array] is in the [http://hackage.haskell.org/package/array array] package.<br />
<br />
=== GHC bootstrap libraries ===<br />
GHC comes with an expanded version of the Haskell 2010 libraries. Together these are called the [http://www.haskell.org/ghc/docs/latest/html/libraries/index.html GHC bootstrap libraries]. You should not rely on this list being stable, since it is just the list of packages that are needed to build GHC itself (including ghci and the user guide). <br />
<br />
Examples of libraries that are GHC 7.8.3 boot libraries (but not core libraries, see below): <br />
[http://hackage.haskell.org/package/haskeline haskeline],<br />
[http://hackage.haskell.org/package/integer-gmp integer-gmp] and<br />
[http://hackage.haskell.org/package/terminfo terminfo].<br />
<br />
=== Core Libraries ===<br />
The core libraries form a subset of the packages in the Haskell Platform that has submitted to the management process described on the [[Library submissions]] page. <br />
<br />
These packages typically define basic APIs that are expected to be available in any Haskell implementation, packages that are being maintained for backwards compatibility, or in some cases, which are just needed as glue to hold the rest of the platform together.<br />
<br />
Not all GHC boot libraries are core libraries.<br />
<br />
Examples of libraries, or packages, that belong to this group are: <br />
[http://hackage.haskell.org/package/mtl Monad transformer library],<br />
[http://hackage.haskell.org/package/random random] and<br />
[http://hackage.haskell.org/package/parallel parallel].<br />
<br />
=== Haskell Platform libraries ===<br />
On top of the Core Libraries, the [http://www.haskell.org/platform/ Haskell Platform] comes preinstalled with some additional packages that together form the [http://www.haskell.org/platform/contents.html#packages-and-documentation Haskell Platform libraries]. These libraries have been thoroughly tested before being included. The addition of these libraries with the [http://www.haskell.org/platform/ Haskell Platform] is what makes it 'batteries included'.<br />
<br />
Examples of included packages are:<br />
[http://hackage.haskell.org/package/attoparsec attoparsec], [http://hackage.haskell.org/package/network network] and [http://hackage.haskell.org/package/QuickCheck QuickCheck].<br />
<br />
=== The Hackage database ===<br />
[http://hackage.haskell.org/packages/archive/pkg-list.html Hackage] is the final layer of the Haskell library collections. [http://hackage.haskell.org/packages/archive/pkg-list.html Hackage] aims to provide a comprehensive collection of released Haskell packages, similar to Perl's CPAN or Python's PyPI. <br />
<br />
Start on [http://hackage.haskell.org/packages/archive/pkg-list.html Hackage] if you are looking for some functionality that did not come preinstalled with the [http://www.haskell.org/platform/ Haskell Platform]. <br />
<br />
See also the [[Hackage|Hackage wiki page]] and [[Cabal/How to install a Cabal package | how to install a Cabal package]].<br />
<br />
== Haskell applications and libraries ==<br />
<br />
Applications, libraries and tools for Haskell or written in Haskell have been classified below, but you should check [http://hackage.haskell.org/packages/archive/pkg-list.html Hackage] for the latest list.<br />
<br />
* [[/Music and sound/|Audio, music and sound]]<br />
* [[/Bioinformatics/]]<br />
* [[/Concurrency and parallelism/]]<br />
* [[/Compilers and interpreters/]]<br />
* [[/Compiler tools|Compiler construction, lexing, parsing, pretty printing]]<br />
* [[/Cryptography|Cryptography and hashing]]<br />
* [[/Data structures | Data Structures and IO Libraries]]<br />
* [[/Database interfaces/]]<br />
* [[/Editors|Editors written in Haskell]] and [[Editors|editors for Haskell]].<br />
* [[/Extended Haskell/]]<br />
* [[/Games/]]<br />
* [[/Generic programming/]]<br />
* [[/GUI libraries|Graphical User Interface (GUI) Libraries]]<br />
* [[/Graphics/]]<br />
* [[/Hardware verification/]]<br />
* [[/Linguistics|Linguistics and natural language processing]]<br />
* [[/Mathematics/|Mathematics and physics]]<br />
* [[/Network/]]<br />
* [[/Operating system/|Operating systems and systems programming]] (also emulators)<br />
* [[/Program development/]]<br />
* [[/Robotics/]]<br />
* [[/Statistics/]]<br />
* [[/Theorem provers/]]<br />
* [[/Interfacing other languages|Tools for interfacing with other languages]]<br />
* [[Web|Web, HTML, XML]]<br />
<br />
Other places to look include:<br />
* The [[Library]] hierarchy page on this wiki.<br />
* The Haskell [[Haskell Communities and Activities Report|community reports]].<br />
* The [http://www.haskell.org/mailman/listinfo/libraries mailing list] for discussion of issues related to libraries.<br />
<br />
You can also [http://www.reddit.com/r/haskell_proposals/top/?t=month propose and vote on new libraries] that you'd like on [http://www.reddit.com/r/haskell reddit], and look at our past [http://hackage.haskell.org/trac/summer-of-code/ Summer of Code proposals].<br />
<br />
== Guidelines for developers ==<br />
<br />
[[Image:Cabal-With-Text-small.png|frame|Built with Cabal]]<br />
<br />
Developer guides:<br />
<br />
* [[How to write a Haskell program|How to write a new Haskell library]]<br />
* [[Library submissions|How to propose changes to the standard libraries]]<br />
* [http://web.archive.org/web/20071011215053/http://pupeno.com/2006/12/12/the-lambda-revolution-v/ Creating a .deb from a Haskell Cabal package] (in the Web Archive)<br />
* Guide to making standard [[Library submissions|library submissions]]<br />
* If you notice the library documentation is lacking, or could be improved, [http://www.haskell.org/haskellwiki/Improving_library_documentation please report it here]<br />
* [http://www.google.com/codesearch Google Code Search] can help identify common idioms, improving your API.<br />
* [[Future projects]], more projects people would like.<br />
* [[Cabal]], The Common Architecture for Building Applications and Libraries, is a framework for packaging, building, and installing any tool developed in the Haskell language.<br />
* [[Hack-Nix]], a set of tools based on the [http://nixos.org Nix] package manager to manage multiple setups to build a project<br />
<br />
Proposals for the module name space layout that can be used to guide the construction of new libraries. <br />
<br />
* [[Hierarchical module names|Almost current guide]]<br />
* [http://www.cs.york.ac.uk/fp/libraries/layout.html Proposal 1]<br />
* [http://www.cs.york.ac.uk/fp/libraries/layoutSM.html Proposal 2]<br />
<br />
=== Libraries for other languages ===<br />
<br />
If you are thinking about designing a new library for Haskell, you ought to look what has been done in other languages. Here are standard library definitions for <br />
<br />
* [http://wiki.clean.cs.ru.nl/Libraries Clean]<br />
* [http://www.standardml.org/Basis/ Standard ML]<br />
* [http://caml.inria.fr/pub/docs/manual-ocaml/manual034.html Objective Caml]<br />
* [http://srfi.schemers.org/ Scheme]</div>Beerdude26https://wiki.haskell.org/index.php?title=The_JavaScript_Problem&diff=59953The JavaScript Problem2015-08-04T06:08:36Z<p>Beerdude26: /* GHCJS */</p>
<hr />
<div>== The problem ==<br />
<br />
The JavaScript problem is two-fold and can be described thus:<br />
<br />
# '''JavaScript, the language.''' JavaScript, the language, has some issues that make working with it inconvenient and make developing software harder :<br />
<br />
* lack of module system (only pre-ES6) , <br />
* weak-typing, <br />
* verbose function syntax<sup>1</sup>, <br />
* late binding<sup>2</sup>, which has led to the creation of various static analysis tools to alleviate this language flaw<sup>3</sup>, but with limited success<sup>4</sup> (there is even a static type checker<sup>5</sup>), <br />
* finicky equality/automatic conversion, <br />
* <code>this</code> behaviour, <br />
* and lack of static types.<br />
<br />
# '''We need JavaScript.''' Using it for what it is good for, i.e. providing a platform for browser development, but not using the language ''per se'', is therefore desirable, and many are working to achieve this, in varying forms. There are various ways to do it, but we ought to opt for compiling an existing language, Haskell, to JavaScript, because we do not have time to learn or teach other people a new language, garner a new library set and a new type checker and all that Haskell implementations provide.<br />
<br />
== Mainstream alternatives ==<br />
<br />
=== CoffeeScript ===<br />
It makes many aspects of JavaScript sane and convenient, and you get a compilation check that verifies syntax, however it still suffers greatly from weak-typing.<br />
<br />
=== TypeScript ===<br />
<br />
Structural typing with traditional generics on top of JavaScript.<br />
Of all the alternatives, TypeScript's advantage is that it makes no changes to JavaScript. Existing JavaScript code that passes jshint is valid Typescript code. TypeScript also adds features from the latest JavaScript standards that it can compile down to older versions of JavaScript. TypeScript is by far the easiest JavaScript variant to learn. The downside is that one might desire a better language than just JavaScript + types.<br />
<br />
TypeScript defaults to dynamic typing when it can't figure the type out. However, it now has a noImplicitAny setting that will give a compilation error if it can't figure out the type.<br />
<br />
Structural sub-typing seems a good fit for JavaScript. <br />
Perhaps Typescript's biggest problem is that null is a valid value for any type.<br />
<br />
== Haskell -> JS ==<br />
<br />
=== UHC ===<br />
<br />
Original blog post [https://github.com/atzedijkstra/javascript-runtime-for-UHC here.] Quickstart guide [http://chrisdone.com/posts/2012-01-06-uhc-javascript.html here.] A more in-depth discussion about the current capabilities of the backend [http://www.norm2782.com/improving-uhc-js-report.pdf here.] For an example of using the JavaScript compilation for a real app see this [http://alessandrovermeulen.me/2012/01/26/getting-rid-of-javascript-with-haskell/ blog post], there is also a port of wxAsteroids to the browser (see [http://uu-computerscience.github.io/js-asteroids/ github] or a [http://www.rubendegooijer.nl/posts/2013-04-06-haskell-oop.html blog post]). <br />
<br />
* Beta.<br />
* Only works for UHC, but promising. <br />
* UHC compiles enough of Hackage to be very useful.<br />
* Doesn't produce an explosion of code, seemingly.<br />
* Fairly substantial JS/DOM/W3C/HTML5 API.<br />
* Currently works.<br />
<br />
=== Fay ===<br />
<br />
Website: https://github.com/faylang/fay/wiki<br />
Discussion on Reddit: [http://www.reddit.com/r/haskell/comments/11yrpi/fay_slides/ Fay slides]. The package is on [http://hackage.haskell.org/package/fay Hackage]. Fetch with Git: <br />
git clone git://github.com/faylang/fay.git<br />
<br />
* Compiles a subset of Haskell, needs more<br />
* Currently works.<br />
<br />
=== GHCJS ===<br />
<br />
The GitHub page is [https://github.com/ghcjs/ghcjs here.]<br />
<br />
* Alpha, has picked up development speed.<br />
* Works.<br />
* Incomplete.<br />
* Nicely designed.<br />
* Compiles most pure Haskell libraries no problem.<br />
* FFI to JS works, and the author, sviperll is a helpful guy.<br />
<br />
==== Libraries ====<br />
<br />
* The [http://hackage.haskell.org/package/reflex reflex] library provides [http://hackage.haskell.org/package/reflex-dom reflex-dom] for building web GUIs using GHCJS. ([https://www.youtube.com/watch?v=mYvkcskJbc4 Presentation], [https://obsidian.systems/reflex-nyhug/#/step-1 Slides])<br />
<br />
==== Apps ====<br />
<br />
* [http://markup.rocks markup.rocks] - Pandoc + GHCJS + Reflex. [https://www.reddit.com/r/haskell/comments/35ax22/ive_compiled_pandoc_with_ghcjs_and_built_an/ Reddit Thread], [https://github.com/osener/markup.rocks Source code].<br />
<br />
=== Haste ===<br />
<br />
[http://haste-lang.org Website], [http://hackage.haskell.org/package/haste-compiler Hackage]<br />
<br />
* Seamless, type-safe single program framework for client-server communication<br />
* Easy JavaScript interoperability<br />
* Generates small, fast, minifiable code.<br />
* Lightweight concurrency, Cabal integration, FFI and GHC extensions supported.<br />
* Cross platform.<br />
* Works.<br />
<br />
=== [[JMacro]] ===<br />
<br />
On the Haskell wiki (see above) and on [http://hackage.haskell.org/package/jmacro Hackage]<br />
<br />
* Mature, Maintained<br />
* Not Haskell but an EDSL _in_ Haskell nonetheless.<br />
* JMacro Panels provides a purely Haskell combinator library that generates dynamically updating html and js with asynchronous client-server communication.<br />
* Syntax is a fusion of Haskell and JavaScript<br />
* Untyped, but with syntactic correctness (at least) enforced at compile-time.<br />
* Embeddable through quasi-quoting<br />
* Support for various forms of code-generation<br />
<br />
=== Others ===<br />
<br />
* [https://github.com/johang88/haskellinjavascript Haskell interpreter in JS] — An interpreter. Haven't tried but is apparently dead.<br />
* YHC JS backend — Beta-ish. Apparently works, but I was unable to compile YHC, so haven't tried yet. I would be interested in anyone's experience using it. There's [http://www.haskell.org/haskellwiki/Yhc/Javascript an old wiki page] about Yhc's JavaScript support, but Yhc itself is a dead project.<br />
* Emscripten — not Haskell→JS, but compiles LLVM/Clang output to JavaScript. Could possibly be used for GHC→LLVM→JS compiling, which I tried, and works, but would have to also compile the GHC runtime which is not straight-forward (to me) for it to actually run. <br />
* HJScript — Beta. EDSL, not Haskell→JS. Works. Not ''very'' annoying to program in, but is JS semantics, not Haskell. Hackage package [http://hackage.haskell.org/package/HJScript here.]<br />
* Some have also tried writing a Haskell→JS compiler to make a more direct JS-aware translation of code (to not have huge code output a la GHCJS, YHC, Emscripten).<br />
* I've tried [http://lpaste.net/84342 compiling via JHC and Emscripten] a while ago, which worked, but IIRC the output was rather slow.<br />
* It's also possible to compile Hugs via Emscripten, which works (with minor tweaks), but again, it's too slow.<br />
<br />
<br />
== FP -> JS ==<br />
<br />
=== Ur/Web ===<br />
<br />
http://www.impredicative.com/ur/<br />
<br />
Perhaps the problem with Ur is that they are selling both a backend and a frontend together. Being a new language, the backend is lacking in libraries to be practical for many tasks. However, there is an RSS reader that is using Ur for the front-end and Haskell for the backend: https://bazqux.com/<br />
<br />
=== Opa ===<br />
<br />
Similar to Ur/Web, write one language in the front-end and backend: http://opalang.org/ Haven't tried it. No idea what its type-system is like.<br />
<br />
=== OCaml ===<br />
<br />
The OCaml -> JS compiler is supposed to be good, it is now used at Facebook for an internal in-browser code editor.<br />
http://ocsigen.org/js_of_ocaml/<br />
<br />
=== GorillaScript ===<br />
<br />
http://ckknight.github.io/gorillascript/<br />
<br />
immutable by default, global type inference, macros, what coffeescript should have been. The syntax is similar to coffeescript<br />
<br />
=== Roy ===<br />
<br />
[http://roy.brianmckenna.org/ Roy]: meld JavaScript semantics with functional languages. Experimental, but has many bread-and-butter Haskell features.<br />
Roy is written in JS.<br />
<br />
=== PureScript ===<br />
<br />
[http://purescript.org/ PureScript] aims to provide a type system for a fragment of JavaScript. It includes many features which are similar to features of Haskell, such as type classes and RankNTypes, and its syntax mirrors that of Haskell very closely, but it is a fundamentally different language with the execution model of JavaScript. PureScript is written in Haskell. The project has a focus on the generation of efficient, readable JavaScript.<br />
<br />
=== Idris ===<br />
<br />
Idris is a compiled language with dependent types, implemented in Haskell, with backends for both LLVM and JavaScript. Experimental.<br />
<br />
* Full dependent types with dependent pattern matching where clauses, with rule, simple case expressions, pattern matching let and lambda bindings<br />
* Dependent records with projection and update<br />
* Type classes<br />
* Monad comprehensions<br />
* Syntactic conveniences for lists, tuples, dependent pairs do notation and idiom brackets<br />
* Indentation significant syntax<br />
* Extensible syntax<br />
* Tactic based theorem proving (influenced by Coq)<br />
* Cumulative universes<br />
* Totality checking<br />
* Simple foreign function interface (to C)<br />
* Hugs style interactive environment<br />
<br />
Links:<br />
* [http://idris-lang.org/ Website idris-lang.org] <br />
* [[Dependent_type|Dependent Type in haskell wiki]]<br />
* [http://en.wikipedia.org/wiki/Dependent_type WP (en) Dependent type] (with Idris listed under language comparison)<br />
<br />
== Links ==<br />
<br />
* [http://www.reddit.com/r/haskell/comments/28o7my/what_is_the_state_of_the_javascript_problem_what/ What is the state of "The JavaScript Problem"? What is the currently preferred way to solve in a real world application?] (reddit, 2014-06-20)<br />
* [https://github.com/yesodweb/yesod/wiki/JavaScript-Options Yesod - JavaScript Options]<br />
* [http://chrisdone.com/tags/javascript Chris Done Blog] - Tag: JavaScript<br />
<br />
== Footnotes ==<br />
<br />
# Its support for closures is commonly noted as being one of JavaScript’s redeeming features.<br />
# Early binding allows for static verification of the existence of method-signature pairs (e.g. v-tables). Late binding does not give the compiler (or an IDE) enough information for existence verification, it has to be looked up at run-time.<br />
# There are several hinting libraries, which developers insist are indispensable tools when developing JavaScript seriously, such as JavaScript lint, JSLint, and JSure.<br />
# “Any non-trivial analysis is very difficult due to JavaScript’s dynamic nature.” — Berke Durak, Ph.D., author of jsure.<br />
# Google Inc. thought it necessary to develop a compiler, Google Closure, which does type-checking and limited inference.<br />
<br />
<br />
[[Category:Web|*]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Performance&diff=59952Performance2015-08-04T05:55:21Z<p>Beerdude26: /* More information */ Added more links + layout</p>
<hr />
<div>{{Performance infobox}}<br />
Welcome to the '''Haskell Performance Resource''', the collected wisdom on how to make your Haskell programs go faster. <br />
<br />
== Introduction ==<br />
<br />
One question that often comes up is along the general lines of "Can I write this program in Haskell so that it performs as well as, or better than, the same program written in some other language?"<br />
<br />
This is a difficult question to answer in general because Haskell is a language, not an implementation. Performance can only be measured relative to a specific language implementation.<br />
<br />
Moreover, it's often not clear if two programs which supposedly have the same functionality really do the same thing. Different languages sometimes require very different ways of expressing the same intent. Certain types of bug are rare in typical Haskell programs that are more common in other languages and vice versa, due to strong typing, automatic memory management and lazy evaluation.<br />
<br />
Nonetheless, it is usually possible to write a Haskell program that performs as well as, or better than, the same program written in any other language. The main caveat is that you may have to modify your code significantly in order to improve its performance. Compilers such as GHC are good at eliminating layers of abstraction, but they aren't perfect, and often need some help. <br />
<br />
There are many non-invasive techniques: compiler options, for example. Then there are techniques that require adding some small amounts of performance cruft to your program: strictness annotations, for example. If you still don't get the best performance, though, it might be necessary to resort to larger refactorings.<br />
<br />
Sometimes the code tweaks required to get the best performance are non-portable, perhaps because they require language extensions that aren't implemented in all compilers (e.g. unboxing), or because they require using platform-specific features or libraries. This might not be acceptable in your setting.<br />
<br />
If the worst comes to the worst, you can always write your critical code in C and use the FFI to call it. Beware of the boundaries though - marshaling data across the FFI can be expensive, and multi-language memory management can be complex and error-prone. It's usually better to stick to Haskell if possible.<br />
<br />
== Basic techniques ==<br />
<br />
The key tool to use in making your Haskell program run faster is ''profiling''. Profiling is provided by [[GHC]] and [[nhc98]]. There is ''no substitute'' for finding where your program's time/space is ''really'' going, as opposed to where you imagine it is going.<br />
<br />
Another point to bear in mind: By far the best way to improve a program's performance ''dramatically'' is to use better algorithms. Once profiling has thrown the spotlight on the guilty time-consumer(s), it may be better to re-think your program than to try all the tweaks listed below.<br />
<br />
Another extremely efficient way to make your program snappy is to use library code that has been Seriously Tuned By Someone Else. You ''might'' be able to write a better sorting function than the one in <tt>Data.List</tt>, but it will take you much longer than typing <tt>import Data.List</tt>.<br />
<br />
We have chosen to organise the rest of this resource first by Haskell construct (data types, pattern matching, integers), and then within each category to describe techniques that apply across implementations, and also techniques that are specific to a certain Haskell implementation (e.g. GHC). There are some implementation-specific techniques that apply in general - those are linked from the [[Haskell Performance Resource#General_Implementation-Specific_Techniques | General Implementation-Specific Techniques]] section below.<br />
<br />
== Haskell constructs ==<br />
<br />
* [[/Data Types/]]<br />
* [[/Functions/]]<br />
* [[/Overloading/]]<br />
* [[/FFI/]]<br />
* [[/Arrays/]]<br />
* [[/Strings/]]<br />
* [[/Integers/]]<br />
* [[/IO | I/O ]]<br />
* [[/Floating Point/]]<br />
* [[/Concurrency/]]<br />
* [[/Modules/]]<br />
* [[/Monads/]]<br />
<br />
== General techniques ==<br />
<br />
The most important thing for a beginner to keep in mind is that Haskell programs are evaluated using [[lazy evaluation]], which has many advantages, but also drawbacks when it comes to performance. The rule of thumb is that if you really want to have explicit control over the evaluation, then you should try to avoid it.<br />
<br />
* [[/Strictness/]]<br />
* [[/Laziness/]]<br />
* [[/Space | Avoiding space leaks]]<br />
* [[/Accumulating parameter|Accumulating parameters]]<br />
* [[Stack_overflow|Avoiding stack overflow]]<br />
<br />
== Benchmarking libraries ==<br />
<br />
The [[Criterion]] library is used often to generate detailed benchmark results.<br />
<br />
* [http://www.serpentine.com/blog/2009/09/29/criterion-a-new-benchmarking-library-for-haskell/ Introductory blog post]<br />
* [http://www.stackbuilders.com/news/obverse-versus-reverse-benchmarking-in-haskell-with-criterion Simple example of using Criterion]<br />
<br />
== Compiler specific techniques ==<br />
<br />
* [[/GHC/]]<br />
* [[/NHC98| nhc98]]<br />
* [[/Hugs/]]<br />
* [[/Yhc/]]<br />
* [[/JHC/]]<br />
<br />
== More information ==<br />
<br />
* [https://donsbot.wordpress.com/2008/06/04/haskell-as-fast-as-c-working-at-a-high-altitude-for-low-level-performance/ Haskell as fast as C: working at a high altitude for low level performance]<br />
<br />
* [http://www.randomhacks.net/2007/01/22/high-performance-haskell/ High-Performance Haskell] ([http://www.slideshare.net/tibbe/highperformance-haskell Slide deck])<br />
<br />
* [http://lambda.jstolarek.com/2013/04/haskell-as-fast-as-c-a-case-study/ Haskell as fast as C: A case study]<br />
<br />
* [https://www.youtube.com/watch?v=McFNkLPTOSY&feature=youtu.be Dan Doel - Introduction to Low Level Haskell Optimization]; a video of Dan Doel's talk at the Boston Haskell Meetup, Sept 17, 2014 ([http://hub.darcs.net/dolio/optimization-talk-demo/browse/slides/slides.md slides]).<br />
<br />
* Don Stewart's [http://stackoverflow.com/questions/3276240/tools-for-analyzing-performance-of-a-haskell-program/3276557#3276557 Haskell performance overview] on StackOverflow (2013)<br />
<br />
* There are plenty of good examples of Haskell code written for performance in the [http://benchmarksgame.alioth.debian.org/ The Computer Language Benchmarks Game]<br />
<br />
* And many alternatives, with discussion, on this wiki: [[Benchmarks Game]] and on the [http://web.archive.org/web/20060209215702/http://haskell.org/hawiki/ShootoutEntry old Haskell wiki] (in the Web Archive)<br />
<br />
* There are ~100 [http://blog.johantibell.com/2010/09/slides-from-my-high-performance-haskell.html slides on High-Performance Haskell] from the 2010 CUFP tutorial on that topic.<br />
<br />
== Specific comparisons of data structures ==<br />
<br />
=== Ropes ===<br />
<br />
For modifying very long strings, a [https://en.wikipedia.org/wiki/Rope_(data_structure) rope data structure] is very efficient. The [https://hackage.haskell.org/package/rope rope] package provides these.<br />
<br />
=== Data.Sequence vs. lists ===<br />
<br />
Data.Sequence has complexity O(log(min(i,n-i))) for access, insertion and update to position i of a sequence of length n.<br />
<br />
List has complexity O(i).<br />
<br />
List is a non-trivial constant-factor faster for operations at the head (cons and head), making it a more efficient choice for stack-like and stream-like access patterns. Data.Sequence is faster for every other access pattern, such as queue and random access.<br />
<br />
See the following program for proof:<br />
<haskell><br />
import Data.Sequence<br />
<br />
insert_million 0 sequence = sequence<br />
insert_million n sequence = insert_million (n - 1)(sequence |> n)<br />
<br />
main = print (Data.Sequence.length (insert_million 1000000 empty))<br />
</haskell><br />
<pre><br />
$ ghc -O2 --make InsertMillionElements.hs && time ./InsertMillionElements +RTS -K100M<br />
1000000<br />
real 0m7.238s<br />
user 0m6.804s<br />
sys 0m0.228s<br />
</pre><br />
<haskell><br />
insert_million 0 list = reverse list<br />
insert_million n list = insert_million (n -1) (n:list)<br />
<br />
main = print (length (insert_million 1000000 []))<br />
</haskell><br />
<pre><br />
$ ghc -O2 --make InsertMillionElements.hs && time ./InsertMillionElementsList +RTS -K100M<br />
1000000<br />
real 0m0.588s<br />
user 0m0.528s<br />
sys 0m0.052s<br />
</pre><br />
Lists are substantially faster on this micro-benchmark.<br />
<br />
A sequence uses between 5/6 and 4/3 times as much space as the equivalent list (assuming an overhead of one word per node, as in GHC).<br />
If only deque operations are used, the space usage will be near the lower end of the range, because all internal nodes will be ternary.<br />
Heavy use of split and append will result in sequences using approximately the same space as lists.<br />
In detail:<br />
* a list of length ''n'' consists of ''n'' cons nodes, each occupying 3 words.<br />
* a sequence of length ''n'' has approximately ''n''/(''k''-1) nodes, where ''k'' is the average arity of the internal nodes (each 2 or 3). There is a pointer, a size and overhead for each node, plus a pointer for each element, i.e. ''n''(3/(''k''-1) + 1) words.<br />
<br />
== Additional Tips ==<br />
<br />
* Use strict returns ( return $! ...) unless you absolutely need them lazy.<br />
* Profile, profile, profile - understand who is allocated the memory on your heap (+RTS -hc) and how it's being used (+RTS -hb).<br />
* Use +RTS -p to understand who's doing all the allocations and where your time is being spent.<br />
* Approach profiling like a science experiment - make one change, observe if anything is different, rollback and make another change - observe the change. Keep notes!<br />
* Use [[ThreadScope]] to visualize GHC eventlog traces.<br />
<br />
[[Category:Idioms]]<br />
[[Category:Language]]<br />
[[Category:Performance|*]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Performance&diff=59951Performance2015-08-04T05:51:27Z<p>Beerdude26: /* Specific comparisons of data structures */ Added ropes</p>
<hr />
<div>{{Performance infobox}}<br />
Welcome to the '''Haskell Performance Resource''', the collected wisdom on how to make your Haskell programs go faster. <br />
<br />
== Introduction ==<br />
<br />
One question that often comes up is along the general lines of "Can I write this program in Haskell so that it performs as well as, or better than, the same program written in some other language?"<br />
<br />
This is a difficult question to answer in general because Haskell is a language, not an implementation. Performance can only be measured relative to a specific language implementation.<br />
<br />
Moreover, it's often not clear if two programs which supposedly have the same functionality really do the same thing. Different languages sometimes require very different ways of expressing the same intent. Certain types of bug are rare in typical Haskell programs that are more common in other languages and vice versa, due to strong typing, automatic memory management and lazy evaluation.<br />
<br />
Nonetheless, it is usually possible to write a Haskell program that performs as well as, or better than, the same program written in any other language. The main caveat is that you may have to modify your code significantly in order to improve its performance. Compilers such as GHC are good at eliminating layers of abstraction, but they aren't perfect, and often need some help. <br />
<br />
There are many non-invasive techniques: compiler options, for example. Then there are techniques that require adding some small amounts of performance cruft to your program: strictness annotations, for example. If you still don't get the best performance, though, it might be necessary to resort to larger refactorings.<br />
<br />
Sometimes the code tweaks required to get the best performance are non-portable, perhaps because they require language extensions that aren't implemented in all compilers (e.g. unboxing), or because they require using platform-specific features or libraries. This might not be acceptable in your setting.<br />
<br />
If the worst comes to the worst, you can always write your critical code in C and use the FFI to call it. Beware of the boundaries though - marshaling data across the FFI can be expensive, and multi-language memory management can be complex and error-prone. It's usually better to stick to Haskell if possible.<br />
<br />
== Basic techniques ==<br />
<br />
The key tool to use in making your Haskell program run faster is ''profiling''. Profiling is provided by [[GHC]] and [[nhc98]]. There is ''no substitute'' for finding where your program's time/space is ''really'' going, as opposed to where you imagine it is going.<br />
<br />
Another point to bear in mind: By far the best way to improve a program's performance ''dramatically'' is to use better algorithms. Once profiling has thrown the spotlight on the guilty time-consumer(s), it may be better to re-think your program than to try all the tweaks listed below.<br />
<br />
Another extremely efficient way to make your program snappy is to use library code that has been Seriously Tuned By Someone Else. You ''might'' be able to write a better sorting function than the one in <tt>Data.List</tt>, but it will take you much longer than typing <tt>import Data.List</tt>.<br />
<br />
We have chosen to organise the rest of this resource first by Haskell construct (data types, pattern matching, integers), and then within each category to describe techniques that apply across implementations, and also techniques that are specific to a certain Haskell implementation (e.g. GHC). There are some implementation-specific techniques that apply in general - those are linked from the [[Haskell Performance Resource#General_Implementation-Specific_Techniques | General Implementation-Specific Techniques]] section below.<br />
<br />
== Haskell constructs ==<br />
<br />
* [[/Data Types/]]<br />
* [[/Functions/]]<br />
* [[/Overloading/]]<br />
* [[/FFI/]]<br />
* [[/Arrays/]]<br />
* [[/Strings/]]<br />
* [[/Integers/]]<br />
* [[/IO | I/O ]]<br />
* [[/Floating Point/]]<br />
* [[/Concurrency/]]<br />
* [[/Modules/]]<br />
* [[/Monads/]]<br />
<br />
== General techniques ==<br />
<br />
The most important thing for a beginner to keep in mind is that Haskell programs are evaluated using [[lazy evaluation]], which has many advantages, but also drawbacks when it comes to performance. The rule of thumb is that if you really want to have explicit control over the evaluation, then you should try to avoid it.<br />
<br />
* [[/Strictness/]]<br />
* [[/Laziness/]]<br />
* [[/Space | Avoiding space leaks]]<br />
* [[/Accumulating parameter|Accumulating parameters]]<br />
* [[Stack_overflow|Avoiding stack overflow]]<br />
<br />
== Benchmarking libraries ==<br />
<br />
The [[Criterion]] library is used often to generate detailed benchmark results.<br />
<br />
* [http://www.serpentine.com/blog/2009/09/29/criterion-a-new-benchmarking-library-for-haskell/ Introductory blog post]<br />
* [http://www.stackbuilders.com/news/obverse-versus-reverse-benchmarking-in-haskell-with-criterion Simple example of using Criterion]<br />
<br />
== Compiler specific techniques ==<br />
<br />
* [[/GHC/]]<br />
* [[/NHC98| nhc98]]<br />
* [[/Hugs/]]<br />
* [[/Yhc/]]<br />
* [[/JHC/]]<br />
<br />
== More information ==<br />
<br />
* [https://www.youtube.com/watch?v=McFNkLPTOSY&feature=youtu.be Dan Doel - Introduction to Low Level Haskell Optimization]; a video of Dan Doel's talk at the Boston Haskell Meetup, Sept 17, 2014 ([http://hub.darcs.net/dolio/optimization-talk-demo/browse/slides/slides.md slides]).<br />
* Don Stewart's [http://stackoverflow.com/questions/3276240/tools-for-analyzing-performance-of-a-haskell-program/3276557#3276557 Haskell performance overview] on StackOverflow (2013)<br />
* There are plenty of good examples of Haskell code written for performance in the [http://benchmarksgame.alioth.debian.org/ The Computer Language Benchmarks Game]<br />
* And many alternatives, with discussion, on this wiki: [[Benchmarks Game]] and on the [http://web.archive.org/web/20060209215702/http://haskell.org/hawiki/ShootoutEntry old Haskell wiki] (in the Web Archive)<br />
* There are ~100 [http://blog.johantibell.com/2010/09/slides-from-my-high-performance-haskell.html slides on High-Performance Haskell] from the 2010 CUFP tutorial on that topic.<br />
<br />
== Specific comparisons of data structures ==<br />
<br />
=== Ropes ===<br />
<br />
For modifying very long strings, a [https://en.wikipedia.org/wiki/Rope_(data_structure) rope data structure] is very efficient. The [https://hackage.haskell.org/package/rope rope] package provides these.<br />
<br />
=== Data.Sequence vs. lists ===<br />
<br />
Data.Sequence has complexity O(log(min(i,n-i))) for access, insertion and update to position i of a sequence of length n.<br />
<br />
List has complexity O(i).<br />
<br />
List is a non-trivial constant-factor faster for operations at the head (cons and head), making it a more efficient choice for stack-like and stream-like access patterns. Data.Sequence is faster for every other access pattern, such as queue and random access.<br />
<br />
See the following program for proof:<br />
<haskell><br />
import Data.Sequence<br />
<br />
insert_million 0 sequence = sequence<br />
insert_million n sequence = insert_million (n - 1)(sequence |> n)<br />
<br />
main = print (Data.Sequence.length (insert_million 1000000 empty))<br />
</haskell><br />
<pre><br />
$ ghc -O2 --make InsertMillionElements.hs && time ./InsertMillionElements +RTS -K100M<br />
1000000<br />
real 0m7.238s<br />
user 0m6.804s<br />
sys 0m0.228s<br />
</pre><br />
<haskell><br />
insert_million 0 list = reverse list<br />
insert_million n list = insert_million (n -1) (n:list)<br />
<br />
main = print (length (insert_million 1000000 []))<br />
</haskell><br />
<pre><br />
$ ghc -O2 --make InsertMillionElements.hs && time ./InsertMillionElementsList +RTS -K100M<br />
1000000<br />
real 0m0.588s<br />
user 0m0.528s<br />
sys 0m0.052s<br />
</pre><br />
Lists are substantially faster on this micro-benchmark.<br />
<br />
A sequence uses between 5/6 and 4/3 times as much space as the equivalent list (assuming an overhead of one word per node, as in GHC).<br />
If only deque operations are used, the space usage will be near the lower end of the range, because all internal nodes will be ternary.<br />
Heavy use of split and append will result in sequences using approximately the same space as lists.<br />
In detail:<br />
* a list of length ''n'' consists of ''n'' cons nodes, each occupying 3 words.<br />
* a sequence of length ''n'' has approximately ''n''/(''k''-1) nodes, where ''k'' is the average arity of the internal nodes (each 2 or 3). There is a pointer, a size and overhead for each node, plus a pointer for each element, i.e. ''n''(3/(''k''-1) + 1) words.<br />
<br />
== Additional Tips ==<br />
<br />
* Use strict returns ( return $! ...) unless you absolutely need them lazy.<br />
* Profile, profile, profile - understand who is allocated the memory on your heap (+RTS -hc) and how it's being used (+RTS -hb).<br />
* Use +RTS -p to understand who's doing all the allocations and where your time is being spent.<br />
* Approach profiling like a science experiment - make one change, observe if anything is different, rollback and make another change - observe the change. Keep notes!<br />
* Use [[ThreadScope]] to visualize GHC eventlog traces.<br />
<br />
[[Category:Idioms]]<br />
[[Category:Language]]<br />
[[Category:Performance|*]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Performance/Strictness&diff=59950Performance/Strictness2015-08-04T05:46:18Z<p>Beerdude26: /* Explicit strictness */ Added reference to Performance/Data_types</p>
<hr />
<div>{{Performance infobox}}<br />
[[Category:Performance|Strictness]]<br />
Haskell is a non-strict language, and most implementations use a strategy called ''laziness'' to run your program. Basically laziness == non-strictness + sharing.<br />
<br />
[[Performance/Laziness|Laziness]] can be a useful tool for improving performance, but more often than not it reduces performance by adding a constant overhead to everything. Because of laziness, the compiler can't evaluate a function argument and pass the value to the function, it has to record the expression in the heap in a ''suspension'' (or ''[[thunk]]'') in case it is evaluated later. Storing and evaluating suspensions is costly, and unnecessary if the expression was going to be evaluated anyway. <br />
<br />
== Strictness analysis ==<br />
<br />
Optimising compilers like GHC try to reduce the cost of laziness using ''strictness analysis'', which attempts to determine which function arguments are always evaluated by the function, and hence can be evaluated by the caller instead. Sometimes this leads to bigger gains; a strict <code-haskell>Int</code-haskell> can be passed as an unboxed value, for example. Strictness analysis sometimes does wonderful things; for example it is very good at optimising <code-haskell>fac</code-haskell>:<br />
<pre-haskell><br />
fac :: Int -> Int<br />
fac n = if n <= 1 then 1 else n * fac (n-1)<br />
</pre-haskell><br />
<br />
Strictness analysis can spot the fact that the argument <code-haskell>n</code-haskell> is strict, and can be represented unboxed. The resulting function won't use any heap while it is running, as you'd expect.<br />
<br />
The common case of misunderstanding of strictness analysis is when [[Fold|folding]] (reducing) lists. If this program<br />
<pre-haskell><br />
main = print (foldl (+) 0 [1..1000000])<br />
</pre-haskell><br />
is compiled in GHC without "-O" flag, it uses a lot of heap and stack. A programmer knows that the long list (<code-haskell>[1..1000000]</code-haskell>) is stored as a thunk, not fully, because the programmer read about [[non-strict semantics]] and [[lazy vs. non-strict]]. The programmer explicitly wrote <code-haskell>sum</code-haskell> as [[Tail recursion|tail recursive]], so the program should use a small amount of stack, because the programmer knows about [[stack overflow]]. So behavior of the program looks mysterious to the programmer.<br />
<br />
The programmer concludes that the program somehow decides to store the long list fully in the heap, or garbage collector is not able to remove dead prefix of the long list. Wrong. The long list is fine.<br />
<br />
Look at the definition from the standard library.<br />
<pre-haskell><br />
foldl :: (a -> b -> a) -> a -> [b] -> a<br />
foldl f z0 xs0 = lgo z0 xs0<br />
where<br />
lgo z [] = z<br />
lgo z (x:xs) = lgo (f z x) xs<br />
</pre-haskell><br />
<br />
<code-haskell>lgo</code-haskell>, instead of adding elements of the long list, creates '''a thunk''' for <code-haskell>(f z x)</code-haskell>. <code-haskell>z</code-haskell> is stored within that thunk, and <code-haskell>z</code-haskell> is a thunk also, created during the previous call to <code-haskell>lgo</code-haskell>. The program creates the long chain of thunks. Stack is bloated when evaluating that chain.<br />
<br />
With "-O" flag GHC performs strictness analysis, than it knows that <code-haskell>lgo</code-haskell> is strict in <code-haskell>z</code-haskell> argument, therefore thunks are not needed and are not created.<br />
<br />
== Limitations of strictness analysis ==<br />
<br />
It's easy to accidentally write functions that aren't strict, though. Often a lazy function can be sitting around eating up your performance, when making it strict wouldn't change the meaning of the program. For example:<br />
<pre-haskell><br />
suminit :: [Int] -> Int -> Int -> (Int,[Int])<br />
suminit xs len acc = case len == 0 of<br />
True -> (acc,xs)<br />
False -> case xs of<br />
[] -> (acc,[])<br />
x:xs -> suminit xs (len-1) (acc+x)<br />
main = print (fst (suminit [1..] 1000000 0))<br />
</pre-haskell><br />
this function sums the first len elements of a list, returning the sum and the remaining list. We've already tried to improve performance by using an [[Performance/Accumulating parameter|accumulating parameter]]. However, the parameter <code-haskell>acc</code-haskell> isn't strict, because there's no guarantee that the caller will evaluate it. The compiler will use a fully boxed <code-haskell>Int</code-haskell> to represent <code-haskell>acc</code-haskell>, although it will probably use an unboxed <code-haskell>Int</code-haskell> to represent <code-haskell>len</code-haskell>. The expression <code-haskell>(acc+x)</code-haskell> will be saved as a suspension, rather than evaluated on the spot. (Incidentally, this is a common pattern we see crop up time and again in small recursive functions with a few parameters).<br />
<br />
== Explicit strictness ==<br />
<br />
We can make an argument strict explicitly.<br />
<br />
In the <code-haskell>foldl</code-haskell> example, replace <code-haskell>foldl</code-haskell> with <code-haskell>foldl'</code-haskell>.<br />
<br />
For <code-haskell>suminit</code-haskell>, we need to make <code-haskell>acc</code-haskell> strict. The way to do this is using <code-haskell>seq</code-haskell>:<br />
<pre-haskell><br />
suminit :: [Int] -> Int -> Int -> (Int,[Int])<br />
suminit xs len acc = acc `seq` case len == 0 of<br />
True -> (acc,xs)<br />
False -> case xs of<br />
[] -> (acc,[])<br />
x:xs -> suminit xs (len-1) (acc+x)<br />
</pre-haskell><br />
<br />
Some other languages (eg. Clean) have strictness annotations on types, which is a less ugly way to express this, but for now there are no Haskell compilers that support this.<br />
<br />
With the <tt>BangPatterns</tt> GHC extension enabled, the above can be written as<br />
<br />
{{Note|For strict data structures, see [[Performance/Data_types]].}}<br />
<br />
<pre-haskell><br />
suminit xs !len !acc = …<br />
</pre-haskell><br />
<br />
Incidentally, GHC will also eliminate the tuple returned by this function if the caller immediately deconstructs it.<br />
<br />
== Evaluating expressions strictly ==<br />
<br />
There's a useful variant of the infix application operator <code-haskell>($)</code-haskell> that evaluates its argument strictly: <code-haskell>($!)</code-haskell>. This can often be used to great effect in eliminating unnecessary suspensions that the compiler hasn't spotted. eg. in a function application<br />
<pre-haskell><br />
f (g x)<br />
</pre-haskell><br />
writing instead<br />
<pre-haskell><br />
f $! (g x)<br />
</pre-haskell><br />
will be more efficient if (a) you were going to evaluate <code-haskell>(g x)</code-haskell> anyway, and (b) <code-haskell>f</code-haskell> isn't visibly strict, or inlined. If <code-haskell>f</code-haskell> is strict or inlined, then the chances are that <code-haskell>($!)</code-haskell> is unnecessary cruft here.<br />
<br />
A good example is the monadic return. If you find yourself writing<br />
<pre-haskell><br />
do …<br />
…<br />
return (fn x)<br />
</pre-haskell><br />
<br />
then consider instead writing<br />
<pre-haskell><br />
do …<br />
…<br />
return $! fn x<br />
</pre-haskell><br />
it is very rare to actually need laziness in the argument of return here.<br />
<br />
Warning: Using any kind of strictness annotations as above can have unexpected impact on program semantics, in particular when certain optimizations are performed by the compiler. See [[correctness of short cut fusion]].<br />
<br />
== Rule of Thumb for Strictness Annotation ==<br />
<br />
A rule of thumb for when strictness annotation might be needed:<br />
<br />
When a function <code-haskell>f</code-haskell> with argument <code-haskell>x</code-haskell> satisfies both conditions:<br />
* <code-haskell>f</code-haskell> calls a function on a function of <code-haskell>x</code-haskell>: <code-haskell>(h (g x))</code-haskell><br />
* is not already strict in <code-haskell>x</code-haskell> (does not inspect <code-haskell>x</code-haskell>'s value), <br />
then it can be helpful to force evaluation:<br />
<br />
Example:<br />
<pre-haskell><br />
-- Force Strict: Make g's argument smaller.<br />
f x = g $! (h x)<br />
<br />
-- Don't force: f isn't building on x, so just let g deal with it.<br />
f x = g x <br />
<br />
-- Don't force: f is already strict in x<br />
f x = case x of <br />
0 -> (h (g x))<br />
</pre-haskell></div>Beerdude26https://wiki.haskell.org/index.php?title=Performance/GHC&diff=59949Performance/GHC2015-08-04T05:40:14Z<p>Beerdude26: /* Measuring performance */ Added link to ThreadScope</p>
<hr />
<div>{{Performance infobox}}<br />
[[Category:Performance|GHC]] [[Category:GHC]]<br />
Please report any overly-slow GHC-compiled programs. Since [[GHC]] doesn't have any credible competition in the performance department these days it's hard to say what overly-slow means, so just use your judgement! Of course, if a GHC compiled program runs slower than the same program compiled with another Haskell compiler, then it's definitely a bug. Furthermore, if an equivalent OCaml, SML or Clean program is faster, this ''might'' be a bug.<br />
<br />
== Use optimisation ==<br />
<br />
Optimise, using <tt>-O</tt> or <tt>-O2</tt>: this is the most basic way to make your program go faster. Compilation time will be slower, especially with <tt>-O2</tt>.<br />
<br />
At present, <tt>-O2</tt> is nearly indistinguishable from <tt>-O</tt>.<br />
<br />
GHCi cannot optimise interpreted code, so when using GHCi, compile critical modules using <tt>-O</tt> or <tt>-O2</tt>, then load them into GHCi.<br />
<br />
Here is a short summary of useful compile time flags:<br />
* <tt>-O</tt>:<br />
* <tt>-O2</tt>:<br />
* <tt>-funfolding-use-threshold=16</tt>: demand more inlining.<br />
* <tt>-fexcess-precision</tt>: see [[Performance/Floating_point]]<br />
* <tt>-optc-O3</tt>: Enables a suite of optimizations in the GCC compiler. See the [http://www.openbsd.org/cgi-bin/man.cgi?query=gcc&sektion=1 gcc(1) man-page] for details. (a C-compiler option).<br />
* <tt>-optc-ffast-math</tt>: A C-compiler option which allows it to be less strict with respect to the standard when compiling IEEE 754 floating point arithmetic. Math operations will not trap if something goes wrong and math operations will assume that NaN and +- Infinity are not in arguments or results. For most practical floating point processing, this is a non-issue and enabling the flag can speed up FP arithmetic by a considerable amount. Also see the gcc(1) man-page. (a C-compiler option).<br />
<br />
Other useful flags:<br />
* <tt>-ddump-simpl > core.txt</tt>: generate core.txt file (see below).<br />
<br />
<br />
== Measuring performance ==<br />
<br />
The first thing to do is measure the performance of your program, and find out whether all the time is being spent in the garbage collector or not. Run your program with the <tt>+RTS -sstderr</tt> option:<br />
<br />
$ ./clausify 20 +RTS -sstderr<br />
42,764,972 bytes allocated in the heap<br />
6,915,348 bytes copied during GC (scavenged)<br />
360,448 bytes copied during GC (not scavenged)<br />
36,616 bytes maximum residency (7 sample(s))<br />
<br />
81 collections in generation 0 ( 0.07s)<br />
7 collections in generation 1 ( 0.00s)<br />
<br />
2 Mb total memory in use<br />
<br />
INIT time 0.00s ( 0.00s elapsed)<br />
MUT time 0.65s ( 0.94s elapsed)<br />
GC time 0.07s ( 0.06s elapsed)<br />
EXIT time 0.00s ( 0.00s elapsed)<br />
Total time 0.72s ( 1.00s elapsed)<br />
<br />
%GC time 9.7% (6.0% elapsed)<br />
<br />
Alloc rate 65,792,264 bytes per MUT second<br />
<br />
Productivity 90.3% of total user, 65.1% of total elapsed<br />
<br />
{{Note|Hint: You can use [[ThreadScope]] to visualize GHC's output.}}<br />
<br />
This tells you how much time is being spent running the program itself (MUT time), and how much time spent in the garbage collector (GC time). <br />
<br />
If your program is doing a lot of GC, then your first priority should be to check for [[Memory leak|Space Leaks]] using [http://www.haskell.org/ghc/docs/latest/html/users_guide/prof-heap.html heap profiling], and then to try to reduce allocations by [http://www.haskell.org/ghc/docs/latest/html/users_guide/prof-time-options.html time and allocation profiling]. <br />
<br />
If you can't reduce the GC cost any further, then using more memory by tweaking the [http://www.haskell.org/ghc/docs/latest/html/users_guide/runtime-control.html#rts-options-gc GC options] will probably help. For example, increasing the default heap size with <tt>+RTS -H128m</tt> will reduce the number of GCs.<br />
<br />
If your program isn't doing too much GC, then you should proceed to [http://www.haskell.org/ghc/docs/latest/html/users_guide/prof-time-options.html time and allocation profiling] to see where the big hitters are.<br />
<br />
== Modules and separate compilation ==<br />
<br />
In general, splitting code across modules should not make programs less efficient. GHC does quite aggressive cross-module inlining: when you import a function f from another module M, GHC consults the "interface file" M.hi to get f's definition.<br />
<br />
For best results, ''use an explicit export list''. If you do, GHC can inline any non-exported functions that are only called once, even if they are very big. Without an explicit export list, GHC must assume that every function is exported, and hence (to avoid code bloat) is more conservative about inlining.<br />
<br />
There is one exception to the general rule that splitting code across modules does not harm performance. As mentioned above, if a non-exported non-recursive function is called exactly once, then it is inlined ''regardless of size'', because doing so does not cause code duplication. But if it's exported and is large, then its inlining is not exposed -- and even if it were it might not be inlined, because doing so duplicates its code an unknown number of times. You can change the threshold for (a) exposing and (b) using an inlining, with flags <tt>-funfolding-creation-threshold</tt> and <tt>-funfolding-use-threshold</tt> respectively.<br />
<br />
== Unboxed types ==<br />
<br />
When you are ''really'' desperate for speed, and you want to get right down to the &ldquo;raw bits.&rdquo; Please see [http://www.haskell.org/ghc/docs/latest/html/users_guide/primitives.html GHC Primitives] for some information about using unboxed types.<br />
<br />
This should be a last resort, however, since unboxed types and primitives are non-portable. Fortunately, it is usually not necessary to resort to using explicit unboxed types and primitives, because GHC's optimiser can do the work for you by inlining operations it knows about, and unboxing strict function arguments (see [[Performance/Strictness]]). Strict and unpacked constructor fields can also help a lot (see [[Performance/Data Types]]). Sometimes GHC needs a little help to generate the right code, so you might have to look at the Core output to see whether your tweaks are actually having the desired effect.<br />
<br />
One thing that can be said for using unboxed types and primitives is that you ''know'' you're writing efficient code, rather than relying on GHC's optimiser to do the right thing, and being at the mercy of changes in GHC's optimiser down the line. This may well be important to you, in which case go for it.<br />
<br />
=== An example ===<br />
<br />
Usually unboxing is not explicitly required (see the Core tutorial below), however there<br />
are circumstances where you require precise control over how your code is<br />
unboxed. The following program was at one point an entry in the<br />
[http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=ghc&lang2=ghc Great Language Shootout]. <br />
GHC did a good job unboxing the loop, but wouldn't generate the best loop. The<br />
solution was to unbox the loop function by hand, resulting in better code.<br />
<br />
The original code:<br />
<br />
loop :: Int -> Double -> Double<br />
loop d s = if d == 0 then s<br />
else loop (d-1) (s + 1/fromIntegral d)<br />
The hand-unboxed code (note that it is uglier, and harder to read):<br />
<br />
import GHC.Base<br />
import GHC.Float<br />
<br />
loop :: Int# -> Double# -> Double#<br />
loop d s = if d ==# 0# then s <br />
else loop (d -# 1#) (s +## (1.0## /## int2Double# d))<br />
<br />
GHC 6.4.1 compiles the first loop to:<br />
<br />
$wloop :: Int# -> Double# -> Double#<br />
$wloop = \ (ww_s2Ga :: Int#) (ww1_s2Ge :: Double#) -><br />
case Double# ww_s2Ga of wild_XC {<br />
__DEFAULT -><br />
case /## 1.0 (int2Double# wild_XC) of y_a2Cd { <br />
__DEFAULT -> $wloop (-# wild_XC 1) (+## ww1_s2Ge y_a2Cd)<br />
};<br />
0 -> ww1_s2Ge<br />
}<br />
<br />
And the second, unboxed loop is translated to<br />
<br />
loop1 :: Int# -> Double# -> Double#<br />
loop1 = \ (d_a1as :: Int#) (s_a1at :: Double#) -><br />
case Double# d_a1as of wild_B1 {<br />
__DEFAULT -> loop1 (-# wild_B1 1) (+## s_a1at (/## 1.0 (int2Double# wild_B1)));<br />
0 -> s_a1at<br />
}<br />
<br />
which contains 1 less case statement. The second version runs as fast as C, the<br />
first a bit slower. A similar problem was also solved with explicit unboxing in the [http://shootout.alioth.debian.org/gp4/benchmark.php?test=recursive&lang=all recursive benchmark entry].<br />
<br />
== Primops ==<br />
<br />
If you really, really need the speed, and other techniques don't seem to<br />
be helping, programming your code in raw GHC primops can sometimes do<br />
the job. As for unboxed types, you get some guarantees that your code's<br />
performance isn't subject to changes to the GHC optimisations, at the<br />
cost of more unreadable code.<br />
<br />
For example, in an imperative benchmark program a bottleneck was<br />
swapping two values. Raw primops solved the problem:<br />
<br />
swap i j a s =<br />
if i <# j then case readIntOffAddr# a i s of { (# s, x #) -><br />
case readIntOffAddr# a j s of { (# s, y #) -><br />
case writeIntOffAddr# a j x s of { s -><br />
case writeIntOffAddr# a i y s of { s -><br />
swap (i +# 1#) (j -# 1#) a s<br />
}}}}<br />
else (# s, () #)<br />
{-# INLINE swap #-}<br />
<br />
== Inlining ==<br />
<br />
GHC does a lot of inlining, which has a dramatic effect on performance.<br />
<br />
Without -O, GHC does inlining ''within'' a module, but no ''cross-module'' inlining. <br />
<br />
With -O, it does a lot of cross-module inlining. Indeed, generally<br />
speaking GHC will inline ''across'' modules just as much as it does<br />
''within'' modules, with a single large exception. If GHC sees that a<br />
function 'f' is called just once, it inlines it regardless of how big<br />
'f' is. But once 'f' is exported, GHC can never see that it's called<br />
exactly once, even if that later turns out to be the case. This<br />
inline-once optimisation is pretty important in practice. <br />
<br />
So: if you care about performance, do not export functions that are not used outside the module (i.e. use an explicit export list, and keep it as small as possible).<br />
<br />
Sometimes ''explicitly'' inlining critical chunks of code can help.<br />
The INLINE pragma can be used for this purpose; but not for recursive functions, since inlining them forever would obviously be a bad idea.<br />
<br />
If a function you want inlined contains a slow path, it can help a<br />
good deal to separate the slow path into its own function and NOINLINE<br />
it. <br />
<br />
== Looking at the Core ==<br />
<br />
GHC's compiler intermediate language can be very useful for improving<br />
the performance of your code. Core is a functional language much like a very<br />
stripped down Haskell (by design), so it's still readable, and still purely<br />
functional. The general technique is to iteratively inspect how the critical<br />
functions of your program are compiled to Core, checking that they're compiled<br />
in the most optimal manner. Sometimes GHC doesn't quite manage to unbox your<br />
function arguments, float out common subexpressions, or unfold loops ideally --<br />
but you'll only know if you read the Core.<br />
<br />
References:<br />
* [http://haskell.org/ghc/docs/papers/core.ps.gz An External Representation for the GHC Core Language], Andrew Tolmach<br />
* [http://research.microsoft.com/Users/simonpj/Papers/comp-by-trans-scp.ps.gz A transformation-based optimiser for Haskell], Simon L. Peyton Jones and Andre Santos<br />
* [http://research.microsoft.com/Users/simonpj/Papers/inlining/index.htm Secrets of the Glasgow Haskell Compiler Inliner], Simon L. Peyton Jones and Simon Marlow<br />
* [http://research.microsoft.com/users/simonpj/papers/spineless-tagless-gmachine.ps.gz Implementing lazy functional languages on stock hardware: the Spineless Tagless G-machine], Simon L. Peyton Jones<br />
<br />
== Core by example ==<br />
<br />
Here's a step-by-step guide to optimising a particular program, <br />
the [http://shootout.alioth.debian.org/gp4/benchmark.php?test=partialsums&lang=ghc&id=2 partial-sums problem] from the [http://shootout.alioth.debian.org Great Language Shootout]. We developed a number<br />
of examples on [http://haskell.org/haskellwiki/Shootout/Partial_sums Haskell shootout entry] page.<br />
<br />
Begin with the naive translation of the Clean entry (which was fairly quick):<br />
Lots of math in a tight loop.<br />
<br />
import System<br />
import Numeric<br />
<br />
main = do n <- getArgs >>= readIO . head<br />
let sums = loop 1 n 1 0 0 0 0 0 0 0 0 0<br />
fn (s,t) = putStrLn $ (showFFloat (Just 9) s []) ++ "\t" ++ t<br />
mapM_ (fn :: (Double, String) - IO ()) (zip sums names)<br />
<br />
names = ["(2/3)^k", "k^-0.5", "1/k(k+1)", "Flint Hills", "Cookson Hills"<br />
, "Harmonic", "Riemann Zeta", "Alternating Harmonic", "Gregory"]<br />
<br />
loop k n alt a1 a2 a3 a4 a5 a6 a7 a8 a9<br />
| k > n = [ a1, a2, a3, a4, a5, a6, a7, a8, a9 ]<br />
| otherwise = loop (k+1) n (-alt)<br />
(a1 + (2/3) ** (k-1))<br />
(a2 + k ** (-0.5))<br />
(a3 + 1 / (k * (k + 1)))<br />
(a4 + 1 / (k*k*k * sin k * sin k))<br />
(a5 + 1 / (k*k*k * cos k * cos k))<br />
(a6 + 1 / k)<br />
(a7 + 1 / (k*k))<br />
(a8 + alt / k)<br />
(a9 + alt / (2 * k - 1))<br />
<br />
Compiled with '''-O2''' it runs. However, the performance is ''really'' bad.<br />
Somewhere greater than 128M heap -- in fact eventually running out of<br />
memory. A classic space leak. So look at the generated Core. <br />
<br />
=== Inspect the Core ===<br />
<br />
The best way to check the Core that GHC generates is with the<br />
'''-ddump-simpl''' flag (dump the results after code simplification, and<br />
after all optimisations are run). The result can be verbose, so pipe it into a pager.<br />
<br />
Looking for the 'loop', we find that it has been compiled to a function with<br />
the following type:<br />
<br />
$sloop_r2U6 :: GHC.Prim.Double#<br />
-> GHC.Float.Double<br />
-> GHC.Float.Double<br />
-> GHC.Float.Double<br />
-> GHC.Float.Double<br />
-> GHC.Float.Double<br />
-> GHC.Float.Double<br />
-> GHC.Float.Double<br />
-> GHC.Float.Double<br />
-> GHC.Float.Double<br />
-> GHC.Float.Double<br />
-> GHC.Prim.Double#<br />
-> [GHC.Float.Double]<br />
<br />
Hmm, I certainly don't want boxed doubles in such a tight loop (boxed values<br />
are represented as pointers to closures on the heap, unboxed values are raw<br />
machine values).<br />
<br />
=== Strictify ===<br />
<br />
The next step then is to encourage GHC to unbox this loop, by providing some<br />
strictness annotations. So rewrite the loop like this:<br />
<br />
loop k n alt a1 a2 a3 a4 a5 a6 a7 a8 a9<br />
| () !k !n !alt !a1 !a2 !a3 !a4 !a5 !a6 !a7 !a8 !a9 !False = undefined<br />
| k > n = [ a1, a2, a3, a4, a5, a6, a7, a8, a9 ]<br />
| otherwise = loop (k+1) n (-alt)<br />
(a1 + (2/3) ** (k-1))<br />
(a2 + k ** (-0.5))<br />
(a3 + 1 / (k * (k + 1)))<br />
(a4 + 1 / (k*k*k * sin k * sin k))<br />
(a5 + 1 / (k*k*k * cos k * cos k))<br />
(a6 + 1 / k)<br />
(a7 + 1 / (k*k))<br />
(a8 + alt / k)<br />
(a9 + alt / (2 * k - 1)) where x ! y = x `seq` y<br />
<br />
Here the first guard is purely a syntactic trick to inform ghc that the<br />
arguments should be strictly evaluated. I've played a little game here, using<br />
'''!''' for '''`seq`''' is reminiscent of the new bang-pattern proposal for<br />
strictness. Let's see how this compiles. Strictifying all args GHC produces an<br />
inner loop of:<br />
<br />
$sloop_r2WS :: GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> GHC.Prim.Double#<br />
-> [GHC.Float.Double]<br />
<br />
Ah! perfect. Let's see how that runs:<br />
<br />
$ ghc Naive.hs -O2 -no-recomp<br />
$ time ./a.out 2500000<br />
3.000000000 (2/3)^k<br />
3160.817621887 k^-0.5<br />
0.999999600 1/k(k+1)<br />
30.314541510 Flint Hills<br />
42.995233998 Cookson Hills<br />
15.309017155 Harmonic<br />
1.644933667 Riemann Zeta<br />
0.693146981 Alternating Harmonic<br />
0.785398063 Gregory<br />
./a.out 2500000 4.45s user 0.02s system 99% cpu 4.482 total<br />
<br />
=== Crank up the gcc flags ===<br />
<br />
Not too bad. No space leak and quite zippy. But let's see what more can be<br />
done. First, double arithmetic usually (always?) benefits from<br />
-fexcess-precision, and cranking up the flags to gcc:<br />
<br />
paprika$ ghc Naive.hs -O2 -fexcess-precision -optc-O3 -optc-ffast-math -no-recomp<br />
paprika$ time ./a.out 2500000<br />
3.000000000 (2/3)^k<br />
3160.817621887 k^-0.5<br />
0.999999600 1/k(k+1)<br />
30.314541510 Flint Hills<br />
42.995233998 Cookson Hills<br />
15.309017155 Harmonic<br />
1.644933667 Riemann Zeta<br />
0.693146981 Alternating Harmonic<br />
0.785398063 Gregory<br />
./a.out 2500000 3.71s user 0.01s system 99% cpu 3.726 total<br />
<br />
Even better! Now, let's dive into the Core to see if there are any optimisation<br />
opportunites that GHC missed. So add '''-ddump-simpl''' and peruse the output.<br />
<br />
=== Common subexpressions ===<br />
<br />
Looking at the Core, I see firstly that some of the common subexpressions<br />
haven't been factored out:<br />
<br />
case [GHC.Float.Double] GHC.Prim./## 1.0<br />
(GHC.Prim.*## (GHC.Prim.*##<br />
(GHC.Prim.*## (GHC.Prim.*## sc10_s2VS sc10_s2VS) sc10_s2VS)<br />
(GHC.Prim.sinDouble# sc10_s2VS))<br />
(GHC.Prim.sinDouble# sc10_s2VS))<br />
<br />
Multiple calls to '''sin'''. Hmm... And similar for '''cos''' and '''k*k'''. <br />
Simon Peyton-Jones says:<br />
<br />
GHC doesn't do full CSE. It'd be a relatively easy pass for someone to<br />
add, but it can cause space leaks. And it can replace two<br />
strictly-evaluated calls with one lazy thunk:<br />
let { x = case e of ....; y = case e of .... } in ...<br />
==><br />
let { v = e; x = case v of ...; y = case v of ... } in ...<br />
<br />
Instead GHC does "opportunistic CSE". If you have<br />
let x = e in .... let y = e in ....<br />
then it'll discard the duplicate binding. But that's very weak.<br />
<br />
So it looks like we might have to float out the commmon subexpressions by hand.<br />
The inner loop now looks like:<br />
<br />
loop k n alt a1 a2 a3 a4 a5 a6 a7 a8 a9<br />
| () !k !n !alt !a1 !a2 !a3 !a4 !a5 !a6 !a7 !a8 !a9 !False = undefined<br />
| k > n = [ a1, a2, a3, a4, a5, a6, a7, a8, a9 ]<br />
| otherwise = loop (k+1) n (-alt)<br />
(a1 + (2/3) ** (k-1))<br />
(a2 + k ** (-0.5))<br />
(a3 + 1 / (k * (k + 1)))<br />
(a4 + 1 / (k3 * sk * sk))<br />
(a5 + 1 / (k3 * ck * ck))<br />
(a6 + 1 / k)<br />
(a7 + 1 / k2)<br />
(a8 + alt / k)<br />
(a9 + alt / (2 * k - 1))<br />
where sk = sin k<br />
ck = cos k<br />
k2 = k * k<br />
k3 = k2 * k<br />
x ! y = x `seq` y<br />
<br />
looking at the Core shows the sins are now allocated and shared:<br />
<br />
let a9_s2MI :: GHC.Prim.Double#<br />
a9_s2MI = GHC.Prim.sinDouble# sc10_s2Xa<br />
<br />
So the common expressions are floated out, and it now runs:<br />
<br />
paprika$ time ./a.out 2500000 <br />
3160.817621887 k^-0.5<br />
0.999999600 1/k(k+1)<br />
30.314541510 Flint Hills<br />
42.995233998 Cookson Hills<br />
15.309017155 Harmonic<br />
1.644933667 Riemann Zeta<br />
0.693146981 Alternating Harmonic<br />
0.785398063 Gregory<br />
./a.out 2500000 3.29s user 0.00s system 99% cpu 3.290 total<br />
<br />
Faster. So we gained 12% by floating out those common expressions.<br />
<br />
See also the [[GCD inlining strictness and CSE]] for another example of<br />
where CSE should be performed to improve performance.<br />
<br />
=== Strength reduction ===<br />
<br />
Finally, another trick -- manual <br />
[http://en.wikipedia.org/wiki/Strength_reduction strength reduction]. When I checked the C<br />
entry, it used an integer for the k parameter to the loop, and cast it<br />
to a double for the math each time around, so perhaps we can make it an<br />
Int parameter. Secondly, the alt parameter only has it's sign flipped<br />
each time, so perhaps we can factor out the alt / k arg (it's either 1 /<br />
k or -1 on k), saving a division. Thirdly, '''(k ** (-0.5))''' is just a<br />
slow way of doing a '''sqrt'''.<br />
<br />
The final loop looks like:<br />
<br />
loop i n alt a1 a2 a3 a4 a5 a6 a7 a8 a9<br />
| i !n !alt !a1 !a2 !a3 !a4 !a5 !a6 !a7 !a8 !a9 !False = undefined -- strict<br />
| k > n = [ a1, a2, a3, a4, a5, a6, a7, a8, a9 ]<br />
| otherwise = loop (i+1) n (-alt)<br />
(a1 + (2/3) ** (k-1))<br />
(a2 + 1 / sqrt k)<br />
(a3 + 1 / (k * (k + 1)))<br />
(a4 + 1 / (k3 * sk * sk))<br />
(a5 + 1 / (k3 * ck * ck))<br />
(a6 + dk)<br />
(a7 + 1 / k2)<br />
(a8 + alt * dk)<br />
(a9 + alt / (2 * k - 1))<br />
where k3 = k2*k; k2 = k*k; dk = 1/k; k = fromIntegral i :: Double<br />
sk = sin k; ck = cos k; x!y = x`seq`y<br />
<br />
Checking the generated C code (for another tutorial, perhaps) shows that the<br />
same C operations are generated as the C entry uses.<br />
<br />
And it runs:<br />
$ time ./i 2500000<br />
3.000000200 (2/3)^k<br />
3186.765000000 k^-0.5<br />
0.999852700 1/k(k+1)<br />
30.314493000 Flint Hills<br />
42.995068000 Cookson Hills<br />
15.403683000 Harmonic<br />
1.644725300 Riemann Zeta<br />
0.693137470 Alternating Harmonic<br />
0.785399100 Gregory<br />
./i 2500000 2.37s user 0.01s system 99% cpu 2.389 total<br />
<br />
A big speedup!<br />
<br />
This entry in fact <br />
[http://shootout.alioth.debian.org/gp4/benchmark.php?test=partialsums&lang=all runs] <br />
faster than hand optimised (and vectorised) GCC! And is only slower than<br />
optimised Fortran. Lesson: Haskell can be very, very fast.<br />
<br />
So, by carefully tweaking things, we first squished a space leak, and then<br />
gained another 45%.<br />
<br />
=== Summary ===<br />
<br />
* Manually inspect the Core that is generated<br />
* Use strictness annotations to ensure loops are unboxed<br />
* Watch out for optimisations such as CSE and strength reduction that are missed<br />
* Read the generated C for really tight loops.<br />
* Use -fexcess-precision and -optc-ffast-math for doubles<br />
<br />
== Parameters ==<br />
<br />
On x86 (possibly others), adding parameters to a loop is rather<br />
expensive, and it can be a large win to "hide" your parameters in a<br />
mutable array. (Note that this is the kind of thing quite likely to<br />
change between GHC versions, so measure before using this trick!)<br />
<br />
== Pattern matching ==<br />
<br />
On rare occasions pattern matching can give improvements in code that<br />
needs to repeatedly take apart data structures. This code:<br />
<br />
flop :: Int -> [Int] -> [Int]<br />
flop n xs = rs<br />
where (rs, ys) = fl n xs ys<br />
fl 0 xs ys = (ys, xs)<br />
fl n (x:xs) ys = fl (n-1) xs (x:ys)<br />
<br />
Can be rewritten to be faster (and more ugly) as:<br />
<br />
flop :: Int -> [Int] -> [Int]<br />
flop 2 (x1:x2:xs) = x2:x1:xs<br />
flop 3 (x1:x2:x3:xs) = x3:x2:x1:xs<br />
flop 4 (x1:x2:x3:x4:xs) = x4:x3:x2:x1:xs<br />
flop 5 (x1:x2:x3:x4:x5:xs) = x5:x4:x3:x2:x1:xs<br />
flop 6 (x1:x2:x3:x4:x5:x6:xs) = x6:x5:x4:x3:x2:x1:xs<br />
flop 7 (x1:x2:x3:x4:x5:x6:x7:xs) = x7:x6:x5:x4:x3:x2:x1:xs<br />
flop 8 (x1:x2:x3:x4:x5:x6:x7:x8:xs) = x8:x7:x6:x5:x4:x3:x2:x1:xs<br />
flop 9 (x1:x2:x3:x4:x5:x6:x7:x8:x9:xs) = x9:x8:x7:x6:x5:x4:x3:x2:x1:xs<br />
flop 10 (x1:x2:x3:x4:x5:x6:x7:x8:x9:x10:xs) = x10:x9:x8:x7:x6:x5:x4:x3:x2:x1:xs<br />
flop n xs = rs<br />
where (rs, ys) = fl n xs ys<br />
fl 0 xs ys = (ys, xs)<br />
fl n (x:xs) ys = fl (n-1) xs (x:ys)<br />
<br />
== Arrays ==<br />
<br />
If you are using array access and GHC primops, do not be too eager to<br />
use raw Addr#esses; MutableByteArray# is just as fast and frees you<br />
from memory management.<br />
<br />
== Memory allocation and arrays ==<br />
<br />
When you are allocating arrays, it may help to know a little about GHC's memory allocator. There are lots of deatils in [http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage The GHC Commentary]), but here are some useful facts:<br />
<br />
* For larger objects ghc has an allocation granularity of 4k. That is it always uses a multiple of 4k bytes, which can lead to wasteage of up to 4k per array. Furthermore, a byte array has some overhead: it needs one word for the heap cell header and another for the length. So if you allocate a 4k byte array then it uses 8k. So the trick is to allocate 4k - overhead. This is what the Data.ByteString library does<br />
<br />
* GHC allocates memory from the OS in units of a "megablock", currently 1Mbyte. So if you allocate a 1Mb array, the storage manager has to allocate 1Mb + overhead, which will cause it to allocate a 2Mb megablock. The surplus will be returned to the system in the form of free blocks, but if all you do is allocate lots of 1Mb arrays, you'll waste about half the space because there's never enough contiguous free space to contain another 1Mb array. Similar problem for 512k arrays: the storage manager allocates a 1Mb block, and returns slightly less than half of it as free blocks, so each 512k allocation takes a whole new 1Mb block.<br />
<br />
== Rewrite rules ==<br />
<br />
Algebraic properties in your code might be missed by the GHC optimiser.<br />
You can use [[Playing by the rules|user-supplied rewrite rules]] to<br />
teach the compiler to optimise your code using domain-specific<br />
optimisations.<br />
<br />
<br />
== See also == <br />
<br />
* [http://www.haskell.org/ghc/docs/latest/html/users_guide/faster.html Faster: producing a program that runs quicker] (part of the GHC User's Guide)<br />
<br />
* [http://stackoverflow.com/questions/12653787/what-optimizations-can-ghc-be-expected-to-perform-reliably What optimizations can GHC be expected to perform reliably?] (stackoverflow.com)</div>Beerdude26https://wiki.haskell.org/index.php?title=Performance&diff=59948Performance2015-08-04T05:33:41Z<p>Beerdude26: Added benchmarking block</p>
<hr />
<div>{{Performance infobox}}<br />
Welcome to the '''Haskell Performance Resource''', the collected wisdom on how to make your Haskell programs go faster. <br />
<br />
== Introduction ==<br />
<br />
One question that often comes up is along the general lines of "Can I write this program in Haskell so that it performs as well as, or better than, the same program written in some other language?"<br />
<br />
This is a difficult question to answer in general because Haskell is a language, not an implementation. Performance can only be measured relative to a specific language implementation.<br />
<br />
Moreover, it's often not clear if two programs which supposedly have the same functionality really do the same thing. Different languages sometimes require very different ways of expressing the same intent. Certain types of bug are rare in typical Haskell programs that are more common in other languages and vice versa, due to strong typing, automatic memory management and lazy evaluation.<br />
<br />
Nonetheless, it is usually possible to write a Haskell program that performs as well as, or better than, the same program written in any other language. The main caveat is that you may have to modify your code significantly in order to improve its performance. Compilers such as GHC are good at eliminating layers of abstraction, but they aren't perfect, and often need some help. <br />
<br />
There are many non-invasive techniques: compiler options, for example. Then there are techniques that require adding some small amounts of performance cruft to your program: strictness annotations, for example. If you still don't get the best performance, though, it might be necessary to resort to larger refactorings.<br />
<br />
Sometimes the code tweaks required to get the best performance are non-portable, perhaps because they require language extensions that aren't implemented in all compilers (e.g. unboxing), or because they require using platform-specific features or libraries. This might not be acceptable in your setting.<br />
<br />
If the worst comes to the worst, you can always write your critical code in C and use the FFI to call it. Beware of the boundaries though - marshaling data across the FFI can be expensive, and multi-language memory management can be complex and error-prone. It's usually better to stick to Haskell if possible.<br />
<br />
== Basic techniques ==<br />
<br />
The key tool to use in making your Haskell program run faster is ''profiling''. Profiling is provided by [[GHC]] and [[nhc98]]. There is ''no substitute'' for finding where your program's time/space is ''really'' going, as opposed to where you imagine it is going.<br />
<br />
Another point to bear in mind: By far the best way to improve a program's performance ''dramatically'' is to use better algorithms. Once profiling has thrown the spotlight on the guilty time-consumer(s), it may be better to re-think your program than to try all the tweaks listed below.<br />
<br />
Another extremely efficient way to make your program snappy is to use library code that has been Seriously Tuned By Someone Else. You ''might'' be able to write a better sorting function than the one in <tt>Data.List</tt>, but it will take you much longer than typing <tt>import Data.List</tt>.<br />
<br />
We have chosen to organise the rest of this resource first by Haskell construct (data types, pattern matching, integers), and then within each category to describe techniques that apply across implementations, and also techniques that are specific to a certain Haskell implementation (e.g. GHC). There are some implementation-specific techniques that apply in general - those are linked from the [[Haskell Performance Resource#General_Implementation-Specific_Techniques | General Implementation-Specific Techniques]] section below.<br />
<br />
== Haskell constructs ==<br />
<br />
* [[/Data Types/]]<br />
* [[/Functions/]]<br />
* [[/Overloading/]]<br />
* [[/FFI/]]<br />
* [[/Arrays/]]<br />
* [[/Strings/]]<br />
* [[/Integers/]]<br />
* [[/IO | I/O ]]<br />
* [[/Floating Point/]]<br />
* [[/Concurrency/]]<br />
* [[/Modules/]]<br />
* [[/Monads/]]<br />
<br />
== General techniques ==<br />
<br />
The most important thing for a beginner to keep in mind is that Haskell programs are evaluated using [[lazy evaluation]], which has many advantages, but also drawbacks when it comes to performance. The rule of thumb is that if you really want to have explicit control over the evaluation, then you should try to avoid it.<br />
<br />
* [[/Strictness/]]<br />
* [[/Laziness/]]<br />
* [[/Space | Avoiding space leaks]]<br />
* [[/Accumulating parameter|Accumulating parameters]]<br />
* [[Stack_overflow|Avoiding stack overflow]]<br />
<br />
== Benchmarking libraries ==<br />
<br />
The [[Criterion]] library is used often to generate detailed benchmark results.<br />
<br />
* [http://www.serpentine.com/blog/2009/09/29/criterion-a-new-benchmarking-library-for-haskell/ Introductory blog post]<br />
* [http://www.stackbuilders.com/news/obverse-versus-reverse-benchmarking-in-haskell-with-criterion Simple example of using Criterion]<br />
<br />
== Compiler specific techniques ==<br />
<br />
* [[/GHC/]]<br />
* [[/NHC98| nhc98]]<br />
* [[/Hugs/]]<br />
* [[/Yhc/]]<br />
* [[/JHC/]]<br />
<br />
== More information ==<br />
<br />
* [https://www.youtube.com/watch?v=McFNkLPTOSY&feature=youtu.be Dan Doel - Introduction to Low Level Haskell Optimization]; a video of Dan Doel's talk at the Boston Haskell Meetup, Sept 17, 2014 ([http://hub.darcs.net/dolio/optimization-talk-demo/browse/slides/slides.md slides]).<br />
* Don Stewart's [http://stackoverflow.com/questions/3276240/tools-for-analyzing-performance-of-a-haskell-program/3276557#3276557 Haskell performance overview] on StackOverflow (2013)<br />
* There are plenty of good examples of Haskell code written for performance in the [http://benchmarksgame.alioth.debian.org/ The Computer Language Benchmarks Game]<br />
* And many alternatives, with discussion, on this wiki: [[Benchmarks Game]] and on the [http://web.archive.org/web/20060209215702/http://haskell.org/hawiki/ShootoutEntry old Haskell wiki] (in the Web Archive)<br />
* There are ~100 [http://blog.johantibell.com/2010/09/slides-from-my-high-performance-haskell.html slides on High-Performance Haskell] from the 2010 CUFP tutorial on that topic.<br />
<br />
== Specific comparisons of data structures ==<br />
=== Data.Sequence vs. lists ===<br />
<br />
Data.Sequence has complexity O(log(min(i,n-i))) for access, insertion and update to position i of a sequence of length n.<br />
<br />
List has complexity O(i).<br />
<br />
List is a non-trivial constant-factor faster for operations at the head (cons and head), making it a more efficient choice for stack-like and stream-like access patterns. Data.Sequence is faster for every other access pattern, such as queue and random access.<br />
<br />
See the following program for proof:<br />
<haskell><br />
import Data.Sequence<br />
<br />
insert_million 0 sequence = sequence<br />
insert_million n sequence = insert_million (n - 1)(sequence |> n)<br />
<br />
main = print (Data.Sequence.length (insert_million 1000000 empty))<br />
</haskell><br />
<pre><br />
$ ghc -O2 --make InsertMillionElements.hs && time ./InsertMillionElements +RTS -K100M<br />
1000000<br />
real 0m7.238s<br />
user 0m6.804s<br />
sys 0m0.228s<br />
</pre><br />
<haskell><br />
insert_million 0 list = reverse list<br />
insert_million n list = insert_million (n -1) (n:list)<br />
<br />
main = print (length (insert_million 1000000 []))<br />
</haskell><br />
<pre><br />
$ ghc -O2 --make InsertMillionElements.hs && time ./InsertMillionElementsList +RTS -K100M<br />
1000000<br />
real 0m0.588s<br />
user 0m0.528s<br />
sys 0m0.052s<br />
</pre><br />
Lists are substantially faster on this micro-benchmark.<br />
<br />
A sequence uses between 5/6 and 4/3 times as much space as the equivalent list (assuming an overhead of one word per node, as in GHC).<br />
If only deque operations are used, the space usage will be near the lower end of the range, because all internal nodes will be ternary.<br />
Heavy use of split and append will result in sequences using approximately the same space as lists.<br />
In detail:<br />
* a list of length ''n'' consists of ''n'' cons nodes, each occupying 3 words.<br />
* a sequence of length ''n'' has approximately ''n''/(''k''-1) nodes, where ''k'' is the average arity of the internal nodes (each 2 or 3). There is a pointer, a size and overhead for each node, plus a pointer for each element, i.e. ''n''(3/(''k''-1) + 1) words.<br />
<br />
== Additional Tips ==<br />
<br />
* Use strict returns ( return $! ...) unless you absolutely need them lazy.<br />
* Profile, profile, profile - understand who is allocated the memory on your heap (+RTS -hc) and how it's being used (+RTS -hb).<br />
* Use +RTS -p to understand who's doing all the allocations and where your time is being spent.<br />
* Approach profiling like a science experiment - make one change, observe if anything is different, rollback and make another change - observe the change. Keep notes!<br />
* Use [[ThreadScope]] to visualize GHC eventlog traces.<br />
<br />
[[Category:Idioms]]<br />
[[Category:Language]]<br />
[[Category:Performance|*]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Web/Cloud&diff=59943Web/Cloud2015-08-03T12:41:27Z<p>Beerdude26: /* OpenShift */ This should be fixed now</p>
<hr />
<div>[[Category:Web|*]]<br />
{{Web infobox}}<br />
<br />
[http://en.wikipedia.org/wiki/Platform_as_a_service PaaS] (platform as a service) cloud providers generally limit you to a fixed technology stack. However, [https://www.openshift.com/developers/download-cartridges OpenShift] and [https://devcenter.heroku.com/articles/buildpacks Heroku] allow third-party extensions, which can be used to support Haskell.<br />
<br />
== DigitalOcean ==<br />
<br />
[https://www.digitalocean.com/ DigitalOcean] gives you a clean server that you can SSH to. Supports Docker as well.<br />
<br />
== OpenShift ==<br />
<br />
{| class="wikitable"<br />
! GHC version:<br />
| 7.8.4<br />
|-<br />
! Author:<br />
| Gideon Sireling<br />
|-<br />
! Home page:<br />
| https://bitbucket.org/accursoft/haskell-cloud/<br />
|-<br />
! Documentation:<br />
| https://bitbucket.org/accursoft/haskell-cloud/src/tip/README.md<br />
|}<br />
<br />
The cartridge comes in several flavours, with just the network and text packages or a pre-installed framework:<br />
<br />
{| class="wikitable"<br />
! Framework || Cartridge || QuickStart || Deploy<br />
|-<br />
| network<br />
| [http://www.accursoft.com/cartridges/network.yml manifest]<br />
| [https://www.openshift.com/quickstarts/haskell quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fnetwork.yml deploy]<br />
|-<br />
| Yesod<br />
| [http://www.accursoft.com/cartridges/yesod.yml manifest]<br />
| [https://www.openshift.com/quickstarts/yesod quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fyesod.yml deploy]<br />
|-<br />
| Snap<br />
| [http://www.accursoft.com/cartridges/snap.yml manifest]<br />
| [https://www.openshift.com/quickstarts/snap quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fsnap.yml deploy]<br />
|-<br />
| Happstack<br />
| [http://www.accursoft.com/cartridges/happstack.yml manifest]<br />
| [https://www.openshift.com/quickstarts/happstack quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fhappstack.yml deploy]<br />
|-<br />
| MFlow<br />
| [http://www.accursoft.com/cartridges/mflow.yml manifest]<br />
| [https://www.openshift.com/quickstarts/mflow quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fmflow.yml deploy]<br />
|-<br />
| Scotty<br />
| [http://www.accursoft.com/cartridges/scotty.yml manifest]<br />
| [https://www.openshift.com/quickstarts/scotty quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fscotty.yml deploy]<br />
|}<br />
<br />
== Heroku ==<br />
<br />
{| class="wikitable"<br />
! GHC version:<br />
| 7.8.2<br />
|-<br />
! Author:<br />
| Joe Nelson<br />
|-<br />
! Home page:<br />
| https://github.com/begriffs/heroku-buildpack-ghc<br />
|-<br />
! Documentation:<br />
| https://github.com/begriffs/heroku-buildpack-ghc/blob/master/README.md<br />
|}<br />
<br />
== Heroku-Haste ==<br />
<br />
{| class="wikitable"<br />
! GHC version:<br />
| 7.4.1<br />
|-<br />
! Author:<br />
| Alberto G. Corona<br />
|-<br />
! Home page:<br />
| https://github.com/agocorona/heroku-buildpack-haste<br />
|-<br />
! Documentation:<br />
| https://github.com/agocorona/heroku-buildpack-haste/blob/master/README.md<br />
|}<br />
<br />
Heroku buildpack for [https://github.com/agocorona/tryhplay tryhplay], incorporating [http://haste-lang.org/ Haste] and [https://github.com/agocorona/hplayground HPlayground]. Demo at http://tryplayg.herokuapp.com/.<br />
<br />
== See also ==<br />
<br />
* [https://www.fpcomplete.com/ FP Haskell Center]</div>Beerdude26https://wiki.haskell.org/index.php?title=Web/Cloud&diff=59942Web/Cloud2015-08-03T12:40:45Z<p>Beerdude26: Added DigitalOcean</p>
<hr />
<div>[[Category:Web|*]]<br />
{{Web infobox}}<br />
<br />
[http://en.wikipedia.org/wiki/Platform_as_a_service PaaS] (platform as a service) cloud providers generally limit you to a fixed technology stack. However, [https://www.openshift.com/developers/download-cartridges OpenShift] and [https://devcenter.heroku.com/articles/buildpacks Heroku] allow third-party extensions, which can be used to support Haskell.<br />
<br />
== DigitalOcean ==<br />
<br />
[https://www.digitalocean.com/ DigitalOcean] gives you a clean server that you can SSH to. Supports Docker as well.<br />
<br />
== OpenShift ==<br />
<br />
'''Warning''': The cartridges are currently liable to fail on small (free) gears due to cabal-install issue [https://github.com/haskell/cabal/issues/2396 #2396].<br />
<br />
{| class="wikitable"<br />
! GHC version:<br />
| 7.8.4<br />
|-<br />
! Author:<br />
| Gideon Sireling<br />
|-<br />
! Home page:<br />
| https://bitbucket.org/accursoft/haskell-cloud/<br />
|-<br />
! Documentation:<br />
| https://bitbucket.org/accursoft/haskell-cloud/src/tip/README.md<br />
|}<br />
<br />
The cartridge comes in several flavours, with just the network and text packages or a pre-installed framework:<br />
<br />
{| class="wikitable"<br />
! Framework || Cartridge || QuickStart || Deploy<br />
|-<br />
| network<br />
| [http://www.accursoft.com/cartridges/network.yml manifest]<br />
| [https://www.openshift.com/quickstarts/haskell quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fnetwork.yml deploy]<br />
|-<br />
| Yesod<br />
| [http://www.accursoft.com/cartridges/yesod.yml manifest]<br />
| [https://www.openshift.com/quickstarts/yesod quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fyesod.yml deploy]<br />
|-<br />
| Snap<br />
| [http://www.accursoft.com/cartridges/snap.yml manifest]<br />
| [https://www.openshift.com/quickstarts/snap quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fsnap.yml deploy]<br />
|-<br />
| Happstack<br />
| [http://www.accursoft.com/cartridges/happstack.yml manifest]<br />
| [https://www.openshift.com/quickstarts/happstack quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fhappstack.yml deploy]<br />
|-<br />
| MFlow<br />
| [http://www.accursoft.com/cartridges/mflow.yml manifest]<br />
| [https://www.openshift.com/quickstarts/mflow quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fmflow.yml deploy]<br />
|-<br />
| Scotty<br />
| [http://www.accursoft.com/cartridges/scotty.yml manifest]<br />
| [https://www.openshift.com/quickstarts/scotty quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fscotty.yml deploy]<br />
|}<br />
<br />
== Heroku ==<br />
<br />
{| class="wikitable"<br />
! GHC version:<br />
| 7.8.2<br />
|-<br />
! Author:<br />
| Joe Nelson<br />
|-<br />
! Home page:<br />
| https://github.com/begriffs/heroku-buildpack-ghc<br />
|-<br />
! Documentation:<br />
| https://github.com/begriffs/heroku-buildpack-ghc/blob/master/README.md<br />
|}<br />
<br />
== Heroku-Haste ==<br />
<br />
{| class="wikitable"<br />
! GHC version:<br />
| 7.4.1<br />
|-<br />
! Author:<br />
| Alberto G. Corona<br />
|-<br />
! Home page:<br />
| https://github.com/agocorona/heroku-buildpack-haste<br />
|-<br />
! Documentation:<br />
| https://github.com/agocorona/heroku-buildpack-haste/blob/master/README.md<br />
|}<br />
<br />
Heroku buildpack for [https://github.com/agocorona/tryhplay tryhplay], incorporating [http://haste-lang.org/ Haste] and [https://github.com/agocorona/hplayground HPlayground]. Demo at http://tryplayg.herokuapp.com/.<br />
<br />
== See also ==<br />
<br />
* [https://www.fpcomplete.com/ FP Haskell Center]</div>Beerdude26https://wiki.haskell.org/index.php?title=Web/Cloud&diff=59941Web/Cloud2015-08-03T12:35:29Z<p>Beerdude26: Removed dead links</p>
<hr />
<div>[[Category:Web|*]]<br />
{{Web infobox}}<br />
<br />
[http://en.wikipedia.org/wiki/Platform_as_a_service PaaS] (platform as a service) cloud providers generally limit you to a fixed technology stack. However, [https://www.openshift.com/developers/download-cartridges OpenShift] and [https://devcenter.heroku.com/articles/buildpacks Heroku] allow third-party extensions, which can be used to support Haskell.<br />
<br />
== OpenShift ==<br />
<br />
'''Warning''': The cartridges are currently liable to fail on small (free) gears due to cabal-install issue [https://github.com/haskell/cabal/issues/2396 #2396].<br />
<br />
{| class="wikitable"<br />
! GHC version:<br />
| 7.8.4<br />
|-<br />
! Author:<br />
| Gideon Sireling<br />
|-<br />
! Home page:<br />
| https://bitbucket.org/accursoft/haskell-cloud/<br />
|-<br />
! Documentation:<br />
| https://bitbucket.org/accursoft/haskell-cloud/src/tip/README.md<br />
|}<br />
<br />
The cartridge comes in several flavours, with just the network and text packages or a pre-installed framework:<br />
<br />
{| class="wikitable"<br />
! Framework || Cartridge || QuickStart || Deploy<br />
|-<br />
| network<br />
| [http://www.accursoft.com/cartridges/network.yml manifest]<br />
| [https://www.openshift.com/quickstarts/haskell quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fnetwork.yml deploy]<br />
|-<br />
| Yesod<br />
| [http://www.accursoft.com/cartridges/yesod.yml manifest]<br />
| [https://www.openshift.com/quickstarts/yesod quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fyesod.yml deploy]<br />
|-<br />
| Snap<br />
| [http://www.accursoft.com/cartridges/snap.yml manifest]<br />
| [https://www.openshift.com/quickstarts/snap quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fsnap.yml deploy]<br />
|-<br />
| Happstack<br />
| [http://www.accursoft.com/cartridges/happstack.yml manifest]<br />
| [https://www.openshift.com/quickstarts/happstack quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fhappstack.yml deploy]<br />
|-<br />
| MFlow<br />
| [http://www.accursoft.com/cartridges/mflow.yml manifest]<br />
| [https://www.openshift.com/quickstarts/mflow quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fmflow.yml deploy]<br />
|-<br />
| Scotty<br />
| [http://www.accursoft.com/cartridges/scotty.yml manifest]<br />
| [https://www.openshift.com/quickstarts/scotty quickstart]<br />
| [https://openshift.redhat.com/app/console/application_type/custom?cartridges%5B%5D=http%3A%2F%2Fwww.accursoft.com%2Fcartridges%2Fscotty.yml deploy]<br />
|}<br />
<br />
== Heroku ==<br />
<br />
{| class="wikitable"<br />
! GHC version:<br />
| 7.8.2<br />
|-<br />
! Author:<br />
| Joe Nelson<br />
|-<br />
! Home page:<br />
| https://github.com/begriffs/heroku-buildpack-ghc<br />
|-<br />
! Documentation:<br />
| https://github.com/begriffs/heroku-buildpack-ghc/blob/master/README.md<br />
|}<br />
<br />
== Heroku-Haste ==<br />
<br />
{| class="wikitable"<br />
! GHC version:<br />
| 7.4.1<br />
|-<br />
! Author:<br />
| Alberto G. Corona<br />
|-<br />
! Home page:<br />
| https://github.com/agocorona/heroku-buildpack-haste<br />
|-<br />
! Documentation:<br />
| https://github.com/agocorona/heroku-buildpack-haste/blob/master/README.md<br />
|}<br />
<br />
Heroku buildpack for [https://github.com/agocorona/tryhplay tryhplay], incorporating [http://haste-lang.org/ Haste] and [https://github.com/agocorona/hplayground HPlayground]. Demo at http://tryplayg.herokuapp.com/.<br />
<br />
== See also ==<br />
<br />
* [https://www.fpcomplete.com/ FP Haskell Center]</div>Beerdude26https://wiki.haskell.org/index.php?title=Web/Existing_software&diff=59940Web/Existing software2015-08-03T10:14:16Z<p>Beerdude26: Added instantwatcher.com</p>
<hr />
<div>[[Category:Web|*]]<br />
{{Web infobox}}<br />
<br />
== amelie ==<br />
<br />
A pastebin site for Haskell, written with FastCGI and nginx.<br />
<br />
{| class="wikitable"<br />
|-<br />
! License<br />
| GPL<br />
|-<br />
! Author(s)<br />
| Chris Done<br />
|-<br />
! Maintainer(s)<br />
| chrisdone@gmail.com<br />
|-<br />
! Home page<br />
| http://hpaste.org/<br />
|-<br />
! Package & repositories<br />
| [http://github.com/chrisdone/amelie Github]<br />
|}<br />
<br />
== BazQux Reader ==<br />
<br />
RSS feed reader which shows comments to posts.<br />
<br />
Web client and part of server runs on Ur/Web. Crawler and most of server-side logic is written in Haskell using<br />
[http://hackage.haskell.org/package/http-conduit http-conduit], [http://hackage.haskell.org/package/hsdns hsdns],<br />
[http://hackage.haskell.org/package/text text],<br />
[http://hackage.haskell.org/package/text-icu text-icu],<br />
[http://hackage.haskell.org/package/regex-tdfa regex-tdfa],<br />
[http://hackage.haskell.org/package/fast-tagsoup fast-tagsoup], [http://hackage.haskell.org/package/riak riak], <br />
[http://hackage.haskell.org/package/warp warp],<br />
[http://hackage.haskell.org/package/authenticate authenticate] and<br />
[http://hackage.haskell.org/package/authenticate-oauth authenticate-oauth].<br />
<br />
{| class="wikitable"<br />
|-<br />
! Author(s)<br />
| Vladimir Shabanov<br />
|-<br />
! Maintainer(s)<br />
| vshabanoff@gmail.com<br />
|-<br />
! Home page<br />
| http://bazqux.com/<br />
|}<br />
<br />
== hpaste ==<br />
<br />
A pastebin site for Haskell running on top of [[HAppS]].<br />
<br />
{| class="wikitable"<br />
|-<br />
! License<br />
| BSD3<br />
|-<br />
! Author(s)<br />
| Eric Mertens<br />
|-<br />
! Maintainer(s)<br />
| emertens@gmail.com<br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/hpaste Hackage] - [http://github.com/glguy/hpaste Github]<br />
|}<br />
<br />
<br />
<br />
== Parallel web ==<br />
<br />
Suggests that you travel through a parallel web with translated content. It's based on Simon Marlow's Haskell Web Server.<br />
<br />
{| class="wikitable"<br />
|-<br />
! Home page<br />
| http://www.parallelnetz.de/<br />
|-<br />
! For Haskellers<br />
| Test the Monad Transformer in [http://www.haskell.org.MonadTransformer.parallelnetz.de/haskellwiki/Category:Monad a parallel Haskell wiki].<br />
|-<br />
! For Germans<br />
| Search in [http://www.google.de.saxophone.parallelnetz.de/ Saxon dialect] or check out the [http://www.stoiber.de.Ehmulator.parallelnetz.de/ Ehmulator].<br />
|-<br />
! Package & repositories<br />
| [http://sourceforge.net/projects/parallelweb SourceForge]<br />
|}<br />
<br />
== Try Haskell! ==<br />
<br />
Try Haskell in your browser: This interactive tutorial and evaluation has now been incorporated directly into the haskell.org homepage.<br />
<br />
{| class="wikitable"<br />
|-<br />
! Author<br />
| Chris Done <chrisdone@gmail.com><br />
|-<br />
! Maintainer<br />
| Chris Done <chrisdone@gmail.com><br />
|-<br />
! Home page<br />
| http://tryhaskell.org/<br />
|-<br />
! Package & repositories<br />
| [http://github.com/chrisdone/tryhaskell TryHaskell Github] - [http://github.com/chrisdone/haskell-json haskell-json Github]<br />
|}<br />
<br />
== Defunkt ==<br />
<br />
=== <del>Juicy</del> (404) ===<br />
<br />
A simple web site that shows the relative "juiciness" of reddit, Hacker News, Digg, etc, written with FastCGI and nginx.<br />
<br />
{| class="wikitable"<br />
|-<br />
! License<br />
| BSD3<br />
|-<br />
! Author(s)<br />
| Chris Done<br />
|-<br />
! Maintainer(s)<br />
| chrisdone@gmail.com<br />
|-<br />
! Home page<br />
| <del>http://chrisdone.com/juicy</del> (404)<br />
|-<br />
! Package & repositories<br />
| <del>[http://github.com/chrisdone/juicy Github]</del> (404)<br />
|}<br />
<br />
=== Instantwatcher ===<br />
<br />
Instantwatcher (Netflix watching guide), 100% Haskell back-end, front-end generated by Haskell.<br />
<br />
{| class="wikitable"<br />
|-<br />
! Home page<br />
| http://instantwatcher.com/<br />
|-<br />
! Reddit thread<br />
| https://www.reddit.com/r/haskell/comments/3am3qu/should_i_put_a_powered_by_haskell_tag_w_haskell/<br />
|}<br />
<br />
=== <del>Pass.net</del> (404) ===<br />
<br />
Replaces registration, confirmation mails, and multiple passwords with a single login. at your email domain. Runs on HAppS.<br />
<br />
{| class="wikitable"<br />
|-<br />
! Home page<br />
| http://pass.net/<br />
|}</div>Beerdude26https://wiki.haskell.org/index.php?title=Type_class&diff=59939Type class2015-08-03T10:06:56Z<p>Beerdude26: </p>
<hr />
<div>For now we refer to Wikipedia's article about [http://en.wikipedia.org/wiki/Type_class type classes].<br />
<br />
== Related ==<br />
<br />
* [[Orphan instance]] - What they are and how to avoid / work around them.<br />
* [[Typeclassopedia]] - An extensive explanation of type classes.<br />
* [[Switching type classes at runtime]] <br />
<br />
[[Category:Glossary]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Switching_type_classes_at_runtime&diff=59938Switching type classes at runtime2015-08-03T10:06:45Z<p>Beerdude26: Created page</p>
<hr />
<div>== Introduction ==<br />
<br />
It is possible to create a type class instance at runtime by coercing a value to another one. [http://www.well-typed.com/blog/2015/07/checked-exceptions/ This blog post mentions the concept].<br />
<br />
== Tutorial ==<br />
<br />
Let's say we have a type class called "Throws":<br />
<br />
<haskell><br />
-- | Checked exceptions<br />
class Throws e where<br />
throwChecked :: e -> IO a<br />
</haskell><br />
<br />
We'd like to turn this into a "IOError", so that it can be caught:<br />
<br />
<haskell><br />
-- | Rethrow checked exceptions as unchecked (regular) exceptions<br />
rethrowUnchecked :: forall e a. (Throws e => IO a) -> (Exception e => IO a)<br />
rethrowUnchecked act = aux act throwIO<br />
where<br />
aux :: (Throws e => IO a) -> ((e -> IO a) -> IO a)<br />
aux = unsafeCoerce . Wrap<br />
<br />
-- | Wrap an action that may throw a checked exception<br />
--<br />
-- This is used internally in 'rethrowUnchecked' to avoid impredicative<br />
-- instantiation of the type of 'unsafeCoerce'.<br />
newtype Wrap e a = Wrap (Throws e => IO a)<br />
<br />
-- | Catch a checked exception<br />
--<br />
-- This is the only way to discharge a 'Throws' type class constraint.<br />
catchChecked :: Exception e => (Throws e => IO a) -> (e -> IO a) -> IO a<br />
catchChecked = catch . rethrowUnchecked<br />
<br />
-- | 'catchChecked' with the arguments reversed<br />
handleChecked :: Exception e => (e -> IO a) -> (Throws e => IO a) -> IO a<br />
handleChecked act handler = catchChecked handler act<br />
</haskell></div>Beerdude26https://wiki.haskell.org/index.php?title=Catch&diff=59937Catch2015-08-03T09:49:45Z<p>Beerdude26: Added related work</p>
<hr />
<div><br />
= Introduction =<br />
<br />
Do you sometimes encounter the dreaded "pattern match failure: head<br />
[]" message? Do you have incomplete patterns which sometimes fail? Do<br />
you have incomplete patterns which you know don't fail, but still get<br />
compiler warnings about them? Would you like to statically ensure the<br />
absence of all calls to error?<br />
<br />
==Location==<br />
For the last few years I've been working on a pattern-match checker<br />
for Haskell, named Catch. I'm now happy to make a release:<br />
<br />
* Hackage: {{HackagePackage|id=catch}}<br />
* Homepage: http://www-users.cs.york.ac.uk/~ndm/catch/<br />
* User manual: http://www.cs.york.ac.uk/fp/darcs/catch/catch.htm<br />
<br />
While a [[darcs]] version is available, I strongly recommend the use of<br />
the tarball on [[hackage]].<br />
<br />
==Features:==<br />
*Detects all incomplete patterns and calls to error<br />
*Full support for Haskell, through the use of Yhc as a front end<br />
*Automatic - no annotations are required<br />
*Fast - checking times of under 5 seconds (excluding Yhc compilation)are typical<br />
<br />
==Drawbacks:==<br />
* Requires [[Yhc]] to be installed, which doesn't accept most Haskell extensions. This will be fixed once [[GHC]].Core is available as a standalone library. The underlying tool is not limited to Haskell 98 in any way.<br />
<br />
==Related work:==<br />
<br />
* [https://wiki.haskell.org/Liquid_Haskell Liquid Haskell] - Annotate your functions with invariants that are checked at compile-time.<br />
<br />
* The [https://hackage.haskell.org/package/total total] package does not search for incomplete pattern matches, but it does allow you to write compiler-checked exhaustive pattern matches. [http://www.haskellforall.com/2015/01/total-100-exhaustive-pattern-matching.html Blog post here].<br />
<br />
==Success stories:==<br />
*The [[darcs]] version of [[HsColour]] has been proven free of pattern match errors, and [[Catch]] was able to find three previously unknown crashes which could be triggered by a malformed document.<br />
*System.[[FilePath]] has been checked, and is free of pattern-match errors.<br />
*[[Xmonad]]'s central StackSet.hs module has been checked repeatedly, as it has evolved. Unfortunately currently this module goes beyond Haskell 98, but this should be fixed shortly.<br />
<br />
If you have any experiences with Catch, I'd be very interested to hear them.<br />
<br />
Thanks<br />
Neil<br />
[[Category:Development tools]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Smart_constructors&diff=59936Smart constructors2015-08-03T09:47:04Z<p>Beerdude26: /* Related work */ Added reference to Liquid Haskell</p>
<hr />
<div>'''Smart constructors'''<br />
<br />
This is an introduction to a programming idiom for placing extra<br />
constraints on the construction of values by using ''smart<br />
constructors''.<br />
<br />
Sometimes you need guarantees about the values in your program beyond<br />
what can be accomplished with the usual [[Type|type system]] checks.<br />
Smart constructors can be used for this purpose.<br />
<br />
Consider the following problem: we want to be able to specify a data<br />
type for electronic resistors. The resistors come in two forms, metal<br />
and ceramic. Resistors are labelled with a number of bands, from 4 to 8.<br />
<br />
We'd like to be able to<br />
* ensure only resistors with the right number of bands are constructed.<br />
<br />
== Runtime checking : smart constructors ==<br />
<br />
=== A first attempt ===<br />
<br />
Code up a typical [[Type|data type]] describing a resistor value:<br />
<br />
<haskell><br />
data Resistor = Metal Bands<br />
| Ceramic Bands <br />
deriving Show<br />
<br />
type Bands = Int<br />
</haskell><br />
<br />
This has a problem however, that the [[constructor]]s of type ''Resistor'' are<br />
unable to check that only bands of size 4 to 8 are built. It is quite<br />
legal to say:<br />
<br />
*Main> :t Metal 23<br />
Metal 23 :: Resistor<br />
<br />
for example.<br />
<br />
=== Smart(er) constructors ===<br />
<br />
Smart constructors are just functions that build values of the required<br />
type, but perform some extra checks when the value is constructed, like<br />
so:<br />
<br />
<haskell><br />
metalResistor :: Bands -> Resistor<br />
metalResistor n | n < 4 || n > 8 = error "Invalid number of resistor bands" <br />
| otherwise = Metal n<br />
</haskell><br />
<br />
This function behaves like the constructor ''Metal'', but also performs<br />
a check. This check will be carried out at runtime, once, when the value<br />
is built.<br />
<br />
Running this code:<br />
> metalResistor 4<br />
Metal 4<br />
<br />
> metalResistor 7<br />
Metal 7<br />
<br />
> metalResistor 9<br />
*** Exception: Invalid number of resistor bands<br />
<br />
> metalResistor 0<br />
*** Exception: Invalid number of resistor bands<br />
<br />
One extra step has to be made though, to make the interface safe. When<br />
exporting the type ''Resistor'' we need to hide the (unsafe)<br />
constructors, and only export the smart constructors, otherwise a<br />
reckless user could bypass the smart constructor:<br />
<br />
<haskell><br />
module Resistor (<br />
Resistor, -- abstract, hiding constructors<br />
metalResistor, -- only way to build a metal resistor<br />
) where<br />
<br />
...<br />
</haskell><br />
<br />
=== Using assertions ===<br />
<br />
Hand-coding error messages can be tedious when used often. Instead we<br />
can use the ''assert'' function, provided (from Control.Exception). We<br />
rewrite the smart constructor as:<br />
<br />
<haskell><br />
metalResistor :: Bands -> Resistor<br />
metalResistor n = assert (n >= 4 && n <= 8) $ Metal n<br />
</haskell><br />
<br />
And now obtain more detailed error messages, automatically generated for us:<br />
<br />
> metalResistor 0<br />
*** Exception: A.hs:4:18-23: Assertion failed<br />
<br />
We at least now are given the line and column in which the error occured.<br />
<br />
== Compile-time checking : the type system ==<br />
<br />
=== Enforcing the constraint statically ===<br />
<br />
There are other ways to obtain numerical checks like this. The most<br />
interesting are probably the static checks that can be done with [[Type arithmetic]], <br />
that enforce the number of bands at compile time, rather than runtime,<br />
by lifting the band count into the type level.<br />
<br />
In the following example, instead of checking the band count at runtime,<br />
we instead lift the resistor band count into the type level, and have<br />
the typecheck perform the check statically, using [[Phantom type|phantom types]] <br />
and [[Peano numbers]].<br />
<br />
We thus remove the need for a runtime check, meaning faster code. A consequence<br />
of this decision is that since the band count is now represented in the type,<br />
it is no longer necessary to carry it around at runtime, meaning less data has<br />
to be allocated.<br />
<br />
Firstly, define some [[Peano numbers]] to represent the number of bands as types:<br />
<br />
<haskell> <br />
data Z = Z<br />
data S a = S a<br />
</haskell> <br />
<br />
Now specify a class for cardinal numbers.<br />
<br />
<haskell><br />
class Card c where<br />
<br />
instance Card Z where<br />
instance (Card c) => Card (S c) where<br />
</haskell> <br />
<br />
Ok, now we're set. So encode a type-level version of the bounds check.<br />
Only resistors with bands >= 4 and <= 8 are valid:<br />
<br />
<haskell><br />
class Card size => InBounds size where<br />
<br />
instance InBounds (S (S (S (S Z)))) where -- four<br />
instance InBounds (S (S (S (S (S Z))))) where -- five<br />
instance InBounds (S (S (S (S (S (S Z)))))) where -- six<br />
instance InBounds (S (S (S (S (S (S (S Z))))))) where -- seven<br />
instance InBounds (S (S (S (S (S (S (S (S Z)))))))) where -- eight<br />
</haskell> <br />
<br />
Now define a new resistor type. Note that since the bounds is represented in the<br />
type, ''we no longer need to store the bounds in the resistor value''.<br />
<br />
<haskell><br />
data Resistor size = Resistor deriving Show<br />
</haskell> <br />
<br />
And, finally, a convenience constructor for us to use, encoding the bounds<br />
check in the type:<br />
<br />
<haskell> <br />
resistor :: InBounds size => size -> Resistor size<br />
resistor _ = Resistor<br />
</haskell><br />
<br />
=== Examples ===<br />
<br />
First, define some convenience values:<br />
<br />
<haskell><br />
d0 = undefined :: Z<br />
d3 = undefined :: S (S (S Z))<br />
d4 = undefined :: S (S (S (S Z)))<br />
d6 = undefined :: S (S (S (S (S (S Z)))))<br />
d8 = undefined :: S (S (S (S (S (S (S (S Z)))))))<br />
d10 = undefined :: S (S (S (S (S (S (S (S (S (S Z)))))))))<br />
</haskell><br />
<br />
Now try to construct some resistors:<br />
<br />
> resistor d0<br />
No instance for (InBounds Z)<br />
<br />
So the value 0 isn't in bounds, as we want. And it is a ''compile-time error''<br />
to try to create such a resistor.<br />
<br />
> resistor d3<br />
No instance for (InBounds (S (S (S Z))))<br />
<br />
Ok, how about a valid resistor?<br />
<br />
> resistor d4<br />
Resistor<br />
<br />
Great!<br />
<br />
> :t resistor d4<br />
resistor d4 :: Resistor (S (S (S (S Z))))<br />
<br />
And its type encodes the number of bands.<br />
<br />
> resistor d6<br />
Resistor<br />
> resistor d8<br />
Resistor<br />
<br />
> :t resistor d8<br />
resistor d8 :: Resistor (S (S (S (S (S (S (S (S Z))))))))<br />
<br />
Similar result for other valid resistors.<br />
<br />
> resistor d10<br />
No instance for (InBounds (S (S (S (S (S (S (S (S (S (S Z)))))))))))<br />
<br />
And 10 is too big.<br />
<br />
=== Summary ===<br />
<br />
By using a standard encoding of numeric values on the type level we are able to<br />
encode a bounds check in the type of a value, thus removing a runtime check,<br />
and removing the need to store the numeric value at runtime. The code is safer,<br />
as it is impossible to compile the program unless all resistors have the<br />
correct number of bands.<br />
<br />
An extension would be to use a decimal encoding for the integers (at the<br />
expense of longer code).<br />
<br />
=== Extensions ===<br />
<br />
Further checks can be obtained by separating the metal and ceramic<br />
values on the type level, so no function that takes a metal resistor can<br />
be accidentally passed a ceramic one.<br />
<br />
A ''newtype'' is useful for this:<br />
<br />
<haskell><br />
newtype MetalResistor = Metal Bands<br />
newtype CeramicResistor = Ceramic Bands<br />
</haskell><br />
<br />
now, a function of resistors must have either a ''MetalResistor'' type, or a<br />
''CeramicResistor'' type:<br />
<br />
<haskell><br />
foo :: MetalResistor -> Int<br />
foo (MetalResistor n) = n<br />
</haskell><br />
<br />
You can't write a function over both resistor types (other than a purely<br />
polymorphic function).<br />
<br />
=== Related work ===<br />
<br />
* These ideas are also discussed in [[Dimensionalized numbers]] and on the old wiki [http://web.archive.org/web/20050227183721/http://www.haskell.org/hawiki/NonTrivialTypeSynonyms here] (for compile-time unit analysis error catching at the type level).<br />
<br />
* There is also [https://wiki.haskell.org/Liquid_Haskell Liquid Haskell], which allows you to annotate your functions with invariants ("the list that this function produces has to be sorted", etc) that are run through a SMT solver at compile time.<br />
<br />
* Recently migrated are the pages on [[worker wrapper]] and [[factory function]].<br />
<br />
In general, the more information you place on the type level, the more<br />
static checks you get -- and thus less chance for bugs.<br />
<br />
== Runtime Optimisation : smart constructors ==<br />
<br />
Another use for smart constructors is to perform basic optimisations, often to obtain a normal form for constructed data. For example, consider a data structure representing addition and multiplication of variables.<br />
<br />
<haskell><br />
data Expression = Variable String<br />
| Add [Expression]<br />
| Multiply [Expression]<br />
</haskell><br />
<br />
In this data structure, it is possible to represent a value such as <tt>Add [Variable "a", Add [Variable "b", Variable "c"]]</tt> more compactly as <tt>Add [Variable "a", Variable "b", Variable "c"]</tt>.<br />
<br />
This can be done automatically with smart constructors such as:<br />
<br />
<haskell><br />
add :: [Expression] -> Expression<br />
add xs = Add (concatMap fromAdd xs)<br />
multiply :: [Expression] -> Expression<br />
multiply xs = Multiply (concatMap fromMultiply xs)<br />
<br />
fromAdd (Add xs) = xs<br />
fromAdd x = [x]<br />
fromMultiply (Multiply xs) = xs<br />
fromMultiply x = [x]<br />
</haskell><br />
<br />
[[Category:Idioms]]<br />
[[Category:Glossary]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Stack&diff=59930Stack2015-07-24T19:10:23Z<p>Beerdude26: </p>
<hr />
<div>{{Stub}}<br />
<br />
For the description of the common computer term <code>stack</code>, see the Wikipedia article [https://en.wikipedia.org/wiki/Stack_(abstract_data_type) Stack (abstract data type)] or [https://en.wikipedia.org/wiki/Call_stack Call stack].<br />
<br />
<br />
== Introduction ==<br />
<br />
Stack is a development tool for the entire Haskell development cycle, from installing the compiler and packages to testing and benchmarking software.<br />
<br />
== Blog posts / Tutorials ==<br />
<br />
* [https://duplode.github.io/posts/casual-hacking-with-stack.html Casual Hacking With stack] - Explains how to start up a new package with Stack and what each file is for.<br />
<br />
<br />
== Important links ==<br />
<br />
* [https://github.com/commercialhaskell/stack#readme The readme file]<br />
* [https://github.com/commercialhaskell/stack/wiki/Downloads Download instructions]<br />
* [https://github.com/commercialhaskell/stack/wiki/FAQ FAQ list]<br />
* [https://groups.google.com/forum/#!forum/haskell-stack The mailing list]<br />
* [https://github.com/commercialhaskell/stack The stack repository]<br />
<br />
<br />
== Other links ==<br />
<br />
* [https://www.fpcomplete.com/blog/2015/06/announcing-first-public-beta-stack ANNOUNCING: first public beta of stack]<br />
* [https://www.fpcomplete.com/blog/2015/06/stack-0-1-release stack 0.1 released]</div>Beerdude26https://wiki.haskell.org/index.php?title=Web/Frameworks&diff=59929Web/Frameworks2015-07-24T12:06:46Z<p>Beerdude26: /* Snap */</p>
<hr />
<div>[[Category:Web|*]]<br />
{{Web infobox}}<br />
<br />
Content from [[Web]] should be merged here.<br />
<br />
Below is a list of known to be active Haskell web frameworks. Rather than one framework to rule them all, Haskell provides several options. You can view the [[Web/Deploy]] page to get an idea of how you might deploy an application written in some of these frameworks.<br />
<br />
See also: there are also many [[Web/Frameworks/Inactive|inactive web frameworks]] to draw inspiration from<br />
<br />
== Happstack ==<br />
<br />
Happstack is a Haskell web framework. Happstack is designed so that developers can prototype quickly, deploy painlessly, scale massively, operate reliably, and change easily. It supports GNU/Linux, OS X, FreeBSD, and Windows environments.<br />
<br />
{| class="wikitable"<br />
! License<br />
| BSD3<br />
|-<br />
! Author:<br />
| Happstack team, HAppS LLC<br />
|-<br />
! Maintainer:<br />
| Happstack team <happs@googlegroups.com><br />
|-<br />
! Home page:<br />
| http://happstack.com/<br />
|-<br />
! Documentation:<br />
| http://www.happstack.com/c/view-page-slug/3/documentation/<br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/happstack-server Hackage] - [http://patch-tag.com/r/mae/happstack Darcs]<br />
|}<br />
<br />
[http://happstack.com/ Happstack] is a complete web framework. The main component is [http://hackage.haskell.org/package/happstack-server happstack-server]: an integrated HTTP server, routing combinators, fileserving, etc. In addition, a number of packages that used to be coupled to Happstack have now been decoupled from it, but remain promoted and documented for use with Happstack:<br />
<br />
* [http://hackage.haskell.org/package/safecopy safecopy]: datatype serialization and migration support<br />
* [http://hackage.haskell.org/package/acid-state acid-state]: a powerful NoSQL ACID storage system with native support for Haskell types<br />
<br />
It also includes integration with many 3rd party libraries including:<br />
<br />
*templating: [http://hackage.haskell.org/package/blaze-html Blaze HTML], [http://www.yesodweb.com/book/shakespearean-templates Hamlet], [[HSP]], [[HStringTemplate]], [[Heist]], and more<br />
*forms: [http://hackage.haskell.org/package/reform reform]<br />
*routing: [http://hackage.haskell.org/package/web-routes web-routes] type-safe urls and routing<br />
*databases: can be used with most [[Database interfaces]] with no special support required<br />
<br />
See the [http://happstack.com/ Happstack Home Page] for more information and to learn how to get support via IRC and mailing lists.<br />
<br />
== Snap ==<br />
<br />
Snap is a simple web development framework for unix systems, written in the Haskell programming language.<br />
<br />
Snap is well-documented and has a test suite with a high level of code coverage, but it is early-stage software with still-evolving interfaces. Snap is therefore likely to be most appropriate for early adopters and potential contributors.<br />
<br />
* A fast HTTP server library with an optional high-concurrency backend using the libev event loop library<br />
* A sensible and clean monad for web programming<br />
* An XML-based templating system for generating HTML<br />
<br />
{| class="wikitable"<br />
! License:<br />
| BSD3<br />
|-<br />
! Author:<br />
| James Sanders, Gregory Collins, Doug Beardsley<br />
|-<br />
! Maintainer:<br />
| snap@snapframework.com<br />
|-<br />
! Home page:<br />
| http://snapframework.com/<br />
|-<br />
! Documentation:<br />
| http://snapframework.com/docs<br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/snap-server Hackage] - [https://github.com/snapframework Git]<br />
|}<br />
<br />
== Yesod ==<br />
<br />
Yesod is designed for RESTful, type-safe, performant web apps. By leveraging quasi-quotation for the more boilerplate tasks, we get concise web apps with high levels of type safety. Its Hamlet templates are compile-time checked for correctness, and the controller (web-routes-quasi) uses type-safe URLs to make certain you are only generating valid URLs. It loosely follows Model/View/Controller principles.<br />
<br />
{| class="wikitable"<br />
! License:<br />
| BSD3<br />
|-<br />
! Author:<br />
| Michael Snoyman <michael@snoyman.com><br />
|-<br />
! Maintainer:<br />
| Michael Snoyman <michael@snoyman.com><br />
|-<br />
! Announcement:<br />
| http://www.haskell.org/pipermail/haskell-cafe/2010-March/074271.html<br />
|-<br />
! Home page:<br />
| http://www.yesodweb.com/<br />
|-<br />
! Documentation:<br />
| http://www.yesodweb.com/book<br />
|-<br />
! Screencast:<br />
| http://www.yesodweb.com/page/screencasts<br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/yesod Hackage] - [https://github.com/yesodweb/yesod Github]<br />
|}<br />
<br />
[http://docs.yesodweb.com/ Yesod] is a full-featured web framework. It takes a modular approach to development, so many parts of the framework such as [http://www.yesodweb.com/book/shakespearean-templates Hamlet] and [http://www.yesodweb.com/book/persistent Persistent] are available as standalone packages. However, put together, Yesod provides you with solutions for templating, routing, persistence, sessions, JSON, authentication/authorization, and more. Yesod's major guiding principle is type safety: if your application compiles, it works.<br />
<br />
Yesod is very well documented through a combination of haddocks and the [http://docs.yesodweb.com/book Yesod book].<br />
<br />
Yesod is built on [http://hackage.haskell.org/package/wai WAI], or the Web Application Interface. This is similar to WSGI in Python or Rack in Ruby. It provides a single interface that all applications can target and work on multiple backends. Backends exist for CGI, FastCGI, SCGI, development server (auto-recompile) and even a Webkit-powered desktop version.<br />
<br />
But the premier backend is [http://hackage.haskell.org/package/warp Warp]: a very simple web server which, at the time of writing, is the fastest Haskell has to offer. You can read more in its [http://docs.yesodweb.com/blog/announcing-warp release announcement] and see some [http://www.yesodweb.com/blog/2011/02/warp-speed-ahead followup benchmarks]. Warp is already powering Yesod; some other major players that are planning a move are Hoogle and Happstack.<br />
<br />
You can see a [https://github.com/yesodweb/yesod/wiki/Powered-by-Yesod list of Yesod-powered sites and packages], or check out the [https://github.com/snoyberg/haskellers source code for Haskellers]. Most discussions for Yesod take place on the [http://groups.google.com/group/yesodweb yesodweb list], so feel free to join in and ask any questions you have, the Yesod community is very beginner-friendly.<br />
<br />
== miku ==<br />
<br />
A simple library for fast web prototyping in Haskell, inspired by Ruby's Rack and Sinatra.<br />
<br />
{| class="wikitable"<br />
! License<br />
| BSD3<br />
|-<br />
! Author<br />
| Wang, Jinjing<br />
|-<br />
! Maintainer<br />
| Wang, Jinjing <nfjinjing@gmail.com><br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/miku Hackage] - [http://github.com/nfjinjing/miku Github]<br />
|}<br />
<br />
== Lemmachine ==<br />
<br />
Lemmachine is a REST'ful web framework that makes it easy to get HTTP right by exposing users to overridable hooks with sane defaults. The main architecture is a copy of Erlang-based Webmachine, which is currently the best documentation reference (for hooks & general design).<br />
<br />
Lemmachine stands out from the dynamically typed Webmachine by being written in dependently typed Agda. The goal of the project is to show the advantages gained from compositional testing by taking advantage of proofs being inherently compositional. See proofs for examples of universally quantified proofs (tests over all possible input values) written against the default resource, which does not override any hooks.<br />
<br />
[http://github.com/larrytheliquid/Lemmachine#readme More information]<br />
<br />
{| class="wikitable"<br />
! Author<br />
| Larry Diehl<br />
|-<br />
! Packages & repositories<br />
| [http://github.com/larrytheliquid/Lemmachine Github]<br />
|}<br />
<br />
== mohws ==<br />
<br />
A web server with a module system and support for CGI. Based on Simon Marlow's original Haskell Web Server.<br />
<br />
{| class="wikitable"<br />
!License:<br />
|BSD3<br />
|-<br />
!Copyright:<br />
|Simon Marlow, Bjorn Bringert<br />
|-<br />
!Author:<br />
|Simon Marlow, Bjorn Bringert <bjorn@bringert.net><br />
|-<br />
!Maintainer:<br />
|Henning Thielemann <webserver@henning-thielemann.de><br />
|-<br />
!Packages & repositories<br />
|[http://hackage.haskell.org/cgi-bin/hackage-scripts/package/mohws Hackage] - [http://code.haskell.org/mohws/ Darcs]<br />
|}<br />
<br />
== Salvia ==<br />
<br />
Salvia is a feature rich modular web server and web application framework that can be used to write dynamic websites in Haskell. From the lower level protocol code up to the high level application code, everything is written as a Salvia handler. This approach makes the server extremely extensible. To see a demo of a Salvia website, please see the salvia-demo package.<br />
<br />
All the low level protocol code can be found in the salvia-protocol package, which exposes the datatypes, parsers and pretty-printers for the URI, HTTP, Cookie and MIME protocols.<br />
<br />
This Salvia package itself can be separated into three different parts: the interface, the handlers and the implementation. The interface module defines a number of type classes that the user can build the web application against. Reading the request object, writing to the response, or gaining direct access to the socket, all of these actions are reflected using one type class aspect in the interface. The handlers are self contained modules that implement a single aspect of the Salvia web server. The handlers expose their interface requirements in their type context. Salvia can have multiple implementations which can be switched by using different instances for the interface type classes. This package has only one implementation, a simple accepting socket loop server. The salvia-extras package has two additional implementations. Keeping a clear distinction between the abstract server aspects and the actual implementation makes it very easy to migrate existing web application to different back-ends.<br />
<br />
{| class="wikitable"<br />
! License:<br />
| BSD3<br />
|-<br />
! Author:<br />
| Sebastiaan Visser<br />
|-<br />
! Maintainer:<br />
| sfvisser@cs.uu.nl<br />
|-<br />
! Announcement:<br />
| http://www.haskell.org/pipermail/haskell-cafe/2010-March/074870.html<br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/salvia Hackage] - [http://github.com/sebastiaanvisser/salvia Git]<br />
|}<br />
<br />
== Scotty ==<br />
<br />
A Haskell web framework inspired by Ruby's Sinatra, using WAI and Warp. Sinatra + Warp = Scotty.<br />
<br />
Scotty is the cheap and cheerful way to write RESTful, declarative web applications.<br />
<br />
* A page is as simple as defining the verb, url pattern, and Text content.<br />
* It is template-language agnostic. Anything that returns a Text value will do.<br />
* Conforms to WAI Application interface.<br />
* Uses very fast Warp webserver by default.<br />
<br />
{| class="wikitable"<br />
! License:<br />
| BSD3<br />
|-<br />
! Author:<br />
| Andrew Farmer<br />
|-<br />
! Maintainer:<br />
| Andrew Farmer<br />
|-<br />
! Home page:<br />
| https://github.com/scotty-web/scotty<br />
|-<br />
! Documentation:<br />
| http://hackage.haskell.org/package/scotty<br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/scotty Hackage] - [https://github.com/scotty-web/scotty Git]<br />
|}<br />
<br />
== Servant ==<br />
<br />
Servant is a a light-weight framework primarily for REST APIs. It allows to specify API specifications as type aliases and then work with these type aliases to create servers, but also documentation, client code in Haskell and Javascript, etc.. It is based on wai.<br />
<br />
<br />
{| class="wikitable"<br />
! License:<br />
| BSD3<br />
|-<br />
! Author:<br />
| Alp Mestanogullari, Sönke Hahn, Julian K. Arni<br />
|-<br />
! Maintainer:<br />
| alpmestan@gmail.com<br />
|-<br />
! Home page:<br />
| http://haskell-servant.github.io/<br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/servant Hackage] - [https://github.com/haskell-servant Git]<br />
|}<br />
<br />
== MFlow==<br />
<br />
A Haskell application server ++ Web Framework. MFlow is a shorthand for "Message Flow". It is a continuation-based framework without continuations. Instead of other continuation based frameworks like Ocsigen(Ocaml), Coccoon (javascript) or Seaside (Smalltalk), it is based on a backtracking monad that keep the synchornization of the execution state with the user navigation. Since the discontinuation of [http://www.informatik.uni-freiburg.de/~thiemann/WASH/ WASH], MFlow is the only continuation-style framework written in Haskell to date.<br />
<br />
Unlike real continuations, the state in MFlow applications is pretty small and serializable, so it is horizontally scalable. The navigation in a MFlow application is safe at compilation time, since even the internal HTML links are checked by the compiler. The code is very short and has little configuration. Routes in MFlow are defined and typechecked in pure haskell code, just like in the case of the menus in a console application. Each page has its own URL so it is RESTful to a certain extent. It is planned to have REST-style URLs in the future (done in the head of the github repo).<br />
<br />
It uses standard Haskell web libraries and/or techniques: WAI, Warp, Blaze HTML, HSP. Its core is server and rendering independent. A kind of extended [http://groups.inf.ed.ac.uk/links/formlets/ formlets] are used to create self contained components, called widgets. They have formatting, Ajax, and server code. They can be composed to create the user interface.<br />
<br />
A MFlow application resembles a console application. This is an example of a complete application with three pages. It ask for two numbers and return the sum. At any time, even if the user press the back button, the state is synchronized with the navigation. <br />
<br />
module Main where<br />
import MFlow.Wai.Blaze.Html.All<br />
<br />
main= do<br />
addMessageFlows [("sum", transient . runFlow $ sumIt )]<br />
wait $ run 8081 waiMessageFlow<br />
<br />
sumIt= do<br />
setHeader $ html . body<br />
n1 <- ask $ p << "give me the first number" <br />
++> getInt Nothing <br />
<** submitButton "send"<br />
<br />
n2 <- ask $ p << "give me the second number" <br />
++> getInt Nothing <br />
<** submitButton "send"<br />
<br />
ask $ p << ("the result is " ++ show (n1 + n2)) <br />
++> wlink () << p << "click here"<br />
<br />
<br />
<br />
{| class="wikitable"<br />
! License:<br />
| BSD3<br />
|-<br />
! Author:<br />
| Alberto Gómez Corona<br />
|-<br />
! Maintainer:<br />
| Alberto Gómez Corona<br />
|-<br />
! Home page:<br />
| http://haskell-web.blogspot.com<br />
|-<br />
! Documentation:<br />
| http://hackage.haskell.org/package/MFlow<br />
[https://docs.google.com/file/d/0B2-x2MmiuA32b0RndnZTdTVUb0E MFlow paper]<br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/MFlow Hackage] - [https://github.com/agocorona/MFlow Git]<br />
|}<br />
<br />
== Spock ==<br />
<br />
Another Haskell web framework for rapid development: This toolbox provides everything you need to get a quick start into web hacking with haskell: routing, middleware, json, blaze, sessions, cookies, database helper, csrf-protection, global state<br />
<br />
* Simple API<br />
* Adds lots of useful features for rapid web development<br />
* Fast tree based routing<br />
* Plugins like [http://hackage.haskell.org/package/Spock-auth Spock-auth] and [http://hackage.haskell.org/package/Spock-worker Spock-worker]<br />
<br />
{| class="wikitable"<br />
! License:<br />
| BSD3<br />
|-<br />
! Author:<br />
| Alexander Thiemann<br />
|-<br />
! Maintainer:<br />
| Alexander Thiemann<br />
|-<br />
! Home page:<br />
| https://github.com/agrafix/Spock<br />
|-<br />
! Documentation:<br />
| http://hackage.haskell.org/package/Spock<br />
|-<br />
! Package & repositories<br />
| [http://hackage.haskell.org/package/Spock Hackage] - [https://github.com/agrafix/Spock Git]<br />
|}<br />
<br />
== Wheb ==<br />
<br />
Wheb's a WAI framework for building robust, high-concurrency web applications simply and effectively. Its primary goal is to extend the functionality of the base WAI library and to provide an easy entry point into Haskell web servers. The only prerequisite is "Learn you a Haskell" or another introductory Haskell course. It comes with lots of examples on the github page.<br />
<br />
* The core datatype will let you build anything from a read-only server to a fully interactive web application with basic Haskell.<br />
* Minimal boilerplate to start your application.<br />
* Session, Auth and Cache interfaces are built in. Just drop in a backend.<br />
* Choice between type-safe web-routes or simpler pattern-based named-routes.<br />
* Easy to use for REST APIs<br />
* WebSockets<br />
* Fully database and template agnostic<br />
* Easy handler debugging.<br />
* Middleware<br />
<br />
Wheb makes it easy to write plugins. Plugins can add routes, middleware, settings and even handle resource cleanup on server shutdown. Named routes allow plugins to dynamically generate their routes at runtime based on settings. <br />
<br />
* Sessions<br />
* Auth<br />
* Cache<br />
* [http://hackage.haskell.org/package/wheb-mongo Wheb-Mongo]<br />
* [http://hackage.haskell.org/package/wheb-redis Wheb-Redis]<br />
* [http://hackage.haskell.org/package/wheb-strapped Wheb-Strapped]<br />
<br />
{| class="wikitable"<br />
! License:<br />
| BSD3<br />
|-<br />
! Author:<br />
| Kyle Hanson<br />
|-<br />
! Maintainer:<br />
| Kyle Hanson<br />
|-<br />
! Home page:<br />
| https://github.com/hansonkd/Wheb-Framework<br />
|-<br />
! Documentation:<br />
| http://hackage.haskell.org/package/Wheb<br />
|}<br />
<br />
==See also==<br />
<br />
* [[Web/Framework survey]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Orphan_instance&diff=59828Orphan instance2015-05-31T10:13:19Z<p>Beerdude26: Newtype wrapper workaround</p>
<hr />
<div>== Description ==<br />
<br />
An orphan instance is a type class instance for class C and type T which is neither defined in the module where C is defined nor in the module where T is defined.<br />
<br />
Type class instances are special in that they don't have a name and cannot be imported explicitly. This also means that they cannot be ''excluded'' explicitly. All instances defined in a module A are imported automatically when importing A, or importing any module that imports A, directly or indirectly.<br />
<br />
Say you want to define an alternative instance to an existing instance. This is a bad thing, since if two instances for the same class/type pair are in scope, then you cannot describe in Haskell 98 which instance to use. If you want to use multiple instances for the same class/type, you have to ensure that they are never imported together in a module somewhere. It is almost impossible to assert that, or put differently, it would reduce the composability of libraries considerably.<br />
<br />
Actually, non-orphan instances can avoid definition of [[multiple instances]]. For defining an instance you have to import the class and the type and then you will automatically have the according non-orphan instances imported, too. If you want to define a new instance then the compiler will reject it immediately.<br />
<br />
== Common workaround ==<br />
<br />
Let's say you want an instance for Monoid Int that handles addition. Later on, you'd like to reuse that code for handling multiplication. But now, there's two definitions of Monoid Int!<br />
<br />
A common workaround is to wrap your type in a newtype and create an instance for that newtype. Then, marshal (=convert) between the original type and the newtype where necessary. In our example, we would have two newtypes: one for addition and one for multiplication. We would then create Monoid instances for each newtype wrapper.<br />
<br />
==When Orphan Instances can be useful==<br />
It is worth noting that Orphan Instances can be viewed as a mechanism for writing modules of code with a fixed typed interface, but parameterized over the choice of implementation. In this case, Orphan Instances act as a sort of plugin architecture for providing alternative implementations with a uniform interface. <br />
<br />
A basic treatment of the relationship between type classes and modules (in the SML sense of modules) can be found at http://www.mpi-sws.org/~dreyer/papers/mtc/main-short.pdf and http://www.cse.unsw.edu.au/~chak/papers/modules-classes.pdf<br />
<br />
== See also ==<br />
<br />
* [[Multiple instances]]<br />
* Libraries mailing list on [http://www.haskell.org/pipermail/libraries/2008-August/010399.html Orphan instances can be good]<br />
* [http://www.haskell.org/pipermail/haskell-cafe/2011-July/094014.html Ideas] on possible compiler warnings for coping with orphan instances<br />
* Libraries mailing list on [http://www.haskell.org/pipermail/libraries/2012-September/018398.html Relaxin the PVP with regards to adding instances]<br />
* [http://modula3.elegosoft.com/pm3/pkg/modula3/src/discussion/partialRev.html Partial Revelation feature] of Modula-3 which causes similar problems like Haskell's type class instances<br />
<br />
[[Category:FAQ]]<br />
[[Category:Style]]</div>Beerdude26https://wiki.haskell.org/index.php?title=Safe_Haskell&diff=59750Safe Haskell2015-05-26T11:48:17Z<p>Beerdude26: Added link to blog post, cleaned up layout, fixed typo</p>
<hr />
<div>= Introduction = <br />
<br />
Safe Haskell is a Haskell language extension. It is described in detail:<br />
<br />
; ''In the GHC user manual:'' : http://www.haskell.org/ghc/docs/latest/html/users_guide/safe-haskell.html<br />
; ''In the Safe Haskell paper:'' : http://community.haskell.org/~simonmar/papers/safe-haskell.pdf<br />
; ''Further technical discussion of Safe Haskell is on the GHC Wiki:'' : http://hackage.haskell.org/trac/ghc/wiki/SafeHaskell<br />
<br />
For a high-level overview, see this blog post: http://begriffs.com/posts/2015-05-24-safe-haskell.html.<br />
<br />
= In detail = <br />
<br />
As the Safe Haskell paper describes, it "hardens" the Haskell language by providing five properties: type safety, referential transparency, strict module encapsulation, modular reasoning and semantic consistency.<br />
<br />
Safe Haskell is ''not'' magic, and not about catching bugs. It does not ensure that library authors who mark modules as "Trustworthy" are not lying, or incorrect. It does not ensure code inferred safe but in IO cannot perform arbitrary IO. It ''does'' ensure that untrusted code inferred to be safe will, assuming its "Trustworthy" imports are ''indeed'' "Trustworthy" obey the above five properties. As such, again assuming "Trustworthy" imports are indeed so, Safe Haskell infers that untrusted code inferred safe and ''not'' in IO can be run without fear (aside from fear of resource over-utilization/exhaustion).<br />
<br />
Most code that most people want to write is going to be Safe Haskell by default. As Simon Marlow has pointed out, <br />
<br />
<blockquote><br />
"Normally when you use an unsafe feature, the purpose is to use it to implement a safe API - if that's the case, all you have to do is add Trustworthy to your language pragma, and the API is available to use from Safe code. 99% of Hackage should be either Safe or Trustworthy. We know that 27% is already inferred Safe (see the paper), and a lot of the rest is just waiting for other libraries to add Trustworthy where necessary."<br />
</blockquote><br />
<br />
Again, as Simon Marlow argues, <br />
<br />
<blockquote><br />
"For typical Haskell programmers, using <code>{-# LANGUAGE Safe #-}</code> will be like <code>-Wall</code>: something that is considered good practice from a hygiene point of view. If you don't ''need'' access to unsafe features, then it's better to write in the safe subset, where you have stronger guarantees. Just like -Wall, you get to choose whether to use <code>{-# LANGUAGE Safe #-}</code> or not."<br />
</blockquote></div>Beerdude26https://wiki.haskell.org/index.php?title=Zipper&diff=55184Zipper2012-12-30T22:13:33Z<p>Beerdude26: Fixed the links to the papers by Ralf Hinze and Johan Jeuring</p>
<hr />
<div>The Zipper is an idiom that uses the idea of “context” to the means of<br />
manipulating locations in a data structure. [[Zipper monad]] is a monad<br />
which implements the zipper for binary trees.<br />
<br />
__TOC__<br />
<br />
Sometimes you want to manipulate a ''location'' inside a data structure,<br />
rather than the data itself. For example, consider a simple binary tree type:<br />
<br />
<haskell><br />
data Tree a = Fork (Tree a) (Tree a) | Leaf a<br />
</haskell><br />
<br />
and a sample tree t:<br />
{|<br />
| <haskell><br />
t = Fork (Fork (Leaf 1)<br />
(Leaf 2))<br />
(Fork (Leaf 3)<br />
(Leaf 4))<br />
</haskell><br />
| [[Image:Tree-12-34.png]]<br />
|}<br />
Each subtree of this tree occupies a certain location in the tree taken as a whole. The location consists of the subtree, along with the rest of the tree, which we think of the ''context'' of that subtree. For example, the context of<br />
<br />
<haskell><br />
Leaf 2<br />
</haskell><br />
<br />
in the above tree is<br />
<br />
{|<br />
| <haskell><br />
Fork (Fork (Leaf 1) @)<br />
(Fork (Leaf 3) (Leaf 4))<br />
</haskell><br />
| [[Image:Context-1X-34.png]]<br />
|}<br />
where @ marks a hole: the spot that the subtree appears in. This is the way we shall implement a tree with a focus. One way of expressing this context is as a path from the root of the tree to the hole (to which the required subtree will be attached). To reach our subtree, we needed to go down the left branch, and then down the right one. Note that the context is essentially a way of representing the tree, “missing out” a subtree (the subtree we are interested in). <br />
<br />
A naive implementation of the context, inspired directly by the graphical representation might be to use <hask>Tree (Maybe a)</hask> instead of <hask>Tree a</hask>. However, this would lose an essential point: in any given context, there is exactly one hole.<br />
<br />
Therefore, we will represent a context as follows:<br />
<br />
<haskell><br />
data Cxt a = Top | L (Cxt a) (Tree a) | R (Tree a) (Cxt a)<br />
</haskell><br />
<br />
<code>L c t</code> represents the left part of a branch of which the right part was <code>t</code> and whose parent had context <code>c</code>. The <code>R</code> constructor is similar. <code>Top</code> represents the top of a tree.<br />
<br />
Remarks:<br />
* In fact, this is equivalent to a list, whose elements are the appropriate collateral trees, each element labeled with the information which direction was chosen:<br />
:<hask>type Context a = [(Direction, Tree a)]</hask><br />
:<hask>data Direction = Lft | Rght</hask><br />
* We chose to propagate from the hole towards the root. This is an independent idea of the above considerations: it is not the unicity of the hole that forced us to do so. It is simply more efficient if we want to define operations later for a tree with a focus (move focus left, right, up).<br />
* Note that in the original paper, Huet dealt with B-trees (ones where nodes have arbitrary numbers of branches), so at each node, a list is used instead of the two (Tree a) parameters to represent children. Later we shall see that the solution of the problem can be formalized in a general way which covers solutions for both kinds of trees, as special cases.<br />
<br />
Using this datatype, we can rewrite the sample context above in proper Haskell:<br />
<br />
<haskell><br />
R (Leaf 1) (L Top (Fork (Leaf 3) (Leaf 4)))<br />
</haskell><br />
<br />
Note that the context is actually written by giving the path from the subtree to the root (rather than the other way round):<br />
{| border=1<br />
| <math>c_0</math><br />
| <math>\begin{matrix}\mathrm{R\;(Leaf\;1)}\;\underbrace{\mathrm{(L\;Top\;(Fork\;(Leaf\;3)\;(Leaf\;4)))}}\\\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;c_1\end{matrix}</math><br />
| [[Image:Context-1X-34.png]]<br />
|-<br />
| <math>c_1</math><br />
| <math>\begin{matrix}\mathrm{L}\;\underbrace{\mathrm{Top}}\;\mathrm{(Fork\;(Leaf\;3)\;(Leaf\;4))}\\\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!c_2\end{matrix}</math><br />
| [[Image:Context-X-34.png]]<br />
|-<br />
| <math>c_2</math><br />
| Top<br />
| [[Image:Top.png]]<br />
|}<br />
<br />
Or the more deconstructed representation:<br />
{| border=1<br />
| <math>\left[\begin{matrix}\mathrm{(Rght,\;Leaf\;1),\;\underbrace{\mathrm{(Lft,\;Fork\;(Leaf\;3)\;(Leaf\;4))}}}\\\underbrace{\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;c_1\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;}\\c_0\end{matrix}\right]</math><br />
| [[Image:Path-1X-34.png]]<br />
|}<br />
where <math>c_0</math>, <math>c_1</math> are the appropriate correspondents of the <math>c_0</math>, <math>c_1</math> of the previous image. It is the empty list that represents <math>c_2</math>.<br />
<br />
Now we can define a tree location:<br />
<br />
<haskell><br />
type Loc a = (Tree a, Cxt a)<br />
</haskell><br />
{|<br />
| [[Image:Circum-op12cl-34.png]]<br />
| [[Image:Mount-op12cl-34.png]]<br />
|}<br />
thus, a tree with a focus (drawn here as a tree with a marked subtree) shall be represented as “mounting” the focus (a tree) into the hole of the appropriate context.<br />
<br />
Now, we can define some useful functions for manipulating locations in a tree:<br />
<br />
<haskell><br />
left :: Loc a -> Loc a<br />
left (Fork l r, c) = (l, L c r)<br />
<br />
right :: Loc a -> Loc a<br />
right (Fork l r, c) = (r, R l c)<br />
<br />
top :: Tree a -> Loc a<br />
top t = (t, Top)<br />
<br />
up :: Loc a -> Loc a<br />
up (t, L c r) = (Fork t r, c)<br />
up (t, R l c) = (Fork l t, c)<br />
<br />
upmost :: Loc a -> Loc a<br />
upmost l@(t, Top) = l<br />
upmost l = upmost (up l)<br />
<br />
modify :: Loc a -> (Tree a -> Tree a) -> Loc a<br />
modify (t, c) f = (f t, c)<br />
</haskell><br />
<br />
It is instructive to look at an example of how a location gets transformed as we move from root to leaf. Recall our sample tree t. Let's name some of the relevant subtrees for brevity:<br />
<br />
<haskell><br />
t = let tl = Fork (Leaf 1) (Leaf 2)<br />
tr = Fork (Leaf 3) (Leaf 4)<br />
in Fork tl tr<br />
</haskell><br />
<br />
Then to reach the location of <code>Leaf 2</code>:<br />
<br />
<haskell><br />
(right . left . top) t<br />
= (right . left) (t, Top)<br />
= right (tl, L Top tr)<br />
= (Leaf 2, R (Leaf 1) (L Top tr))<br />
</haskell><br />
<br />
To reach that location and replace <code>Leaf 2</code> by <code>Leaf 0</code>:<br />
<br />
<haskell><br />
modify ((right . left . top) t) (\_ -> Leaf 0)<br />
= ...<br />
= (Leaf 0, R (Leaf 1) (L Top tr))<br />
</haskell><br />
<br />
Afterwards some may like to continue walking to other parts of the new tree, in which case continue applying <code>left</code>, <code>right</code>, and <code>up</code>.<br />
<br />
Some others may like to retrieve the new tree (and possibly forget about locations), in which case <code>upmost</code> is useful:<br />
<br />
<haskell><br />
(fst . upmost) (modify ((right . left . top) t) (\_ -> Leaf 0))<br />
= (fst . upmost) (Leaf 0, R (Leaf 1) (L Top tr))<br />
= fst (Fork (Fork (Leaf 1)<br />
(Leaf 0))<br />
tr<br />
, Top)<br />
= Fork (Fork (Leaf 1)<br />
(Leaf 0))<br />
tr<br />
</haskell><br />
<br />
== Automation ==<br />
<br />
There's a principled way to get the necessary types for contexts and the<br />
context filling functions, namely by differentiating the data structure.<br />
[http://www.cs.nott.ac.uk/~ctm/ Some relevant papers].<br />
<br />
For an actual implementation in [[Generic Haskell]], see the paper [http://www.staff.science.uu.nl/~jeuri101/homepage/Publications/tidata.pdf Type-indexed data types] by Ralf Hinze, Johan Jeuring and Andres Löh, or a similar paper [http://www.staff.science.uu.nl/~jeuri101/homepage/Publications/ghpractice.pdf Generic Haskell: Applications] by Ralf Hinze and Johan Jeuring<br />
<br />
== Alternative formulation ==<br />
<br />
The dual of Huet zipper is generic zipper -- which is a derivative of<br />
a traversal function rather than that of a data structure.<br />
Unlike Huet zipper,<br />
generic zipper can be implemented once and for all data structures,<br />
in the existing Haskell.<br />
[http://pobox.com/~oleg/ftp/Computation/Continuations.html#zipper Generic Zipper and its applications]<br />
<br />
== Comonads and monads ==<br />
<br />
Comonads<br />
* [http://cs.ioc.ee/~tarmo/tsem05/uustalu0812-slides.pdf Structured Computation on Trees or, What’s Behind That Zipper? (A Comonad)], slides by Tarmo Uustalu<br />
* [http://cs.ioc.ee/~tarmo/papers/tfp05-book.pdf Comonadic functional attribute evaluation] by Tarmo Uustalu1 and Varmo Vene, a more detailed treatment<br />
* [http://www.cs.nott.ac.uk/~txa/monads-more-4.pdf Monads and more (Part 4)] by Tarmo Uustalu<br />
* [http://sigfpe.blogspot.com/2006/12/evaluating-cellular-automata-is.html Evaluating cellular automata is comonadic], part of Sigfpe's ''A Neighborhood of Infinity''<br />
Monads:<br />
* [http://sigfpe.blogspot.com/2007/01/monads-hidden-behind-every-zipper.html The Monads Hidden Behind Every Zipper], part of Sigfpe's ''A Neighborhood of Infinity''<br />
<br />
== Applications ==<br />
<br />
;[http://okmij.org/ftp/Computation/Continuations.html#zipper-fs ZipperFS] <br />
:Oleg Kiselyov's zipper-based [[Libraries and tools/Operating system|file server/OS]] where threading and exceptions are all realized via [[Library/CC-delcont|delimited continuation]]s.<br />
;[http://donsbot.wordpress.com/2007/05/17/roll-your-own-window-manager-tracking-focus-with-a-zipper Roll Your Own Window Manager: Tracking Focus with a Zipper]<br />
:describes the use of zippers in [http://www.xmonad.org/ xmonad].<br />
* The [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/AvlTree AVL Tree] package contains a zipper for navigating AVL trees.<br />
* A zipper for navigating rose trees (as found in the standard <code>Data.Tree</code> library) is available in the [http://code.haskell.org/yi/Data/Tree/ Yi code repository].<br />
* [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/rosezipper An implementation of a zipper] for navigating rose trees (as found in the standard <code>Data.Tree</code> library).<br />
<br />
== Further reading ==<br />
* Gerard Huet's [http://www.st.cs.uni-sb.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf paper] where he originally proposed the concept of a zipper<br />
* Hinz's [http://archive.cs.uu.nl/pub/RUU/CS/techreps/CS-2001/2001-33.pdf Weaving a Web] extends this pattern.<br />
* [http://sigfpe.blogspot.com/2006/09/infinitesimal-types.html Infinitesimal Types] writes on interesting generalizations (e.g. derivative of a type — to the analogy of the notion of derivative in rings, e.g. in analysis or for polinomials)<br />
* [http://en.wikibooks.org/wiki/Haskell/Zippers Haskell/Zippers] on Wikibooks, a detailed treatment of zippers, and generalizing the notion as derivative<br />
* [http://okmij.org/ftp/Computation/Continuations.html#zipper Generic Zipper and its applications], writing that “Zipper can be viewed as a [[Library/CC-delcont|delimited continuation]] reified as a data structure” (link added).<br />
* [http://www.cs.indiana.edu/~adamsmd/papers/scrap_your_zippers/ Scrap Your Zippers: A Generic Zipper for Heterogeneous Types], defines a generic zipper that works on arbitrary instances of the Data class. It uses GADTs instead of [[Library/CC-delcont|delimited continuations]].<br />
<br />
[[Category:Idioms]]</div>Beerdude26