1 the Standard Haskell Infrastructure [Infrastructure.Tex 2011-11-17]
Total Page:16
File Type:pdf, Size:1020Kb
infrastructure.tex 2011-11-17 haskell doc September 6, 2012 1 1 The standard Haskell infrastructure [infrastructure.tex 2011-11-17] Announcement Forword I shouldn’t say, that the Haskell Cabal is a mess. Maybe, it is a Haskell amateurs write Haskell modules, Haskell professionals good design and the cabal command is already a powerful and write packages. versatile tool. But its documentation is still a mess: all the good Modules are part of the Haskell language in the narrower sense Haskell books out there either don’t deal with packages at all, of a well–defined syntax and pretty well–explained semantics. or they sketch typical examples without explaining a lot. And Packages are defined by a standard beyond, namely the Ca- the Cabal User Guide itself is rather an overwhelming reference bal, the Common Architecture for Building Applications and Li- than a tutorial. braries. I should understand the matter to write this text. But I wrote In the sequel, we try to shed some light on the standard Haskell it, because I don’t. And nothing out there fills the void, so far. infrastructure. So please, don’t be polite, but criticize. Especially, if you find it (1) The Haskell Platform is the common ground for all Haskellers useful. and we give a summarizing overview of its ingredients. (2) Cabal *** CONTINUE HERE *** libraries.tex 2009-02-14, 2011-10-27 haskell doc September 6, 2012 2 The Standard Haskell Environment Each of the mentioned applications comes with a built–in man- ual, which shows when you add --help to the commands name. The Haskell Platform 3 1 The comprehensive GHC Users’s Guide is available online , but In 2009, the Haskell Platform was introduced as the default also comes as part of the Haskell Platform. “comprehensive, robust developmnet environment for program- ming in Haskell”. If you consider working with Haskell, this is imperative. It is available for all main stream operating systems Different units of Haskell and really easy to install. It is important to understand the following concepts, which all It includes refer to units of Haskell code or software. (1) (1) The standard Haskell machine: namely the Glasgow Haskell Modules Compiler or GHC. A module is a well–formed unit of Haskell source code, with By “Haskell machine” we mean an interpreter or compiler, and an own name (say Example) and stored in a separate file of the in fact, GHC comes with both. same name (i.e. Example.hs or Example.lhs). This concept is In the past, there was a tendency to clearly distinguish the part of the Haskell language and its general form is Haskell language (at that time, the standard was Haskell 98) from the Haskell machines. There exist about a dozen dif- module Example {- optional list/tuple of exports -} where ferent implementations.2 But most of them don’t implement {- imported modules -} the complete current standard of Haskell 2010 and the with {- data type and function definitions -} the introduction of the Haskell Platform, the emphasis is com- pletely turned and GHC has become the standard. The definitions of given modules can be made available in new (2) A couple of developer tools, in particular: module (with say “import Example”), or it can be loaded into ( ) Cabal, an acronym for Common Architecture for Building a Haskell interpreter (like ghci or hugs, by calling the :load Applications and Libraries. In particular, it comes with Example) command. the cabal executable, which is the new universal tool to (2) Libraries work with packages: downloading and installing new pack- A library is a collection of interconnected modules. Often, ages (from HackageDB), but also creating, modifying and there is a kind of principal module in a library, which imports documenting packages can be done with this command. Un- the other modules and exports the (most important) defini- derstanding all that is the main issue of our text below. tions again. And then the functionality of the whole library ( ) Haddock, a document generator, can be loaded or imported by just loading this principal mod- and a couple of other tools for parsing, profiling, debugging ule. In fact, there is often no clear distinction between the and statistics, which are beyond our text here. terms “library” and “module” in the documentations. (3) Applications/Programs/Executables (3) Documentation. An application, also known as program or executable, refers to a Haskell unit that can be run as a standalone command GHC — the Glasgow Haskell Compiler from the command line of the operating system. The Glasgow Haskell Compiler is the default Haskell implemen- In terms of Haskell, this is an action or function of type IO (), tation and is part of the Haskell platform. In fact, the title always called main, embedded into a module called Main and “compiler” is an understatement, because it can compile as well compiled into an executable program of an arbitrary name, as interpret Haskell code (with subtle mixtures of both). say halloworld. This can then be run by calling halloworld Its two most important applications are: from the shell or command line (possibly with additional ar- guments). (1) ghc the compiler, which converts source code into fast pro- (4) Package grams. A package is a collection of modules, applications and related (2) ghci the interpreter, but the “i” can also be read as “in- files like documents etc. The package concept is not part of teractive”. It opens the REPL (Read-Eval-Print Loop) envi- the Haskell language in the narrower sense of Haskell 2010, ronment, where your input is immediately evaluated. That is but is part of a wider standard called Cabal, the Common what one thing that make developing in functional languages Architecture for Building Applications and Libraries4. so fast and comfortable. As a Haskell amateur, you write and use Haskell modules or If you installed the Haskell platform on Windows, you will also libraries and compile them into applications. To explore and have exploit the power and beauty of functional programming in gen- eral and Haskell in particular, you can live a long and happy (2)(a) WinGHCi , a nice and supportive GUI version of the ghci, that more than compensates for the poor standard command life without getting in touch with the package concept. But as a terminal in Windows. serious Haskell programmer, if you want to share your code with others, you write Cabal packages and you need to understand Two other available and useful applications are: some new standards. (3) ghc-pkg is a powerful package management tool for the GHC, that comes with a whole range of subcommands for all sorts of tasks. With the new emphasis on the Cabal standard for Haskell package management independent of Haskell im- plementations (see below), you will normally rather call the cabal command, instead, which often runs ghc-pkg behind the scenes. (4) runghc allows to run a Haskell program without compiling it first. This may occasionally be convenient for your own pro- gram development. This command, too, has an implementation–independent couterpart called runhaskell , and that comes with the Haskell Platform, as well. But in fact, it is just an alias for runghc. 1 http://hackage.haskell.org/platform/ 2 See http://haskell.org/haskellwiki/implementations for an overview of the current Haskell machines 3 The latest version of the GHC Users’s Guide is available at http://www.haskell.org/ghc/docs/latest/html/users guide/ 4 http://haskell.org/cabal/ libraries.tex 2009-02-14, 2011-10-27 haskell doc September 6, 2012 3 \newpage in its menu to pop up the Platform documentation.) Cabal and the Haskell Platform distinguish two main places for The Haskell Hierarchical Libraries the whole installation: 5 There is a standard hierarchy of important libraries: global refers to the system–wide part of the implementation. 6 (1) The Standard Prelude is the core of the whole Haskell On Windows, this is a directory like C:\Programs\Haskell language in the sense that it defines all primitive types and Platform. GNU/Linux uses various directories, depending on functions. All these are summarized in the Prelude 7 mod- the distribution. ule/library. When you start a Haskell interpreter like ghci, user denotes the local and user–specific parts. On GNU/Linux, the Prelude comprises the set of data structures and functions this is always the .cabal directory at /home/myself/.cabal which are available. On Windows, this is the cabal directory at something 8 like C:\Documents and Settings\myself\User data\cabal (In (2) The Haskell 2010 language report is the official definition of Haskell in the narrower sense as the syntax and semantics of a GHCi session you can query this directory by calling System.Directory.getAppUserDataDirectory "cabal" a formal language and the least set of features common to all ). real Haskell implementations. Haskell 2010 is the successor of The Haskell Platform itself is most likely installed globally. But 9 the Haskell 98 Language. if you install additional packages from Hackage (see below) with 10 Next to the Prelude, it includes a couple of other libraries the cabal command, then this is only done for the current user for important data structures (Array, Bits, Char, Complex, Int, by default, although it can easily be done globally as well (by Ix, List, Maybe, Ratio, Numeric), system and control libraries adding the --global flag). (Monad, System) and interfaces with other programming lanu- ages (Foreigh), C in particular. The setup of the whole layout is summarized in the config file 11 (3) The Base library contains additional tools that are es- that resides in the .cabal directory (or cabal on Windows).