Home
The Glorious Glasgow Haskell Compilation System User`s Guide
Contents
1. f Int Int gt Int Int x xX g Int Int gt Int g a b a h x let y X xX in 136 GHC Language Features The typical use of unboxed tuples is simply to return multiple values binding those multiple results with a case expression thus y x 1 y 1 x x case f x x of a b gt a b g You can have an unboxed tuple in a pattern binding thus f x let p q h x in body If the types of p and q are not unboxed the resulting binding is lazy like any other Haskell pattern bind ing The above example desugars like this f x let t case h x o f p q gt p q p fst t q snd t in body Indeed the bindings can even be recursive 7 3 Syntactic extensions 7 3 1 Hierarchical Modules GHC supports a small extension to the syntax of module names a module name is allowed to contain a dot This is also known as the hierarchical module namespace extension because it extends the normally flat Haskell module namespace into a more flexible hierarchy of modules This extension has very little impact on the language itself modules names are always fully qualified so you can just think of the fully qualified module name as the module name In particular this means that the full module name must be given after the module keyword at the begin
2. fwarn un terns that aren t used used matches warn when fields of a re dynamic fwarn miss cord are uninitialised fno ing fields warn miss ing fields warn when class methods dynamic fwarn miss are undefined fno ing methods warn miss ing methods warn about top level fanc dynamic fwarn miss tions without signatures fno ing signatures warn miss ing signatures warn when names are shad dynamic fwarn owed fno name shadowing warn name shadowing fwarn orphans warn when the module con dynamic fno warn orphans tains orphan instance de clarations or rewrite rules warn about overlapping dynamic fwarn over patterns fno lapping patterns warn over lapping patterns warn about lambda patterns dynamic fwarn that can fail fno simple patterns warn simple patterns warn when defaulting hap dynamic fwarn pens fno type defaults warn type defaults warn about bindings that dynamic fwarn unused binds are unused fno warn unused binds warn about unnecessary im dynamic 3 fwarn un ports fno used imports warn un used imports warn about variables in pat dynamic fno warn un used matches 4 17 14 Optimisation levels Section 4 9 Optimisation code improvement Flag Description Static Dynamic Reverse 0 Enable default optimisation dynam
3. The 2 means that the rule is active in Phase 2 and subsequent phases The inverse notation 2 is also accepted meaning that the rule is active up to but not including Phase 2 e Layout applies in a RULES pragma Currently no new indentation level is set so you must lay out your rules starting in the same column as the enclosing definitions e Each variable mentioned in a rule must either be in scope e g map or bound by the forall e g f g xs The variables bound by the forall are called the pattern variables They are separated by spaces just like ina type forall e A pattern variable may optionally have a type signature If the type of the pattern variable is poly morphic it must have a type signature For example here is the foldr build rule fold build forall k z g forall b a gt b gt b gt b gt b foldr k z build g g k z Since g has a polymorphic type it must have a type signature e The left hand side of a rule must consist of a top level variable applied to arbitrary expressions For example this is not OK wrongl forall el e2 case True of True gt el Fals gt e2 wrong2 forall f f True True In wrong1 the LHS is not an application in wrong2 the LHS has a pattern variable in the head e A rule does not need to be in the same module as any of the variables it mentions though of course they need to be in scope e Rules are auto
4. fext core Generate hcr files GHC can also read in External Core files as source just give the hcr file on the command line in stead of the hs or 1hs Haskell source A current infelicity is that you need to give the fglasgow exts flag too because ordinary Haskell 98 when translated to External Core uses things like rank 2 types Debugging the compiler HACKER TERRITORY HACKER TERRITORY You were warned Dumping out compiler intermediate structures ddump pass Make a debugging dump after pass lt pass gt may be common enough to need a short form You can get all of these at once lots of output by using v5 or most of them with v4 Some of the most useful ones are parser output ddump pddsaa rn renamer output ddump tc typechecker output ddump tc typechecker output Dump Template Haskell expressions that we splice in and what ddump Haskell code the expression evaluates to splices 89 Using GHC ddump types ddump de rddump ds Dump a type signature for each value defined at the top level of the module The list is sorted alphabetically Using dppr debug dumps a type signature for all the imported and system defined things as well useful for debugging the compiler derived instances desugarer output 90 Using GHC ddump rules ddump sim pl ddump ddump cpranal ddump stranal inlinin
5. 1860318 19 8 3733184 39 7 3149544 33 5 AJP o o 348140 3 7 partial applications 308906 3 3 normal indirections O 0 0 permanent indirections RETURNS 5870443 2137257 36 4 from entering a new constructor the rest from entering an existing constructor 2349219 40 0 vectored the rest unvectored RET_NEW 2137257 32 5 46 2 21 3 0 0 0 0 0 0 0 0 0 0 0 0 RET_OLD 3733184 2 8 67 9 29 3 0 0 0 0 0 0 0 0 0 0 0 0 RET_UNBOXED_TUP 2 0 0 0 0 100 0 0 0 0 0 0 0 0 0 0 0 0 0 RET_VEC_RETURN 2349219 0 0 0 0 100 0 0 0 0 0 0 0 0 0 0 0 0 0 125 Profiling UPDATE FRAMES 2241725 0 omitted from thunks SEQ FRAMES il CATCH FRAMES 1 UPDATES 2241725 O 0 0 data values 34827 1 6 partial applications 2 in place 34825 allocated new space 2206898 98 4 updates to existing heap objects 46 by squeezing UPD_CON_IN_NI 0 0 0 0 0 0 0 UPD_PAP_IN_NI 34825 0 0 0 34825 0 0 EW EW EW EW NEW GEN UPDAT GI S 2274700 99 9 OLD GEN UPDATES 1852 0 1 Total bytes copied during GC 190096 KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK 3647137 ALLOC_HEAP ctr 11882004 ALLOC_HEAP_ tot 69647 ALLOC_FUN_ctr 69647 ALLOC_FUN_adm 69644 ALLOC_FUN_gds 34819 ALLOC_FUN_slp 34831 ALLOC_FUN_hs 34816 ALLOC_FUN_hs 0 ALLOC_FUN_hs 0 ALLOC_FUN_hs A A A CEB EE
6. 4 8 4 If GHC_PACKAGE_PATH ends in a separator then the default user and system package databases are appended in that order e g to augment the usual set of packages with a database of your own you could say on Unix export GHC_PACKAGE_PATH SHOME my ghc packages conf use instead of on Windows To check whether your GHC_PACKAGE_PATH setting is doing the right thing ghc pkg list will list all the databases in use in the reverse order they are searched Building a package from Haskell source We don t recommend building packages the hard way Instead use the Cabal Cabal index html infra structure if possible If your package is particularly complicated or requires a lot of configuration then you might have to fall back to the low level mechanisms so a few hints for those brave souls follow e You need to build an installed package info file for passing to ghc pkg when installing your package The contents of this file are described in Section 4 8 6 InstalledPackageInfo a package specification e The Haskell code in a package may be built into one or more archive libraries e g libHSfoo a or a single DLL on Windows e g HSf00 d11 The restriction to a single DLL on Windows is be cause the package system is used to tell the compiler when it should make an inter DLL call rather than an intra DLL call inter DLL calls require an extra indirection Building packages as DLLs doesn t work
7. Chapter 9 What to do when something goes wrong If you still have a problem after consulting this section then you may have found a bug please report it See Section 1 2 Reporting bugs in GHC for details on how to report a bug and a list of things we d like to know about your bug If in doubt send a report we love mail from irate users Section 12 1 Haskell 98 vs Glasgow Haskell language non compliance which describes Glasgow Haskell s shortcomings vs the Haskell language definition may also be of interest 9 1 When the compiler does the wrong thing Help The compiler crashed or panic d This is a terrible error message What about this warning from the C compiler Sensitivity to hi interface files T think GHC is producing incor rect code Why did I get a link error Ts this line number right These events are always bugs in the GHC system please report them If you think that GHC could have produced a better error mes sage please report it as a bug For example warning Foo declared static but never defined Unsightly but shouldn t be a problem GHC is very sensitive about interface files For example if it picks up a non standard Prelude hi file pretty terrible things will happen If you turn on fno implicit prelude the compiler will almost surely die unless you know what you are doing Furt
8. UNPACK Int T S will store two unboxed Int s directly in the T constructor The unpacker can see through newtypes too If a field cannot be unpacked you will not get a warning so it might be an idea to check the generated code with ddump simp1l See also the funbox strict fields flag which essentially has the effect of adding UNPACK to every strict constructor field Rewrite rules The programmer can specify rewrite rules as part of the source program in a pragma GHC applies these rewrite rules wherever it can provided a the O flag Section 4 9 Optimisation code improve ment is on and b the frules off flag Section 4 9 2 platform independent flags is not specified and c the fglasgow exts Section 7 1 Language options flag is active Here is an example RULES map map forall f g xs map f map g xs map f g xs Syntax From a syntactic point of view e There may be zero or more rules in a RULES pragma e Each rule has a name enclosed in double quotes The name itself has no significance at all It is only used when reporting how many times the rule fired 193 GHC Language Features e A rule may optionally have a phase control number see Section 7 10 3 3 Phase control immedi ately after the name of the rule Thus RULES map map 2 forall f g xs map f map g xs map f g xs
9. ic type constructors such as Maybe and list to the ones that are allowed e In an instance declaration for a generic class the idea is that the compiler will fill in the methods for you based on the generic templates However it can only do so if e The instance type is simple a type constructor applied to type variables as in Haskell 98 e No constructor of the instance type has unboxed fields Of course these things can only arise if you are already using GHC extensions However you can still give an instance declarations for types which break these rules provided you give explicit code to override any generic default methods The option ddump deriv dumps incomprehensible stuff giving details of what the compiler does with generic declarations 7 13 4 Another example Just to finish with here s another example I rather like class Tag a where nCons a gt Int nCons Unit _ 1 nCons a b _ 1 202 GHC Language Features nCons a b _ nCons bot a nCons bot b tag a gt Ine tag Unit 1 bag All sa e m o be 1 tag a b Inl x tag x tag a b Inr y nCons bot a tag y 7 14 Control over monomorphism GHC supports two flags that control the way in which generalisation is carried out at let and where bind ings 7 14 1 Switching off the dreaded Monomorphism Re striction Haskell s monomorphism restriction see Section 4 5 5
10. v verbose mode equivalent dynamic to v3 vn set verbosity level dynamic V display GHC version mode version display GHC version mode numeric version display GHC version mode numeric only print libdir display GHC library direct mode ory ferror spans output full span in error static messages Hsize Set the minimum heap size static to size Rghc timing Summarise timing stats for static GHC same as RTS tstderr 4 17 2 Which phases to run Section 4 4 3 Batch compiler mode Flag Description Static Dynamic Reverse E Stop after preprocessing mode hspp file C Stop after generating C mode hc file S Stop after generating as mode sembly s file Do not link dynamic x suffix Override default behaviour static for source files 4 17 3 Alternative modes of operation Section 4 4 Modes of operation Flag Description Static Dynamic Reverse interactive Interactive mode normally used by just running ghci make Build a multi module Haskell program automat ically figuring out depend encies Likely to be much easier and faster than us ing make 100 Using GHC Description Evaluate expr Static Dynamic mode Generate dependency in formation suitable for use ina Makefile mode 4 17 4 Redirecting output Section 4 6 4 Redirecting the c
11. In GHC alloca is implemented using MutableByteArray so allocation and deallocation are fast much faster than C s malloc free but not quite as fast as stack allocation in C Use alloca whenever you can mallocForeignPtr Useful for longer term allocation which requires garbage collection If you intend to store the pointer to the memory in a foreign data structure then mallocForeignPtr is not a good choice however In GHC mallocForeignPtr is also implemented using Mut ableByteArray Although the memory is pointed to by a For eignPtr there are no actual finalizers involved unless you add one with addForeignPtrFinalizer and the deallocation is done us ing GC somallocForeignPtr is normally very cheap malloc free If all else fails then you need to resort to Foreign malloc and Foreign free These are just wrappers around the C functions of the same name and their efficiency will depend ultimately on the im plementations of these functions in your platform s C library We usu ally find malloc and free to be significantly slower than the other forms of allocation above Foreign Marshal Pool Pools are currently implemented using malloc free so while they might be a more convenient way to structure your memory allocation than using one of the other forms of allocation they won t be any more efficient We do plan to provide an improved performance implementa tion of Pools in the future however 209
12. Inr y bs where y bs fromBin bs fromBin a b bs x y bs where x bs fromBin bs y bs fromBin bs This class declaration explains how toBin and fromBin work for arbitrary data types They do so by giving cases for unit product and sum which are defined thus in the library module Generics data Unit Unit data a b Inl a Inr b data a b a b Now you can make a data type into an instance of Bin like this instance Bin a Bin b gt Bin a b instance Bin a gt Bin a That is just leave off the where clause Of course you can put in the where clause and over ride whichever methods you please 7 13 1 Using generics To use generics you need to e Use the flags fglasgow exts to enable the extra syntax fgenerics to generate extra per data type code and package lang to make the Generics library available e Import the module Generics from the lang package This import brings into scope the data types Unit and You don t need this import if you don t mention these types explicitly for ex ample if you are simply giving instance declarations 200 GHC Language Features 7 13 2 Changes wrt the paper Note that the type constructors and can be written infix indeed you can now use any operator starting in a colon as an infix type constructor Also note that the type constructors are not exactly as in the paper Unit instead of 1 et
13. M128m export GHCRTS RTS options taken from the GHCRTS environment variable can be overridden by options given on the command line 4 14 2 Miscellaneous RTS options Vsecs Sets the interval that the RTS clock ticks at The runtime uses a single timer signal to count ticks this timer signal is used to control the context switch timer Section 4 11 Using Con current Haskell and the heap profiling timer Section 5 4 1 RTS options for heap profil ing Also the time profiler uses the RTS timer signal directly to record time profiling samples Normally setting the V option directly is not necessary the resolution of the RTS timer is adjusted automatically if a short interval is requested with the C or i options However setting V is required in order to increase the resolution of the time profiler 4 14 3 RTS options to control the garbage collector There are several options to give you precise control over garbage collection Hopefully you won t need any of these in normal operation but there are several things that can be tweaked for maximum perform ance Asize Ffactor Default 256k Set the allocation area size used by the garbage collector The alloca tion area actually generation 0 step 0 is fixed and is never resized unless you use H below Increasing the allocation area size may or may not give better performance a bigger allocation area means worse cache behaviour but fewer garb
14. It may also output a C file which contains additional C functions to be linked into the program together with a C header that gets included into the C code to which the Haskell module will be compiled when compiled via C and into the C file These two files are created when the def construct is used see be low Actually hse2hs does not output the Haskell file directly It creates a C program that includes the head ers gets automatically compiled and run That program outputs the Haskell code In the following Haskell file is the main output usually a hs file compiled Haskell file is the Haskell file after ghc has compiled it to C i e a hc file C program is the program that outputs the Haskell file C file is the optionally generated C file and C header is its header file command line syntax hsc2hs takes input files as arguments and flags that modify its behavior o FILE or Name of the Haskell file output FILE t FILEor The template file see below template FILE c PROG or The C compiler to use default ghc cc PROG 1 PROG or The linker to use default gec 1d PROG C FLAG or An extra flag to pass to the C compiler cflag FLAG I DIR Passed to the C compiler L FLAG or An extra flag to pass to the linker lflag FLAG i FILE or As if the appropriate include directive was placed in the source include FILE D NAME VAL
15. NOINLINE 2 Maybe No INLINE f Yes Yes NOINLINE f No No By Maybe we mean that the usual heuristic inlining rules apply if the function body is small or it is applied to interesting looking arguments etc Another way to understand the semantics is this e For both INLINE and NOINLINE the phase number says when inlining is allowed at all e The INLINE pragma has the additional effect of making the function body look small so that when inlining is allowed it is very likely to happen The same phase numbering control is available for RULES Section 7 11 Rewrite rules 7 10 4 LANGUAGE pragma This allows language extensions to be enabled in a portable way It is the intention that all Haskell com pilers support the LANGUAGE pragma with the same syntax although not all extensions are supported by all compilers of course The LANGUAGE pragma should be used instead of OPTIONS_GHC if pos sible For example to enable the FFI and preprocessing with CPP LANGUAGE ForeignFunctionInterface CPP Any extension from the Extension type defined in Language Haskell Extension libraries Cabal Language Haskell Extension html may be used GHC will report an error if any of the requested extensions are not supported 7 10 5 LINE pragma This pragma is similar to C s 1ine pragma and is mainly for use in automatically generated Haskell code It lets you specify the line nu
16. also for most major re leases FreeBSD x86 On FreeBSD x86 GHC can be installed using either the ports tree cd usr ports lang ghc amp amp make install or from a pre compiled package available from your local FreeBSD mirror Other platform specific packages may be available check the GHC download page for details GHC binary distributions gt Binary distributions come in bundles one bundle per file called bundle platform tar gz See the building guide for the definition of a platform Suppose that you untar a binary distribution bundle thus 17 Installing GHC AP ol cd your scratch space gunzip lt ghc x xx sun sparc solaris2 tar gz tar xvf Then you should find a single directory ghc version with the following structure Makefile in configure README INSTALL ANNOUNC GI NEWS bin platform lib plat form share html 2 1 2 1 Installing the raw material from which the Makefile will be made Section 2 1 2 1 Installing the configuration script Section 2 1 2 1 Installing Contains this file summary Contains this description of how to install the bundle The announcement message for the bundle release notes for the bundle a longer version of ANNOUNCE For GHC the re lease notes are contained in the User Guide and this file isn t present contains platform specific executable files to be invoked directly by the u
17. http haskell org onlinereport decls html sect4 5 5 of the Haskell Report can be completely switched off by Eno monomorphism restriction 7 14 2 Monomorphic pattern bindings As an experimental change we are exploring the possibility of making pattern bindings monomorphic that is not generalised at all A pattern binding is a binding whose LHS has no function arguments and is not a simple variable For example fx x Not a pattern binding f x gt x Not a pattern binding fisi Int gt Int x gt x Not a pattern binding g h e A pattern binding E e A pattern binding x e A pattern binding Experimentally GHC now makes pattern bindings monomorphic by default Use fno mono pat binds to recover the standard behaviour 7 15 Parallel Haskell There are two implementations of Parallel Haskell SMP paralellism which is built in to GHC see Sec tion 4 12 Using SMP parallelism and supports running Parallel Haskell programs on a single multi processor machine and Glasgow Parallel Haskell GPH which supports running Parallel Haskell pro grams on both clusters of machines or single multiprocessors GPH is developed and distributed separ ately from GHC see The GPH Page http www cee hw ac uk dsg gph Ordinary single threaded Haskell programs will not benefit from enabling SMP parallelism alone You must expose parallelism to the compiler in one of the following two ways 7 15
18. 231 Index interface files 60 shadowing warning 62 shell commands in GHCi 38 Show class 34 smaller programs how to produce 132 SMP 81 82 203 source file options 41 space leaks avoiding 132 SPECIALIZE pragma 129 191 192 specifying your own main function 80 stability package specification 71 stack maximum size 86 stack minimum size 86 StackOverflowHook 89 startup files GHCi 39 statements in GHCi 30 static options 39 42 strict constructor fields 75 string gaps vs cpp 78 structure command line 41 suffixes file 42 T temporary files keeping 50 redirecting 50 testing a new GHC 20 ticky ticky profiling 87 ticky ticky bundles 20 ticky ticky profiling 125 125 time profile 117 TMPDIR environment variable 50 Type default 34 type signatures missing 62 U Unboxed types Glasgow extension 135 unfolding controlling 75 75 unicode 46 UNPACK 192 unregisterised compilation 99 unused binds warning 63 unused imports warning 63 unused matches warning 63 using GHC 41 UTF 8 47 utilities Haskell 212 V verbosity options 45 version package specification 71 version of ghc 3 W warnings 57 Y Yacc for Haskell 212 232
19. For example according to the Haskell report the following expression is legal Haskell let x 42 in x 42 Tru and parses as let x 42 in x 42 Tru 221 Known bugs and infelicities because according to the report the let expression extends as far to the right as possible Since it can t extend past the second equals sign without causing a parse error is non fix the let expression must terminate there GHC simply gobbles up the whole expression parsing like this let x 42 in x 42 True The Haskell report is arguably wrong here but nevertheless it s a difference between GHC amp Haskell 98 12 1 1 3 Expressions and patterns None known 12 1 1 4 Declarations and bindings GHC s typechecker makes all pattern bindings monomorphic by default this behaviour can be disabled with fno mono pat binds See Section 7 1 Language options 12 1 1 5 Module system and interface files None known 12 1 1 6 Numbers basic types and built in classes Multiply defined array ele ments not checked This code fragment should elicit a fatal error but it does not main print array 1 1 1 2 1 3 GHC s implementation of array takes the value of an array slot from the last index value pair in the list and does no checking for duplicates The reason for this is efficiency pure and simple 12 1 1 7 In Prelude support Arbitrary sized tuples
20. If Term Bool gt Term a gt Term a gt Terma Pair Term a gt Term b gt Term a b Notice that the return type of the constructors is not always Term a as is the case with ordinary vanilla data types Now we can write a well typed eval function for these Terms eval Terma gt a eval Lit i i eval Succ t 1 eval t eval IsZero t eval t 0 eval If b el e2 if eval b then eval el els val e2 eval Pair el e2 eval el eval e2 These and many other examples are given in papers by Hongwei Xi and Tim Sheard There is a longer introduction on the wiki http haskell org haskellwiki GADT and Ralf Hinze s Fun with phantom types http www informatik uni bonn de ralf publications With pdf also has a number of examples Note that papers may use different notation to that implemented in GHC The rest of this section outlines the extensions to GHC that support GADTs It is far from comprehens ive but the design closely follows that described in the paper Simple unification based type inference for GADTs_ http research microsoft com 7Esimonpj papers gadt index htm which appeared in ICFP 2006 e Data type declarations have a where form as exemplified above The type signature of each con structor is independent and is implicitly universally quantified as usual Unlike a normal Haskell data type declaration the type variable s in the data Term a where header have no scope Indeed one can
21. Standard suffix rules eOmAAs lhs o S HC c lt S HC_OPTS hs o S HC c lt HC_OPTS o boot hi boot Lhs boot o boot S HC c lt S HC_OPTS hs boot o boot S HC c lt S HC_OPTS Inter module dependencies Foo o Foo hc Foo s Baz hi Foo imports Baz Main o Main hc Main s Foo hi Baz hi Main imports Foo and Baz Sophisticated make variants may achieve some of the above more elegantly Notably gmake s pattern rules let you write the more comprehensible ae O lhs S HC c lt HC_OPTS What we ve shown should work with any make Note the cheesy o hi rule It records the dependency of the interface hi file on the source The tule says a hi file can be made from a o file by doing nothing Which is true Note that the suffix rules are all repeated twice once for normal Haskell source files and once for hs boot files see Section 4 6 9 How to compile mutually recursive modules Note also the inter module dependencies at the end of the Makefile which take the form Foo o Foo hc Foo s gt Baz hi Foo imports Baz They tell make that if any of Foo o Foo hc or Foo s have an earlier modification date than Baz hi then the out of date file must be brought up to date To bring it up to date make looks for a tule to do so one of the preceding suffix rules does the job nicely These dependencies can be generated automatically by ghc see Section 4 6 1
22. This generates a file of profiling information 3 Examine the generated profiling information using one of GHC s profiling tools The tool to use will depend on the kind of profiling information generated 5 1 Cost centres and cost centre stacks GHC s profiling system assigns costs to cost centres A cost is simply the time or space required to eval uate an expression Cost centres are program annotations around expressions all costs incurred by the annotated expression are assigned to the enclosing cost centre Furthermore GHC will remember the stack of enclosing cost centres for any given expression at run time and generate a call graph of cost at tributions Let s take a look at an example main print nfib 25 nfib n if n lt 2 then 1 else nfib n 1 nfib n 2 Compile and run this program as follows ghc prof auto all o Main Main hs Main RTS p 121393 When a GHC compiled program is run with the p RTS option it generates a file called lt prog gt prof In this case the file will contain something like this Fri May 12 14 06 2000 Time and Allocation Profiling Report Final Main RTS p RTS total time total alloc 0 14 secs 7 ticks 20 ms 8 741 204 bytes excludes profiling overheads COST CENTRE MODULE Stime Salloc 113 Profiling nfib Main 100 0 100 0 individual inherited COST CENTRE MODULE entries time alloc Stime talloc MA
23. a rewrite tule see Section 7 11 Rewrite rules that rewrites a call to the un specialised function into a call to the specialised one The type in a SPECIALIZE pragma can be any type that is less polymorphic than the type of the original function In concrete terms if the original function is f then the pragma SPI ECIALIZE i lt type gt is valid if and only if the defintion f_spec f_spec lt type gt E is valid Here are some examples where we only give the type signature for the original function not its code f Eq SP ECIALISE a gt gt a OF necte SP Eq a Ix ECIALISE Boss Eq a gt a ECIALISE SP gt b gt b I nt gt b gt b b gt a gt b gt b z gt a gt Int gt Int g Eq a gt aa h Eq a gt a 2 a gt a The last of these examples will generate a RULE with a somewhat complex left hand side try it your self so it might not fire very well If you use this kind of specialisation let us know how well it works 191 GHC Language Features A SPECIALIZE pragma can optionally be followed with a INLINE or NOINLINE pragma optionally followed by a phase as described in Section 7 10 3 INLINE and NOINLINE pragmas The INLINE pragma affects the specialised verison of the function only and applies eve
24. for Haskell s internal state monad strict and lazy respectively There are three important points in using the recursive do notation e The recursive version of the do notation uses the keyword mdo rather than do e You should import Control Monad Fix Note Strictly speaking this import is required only when you need to refer to the name MonadF ix in your program but the import is always safe and the programmers are encouraged to always import this module when using the mdo notation e As with other extensions ghc should be given the flag fglasgow exts The web page http www cse ogi edu PacSoft projects rmb contains up to date information on recurs ive monadic bindings Historical note The old implementation of the mdo notation and most of the existing documents used the name MonadRec for the class and the corresponding library This name is not supported by GHC 7 3 4 Parallel List Comprehensions Parallel list comprehensions are a natural extension to list comprehensions List comprehensions can be thought of as a nice syntax for writing maps and filters Parallel comprehensions extend this to include the zipWith family A parallel list comprehension has multiple independent branches of qualifier lists each separated by a symbol For example the following zips together two lists x y x lt xs y lt ys The behavior of parallel list comprehensions follows that of zip in that the resulting list will h
25. gen Format gt ExpQ gen D n gt show n gen S s gt s gen L s stringE s Here we generate the Haskell code for the splice from an input format string pr String gt ExpQ pr s gen parse s Now run the compiler here we are a Cygwin prompt on Windows ghc make fth main hs o main exe Run main exe and here is your output main Hello 178 GHC Language Features 7 6 4 Using Template Haskell with Profiling Template Haskell relies on GHC s built in bytecode compiler and interpreter to run the splice expres sions The bytecode interpreter runs the compiled expression on top of the same runtime on which GHC itself is running this means that the compiled code referred to by the interpreted expression must be compatible with this runtime and in particular this means that object code that is compiled for profiling cannot be loaded and used by a splice expression because profiled object code is only compatible with the profiling version of the runtime This causes difficulties if you have a multi module program containing Template Haskell code and you need to compile it for profiling because GHC cannot load the profiled object code and use it when ex ecuting the splices Fortunately GHC provides a workaround The basic idea is to compile the program twice 1 Compile the program or library first the normal way without prof 2 Then compile it again wi
26. ghc c adder hs fglasgow exts This will produce two files adder o and adder_stub o compile up aD11Main that starts up the Haskell RTS a possible implementation is include lt windows h gt include lt Rts h gt extern void__stginit_Adder void static char args ghcD1l NULL N B argv arrays must end with NULL BOOL STDCALL D11Main HANDLE hModule DWORD reason void reserved if reason DLL PROCESS_ATTACH By now the RTS DLL should have been hoisted in but we need to start i startupHaskell l1 args __stginit_Adder return TRUE return TRUE Here Adder is the name of the root module in the module tree as mentioned above there must be a single root module and hence a single module tree in the DLL Compile this up ghc c dllMain c Construct the DLL ghc mk dll o adder dll adder o adder_stub o dllMain o Start using adder from VBA here s how I would Declare it Private Declare Function adder Lib adder dll Alias adder 8 ByVal x As Long ByVal y As Long As Long Since this Haskell DLL depends on a couple of the DLLs that come with GHC make sure that they are in scope visible 219 Running GHC on Win32 systems Building statically linked DLLs is the same as in the previous section it suffices to add static to the commands used to compile up the Haskell source and build the DLL 220 Chapter 12 Know
27. i option on the GHCi command line like so or it can be set using the set command from within GHCi see Section 3 7 2 Setting GHC com mand line options in GHCi One consequence of the way that GHCi follows dependencies to find modules to load is that every mod ule must have a source file The only exception to the rule is modules that come from a package includ ing the Prelude and standard libraries such as IO and Complex If you attempt to load a module for which GHCi can t find a source file even if there are object and interface files for the module you ll get an error message 3 2 2 Making changes and recompilation If you make some changes to the source code and want GHCi to recompile the program give the reload command The program will be recompiled as necessary with GHCi doing its best to avoid actually recompiling modules if their external dependencies haven t changed This is the same mechan ism we use to avoid re compiling modules in the batch compilation setting see Section 4 6 8 The re compilation checker Note that in GHCi and make mode the i option is used to specify the search path for source files whereas in standard batch compilation mode the i option is used to specify the search path for interface files see Section 4 6 3 The search path 27 Using GHCi 3 3 Loading compiled code When you load a Haskell source module into GHCi it is normally converted to byte cod
28. lt CC Sean CC gt each cc is a cost centre in the program see Section 5 1 Cost centres and cost centre stacks and the sequence represents the call stack at the point the exception was raised The leftmost item is the innermost function in the call stack and the rightmost item is the outermost function Z Turn off update frame squeezing at garbage collection time There s no particu larly good reason to turn it off except to ensure the accuracy of certain data collected regarding thunk entry counts 4 14 6 Hooks to change RTS behaviour GHC lets you exercise rudimentary control over the RTS settings for any given program by compiling in a hook that is called by the run time system The RTS contains stub definitions for all these hooks but by writing your own version and linking it on the GHC command line you can override the defaults Owing to the vagaries of DLL linking these hooks don t work under Windows when the program is built dynamically The hook ghc_rts_optslets you set RTS options permanently for a given program A common use for this is to give your program a default heap and or stack size that is greater than the default For ex ample to set H128m K1m place the following definition in a C source file char ghc_rts_opts H128m Klm Compile the C file and include the object file on the command line when you link your Haskell pro gram These flags are interpreted firs
29. optF 76 optL 76 optl 76 optm 76 optP 76 osuf 49 179 p 117 RTS option 113 P 115 117 package 64 80 package conf 66 69 package name option pgma 76 110 227 Index pgmc 76 110 pgmdll 76 110 pgmF 76 110 pgmL 75 110 pgml 76 110 pgmm 76 pgmP 76 110 pgms 76 prof 113 116 179 px 117 121 r RTS option 87 r RTS option 125 read dot ghci 40 Rghc timing 46 RTS 84 S 43 44 RTS option 87 S RTS option 87 S RTS option 132 split objs 80 Sstderr RTS option 132 static 80 stubdir 49 t RTS option 87 threaded 81 ticky 87 tmpdir 50 tmpdir lt dir gt option 50 U 77 unreg 99 v 45 45 128 V 45 69 RTS option w 57 W option 57 Wall 58 Werror 59 x 45 XC RTS option 88 117 Xt RTS option 119 Z RTS option 88 ghci file 39 hc files saving 50 hi files 47 O files 47 S files saving 50 21 38 22 37 add 36 browse 36 cd 36 def 36 edit 37 etags 38 38 shelp 37 info 37 kind 38 load 26 37 main 37 module 37 quit 37 reload 27 38 set 38 38 set args 38 set prog 38 show bindings 38 show modules 38 type 38 cundef 38 unset 38 __CONCURRENT_HASKELL _ 77 __GLASGOW_HASKELL_ 4 4 77 __HASKELL1 _ 77 __HASKELL98 __ 77 __ HASKELL ___ 98 77 __PARALLEL_HASKELL_ 77 auto ghci libs 69 force 69 global 69
30. print f 25 g 25 fn nfib n gn nfib n div 2 nfib n if n lt 2 then 1 else nfib n 1 nfib n 2 Compile and run this program as before and take a look at the new profiling results COST CENTRE MODULE scc time alloc Stime alloc MAIN AIN 0 0 0 0 0 100 0 100 0 main Main 0 0 0 0 0 0 0 0 0 CAF PrelHandle 3 0 0 0 0 0 0 0 0 CAF PrelAddr 1 0 0 0 0 0 0 0 0 CAF Main 9 0 0 0 0 100 0 100 0 main Main 1 0 0 0 0 100 0 100 0 Main 1 0 0 0 0 0 0 0 2 nfib Main 465 0 0 0 2 0 0 OieZ f Main 1 0 0 0 0 100 0 99 8 nfib Main 242785 100 0 99 8 100 0 99 8 Now although we had two calls to nfib in the program it is immediately clear that it was the call from f which took all the time 114 Profiling 5 1 1 5 1 2 The actual meaning of the various columns in the output is entries The number of times this particular point in the call graph was entered individual time The percentage of the total run time of the program spent at this point in the call graph individual alloc The percentage of the total memory allocations excluding profiling over heads of the program made by this call inherited time The percentage of the total run time of the program spent below this point in the call graph inherited alloc The percentage of the total memory allocations excluding profiling over heads of the program made by this call and all of its sub calls In addition you can use the P RTS option
31. putStrLn hello return yes hello yes Using do notation at the prompt GHCi actually accepts statements rather than just expressions at the prompt This means you can bind values and functions to names and use them in future expressions or statements The syntax of a statement accepted at the GHCi prompt is exactly the same as the syntax of a statement in a Haskell do expression However there s no monad overloading here statements typed at the prompt must be in the 1O monad Prelude gt x lt return 42 42 Prelude gt print x 42 Prelude gt The statement x lt return 42 means execute return 42 in the IO monad and bind the result to x We can then use x in future statements for example to print it as we did above GHCi will print the result of a statement if and only if e The statement is not a binding or it is a monadic binding p lt e that binds exactly one variable e The variable s type is not polymorphic is not and is an instance of Show The automatic printing of binding results can be supressed with set fno print bind result this does not supress printing the result of non binding statements You might want to do this to pre vent the result of binding statements from being fully evaluated by the act of printing them for example Of course you can also bind normal non IO expressions using the 1et statement 30 Using GHCi 3 4 3 Prelude gt let x 42 Prel
32. see Section 4 10 4 Options affecting a Haskell pre processor Pass option to the C compiler Pass option to the mangler Pass option to the assembler Pass option to the linker Pass option to the DLL generator Pass option to the dependency generator So for example to force an Ewurble option to the assembler you would tell the driver a Ewurble the dash before the E is required opt GHC is itself a Haskell program so if you need to pass options directly to GHC s runtime system you can enclose them in RTS RTS see Section 4 14 Running a compiled program 4 10 3 Options affecting the C pre processor Cpp The C pre processor cpp is run over your Haskell code only if the cpp option is given Unless you are building a large system with significant doses of conditional compilation you really shouldn t need it 76 Using GHC ue Usymbol Idir Define macro symbol in the usual way NB does not affect D macros passed to the C compiler when compiling via C For those use the optc Dfoo hack see Section 4 10 2 Forcing options to a particular phase Undefine macro symbo1 in the usual way Specify a directory in which to look for include files in the usual C way The GHC driver pre defines several macros when processing Haskell source code hs or Lhs files The symbols defined by GHC are listed below To check which symbols are defined by your
33. xclud module lt file gt xclud s gt includ directory lt dir module lt file gt include pkg deps Turn off warnings about interface file shadowing Print a full list of the module depenencies to stdout This is the standard verbosity flag so the list will also be displayed with v3 and v4 Section 4 5 Help and verbosity options Use file as the makefile rather than makefile or Makefile If file doesn t exist mkdependHS creates it We often use f depend to put the dependencies in depend and then include the file depend into Makefile Make extra dependencies that declare that files with suffix lt suf gt _ lt osuf gt depend on interface files with suffix lt suf gt _hi or for SOURCE imports on hi boot Multiple s flags are permitted For example o he s a s b will make dependencies for hc on hi a_hc on a_hi and b_hc on b_hi Useful in conjunc tion with NoFib ways Regard lt file gt as stable i e exclude it from having depend encies on it same as xclude modul Regard the colon separated list of directories lt dirs gt as contain ing stable don t generate any dependencies on modules therein Regard lt file gt as not stable i e generate dependencies on it if any This option is normally used in conjunction with the xclude directory option Regard modules imported from packages as unstable i e gener ate depende
34. 1 2 Infix type constructors classes and type variables Any use of the deprecated item or of anything from a deprecated module will be flagged with an appro priate message However deprecations are not reported for a uses of a deprecated function within its defining module and b uses of a deprecated function in an export list The latter reduces spurious com plaints within a library in which one module gathers together and re exports the exports of several oth ers You can suppress the warnings with the flag fno warn deprecations 7 10 2 INCLUDE pragma The INCLUDE pragma is for specifying the names of C header the C source code generated by the compiler for the current modu INCLUD INCLUD foo h lt stdio h gt Gl Gl files that should be include d into le Gf compiling via C For example The INCLUDE pragma s must appear at the top of your source file with any OPTIONS_GHC pragma s An INCLUDE pragma is the preferred alternative to the include option Section 4 10 5 Options affecting the C compiler if applicable because the INCLUD pilers Yet another alternative is to add the include file to each f code but we don t recommend using this approach with GHC E pragma is understood by other com oreign import declaration in your 7 10 3 INLINE and NOINLINE pragmas 188 GHC Language Features These pragmas control
35. 20 0 cece eeee cece cece eeceee eee ereeeeeeeeeeereseneeeseeresener 41 4 1 3 Setting options Ini GHCI siiis eera sro oe EE i ROE E POTTERS ES E ERTSE yE EEEE Y 41 4 2 Static Dynamic and Mode options seer catie e ENE a EE EENES 42 4 3 Meaningful file suffixes 0 322 e ies iat SN aE E E E E SEET eee EE NnS 42 4 4 Modesof OPeratlOn 50 seansie erne e e a eee o E aN e eee Epea 42 44t Using ehe ma ke e E a E A TE E Eae E EEES 43 4 4 2 Expression evaluation Modens a E aE EE E E N SEN 44 4 4 3 Batch compiler mode 2 3 3 R ee ENE a ee eee 44 4 4 3 1 Overriding the default behaviour for a file eee eee cee ce eeceeeen teen es 45 4 5 Help and verbosity Options ssec eee cee cece a e ceca eeaeeeae EEOSE EEE e i 45 4 6 Filenames and separate compilation 20 0 0 0c cece cece cece cn ee cece ce eene cena eens ceneeeeeeeeeeeeeeeeeeeeeaees 46 4 6 1 Haskell source files ensinos in esa ri oree a eene E stoves casieee desks oueeceveggeebest buen 46 4 6 2 QuipUtGIeS seseeeisicoes eas ssbsaagiscensas ss O E ESEP eE TRESE i PASES ESARTE EERSTES EE 47 46 3 The search path ssena ner esoe cen gseseeh EE ETE EES O bosnca vee EEEN lente cs 47 4 6 4 Redirecting the compilation output s 0 00 cece cee ce ence nce eee ence eeeeeeeeeeeaeeeaeeeaes 48 4 6 5 Keeping Intermediate Piles merser vedas stawes doeshwuness se EESE EAE ETE AS 50 4 6 6 Redirecting temporary files 1 0 0 0 eee cece nee ce eeca cece cece cena een eeneeeeeeeeeseee
36. 4 1 4 4 Restrictions There are several restrictions on the ways in which existentially quantified constructors can be use e When pattern matching each pattern match introduces a new distinct type for each existential type variable These types cannot be unified with any other type nor can they escape from the scope of the pattern match For example these fragments are incorrect fl MkFoo a f a Here the type bound by MkFoo escapes because a is the result of 1 One way to see why this is wrong is to ask what type f1 has fl Foo gt a Weird What is this a in the result type Clearly we don t mean this 147 GHC Language Features fl forall a Foo gt a Wrong The original program is just plain wrong Here s another sort of error f2 Bazl a b Bazli p q a q Its ok to say a b or p q but a q is wrong because it equates the two distinct types arising from the two Baz1 constructors You can t pattern match on an existentially quantified constructor in a 1et or where group of bind ings So this is illegal f3 x a b where Bazl ab x Instead use a case expression f3 x case x of Bazl ab gt a b In general you can only pattern match on an existentially quantified constructor in a case expres sion or in the patterns of a function definition The reason for this restriction is really an implementa tion one Type checking binding groups is already a nightmare
37. Each Haskell source module should be placed in a file on its own Usually the file should be named after the module name replacing dots in the module name by direct ory separators For example on a Unix system the module A B C should be placed in the file A B C hs relative to some base directory If the module is not going to be imported by another module Main for example then you are free to use any filename for it 46 Using GHC 4 6 2 GHC assumes that source files are ASCII or UTF 8 only other encodings are not recognised However invalid UTF 8 sequences will be ignored in comments so it is possible to use other encodings such as Latin 1 as long as the non comment source code is ASCII only Output files When asked to compile a source file GHC normally generates two files an object file and an interface file The object file which normally ends in a o suffix contains the compiled code for the module The interface file which normally ends in a hi suffix contains the information that GHC needs in or der to compile further modules that depend on this module It contains things like the types of exported functions definitions of data types and so on It is stored in a binary format so don t try to read one use the show i face option instead see Section 4 6 7 Other options related to interface files You should think of the object file and the interface file as a pair since the interface file
38. GHCi 34 it 33 L language option 133 LANGUAGE pragma 190 language GHC 133 Latin 1 47 ld options 79 ld options package specification 73 lhs suffix 42 libdir 45 libraries with GHCi 35 library dirs package specification 72 license file package specification 71 LINE pragma 190 link installed as ghc 19 linker options 79 linking Haskell libraries with foreign code 81 lint 97 list comprehensions parallel 140 machine specific options 82 mailing lists Glasgow Haskell 1 maintainer package specification 71 make 53 make and recompilation 46 make mode 42 230 Index Makefile dependencies 54 Makefiles avoiding 43 MallocFailHook 89 matches unused 63 memory using less heap 132 methods missing 60 missing fields warning 60 missing methods warning 60 mode options 42 module system recursion 51 modules and filenames 27 multicore 81 multiprocessor 81 N name package specification 71 native code generator 44 nfib 20 NOINLINE 189 NOTINLINE 189 O object files 47 optimisation 73 optimise aggressively 73 normally 73 optimising customised 74 options for profiling 116 GHCi 38 language 133 OPTIONS_GHC 191 OPTIONS_GHC pragma 41 orphan instance 57 orphan instances warning 62 orphan module 57 orphan rule 57 orphan rules warning 62 OutOfHeapHook 89 output directing options 48 overflow Int 223 overlapping patterns
39. Haskell mailing lists 1 Glasgow Parallel Haskell 203 gransim bundles 19 H haddock html package specification 73 haddock interfaces package specification 73 Happy 212 happy parser generator 212 Haskell 98 language vs GHC 221 hasktags 212 heap profiles 122 heap size factor 85 heap size maximum 87 heap size suggested 86 heap space using less 132 heap minimum free 87 help options 45 hidden modules package specification 71 homepage package specification 71 hooks RTS 88 hp2ps 122 hp2ps program 122 hs boot files 52 hs libraries package specification 72 hsc2hs 213 hs_add_root 207 Hugs 25 hugs options package specification 72 idle GC 86 import dirs package specification 71 importing hi boot files 52 imports unused 63 improvement code 73 in place installation 18 include dirs package specification 72 include file options 78 includes package specification 72 incomplete patterns warning 60 incomplete record updates warning 60 INLINE 189 INLINE pragma 189 inlining controlling 75 75 installation directories 19 installation of binaries 17 installing in place 18 Int size of 223 interactive see GHCi interactive mode 42 Interface files 18 interface files 47 interface files finding them 47 interface files options 50 intermediate code generation 89 intermediate files saving 50 intermediate passes output 89 interpreter see GHCi invoking
40. Section 3 7 The set command for a list of avail able options Executes the shell command command 3 7 The set command set command sets two types of options GHCi options which begin with and command line options which begin with NOTE at the moment the set command doesn t support any kind of quoting in its arguments quotes will not be removed and cannot be used to group words together For example set DFOO BAR BAZ will not do what you expect GHCi options GHCi options may be set using set and unset using unset The available GHCi options are 38 Using GHCi 3 7 2 r Normally any evaluation of top level expressions otherwise known as CAFs or Constant Applicative Forms in loaded modules is retained between evaluations Turning on r causes all evaluation of top level expressions to be discarded after each evaluation they are still retained during a single evaluation This option may help if the evaluated top level expressions are consuming large amounts of space or if you need repeatable performance measurements s Display some stats after evaluating each expression including the elapsed time and number of bytes allocated NOTE the allocation figure is only accurate to the size of the storage manager s allocation area because it is calculated at every GC Hence you might see values of zero if no GC has occurred Ht Display the type of each variable bou
41. The cost of evaluating any expression in your program is attributed to a cost centre stack using the fol lowing rules e If the expression is part of the one off costs of evaluating the enclosing top level definition then costs are attributed to the stack of lexically enclosing SCC annotations on top of the special CAF 115 Profiling cost centre e Otherwise costs are attributed to the stack of lexically enclosing SCC annotations appended to the cost centre stack in effect at the call site of the current top level definition Notice that this is a re cursive definition e Time spent in foreign code see Chapter 8 Foreign function interface FFI is always attributed to the cost centre in force at the Haskell call site of the foreign function What do we mean by one off costs Well Haskell is a lazy language and certain expressions are only ever evaluated once For example if we write x nfib 25 then x will only be evaluated once if at all and subsequent demands for x will immediately get to see the cached result The definition x is called a CAF Constant Applicative Form because it has no argu ments For the purposes of profiling we say that the expression nfib 25 belongs to the one off costs of eval uating x Since one off costs aren t strictly speaking part of the call graph of the program they are attributed to a special top level cost centre CAF There may be one CAF cost centre for each module the
42. The value of basename is the module name with dots replaced by the directory separator or depending on the system and extension is a source extension hs 1hs if we are in make mode and GHCi or hisuf otherwise For example suppose the search path contains directories d1 d2 and d3 and we are in make mode looking for the source file for a module A B C GHC will look in d1 A B C hs d1 A B C lLhs d2 A B C hs and so on 66 99 The search path by default contains a single directory i e the current directory The following op tions can be used to add to or change the contents of the search path idirs This flag appends a colon separated list of dirs to the search path i resets the search path back to nothing This isn t the whole story GHC also looks for modules in pre compiled libraries known as packages See the section on packages Section 4 8 Packages for details Redirecting the compilation output s o file GHC s compiled output normally goes into a hc o etc file depending on the last run compilation phase The option o file re directs the output of that last run phase to file Note this feature can be counterintuitive ghe C o foo o foo hs will put the in termediate C code in the file foo o name notwithstanding This option is most often used when creating an executable file to set the filename of the executable For example ghc o prog make Main will c
43. a gl forall a b Ord a Eq b gt a gt b gt a f2 forall a a gt a gt Int gt Int g2 forall a Eq a gt a gt a gt Bool gt Int gt Int f3 forall a a gt a gt Int gt Bool gt Bool Here 1 and g1 are rank 1 types and can be written in standard Haskell e g f1 a gt b gt a The forall makes explicit the universal quantification that is implicitly added by Haskell The functions 2 and g2 have rank 2 types the forall is on the left of a function arrow As g2 shows the polymorphic type on the left of the function arrow can be overloaded The function f3 has a rank 3 type it has rank 2 types on the left of a function arrow GHC allows types of arbitrary rank you can nest foral1s arbitrarily deep in function arrows GHC used to be restricted to rank 2 but that restriction has now been lifted In particular a forall type also called a type scheme including an operational type class context is legal e On the left of a function arrow e On the right of a function arrow see Section 7 4 5 2 For all hoisting e As the argument of a constructor or type of a field in a data type declaration For example any of the 1 f2 f3 g1 92 above would be valid field type signatures 164 GHC Language Features e As the type of an implicit parameter e Ina pattern type signature see Section 7 4 10 Lexically scoped type variables There is one place you
44. a function unsafeForeignPtrToStorableArray e The new Data ByteString hierarchy provides time and space efficient byte vectors The old Data PackedString module is now deprecated as a result although there is not yet a replace ment if you need full unicode support e GHC Exts now provides a function inline which provided the RHS is visible to the compiler forcibly inlines its argument Otherwise it acts like id For more details see Section 7 12 Special built in functions e GHC Exts now provides a function lazy where lazy f behaves like f except GHC is forced to believe that it is lazy in its first argument For more details see Section 7 12 Special built in func tions e Data FiniteMap has been removed deprecated since 6 4 Use Data Map instead e Data Char now exports isLetter isMark isNumber isPunctuation isSymbol isSeparator isAsciiUpper isAsciiLower and toTitle It also exports a function generalCategory that tells you the category of a character in terms of a datatype GeneralC ategory Introduction to GHC Data Dynamic now exports a function dynTypeRep There is a new module Data Eq which just exports the Eq class Likewise a new module Data Ord exports the Ord class as well as the handy comparing function There is anew module Data Fixed providing fixed precision arithmetic There is a new module Data Foldable providing a class for foldable datatypes It gives in stances
45. a list of the modules currently loaded into GHCi xMain gt show modules D D hs D o C hs interpreted B B hs interpreted A A hs interpreted Main gt If we now modify the source of D or pretend to using Unix command touch on the source file is handy for this the compiler will no longer be able to use the object file because it might be out of date Main gt touch D hs Main gt reload Compiling D D hs interpreted 28 Using GHCi Skipping C C hs interpreted Skipping B B hs interpreted Skipping A A hs interpreted Ok modules loaded A B C D Main gt Note that module D was compiled but in this instance because its source hadn t really changed its inter face remained the same and the recompilation checker determined that A B and C didn t need to be re compiled So let s try compiling one of the other modules Main gt ghe c C hs Main gt load A Compiling D D hs interpreted Compiling C C hs interpreted Compiling B B hs interpreted Compiling A A hs interpreted Ok modules loaded A B C D We didn t get the compiled version of C What happened Well in GHCi a compiled module may only depend on other compiled modules and in this case C depends on D which doesn t have an object file so GHCi also rejected C s object file Ok so let s also compile D Main gt ghc c D hs Main gt reload Ok modules load
46. a static one but in fact it works to set it using set like this The changes won t take effect until the next load though 39 Using GHCi Two command line options control whether the ghci files are read Don t read either ghci or SHOME ghci when starting up ghci and SHOME ghci This is normally the default but the option may be used to override a previous ig ig nore dot ghci Read read read dot ghci dot ghci nore dot ghci option The interpreter can t load modules with foreign export declarations O doesn t work with GHCi Unboxed tuples don t work with GHCi Concurrent threads don t carry on running when GHCi is waiting for input After using getContents I can t use stdin again until I do load or reload 3 9 FAQ and Things To Watch Out For Unfortunately not We haven t implemented it yet Please compile any offending modules by hand before loading them into GHCi For technical reasons the bytecode compiler doesn t interact well with one of the optimisation passes so we have disabled optim isation when using the interpreter This isn t a great loss you ll get a much bigger win by compiling the bits of your code that need to go fast rather than interpreting them with optimisation turned on That s right You can always compile a module that uses unboxed tuples and load it into GHCi however Incidentally the previous point namely that O is incompatible
47. and report a floating point exception probably ter 223 Known bugs and infelicities minating the program 12 2 Known bugs or infelicities The bug tracker lists bugs that have been reported in GHC but not yet fixed see the SourceForge GHC page http sourceforge net projects ghc In addition to those GHC also has the following known bugs or infelicities These bugs are more permanent it is unlikely that any of them will be fixed in the short term 12 2 1 Bugs in GHC e GHC can warn about non exhaustive or overlapping patterns see Section 4 7 Warnings and sanity checking and usually does so correctly But not always It gets confused by string patterns and by guards and can then emit bogus warnings The entire overlap check code needs an overhaul really e GHC does not allow you to have a data type with a context that mentions type variables that are not data type parameters For example data C a b gt T a MkT a so that MkT s type is MkT forall ab C a b gt a gt Ta In principle with a suitable class declaration with a functional dependency it s possible that this type is not ambiguous but GHC nevertheless rejects it The type variables mentioned in the context of the data type declaration must be among the type parameters of the data type e GHC s inliner can be persuaded into non termination using the standard way to encode recursion via a data type data U MkU U gt Bool russe
48. any Haskell functions before calling hs_init strange things will undoubtedly happen We pass argc and argv to hs_init so that it can separate out any arguments for the RTS i e those arguments between RTS RTS Next we call hs_add_root a GHC specific interface which is required to initialise the Haskell mod ules in the program The argument to hs_add_root should be the name of the initialization function for the root module in your program in other words the module which directly or indirectly imports all the other Haskell modules in the program In a standalone Haskell program the root module is nor mally Main but when you are using Haskell code from a library it may not be If your program has multiple root modules then you can call hs_add_root multiple times one for each root The name of the initialization function for module Mis __stginit_ and it may be declared as an external func tion symbol as in the code above After we ve finished invoking our Haskell functions we can call hs_exit which terminates the RTS It runs any outstanding finalizers and generates any profiling or stats output that might have been requested There can be multiple calls to hs_init but each one should be matched by one and only one call tohs_exit NOTE when linking the final program it is normally easiest to do the link using GHC although this isn t essential If you do use GHC then don t forget the flag no hs
49. arrow The weird look will make more sense later It may be read as analogue of application for arrows The above example is equivalent to the Haskell ex pression arr x gt x 1 gt gt gt f That would make no sense if the expression to the left of lt involves the bound variable x More gener ally the expression to the left of lt may not involve any local variable i e a variable bound in the cur rent arrow abstraction For such a situation there is a variant lt lt as in proce x gt x lt lt x41 which is equivalent to arr x gt f x x 1 gt gt gt app so in this case the arrow must belong to the ArrowAppl1y class Such an arrow is equivalent to a mon ad so if you re using this form you may find a monadic formulation more convenient do notation for commands Another form of command is a form of do notation For example you can write proc x gt do y lt lt xt l 180 GHC Language Features 7 7 2 g lt 2 y let z Sry t lt h lt x z returnA lt t z You can read this much like ordinary do notation but with commands in place of monadic expressions The first line sends the value of x 1 as an input to the arrow f and matches its output against y In the next line the output is discarded The arrow returnA is defined in the Control Arrow libraries base Control Arrow html module as arr id The above example is treated as an abbrevi ation for ar
50. at the prompt what identifiers and types are in scope GHCi provides a flexible way to control exactly how the context for an expression is constructed Let s start with the simple cases when you start GHCi the prompt looks like this Prelude gt Which indicates that everything from the module Prelude is currently in scope If we now load a file into GHCi the prompt will change 31 Using GHCi Prelude gt load Main hs Compiling Main Main hs interpreted Main gt The new prompt is Main which indicates that we are typing expressions in the context of the top level of the Main module Everything that is in scope at the top level in the module Main we just loaded is also in scope at the prompt probably including Prelude as long as Main doesn t explicitly hide it The syntax module indicates that it is the full top level scope of module that is contributing to the scope for expressions typed at the prompt Without the just the exports of the module are visible We re not limited to a single module GHCi can combine scopes from multiple modules in any mixture of and non forms GHCi combines the scopes from all of these modules to form the scope that is in effect at the prompt For technical reasons GHCi can only support the form for modules which are in terpreted so compiled modules and package modules can only contribute their exports to the current scope The scope is manipulated using the module command F
51. b gt a g Int gt Discard Int GARE AY Zs Sh Simply expanding the type synonym would give gote Inte gt forall by Int gt p s gt int but GHC hoists the forall to give the isomorphic type g it forall pe Ent gt Int gt b o gt Int In general the rule is this to determine the type specified by any explicit user written type e g in a type signature GHC expands type synonyms and then repeatedly performs the transformation 159 GHC Language Features 7 4 6 typel gt forall al an context2 gt type2 gt forall al an context2 gt typel gt type2 In fact GHC tries to retain as much synonym information as possible for use in error messages but that is a usability issue This rule applies of course whether or not the forall comes from a synonym For example here is another valid way to write g s type signature g Int gt Int gt forall b b gt Int When doing this hoisting operation GHC eliminates duplicate constraints For example type Foo a x Int gt Bool gt a G 34 FOo FOO Ine means g x Int gt Bool gt Bool gt Int Implicit parameters Implicit parameters are implemented as described in Implicit parameters dynamic scoping with static types J Lewis MB Shields E Meijer J Launchbury 27th ACM Symposium on Principles of Program ming Languages POPL 00 Boston Jan 2000 Most of the following stil rather incomplete do
52. been deprecated with a new function parseAb soluteURI taking its place 1 4 5 12 ObjectlO No change version 1 0 1 4 5 13 OpenAL Version number 1 3 was 1 2 No other change 1 4 5 14 OpenGL e Version number 2 1 was 2 0 e No other change 1 4 5 15 QuickCheck e No change version 1 0 1 4 5 16 time e Version 1 0 e time is a new package for dealing with dates times and time intervals 1 4 5 17 X11 e Version number 1 2 was 1 1 e In Graphics X11 Xlib Types XGCValues has been renamed GCValues and XSetWin dowAttributes has been renamed SetWindowAttributes e In Graphics X11 Xlib Misc allocaXSetWindowAttributes has been renamed al locaSetWindowAttributes e The FontStruct type has moved from Graphics X11 Xlib Types to Graph ics X11 Xlib Font e The Point Rectangle Arc Segment and Color types in Graphics X11 Xlib Types 15 Introduction to GHC are now proper datatypes rather than synonyms for tuples They all have a Storable instance e The Byte and Short types from Graphics X11 Xlib Types have been removed The fol lowing type synonyms which had already been marked Backwards compatibility have also been removed ListPoint ListRectangle ListArc ListSegment and ListColor Eq Ord Show Typeable and Data are now derived for XEvent FdSet and TimeZone in Graphics X11 Xlib Event FontStruct in Graphics X11 Xlib Font XErrorEvent XComposeStatus and XTextP
53. behaviour is occasionally useful when controlling evaluation order Notably lazy is used in the library definition of Control Parallel par par ei a gt vps D par x y case par x of _ gt lazy y If Lazy were not lazy par would look strict in y which would defeat the whole purpose of par 7 12 3 The unsafeCoerce function The function unsafeCoerce allows you to side step the typechecker entirely It has type unsafeCoerce a gt b 199 GHC Language Features That is it allows you to coerce any type into any other type If you use this function you had better get it right otherwise segmentation faults await It is generally used when you want to write a program that you know is well typed but where Haskell s type system is not expressive enough to prove that it is well typed 7 13 Generic classes The ideas behind this extension are described in detail in Derivable type classes Ralf Hinze and Si mon Peyton Jones Haskell Workshop Montreal Sept 2000 pp94 105 An example will give the idea import Generics class Bin a where toBin 20a gt Int fromBin Int gt a Int toBin Unit Unit toBin a b Inl x 0 toBin x toBin a b Inr y 1 toBin y toBin a b x y toBin x toBin y fromBin Unit bs Unit bs fromBin a b 0O bs Inl x bs where x bs fromBin bs fromBin a b 1 bs
54. cannot put a forall you cannot instantiate a type variable with a forall type So you cannot make a forall type the argument of a type constructor So these types are illegal xl forall a a gt a x2 forall a a gt a Int x3 Maybe forall a a gt a Of course forall becomes a keyword you can t use forall as a type variable any more 7 4 8 1 Examples In a data or newt ype declaration one can quantify the types of the constructor arguments Here are several examples data T a T1 forall b b gt b gt b a data MonadT m MkMonad return forall a a gt ma bind forall a b m a gt a gt mb gt mb newtype Swizzle MkSwizzle Ord a gt a gt a The constructors have rank 2 types T 2 forakt sax koralk Be D gt D gt p gt a gt Ta MkMonad forall m forall a a gt m a gt forall a b ma gt a gt m b gt m b gt MonadT m MkSwizzle Ord a gt a gt a gt Swizzle Notice that you don t need to use a foral1 if there s an explicit context For example in the first argu ment of the constructor MkSwizzle an implicit forall a is prefixed to the argument type The implicit forall quantifies all type variables that are not already in scope and are mentioned in the type quantified over As for type signatures implicit quantification happens for non overloaded types too So if you write this data T a MkT Either ab b gt b i
55. checking which is on by default Recompilation checking fforce recomp normally stops compilation early leaving an existing o file in place if it can be determined that the module does not need to be recompiled In the olden days GHC compared the newly generated hi file with the previous version if they were identical it left the old one alone and didn t change its modification date In consequence importers of a module with an unchanged output hi file were not recompiled This doesn t work any more Suppose module C imports module B and B imports module A So changes to module A might require module C to be recompiled and hence when A hi changes we should check whether C should be recompiled However the dependencies of C will only list B hi not A hi and some changes to A changing the definition of a function that appears in an inlining of a function expor ted by B say may conceivably not change B hi one jot So now GHC keeps a version number on each interface file and on each type signature within the interface file It also keeps in every interface file a list of the version numbers of everything it used when it last com piled the file If the source file s modification date is earlier than the o file s date i e the source hasn t changed since the file was last compiled and the recompilation checking is on GHC will be clever It compares the version numbers on the things it needs this time with the version numbers on t
56. d Num t4Gt fromiInt t4GS Int 2i gt _4 _NI_ fromInt t4GS fromInt r3JX _4 d Num t4Gt The class method Unique r3JH selects the addition code from a Num dictionary now an explicit lambda d argument Because Core is 2nd order lambda calculus type applications and lambdas are explicit So is first applied to a type _4 then to a dictionary yielding the actual addition function that we will use subsequently We play the exact same game with the non standard class method fromInt Unsurprisingly the type Int is wired into the compiler lit t4Hb _4 _NI_ lit t4Hb Tet 4 ds d4Qz Int 2i _NI_ ds d4Qz I 2 in fromInt t4GS ds d4Qz I 2 is just the literal Int 2 it reflects the fact that GHC defines data Int I Int where Int is the primitive 98 Using GHC unboxed type s relevant info about unboxed types elsewher The after I indicates that this is a saturated application of the I data constructor i e not partially applied skip2 t3Ja _4 gt _4 a NF skip2 t3Ja m rlH4 gt let ds d4Q0Q _4 2NI ds d40Q let ds d4QY _4 _NI_ ds d40Y t4Hg m r1H4 1lit t4Hb in skip2 t3Ja ds d4QY in _4 m rlH4 ds d4QQ end CoRec in skip2 t3Ja It s just a simple functional language is an unregiste
57. declaration sets the fixity for a data constructor and the corresponding type constructor For example TAPER T yp EEs sets the fixity for both type constructor T and data constructor T and similarly for Int a Bool Function arrow is infixr with fixity 0 This might change I m not sure what it should be 7 4 1 3 Liberalised type synonyms Type synonyms are like macros at the type level and GHC does validity checking on types only after expanding type synonyms That means that GHC can be very much more liberal about type synonyms than Haskell 98 You can write a forall including overloading in a type synonym thus type Discard a forall b Show b gt a gt b gt a String f Discard a f x y x show y Discard Int gt Int String A rank 2 type Ges g f f 3 True You can write an unboxed tuple in a type synonym 143 GHC Language Features type Pr Int Int he 3 Tat gt Pr h x x x e You can apply a type synonym to a forall type type Foo a a gt a gt Bool Foo forall b b gt b After expanding the synonym f has the legal in GHC type f forall b b gt b gt forall b b gt b gt Bool e You can apply a type synonym to a partially applied type synonym type Generic io forall x i x gt O x type Id x x foo Generic Id After expanding the synonym foo has the legal
58. default or one for each top level definition with any one off costs this behaviour can be selected by giving GHC the caf a11 flag If you think you have a weird profile or the call graph doesn t look like you expect it to feel free to send it and your program to us at lt glasgow haskell bugs haskell org gt 5 2 Compiler options for profiling prof To make use of the profiling system all modules must be compiled and linked with the prof option Any SCC annotations you ve put in your source will spring to life Without a prof option your SCCs are ignored so you can compile SCC laden code without changing it There are a few other profiling related compilation options Use them in addition to prof These do not have to be used consistently for all modules in a program auto GHC will automatically add _scc_ constructs for all top level exported functions auto all All top level functions exported or not will be automatically _scc_ d caf all The costs of all CAFs in a module are usually attributed to one big CAF cost centre With this option all CAFs get their own cost centre An if all else fails op tion The call site is just the place in the source code which mentions the particular function or variable 116 Profiling Ignore any _scc_ constructs so a module which already has _scc_s can be com piled for profiling with the annotations ignored 5 3 Time and allocation pro
59. example both these declarations are illegal class x Int gt C a where instance x a gt Foo a where Reason exactly which implicit parameter you pick up depends on exactly where you invoke a function But the invocation of instance declarations is done behind the scenes by the compiler so it s hard to figure out exactly where it is done Easiest thing is to outlaw the offending types Implicit parameter constraints do not cause ambiguity For example consider f x a gt Int gt Int f n n length x g Read a Show a gt String gt String g s show read s Here g has an ambiguous type and is rejected but f is fine The binding for x at f s call site is quite unambiguous and fixes the type a 7 4 6 2 Implicit parameter bindings An implicit parameter is bound using the standard let or where binding forms For example we define the min function by binding cmp min a gt a min let cmp lt in least A group of implicit parameter bindings may occur anywhere a normal group of Haskell bindings can oc cur except at top level That is they can occur in a let including in a list comprehension or do notation or pattern guards or a where clause Note the following points 161 GHC Language Features e An implicit parameter binding group must be a collection of simple bindings to implicit style vari ables no function style bindings and no type signatures the
60. for Maybe andArray i There is a new module Data Traversable providing a class for data structures that can be tra versed from left to right It gives instances for Maybe andArray i Data Functor M has been deprecated use Data Foldable and Data Traversable in stead The toConstr definitions for tuples in Data Generics Instances now actually evaluate their arguments to tuples before returning anything Data IntMap now exports notMember alter mapMaybe mapMaybeWithKey mapEither and mapEitherWithKey It also has Monoid Foldable and Read instances Data IntSet now exports notMember It also has Monoid and Read instances Data Map now exports notMember alter mapMaybe mapMaybeWithKey mapEither mapEitherWithKey minView and maxView It also has Monoid Traversable Fold able and Read instances Data Set now exports notMember minView and maxView It also has Monoid Foldable and Read instances The old deprecated since 6 4 interface consisting of emptySet mkSet set ToList unit Set elementOf isEmptySet cardinality unionManySets minusSet mapSet intersect addToSet and delFromSet has been removed Data Monoid no longer contains the Monoid instances for Map IntMap Set and IntSet They have been moved to their own modules as above The a gt a instance has been replaced with a Monoid b gt Monoid a gt b instance The module also now exports Dual Endo All Any Sum and Product type
61. for performance so if you have plenty of memory it s worthwhile supplying a big Hsize For improving GC performance using Hsi ze is usually a better bet than Asize default 0 3 In the threaded and SMP versions of the RTS see threaded Sec tion 4 10 7 Options affecting linking a major GC is automatically performed if the runtime has been idle no Haskell computation has been running for a period of time The amount of idle time which must pass before a GC is performed is set by the Iseconds option Specifying I0 disables the idle GC For an interactive application it is probably a good idea to use the idle GC because this will allow finalizers to run and deadlocked threads to be detected in the idle time when no Haskell computation is happening Also it will mean that a GC is less likely to happen when the application is busy and so responsiveness may be improved However if the amount of live data in the heap is particularly large then the idle GC can cause a significant delay and too small an interval could adversely affect inter active responsiveness This is an experimental feature please let us know if it causes problems and or could benefit from further tuning Default 1k Set the initial stack size for new threads Thread stacks including the main thread s stack live on the heap and grow as required The default value is good for concurrent applications with lots of small threads if your program doe
62. generation mode 43 43 depends package specification 72 DEPRECATED 188 deprecations 59 description package specification 71 directories installation 19 directory layout binary distributions 18 do notation in GHCi 30 dumping GHC intermediates 89 duplicate exports warning 60 dynamic options 39 42 E encoding 47 Environment variable GHC_PACKAGE_PATH 66 environment variable for setting RTS options 84 eval mode 42 export lists duplicates 60 exposed package specification 71 exposed modules package specification 71 extensions options controlling 133 extensions GHC 133 extra libraries package specification 72 F faster compiling 127 faster programs how to produce 128 FFI GHCi support 25 fields missing 60 file suffixes for GHC 42 filenames 46 of modules 27 finding interface files 47 floating point exceptions 224 forcing GHC phase options 76 foreign export with GHC 206 Foreign Function Interface GHCi support 25 foreign import ccall wrapper with GHC 207 framework dirs package specification 73 frameworks package specification 73 fromInteger 223 fromIntegral 223 G garbage collection compacting 85 garbage collector options 85 GCC options 78 generations number of 86 getArgs 38 getProgName 38 229 Index GHC vs the Haskell 98 language 221 GHC using 41 GHCi 25 ghci 42 GHCRTS 84 GHC_PACKAGE_PATH 66 ghc_rts_opts 88 Glasgow
63. is in a sense a compiler readable description of the contents of the object file If the interface file and object file get out of sync for any reason then the compiler may end up making assumptions about the object file that aren t true trouble will almost certainly follow For this reason we recommend keeping object files and interface files in the same place GHC does this by default but it is possible to override the defaults as we ll explain shortly Every module has a module name defined in its source code nodule A B C where The name of the object file generated by GHC is derived according to the following rules where osuf is the object file suffix this can be changed with the osuf option e If there is no odir option the default then the object filename is derived from the source file name ignoring the module name by replacing the suffix with osuf e If odir dir has been specified then the object filename is dir mod osuf where mod is the module name with dots replaced by slashes The name of the interface file is derived using the same rules except that the suffix is hisuf hi by default instead of osuf and the relevant options are hidir and hisuf instead of odir and osuf respectively For example if GHC compiles the module A B C in the file src A B C hs with no odir or hidir flags the interface file will be put in src A B C hi and the object file in src A B C o For any module that is imp
64. is now a flag fex tended default rules to use these defaulting rules with GHC too More details are in Sec tion 3 4 5 Type defaulting in GHCi e You can now give both class and instance declarations in hs boot files More details in Sec tion 4 6 9 How to compile mutually recursive modules e Linear implicit parameters have been scheduled for removal for some time In 6 6 we ve removed them from the user manual and they may well disappear from the compiler itself in 6 6 1 e If the program is idle for a certain amount of time then GHC will now take the opportunity to do a major garbage collection The amount of idle time that is required before that happens is controlled by the new I RTS flag There is more detail in Section 4 14 3 RTS options to control the garbage collector e It is now possible to control the frequency that the RTS clock ticks at with the new Vv RTS flag This is normally handled automatically by other flags but this flag is needed if you want to increase the resolution of the time profiler For more details see Section 4 14 2 Miscellaneous RTS options e The old syntax for FFI declarations deprecated since 5 04 is no longer accepted e The split objs flag which when used to compile libraries means executables using the library will be smaller can now be used with make and hence can be used by cabal See Section 4 10 7 Options affecting linking for more informatio
65. it reads the contents of these two package databases and builds up a list of the packages it knows about You can see GHC s package table by running GHC with the v flag Package databases may overlap for example packages in the user database will override those of the same name in the global database You can control the loading of package databses using the following GHC options package conf file Read in the package configuration file file in addition to the system default file and the user s local file Packages in additional files read this way will override those in the global and user databases no user package conf Prevent loading of the user s local package database To create a new package database just create a new file and put the string in it Packages can be added to the file using the ghc pkg tool described in Section 4 8 5 Package management the ghc pkg command 4 8 3 1 The GHC_PACKAGE PATH environment variable The GHC_PACKAGE_PATH environment variable may be set to a separated separated on Win dows list of files containing package databases This list of package databases is used by GHC and ghc pkg with earlier databases in the list overriding later ones This order was chosen to match the beha viour of the PATH environment variable think of it as a list of package databases that are searched left to right for packages it used to in GHC 6 4 but not since 6 6 66 Using GHC
66. match is strict and the program behaves as if you had written data Foo Foo Int Int f x case rhs of Foo a b w gt body 7 2 2 Unboxed Tuples Unboxed tuples aren t really exported by GHC Exts they re available by default with fglasgow exts An unboxed tuple looks like this e_l en where e_1 e_n are expressions of any type primitive or non primitive The type of an unboxed tuple looks the same Unboxed tuples are used for functions that need to return multiple values but they avoid the heap alloca tion normally associated with using fully fledged tuples When an unboxed tuple is returned the com ponents are put directly into registers or on the stack the unboxed tuple itself does not have a composite representation Many of the primitive operations listed in primops txt pp return unboxed tuples In particular the TO and ST monads use unboxed tuples to avoid unnecessary allocation during sequences of operations There are some pretty stringent restrictions on the use of unboxed tuples e Values of unboxed tuple types are subject to the same restrictions as other unboxed types i e they may not be stored in polymorphic data structures or passed to polymorphic functions e No variable can have an unboxed tuple type nor may a constructor or function argument have an un boxed tuple type The following are all illegal data Foo Foo Int Int
67. more detail below Section 5 4 3 Biographical Profiling In addition the profile can be restricted to heap data which satisfies certain criteria for example you might want to display a profile by type but only for data produced by a certain module or a profile by retainer for a certain type of data Restrictions are specified as follows hcname hCname hmmodule hddesc hytype h ee hbbio Restrict the profile to closures produced by cost centre stacks with one of the spe cified cost centres at the top Restrict the profile to closures produced by cost centre stacks with one of the spe cified cost centres anywhere in the stack Restrict the profile to closures produced by the specified modules Restrict the profile to closures with the specified description strings Restrict the profile to closures with the specified types Restrict the profile to closures with retainer sets containing cost centre stacks with one of the specified cost centres at the top Restrict the profile to closures with one of the specified biographies where bio is one of lag drag void or use For example the following options will generate a retainer profile restricted to Branch and Leaf con structors prog RTS hr hdBranch Leaf There can only be one break down option eg hr in the example above but there is no limit on the number of further restrictions that may be applied All
68. noth ing Bang patterns work in case expressions too of course g5 x let y f x in body g6 x case f x of y gt body g7 x case f x of y gt body The functions g5 and g6 mean exactly the same thing But g7 evalutes f x binds y to the result and then evaluates body Bang patterns work in let and where definitions too For example 185 GHC Language Features 7 8 2 let x y e in b is a strict pattern operationally it evaluates e matches it against the pattern x y and then evaluates b The should not be regarded as part of the pattern after all in a function argument x y means the same as x y Rather the is part of the syntax of let bindings Syntax and semantics We add a single new production to the syntax of patterns pat pat There is one problem with syntactic ambiguity Consider Is this a definition of the infix function or of the f with a bang pattern GHC resolves this am biguity inf favour of the latter If you want to define with bang patterns enabled you have to do so using prefix notation x 3 The semantics of Haskell pattern matching is described in Section 3 17 2 http haskell org onlinereport exps html sect3 17 2 of the Haskell Report To this description add one extra item 10 saying e Matching the pattern pat against a value v behaves as follows e if vis bottom the match diverges e otherwise pat is matched agai
69. order to make graphs more readable hp2ps sorts the shaded bands for each identifier The default sort ordering is for the bands with the largest area to be stacked on top of the smaller ones The ad option causes rougher bands those representing series of values with the largest stand ard deviations to be stacked on top of smoother ones Normally hp2ps puts the title of the graph in a small box at the top of the page However if the JOB string is too long to fit in a small box more than 35 characters then hp2ps will choose to use a big box instead The b option forces hp2ps to use a big box Generate encapsulated PostScript suitable for inclusion in LaTeX docu ments Usually the PostScript graph is drawn in landscape mode in an area 9 inches wide by 6 inches high and hp2ps arranges for this area to be approximately centred on a sheet of a4 paper This format is conveni ent of studying the graph in detail but it is unsuitable for inclusion in LaTeX documents The e option causes the graph to be drawn in por trait mode with float specifying the width in inches millimetres or points the default The resulting PostScript file conforms to the Encapsulated PostScript EPS convention and it can be included in a LaTeX docu ment using Rokicki s dvi to PostScript converter dvips Create output suitable for the gs PostScript previewer or similar In this case the graph is printed in portrait mode without scaling The output is unsui
70. package s version usually in the form A B any number of components are allowed string The type of license under which this package is distributed This field is a value of the License libraries Cabal Distribution License html t License type optional string The name of a file giving detailed license information for this package optional freeform The copyright string optinoal freeform The email address of the package s maintainer optional freeform A string describing the stability of the package eg stable pro visional or experimental optional freeform URL of the package s home page optional freeform URL of a downloadable distribution for this package The dis tribution should be a Cabal package optional freeform Description of the package optinoal freeform Which category the package belongs to This field is for use in conjunction with a future centralised package distribution framework tentatively titled Hackage optional freeform Author of the package bool Whether the package is exposed or not string list modules exposed by this package string list modules provided by this package but not exposed to the programmer These modules cannot be imported but they are still subject to the overlapping constraint no other package in the same program may provide a module of the Same name string list A list of directories containing interface files hi files for this pack ag
71. paper Also the type of the enclosed expression must be Q Dec not Q Dec as in the paper e A expression quotation is written in Oxford brackets thus e where the is an expression the quotation has type Expr e d where the is a list of top level declarations the quotation has type Q Dec e Planned but not implemented yet t where the is a type the quotation has type Type e Reification is written thus e reifyDecl T where T is a type constructor this expression has type Dec e reifyDecl C where C isa class has type Dec e reifyType f where f is an identifier has type Typ e Still to come fixities 7 6 2 Using Template Haskell e The data types and monadic constructor functions for Template Haskell are in the library Lan guage Haskell THSyntax e You can only run a function at compile time if it is imported from another module That is you can t define a function in a module and call it from within a splice in the same module It would make sense to do so but it s hard to implement e The flag ddump splices shows the expansion of all top level splices as they happen e Ifyou are building GHC from source you need at least a stage 2 bootstrap compiler to run Template Haskell A stage 1 compiler will reject the TH constructs Reason TH compiles and runs a program and then looks at the result So it s important that the program it compiles produces resu
72. produced by the compiler 0 An object file produced by an assembler Files with other suffixes or without suffixes are passed straight to the linker 4 4 Modes of operation GHC s behaviour is firstly controlled by a mode flag Only one of these flags may be given but it does not necessarily need to be the first option on the command line The available modes are ghc Interactive mode which is also available as ghci Interactive mode is described in interactive more detail in Chapter 3 Using GHCi ghc make In this mode GHC will build a multi module Haskell program automatically figur ing out dependencies for itself If you have a straightforward Haskell program this is likely to be much easier and faster than using make Make mode is described in Section 4 4 1 Using ghe make ghc e expr Expression evaluation mode This is very similar to interactive mode except that there is a single expression to evaluate expr which is given on the command line 42 Using GHC 4 4 1 See Section 4 4 2 Expression evaluation mode for more details C This is the traditional batch compiler mode in which GHC can compile source files S I c one at a time or link objects together into an executable This mode also applies if there is no other mode flag specified on the command line in which case it means that the specified files should be compiled and then linked to form a program See Se
73. stderr This option is useful for watching how the storage manager adjusts the heap size based on the current amount of live data Write a one line GC stats summary after running the program This output is in the same format as that produced by the Rghc t iming option As with s the default file is program stat The file stderr is treated specially with the output really being sent to stderr 4 14 4 RTS options for profiling and parallelism The RTS options related to profiling are described in Section 5 4 1 RTS options for heap profiling those for concurrency in Section 4 11 Using Concurrent Haskell and those for parallelism in Sec tion 4 12 1 Options to enable SMP parallelism 4 14 5 RTS options for hackers debuggers and over interested souls These RTS options might be used a to avoid a GHC bug b to see what s really happening or c because you feel like it Not recommended for everyday use B Dnum rfile Sound the bell at the start of each major garbage collection Oddly enough people really do use this option Our pal in Durham England Paul Callaghan writes Some people here use it for a variety of pur poses honestly e g confirmation that the code machine is doing something in finite loop detection gauging cost of recently added code Certain people can even tell what stage the program is in by the beep pattern But the major use is for annoy ing othe
74. support does But it is also possible to obtain performance improvements with parallelism on programs that do not use concurrency This section describes how to use GHC to compile and run parallel programs in Section 7 15 Parallel Haskell we desribe the language features that affect parallelism 4 12 1 Options to enable SMP parallelism In order to make use of multiple CPUs your program must be linked with the threaded option see Section 4 10 7 Options affecting linking Then to run a program on multiple CPUs use the RTS N option Nx Use x simultaneous threads when running the program Normally x should be chosen to match the number of CPU cores on the machine There is no means currently by which this value may vary after the program has started For example on a dual core machine we would probably use RTS N2 RTS Whether hyperthreading cores should be counted or not is an open question please feel free to experiment and let us know what results you find 4 12 2 Hints for using SMP parallelism Add the sstderr RTS option when running the program to see timing stats which will help to tell you whether your program got faster by using more CPUs or not If the user time is greater than the elapsed time then the program used more than one CPU You should also run the program without N for comparison GHC s parallelism support is new and experimental It may make your program go faster or it might slow it dow
75. that does actually work quite well in practice without ambiguity problems There is however a catch This version of the Collects class is nowhere near as general as the original class seemed to be only one of the four instances for Collects given above can be used with this version of Collects because only one of them the instance for lists has a collection type that can be written in the form c e for some type constructor c and element type e 7 4 3 2 2 Adding functional dependencies To get a more useful version of the Collects class Hugs provides a mechanism that allows programmers to specify dependencies between the parameters of a multiple parameter class For readers with an in terest in theoretical foundations and previous work The use of dependency information can be seen both as a generalization of the proposal for parametric type classes that was put forward by Chen Hudak and Odersky or as a special case of Mark Jones s later framework for improvement of qualified types The underlying ideas are also discussed in a more theoretical and abstract setting in a manuscript implparam where they are identified as one point in a general design space for systems of implicit parameterization To start with an abstract example consider a declaration such as class C a b where which tells us simply that C can be thought of as a binary relation on types or type constructors de pending on the kinds of a and b Extra cl
76. the Parser type above We hypothesise that multi parameter classes usually have one main parameter for which deriving new instances is most interesting Lastly all of this applies only for classes other than Read Show Typeable and Data for which the built in derivation applies section 4 3 3 of the Haskell Report For the standard classes Eq Ord Ix and Bounded it is immaterial whether the standard method is used or the one described here 7 4 13 Generalised typing of mutually recursive bind ings The Haskell Report specifies that a group of bindings at top level or in a let or where should be sorted into strongly connected components and then type checked in dependency order Haskell Re port Section 4 5 1 http haskell org onlinereport decls html sect4 5 1 As each group is type checked any binders of the group that have an explicit type signature are put in the type environment with the specified polymorphic type and all others are monomorphic until the group is generalised Haskell Report Section 4 5 2 http haskell org onlinereport decls html sect4 5 2 Following a suggestion of Mark Jones in his paper Typing Haskell in Haskell http www cse ogi edu mpj thih GHC implements a more general scheme If fglasgow exts is specified the dependency analysis ignores references to variables that have an explicit type signature As a result of this refined dependency analysis the dependency groups are smaller a
77. tion assert in source code in other words rewriting Ex ception assert p e toe see Section 7 9 Assertions This flag is turned on by O fno cse Turns off the common sub expression elimination optimisation Can be useful if you have some unsafePerformIO expres sions that you don t want commoned up fno strictness Turns off the strictness analyser sometimes it eats too many cycles fno full laziness Turns off the full laziness optimisation also known as let floating Full laziness increases sharing which can lead to in creased memory residency NOTE GHC doesn t implement complete full laziness When op timisation in on and fno full laziness is not given some transformations that increase sharing are performed such as extracting repeated computations from a loop These are the same transformations that a fully lazy implementation would do the difference is that GHC doesn t consistently apply full laziness so 74 Using GHC fno state hack funbox strict fields funfold ing update in place lt n gt funfold ing cre ation threshold lt n gt funfold ing use threshold lt n gt don t rely on it Turn off the state hack whereby any lambda with a State token as argument is considered to be single entry hence it is considered OK to inline things inside it This can improve per formance of IO and ST monad code but it runs the risk of redu cing sharing This opti
78. to your program When your Haskell program starts up its RTS extracts command line arguments bracketed between RTS and RTS as its own For example a out f RTS p S RTS h foo bar The RTS will snaffle p S for itself and the remaining arguments f h foo bar will be handed to your program if when it calls System getArgs No RTS option is required if the runtime system options extend to the end of the command line as in this example hls ltr usr etc RTS A5m If you absolutely positively want all the rest of the options in a command line to go to the program and not the RTS use a RTS As always for RTS options that take sizes If the last character of size is a K ork multiply by 1000 if an M or m by 1 000 000 if a G or G by 1 000 000 000 And any wraparound in the counters is your fault Giving a RTS f option will print out the RTS options actually available in your program which vary depending on how you compiled NOTE since GHC is itself compiled by GHC you can change RTS options in the compiler using the normal RTS RTS combination eg to increase the maximum heap size for a compilation to 128M you would add RTS M128m RTS to the command line 4 14 1 Setting global RTS options RTS options are also taken from the environment variable GHCRTS For example to set the maximum heap size to 128M for all GHC compiled programs using an sh like shell 84 Using GHC GHCRTS
79. to refer to the hs boot file then the module import graph must have no cycles The command ghc M will report an error if a cycle is found e A module M that is SOURCE imported in a program will usually also be ordinarily im ported elsewhere If not ghc make automatically adds M to the set of moudles it tries to compile and link to ensure that M s implementation is included in the final program 52 Using GHC A hs boot file need only contain the bare minimum of information needed to get the bootstrapping pro cess started For example it doesn t need to contain declarations for everything that module A exports only the things required by the module s that import A recursively A hs boot file is written in a subset of Haskell The module header including the export list and import statements are exactly as in Haskell and so are the scoping rules Hence to mention a non Prelude type or class you must import it There must be no value declarations but there can be type signatures for values For example double Int gt Int Fixity declarations are exactly as in Haskell Type synonym declarations are exactly as in Haskell A data type declaration can either be given in full exactly as in Haskell or it can be given abstractly by omitting the sign and everything that follows For example data Tab In a source program this would declare TA to have no constructors a GHC extension see Sec t
80. type t is an arbitrary type e The vk 1 vn are type variables which do not occur in t and e The ci are partial applications of classes of the form C t1 tj where the arity of C is ex actly j 1 That is C lacks exactly one type argument e None of the ci is Read Show Typeable or Data These classes should not look through the type or its constructor You can still derive these classes for a newtype but it happens in the usual way not via this new mechanism Then for each ci the derived instance declaration is instance ci t vk l v gt ci T vl vp where p is chosen so that T v1 vp is of the right kind for the last parameter of class Ci As an example which does not work consider newtype NonMonad m s NonMonad State s m s deriving Monad Here we cannot derive the instance instance Monad State s m gt Monad NonMonad m because the type variable s occurs in State s m and so cannot be eta converted away It is a good thing that this deriving clause is rejected because NonMonad mis not in fact a monad for the same reason Try defining gt gt with the correct type you won t be able to 172 GHC Language Features Notice also that the order of class parameters becomes important since we can only derive instances for the last one If the Stat eMonad class above were instead defined as class StateMonad m s m gt s where then we would not have been able to derive an instance for
81. type with only one data constructor for example tuples are single constructor types If your datatype has a single constructor with a single field use a newtype declaration instead of a data declaration The new type will be optimised away in most cases Don t guess look it up Look for your function in the interface file then for the third field in the pragma it should say __S lt string gt The lt string gt gives the strictness of the function s arguments L is lazy bad S and E are strict good P is primitive good U is strict and unpackable very good and A is absent very good For an unpackable U argument the info inside tells the strictness of its components So if the argument is a pair and it says U AU LSS that means the first component of the pair isn t used the second component is itself unpackable with three components lazy in the first strict in the second amp third If the function isn t exported just compile with the extra flag ddump simp1 next to the signature for any binder it will print the self same pragmatic information as would be put in an inter face file Besides Core syntax is fun to look at Placing INLINE pragmas on certain functions that are used a lot can have a dramatic effect See Section 7 10 3 1 INLINE pragma If you do not have an explicit export list in a module GHC must assume that everything in that module wi
82. using a out The exe extension is appended on Win dows and it can of course be overridden with o GHC s garbage collector now deals more intelligently with mutable data so you mostly no longer need to worry about GC performance when a lot of memory is taken up by STArrays IOArrays STRefs or IORe fs For more details see trac bug 650 http hackage haskell org trac ghc ticket 650 GHC now allows more generalisation when typing mutually recursive bindings resulting in more programs being accepted See Section 7 4 13 Generalised typing of mutually recursive bindings for more details The rules for instance declarations have been further relaxed You are now permitted to have in stances whose heads contain only type variables e g instance Ca and instances whose constraints are not only type variables e g instance C2 Int a gt C3 a b Introduction to GHC For more details see Section 7 4 4 1 Relaxed rules for instance declarations The following flags and where appropriate their inverses used to be static can only be given on the command line but are now dynamic can also be given in a GHC_OPTIONS pragma or with set in GHCi c hcsuf hidir hisuf o odir ohi osuf keep hc file keep s file keep raw s file keep tmp files tmpdir i package hide package ignore package package conf z no user package conf fcontext stack fexcess precision fignore as
83. v3 and the other levels have the following meanings v0 Disable all non essential messages this is the default v1 Minimal verbosity print one line per compilation this is the default when make or interactive is on v2 Print the name of each compilation phase as it is executed equivalent to dshow passes v3 The same as v2 except that in addition the full command line if appro priate for each compilation phase is also printed v4 The same as v3 except that the intermediate program representation after each compilation phase is also printed excluding preprocessed and C assembly files Print a one line string including GHC s version number Print GHC s numeric version number only Print the path to GHC s library directory This is the top of the directory tree con taining GHC s libraries interfaces and include files usually something like usr local lib ghc 5 04 on Unix This is the value of 1ibdirin the package configuration file see Section 4 8 Packages 45 Using GHC ferror spans Causes GHC to emit the full source span of the syntactic entity relating to an error message Normally GHC emits the source location of the start of the syntactic en tity only For example test hs 3 6 parse error on input where becomes test296 hs 3 6 10 parse error on input where And multi line spans are possible too test hs 5 4 6 7 Conflicting definitions fo
84. various options See Section 3 7 The set command for a list of available options The set command by itself shows which options are currently set Sets the list of arguments which are returned when the program calls Sys tem getArgs Sets the command used by edit to cmd Sets the string to be returned when the program calls System get ProgName Sets the string to be used as the prompt in GHCi Inside prompt the sequence s is replaced by the names of the modules currently in scope and is replaced by Show the bindings made at the prompt and their types Show the list of modules currently load Generates a tags file for Vi style editors ctags or Emacs style editors etags If no filename is specified the defaulit tags or TAGS is used respect ively Tags for all the functions constructors and types in the currently loaded mod ules are created All modules must be interpreted for these commands to work See also Section 10 1 Ctags and Etags for Haskell hasktags Infers and prints the type of expression including explicit forall quantifiers for polymorphic types The monomorphism restriction is not applied to the expression during type inference Infers and prints the kind of t ype The latter can be an arbitrary type expression including a partial application of a type constructor such as Either Int Undefines the user defined command name see def above Unsets certain options See
85. when a module or interface file in the current directory is shadowing one with the same mod ule name in a library or other directory Similarly for incomplete patterns the function g below will fail when applied to non empty lists so the compiler will emit a warning about this when fwarn incomplete patterns is enabled This option isn t enabled be default because it can be a bit noisy and it doesn t always indicate a bug in the program However it s generally considered good practice to cover all the cases in your functions The function below will fail when applied to Bar so the com piler will emit a warning about this when fwarn incomplete record updates is enabled data Foo Foo x Int Bar f Foo gt Foo f foo foo x 6 This option isn t enabled be default because it can be very noisy and it often doesn t indicate a bug in the program This option is on by default and warns you whenever the con struction of a labelled field constructor isn t complete missing ini tializers for one or more fields While not an error the missing fields are initialised with bottoms it is often an indication of a programmer error This option is on by default and warns you whenever an instance declaration is missing one or more methods and the correspond ing class declaration has no default declaration for them The warning is suppressed if the method name begins with an un derscore Here s an examp
86. where Dollars a Dollars b Dollars atb All the instance does is apply and remove the newt ype constructor It is particularly galling that since the constructor doesn t appear at run time this instance declaration defines a dictionary which is wholly equivalent to the Int dictionary only slower 7 4 12 1 Generalising the deriving clause GHC now permits such instances to be derived instead so one can write newtype Dollars Dollars Int deriving Eq Show Num and the implementation uses the same Num dictionary for Dollars as for Int Notionally the com piler derives an instance declaration of the form instance Num Int gt Num Dollars which just adds or removes the newt ype constructor according to the type We can also derive instances of constructor classes in a similar way For example suppose we have im plemented state and failure monad transformers such that instance Monad m gt Monad State s m instance Monad m gt Monad Failure m In Haskell 98 we can define a parsing monad by type Parser tok m a State tok Failure m a which is automatically a monad thanks to the instance declarations above With the extension we can make the parser type abstract without needing to write an instance of class Monad via newtype Parser tok m a Parser State tok Failure m a deriving Monad In this case the derived instance declaration is of the form instance Monad State tok Failure m gt Monad Par
87. write a kind signature instead data Term gt where or even a mixture of the two data Eoo ari i F R gt where The type variables if given may be explicitly kinded so we could also write the header for Foo like this data Foo a b gt where e There are no restrictions on the type of the data constructor except that the result type must begin with the type constructor being defined For example in the Term data type above the type of each constructor must end with gt Term e You can use record syntax on a GADT style data type declaration 174 GHC Language Features data Term a where Lit val Sy Tt Term Int Succ num Term Int Term Int Pred num Term Int Term Int IsZero arg Term Int Term Bool Pair argl Term a arg2 Term b Term a b If cnd Term Bool y Eru Term a z ELS Term a Term a For every constructor that has a field f a the type of field must be the same and b the result type of the constructor must be the same both modulo alpha conversion Hence in our example we cannot merge the num and arg fields above into a single name Although their field types are both Term Int their selector functions actually have different types num Term Int gt Term Int arg Term Bool gt Term Int At the moment record updates are not yet possible with GADT so support is limited to record con struction selection and pattern m
88. 0 ddump workwrap 92 debug 81 dfaststring stats 97 dppr debug 97 dppr user length 97 dshow passes 97 dshow rn stats 97 dshow unused imports 97 dstg lint 97 dverbose core2core 97 dverbose stg2stg 97 dynamic 80 E 43 44 E option 45 f 69 RTS option 84 F 78 78 RTS option 85 f options GHC 74 fallow incoherent instances 134 156 fallow overlapping instances 134 156 fallow undecidable instances 134 fallow undecidable instances option 156 farrows 134 fasm 79 fcontext stack 134 ferror spans 46 fexcess precision 74 fext core 89 fextended default rules 134 fffi 133 205 ffi 133 fforce recomp 6 51 fgenerics 134 fglasgow exts 133 205 fignore asserts 74 187 finline phase 134 fmono pat binds 203 fno options GHC 74 fno code 79 fno cse 74 fno force recomp 51 fno full laziness 74 fno implicit prelude option 134 210 fno mono pat binds 203 fno monomorphism restriction 203 fno print bind result 30 fno state hack 75 226 Index fno strictness 74 fno strictness anti option 128 fPIC 79 fprint bind result 30 framework 80 framework path 80 fth 176 funbox strict fields 75 funfolding creation threshold 75 funfolding update in place 75 funfolding use threshold 75 funfolding use threshold0O option 132 fvia C 79 fvia C option 128 fwarn deprecations 59 fwarn duplicate exports 60 fwarn hi shado
89. 1 Dependency generation 4 6 11 Dependency generation Putting inter dependencies of the form Foo o Bar hi into your Makefile by hand is rather er ror prone Don t worry GHC has support for automatically generating the required dependencies Add 54 Using GHC the following to your Makefile depend ghc M HC_OPTS SRCS Now before you start compiling and any time you change the imports in your program do make de pend before you do make cool_pgm The command ghe M will append the needed dependencies to your Makefile In general ghc M Foo does the following For each module M in the set Foo plus all its imports transitively it adds to the Makefile e A line recording the dependence of the object file on the source file M o M hs or M Lhs if that is the filename you used e For each import declaration import X in M a line recording the dependence of M on X M o Xhi bl e For each import declaration import SOURC ofMon X Xin M a line recording the dependence M o X hi boot See Section 4 6 9 How to compile mutually recursive modules for details of hi boot style in terface files If M imports multiple modules then there will be multiple lines with M o as the target There is no need to list all of the source files as arguments to the ghc M command ghc traces the de pendencies just like ghc make a new feature in GHC 6 4 Note that ghc M needs to
90. 1 Running Concurrent Haskell programs in parallel The first possibility is to use concurrent threads to structure your program and make sure that you spread computation amongst the threads The runtime will schedule the running Haskell threads among the available OS threads running as many in parallel as you specified with the N RTS option 203 GHC Language Features 7 15 2 Annotating pure code for parallelism The simplest mechanism for extracting parallelism from pure code is to use the par combinator which is closely related to and often used with seq Both of these are available from Control Parallel libraries base Control Parallel html infixr 0 par infixr 1 seq Pars san gt bo 5 seq a gt b gt b The expression x par y sparks the evaluation of x to weak head normal form and returns y Sparks are queued for execution in FIFO order but are not executed immediately If the runtime detects that there is an idle CPU then it may convert a spark into a real thread and run the new thread on the idle CPU In this way the available parallelism is spread amongst the real CPUs For example consider the following parallel version of our old nemesis nf ib import Control Parallel nee es TAE gt TNE nfib n n lt 1 1 otherwise par nl seq n2 nl n2 1 where n1 nfib n 1 n2 nfib n 2 For values of n greater than 1 we use par to spark a thread to evaluate nfib n 1
91. 13 Generalised typing of mutually recursive bindings 0 0 0 0 eee eee ee eeeeeeceeeceeeee eee 173 7 5 Generalised Algebraic Data Types GADTS 00 0 0 cece cence eee ce eecaeeceeeaneeueeeneeeneeennees 174 1 6 Template Haskell crni oe sdeete one dee dau eadeus syed e a E tens EE 176 POs SYMAX 2255565 shes cov be etbb ee eaor EE puke ouslSyeaesis Wocases iebecesst ES S EE ES ETES 176 1 6 2 Using Template Haskell scscccs A cssigessctasesscosseus srviaagsscbvacssesoinagssesspeaksveneagseutanwsss 177 7 6 3 A Template Haskell Worked Example 00 cceceeeceeeceecc cee eeca cece cena eeneeeneeenees 177 7 6 4 Using Template Haskell with Profiling 2 0 00 cece ceeceeeeeceeneeeeececeeeeeeeeeeeeseeuneeeees 179 TT ATOW NOtAllON ses s sesecis ivi cess tered estiwveecssaueenosvane ease og your essaweedsotpeeduesteey EST EUNE EVITE SEIN 179 7 7 1 do notation for COMMANAS lt siisii osese Sian ds sereas vias gasedennss EE GTE SaS Es PESES a KE Saes 180 1 2 Condinonal commands gisper e eE EE E a tes A EE ENEE 181 7 1 3 Defining your own control structures sese sseessssessrerrsrrerrrrsrsrrerrereerrererrerrereee 182 Loch As PAmItIVe construc tS ier opi ona soa RE E EE E E R EE ITEE REP 183 T t Differences With the paper scrion iore En EA EE ets E EAS ETONE ETVS 184 PRO POMADUALY e E e e a E n e A EIT Beeston 184 1 8 Bane DANNS u o ESE EEE A E S E EE 185 7 8 1 Informal description of bang patterns cece e cece eee e ce
92. AS TO Mth wise ones E use s ban kg heedndes dav otengeva aes nee pedsasg T 14 PAS LU DEtwork son eeso Er E E O santo EEEE ON EE O T SENO eee ss 14 1 45 12 ODJECHO pi ereo aaa ap E eas EE des E TERS 15 1 4 5 13 Open Abese e e E E EEEE E rE E E E E OaE aE 15 145 e EO oE E A E E E E 15 VAS AS QUICK Chek rase ea e E E a E EES NEE Eaa 15 LAD POs ime s ana E E E E SS eho ae E EE 15 LASARI Tiene ere a Ee a E EE EEEE E EN EOE EOSS 15 VAD LS MUM e e T EE a EE N EE E N E E a 16 14 6 GHG ASA Library sirean ea E a A E E E Seaton pre S aaea 16 1 4 7 Internal changes oorsien oe ep rents ie cecdois ENEE TEE wean st SKS EEE EEES TETES IETS 16 2 Installing GAG epee ii sas Mate eaa a R EE oE E e a E EDES e PURER SPEE de ond muses vote Sees uE Eiaa 17 2 1 Installing on Unix a likes oserei uns e a eE o E EE EESE EEE 17 2 1 1 When a platform specific package is available ceceeeeseeeeeeceeceeeeeenereeeereeaneres 17 2 1 2 GHC binary distributions 20 0 0 cece cece ence ne cence ence eeceeeeeeeeaeeeaeseaesea seas seas eeaes 17 ZA Del Wistallan 9 ose ss dos coos ss fecdnw iee EEE a TIPSE ETES PESSE EG EERO TTAR S 18 2 12 2 What bundles there are i sesat eeoa e a a e a e a aaao 19 2 1 2 3 Testing that GHC seems to be working sessesseerssserrsreserererrrresrrerrereeereees 20 2 2 Installing On WINCOWS sereme eresie vein ost phue ep ea E See goes heumesenbed eaves eotnest eee E A E EESE 20 The Glorious Glasgow Haskell Co
93. ER DDWORD and SIZE_T It also has new helper functions ddwordToDwords and dwordsToDdword to split and combine ddwords into high and low components 12 Introduction to GHC e System Win32 re exports System Win32 FileMapping System Win32 Time and System Win32 Console 1 4 5 Extra Libraries 1 4 5 1 ALUT e Version number 2 0 was 1 0 e Sound ALUT BuiltInSounds has been removed Its Phase and Duration exports are now exported by Sound ALUT Loaders and its he lloWorld sine square sawtooth im pulse and whiteNoise exports are now constructors of the Sound ALUT Loaders SoundDataSource datatype 1 4 5 2 arrows e Version number 0 2 was 0 1 e Control Sequence has been removed in favour of the new Control Applicative module in base 1 4 5 3 cgi e Version 2006 8 14 e cgi is anew package developing on what used to be Network CGI in the net work package 1 4 5 4 fgl e Version number 5 3 was 5 2 e Data Graph Inductive Graph no longer exports UContext e Data Graph Inductive Graph now exports delLEdge 1 4 5 5 GLUT e Version number remains 2 0 e In Graphics UI GLUT Initialization DisplayMode has a new constructor WithAuxBuffers and DisplayCapability has a new constructor DisplayAux These rep resent freeglut only features There are new examples in BOGLGP Chapter03 OnYourOwnl hs RedBook AAIndex hs RedBook AARGB hs RedBook AccAnti hs RedBook Ac cPersp hs RedBook Al
94. Er CA 0 LLOC_FUN_hst_ 2382937 LLOC_UP_THK_ctr 0 LLOC_SE_THK_ ctr 308906 ENT_IND_ctr O E NT_PERM_IND_ctr requires RTS Z lots more info omitted 0 GC_SEL_ ABANDONED ctr 0 GC_SEL_MINOR_ctr O GC_SEL_MAJOR_ctr 0 GC_FAILED_ PROMOTION_ctr 47524 GC_WORDS_COPIED_ctr The formatting of the information above the row of asterisks is subject to change but hopefully provides a useful human readable summary Below the asterisks all counters maintained by the ticky ticky sys tem are dumped in a format intended to be machine readable zero or more spaces an integer a space the counter name and a newline In fact not all counters are necessarily dumped compile or run time flags can render certain counters invalid In this case either the counter will simply not appear or it will appear with a modified counter name possibly along with an explanation for the omission notice ENT_PERM_IND_ctr appears with an inserted above Software analysing this output should always check that it has the counters it ex pects Also beware some of the counters can have large values 126 CAS Chapter 6 Advice on sooner faster smaller thriftier Please advise us of other helpful hints that should go here 6 1 Sooner producing a program more quickly Don t use O or especially 02 Use more memory Don t use too much memory Try to use local disks when link
95. GHCi and type J lt tab gt then GHCi will expand itto Just 3 4 3 1 Qualified names To make life slightly easier the GHCi prompt also behaves as if there is an implicit import quali fied declaration for every module in every package and every module currently loaded into GHCi 3 4 3 2 The main command 32 Using GHCi 3 4 4 When a program is compiled and executed it can use the get Args function to access the command line arguments However we cannot simply pass the arguments to the main function while we are test ing in ghci as the main function doesn t take its directly Instead we can use the main command This runs whatever main is in scope with any arguments be ing treated the same as command line arguments e g Prelude gt let main System Environment getArgs gt gt print Prelude gt main foo bar ROOM parwi The it variable Whenever an expression or a non binding statement to be precise is typed at the prompt GHCi impli citly binds its value to the variable it For example Prelude gt 1 2 3 Prelude gt it 2 6 What actually happens is that GHCi typechecks the expression and if it doesn t have an IO type then it transforms it as follows an expression e turns into let it e print Le which is then run as an IO action Hence the original expression must have a type which is an instance of the Show class or GHCi will complain Prelude gt id lt interactiv
96. HC system except in one case If your program is made of several modules each module must have been compiled after any modules on which it depends unless you use hi boot files in which case these must be correct with respect to the module source For example if an interface is lying about the type of an imported value then GHC may well generate duff code for the importing module This applies to pragmas inside interfaces too If the pragma is lying e g about the arity of a value then duff code may result Furthermore arities may change even if types do not In short if you compile a module and its interface changes then all the modules that import that interface must be re compiled A useful option to alert you when interfaces change is hi diffs It will run diff on the changed interface file before and after when applicable If you are using make GHC can automatically generate the de pendencies required in order to make sure that every module is up to date with respect to its imported interfaces Please see Sec tion 4 6 11 Dependency generation If you are down to your last compile before a bug report we would recommend that you add a dcore lint option for ex tra checking to your compilation options So before you report a bug because of a core dump you should probably g rm o scrub your object files make my_prog re make your program use hi diffs to as mentio
97. Hugs for this package 72 Using GHC cc options ld options framework dirs frameworks haddock interfaces haddock html string list Extra arguments to be added to the gcc command line when this pack age is being used only for via C compilations string list Extra arguments to be added to the gec command line for linking when this package is being used string list On Darwin MacOS X a list of directories containing frameworks for this package This corresponds to the framework path option It is ignored on all other platforms string list On Darwin MacOS X a list of frameworks to link to This corresponds to the framework option Take a look at Apple s developer documentation to find out what frameworks actually are This entry is ignored on all other platforms string list A list of filenames containing Haddock http www haskell org haddock interface files haddock files for this pack age optional string The directory containing the Haddock generated HTML for this package 4 9 Optimisation code improvement The O options specify convenient packages of optimisation flags the options described later on specify individual optimisations to be turned on off the m options specify machine specific optim isations to be turned on off 4 9 1 O convenient packages of optimisation flags There are many options that affect the quality of code produce
98. IN MAIN 0 0 0 0 0 100 0 100 0 main Main 0 0 0 0 0 0 0 0 0 CAF PrelHandle 3 0 0 0 0 0 0 0 0 CAF PrelAddr 1 0 0 0 0 0 0 0 0 CAF Main 6 0 0 0 0 100 0 100 0 main Main 1 0 0 0 0 100 0 100 0 nfib ain 242785 100 0 100 0 100 0 100 0 The first part of the file gives the program name and options and the total time and total memory alloca tion measured during the run of the program note that the total memory allocation figure isn t the same as the amount of live memory needed by the program at any one time the latter can be determined using heap profiling which we will describe shortly The second part of the file is a break down by cost centre of the most costly functions in the program In this case there was only one significant function in the program namely nfib and it was responsible for 100 of both the time and allocation costs of the program The third and final section of the file gives a profile break down by cost centre stack This is roughly a call graph profile of the program In the example above it is clear that the costly call to nfib came from main The time and allocation incurred by a given part of the program is displayed in two ways individual which are the costs incurred by the code covered by this cost centre stack alone and inherited which includes the costs incurred by all the children of this node The usefulness of cost centre stacks is better demonstrated by modifying the example slightly main
99. LEL The FFI support in GHC diverges from the Addendum in the following ways e Syntactic forms and library functions proposed in earlier versions of the FFI are still supported for backwards compatibility e GHC implements a number of GHC specific extensions to the FFI Addendum These extensions are described in Section 8 1 GHC extensions to the FFI Addendum but please note that programs us ing these features are not portable Hence these features should be avoided where possible The FFI libraries are documented in the accompanying library documentation see for example the Foreign module 8 1 GHC extensions to the FFI Addendum The FFI features that are described in this section are specific to GHC Avoid them where possible to not compromise the portability of the resulting code Unboxed types The following unboxed types may be used as basic foreign types see FFI Addendum Section 3 2 Int Word Char Float Double Addr StablePtr a MutableByteArray ForeignObj and ByteArray 8 1 2 Newtype wrapping of the IO monad 8 1 1 The FFI spec requires the IO monad to appear in various places but it can sometimes be convenient to wrap the IO monad in a newt ype thus newtype MyIO a MIO IO a A reason for doing so might be to prevent the programmer from calling arbitrary IO procedures in some part of the program The Haskell FFI already specifies that arguments an
100. Optimisation code improvement cee e cece cece ca eeca ceca cece cena eens ceneeeeseeeeeeeeeeseeeeeaees 73 4 9 1 O convenient packages of optimisation flags 1 0 0 0 ee cece eeceeeceeeeeeeen teen es 73 4 9 2 f platform independent flags 20 0 0 cece eee cece cece ce ne ee ne eeneeeeeeeeeeeeeeeeeeaees 74 4 10 Options related to a particular phase 0 2 0 0 cece eee ec ee ee cee ne cece eens eeueeeeeeeeeeeeeeeeeeeeeaees 75 4 10 1 Replacing the program for one or more phases eee eeeceee cence eect eeeeeeceeeeeeeenees 75 4 10 2 Forcing options to a particular phase sesseeseesssressseresreerrsrerrsrrerreresrrrresreersreet 76 4 10 3 Options affecting the C pre processor ses eseseseieeseesrseetersrtesrrerreerrerrrerrreerreeee 76 4 10 3 1 CPP and string Gaps er a E E ke Ta E a T R E O NS 78 4 10 4 Options affecting a Haskell pre processor seeseesseseseseeeetersrrrsrrerererreerrrererereeeee 78 4 10 5 Options affecting the C compiler if applicable 2 0 00 eeeeeeeeceeeeeeeeeceeneeeene eres 78 4 10 6 Options affecting code generation cece cece ce necc eee eeceeeeeeeeeeseeeeaeeeaeeeaes 79 4 10 7 Options affecting linking seeesssesesesessreresrrerrrrssrrsresrrressreersrrrereresrrreesreerereet 79 4 11 Using Concurrent Haskell s sisien eei eeina r oea E I EEEE SE ESES RAEE REEE EE 81 4 12 Using SMP parallelism sssi eena ee esan E seen eves soes sens senscnsessscns
101. Reading integers Tuples are currently limited to size 100 HOWEVER standard in stances for tuples Eq Ord Bounded Ix Read and Show are available only up to 16 tuples This limitation is easily subvertible so please ask if you get stuck on it GHC s implementation of the Read class for integral types accepts hexadecimal and octal literals the code in the Haskell 98 report doesn t So for example read Oxf00 Int works in GHC A possible reason for this is that readLitChar accepts hex and octal escapes so it seems inconsistent not to do so for integers too 222 Known bugs and infelicities isAlpha The Haskell 98 definition of isAlpha is isAlpha c isUpper c isLower c GHC s implementation diverges from the Haskell 98 definition in the sense that Unicode alphabetic characters which are neither upper nor lower case will still be identified as alphabetic by isAlpha 12 1 2 GHC s interpretation of undefined behaviour in Haskell 98 This section documents GHC s take on various issues that are left undefined or implementation specific in Haskell 98 The Char type Sized integral types Unchecked float arithmetic Following the ISO 10646 standard maxBound Char in GHC is Ox10FFFF In GHC the Int type follows the size of an address on the host architecture in other words it holds 32 bits on a 32 bit machine and 64 bits on a 64 bit machine Arithmetic on Int is unchecked for overflow
102. Show statistics for fast dynamic string usage when finished unreg Enable unregisterised com static pilation 4 17 27 Misc compiler options Flag Description Static Dynamic Reverse Don t complain about hi static fno file mismatches hi version check dno black holing Turn off black holing static probably doesn t work Don t share specialisations static fno method sharing of overloaded functions fhistory size Set simplification history static size funregisterised Unregisterised compilation static use unreg instead fno asm mangling Turn off assembly dynamic mangling use unreg in stead Turn off printing of binding dynamic fno print bind result results in GHCi 112 Chapter 5 Profiling Glasgow Haskell comes with a time and space profiling system Its purpose is to help you improve your understanding of your program s execution behaviour so you can improve it Any comments suggestions and or improvements you have are welcome Recommended profiling tricks would be especially cool Profiling a program is a three step process 1 Re compile your program for profiling with the prof option and probably one of the auto or auto all options These options are described in more detail in Section 5 2 Compiler options for profiling 2 Run your program with one of the profiling options eg RTS p RTS
103. The Glorious Glasgow Haskell Compilation System User s Guide Version 6 6 The GHC Team The Glorious Glasgow Haskell Compilation System User s Guide Version 6 6 The GHC Team Table of Contents The Glasgow Haskell Compiler License 20 00 0000 cece eee ee ccee cence eeece ceca ceca cece eeaeceneece eens cea eeneeeeeeeeeeeeees xi T Introduction to GAG 5 ac svoee vs tok agence ye de ees acai ges EE eE S Noted gasetenesns E TESES SEES ETER ES EREE PES 1 1 1 Meta information Web sites mailing lists etc 00 00 eeeeeeeeeeeeceeeeereecereecereeeereeesereseereeenes 1 1 2 Reporting bugs 1n GHC nessen eies nona ne 6 ea Set see E iad Coe Aaa ea pada a EA N 3 1 2 1 How do I tell if I should report my bug 0 eee eeeeeeececneeee ee eeeeeeceuueeeeeececueseenneeees 3 1 2 2 What to put in a bug report resien ce a ee ce T EEE EEE essa sean AET REES 3 1 3 GHC version numbering Polcyn des Lav egten se E ed aees ood os Gav NE EE eb dasesyoek tees 3 1 4 Release notes Tor Version 6 6 2 3 4 SA ee hel be sed test ea a eT a OENRET 4 1 4 1 User visible compiler changes cssi0 03 cose cs sce snag seviaagissssanassh ooacds ces nncaesenteagssvaaaisass bad 4 14 2 GHC1 Changes prerie neona EE O E A det eeuge EEEE ESE aah 7 143 LIbrAriES oerni n e aa E E E as a e E eE EE EEE 8 1 44 Core Libiaries esineen e oes sig aE EEEE E E EEE A Geen ob sb EE E EE EES 8 PAA E o E R EE E E ees sedan ag Seddsacuas vets capes sated tepiead p
104. The recursive do notation The recursive do notation also known as mdo notation is implemented as described in A recursive do for Haskell Levent Erkok John Launchbury Haskell Workshop 2002 pages 29 37 Pittsburgh Pennsylvania The do notation of Haskell does not allow recursive bindings that is the variables bound in a do expression are visible only in the textually following code block Compare this to a let expression where bound variables are visible in the entire binding group It turns out that several applications can benefit from recursive bindings in the do notation and this extension provides the necessary syntactic support Here is a simple yet contrived example import Control Monad Fix justOnes mdo xs lt Just 1 xs return xs As you can guess justOnes will evaluate to Just 1 1 1 The Control Monad Fix library introduces the MonadF ix class It s definition is class Monad m gt MonadFix m where mfix a gt ma gt ma The function mfix dictates how the required recursion operation should be performed If recursive 139 GHC Language Features bindings are required for a monad then that monad must be declared an instance of the MonadFix class For details see the above mentioned reference The following instances of MonadFix are automatically provided List Maybe IO Furthermore the Control Monad ST and Control Monad ST Lazy modules provide the instances of the MonadFix class
105. UE Asif the appropriate define directive was placed in the source or deftaseiAmME Stop after writing out the intermediate C program to disk The file name for the VALUE intermediate C program is the input file name with hsc replaced with _hsc_make c or help Display a summary of the available flags and exit successfully V or version Output version information and exit successfully The input file should end with hsc it should be plain Haskell source only literate Haskell is not suppor ted at the moment Output files by default get names with the hsc suffix replaced hs Haskell file _hsc h C header 213 Other Haskell utility programs _hsc c C file The C program is compiled using the Haskell compiler This provides the include path to HSFFI h which is automatically included into the C program 10 3 2 Input syntax All special processing is triggered by the operator To output a literal write it twice Inside string literals and comments characters are not processed A is followed by optional spaces and tabs an alphanumeric keyword that describes the kind of pro cessing and its arguments Arguments look like C expressions separated by commas they are not writ ten inside parens They extend up to the nearest unmatched or or to the end of line if it occurs outside any and is no
106. UT NOT LIMITED TO THE IMPLIED WARRANTIES OF MER CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT INDIRECT INCIDENTAL SPECIAL EXEM PLARY OR CONSEQUENTIAL DAMAGES INCLUDING BUT NOT LIMITED TO PROCURE MENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE DATA OR PROFITS OR BUSI NESS INTERRUPTION HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY WHETH ER IN CONTRACT STRICT LIABILITY OR TORT INCLUDING NEGLIGENCE OR OTHER WISE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE xi Chapter 1 Introduction to GHC This is a guide to using the Glasgow Haskell Compiler GHC an interactive and batch compilation sys tem for the Haskell 98 http www haskell org language GHC has two main components an interactive Haskell interpreter also known as GHCi described in Chapter 3 Using GHCi and a batch compiler described throughout Chapter 4 Using GHC In fact GHC consists of a single program which is just run with different options to provide either the interact ive or the batch system The batch compiler can be used alongside GHCi compiled modules can be loaded into an interactive session and used in the same way as interpreted code and in fact when using GHCi most of the library code will be pre compiled This means you get the best of both worl
107. Unregisterised compilation 00 0 0 cece cece nee cn cece cece cece cena cen eeneeeeeeeeeeeeeeeeeeae es 99 ANT Plas TELSTEN CE e n sane syeted susp Soumaneys E EEE E E lobe E nvetegees weaeshute leben E E tere 99 4 17 1 Help and verbosity Options 20 0 0 cece cece cece ce eece cece cece eens eeneeeneeeeeeeeeeeeeeeeseaees 99 417 2 Which phasesto nun arie toes Ses sabe toeeteest E a dos ntaet ter Raccegsh Benstoee 100 4 17 3 Alternative modes of Operation cece ce cece cece eee eceeeeeeeeeeeeeeeeaeeea essa eeaaeeaes 100 4 17 4 Redirecting Output eiker eeren acdsee aE AEE a TESE ITE E E EE pa i oiee 101 4 17 5 Keeping intermediate files sssi nsnur e EE E ESEE eE 101 4 17 6 Temporary Giles cssccsste saat eea TE E E Ee s o EEE EEES 101 417 7 Finding Imports seiecsen esi ter eae ES E E EEE E EEEE SEES 102 4 17 8 Interface fle options sssissro ipress ots capsasiawas sevcesg ao PESEE PORSE SPISAET hoses savas sess 102 4 17 9 Recompilation checking esssseessseesieressserrssrerrrrrserrresrrrrsrrerrerreereresrrererererert 102 4 17 10 Interactive mode Options sirena e ar E A E ET E E E eeaaeeaes 102 Ae VTA Is P cka tes iino eeo n e othe ph AEA E ap A AEE AE Aa 102 4 17 12 Language Options so e eea EE E E EE EE ETE E EEO a ae 103 1713 Warmin eS osse ae e a a E a E E EE EE 104 417 14 Optimisation Jevels nne an n a bec a E N Naa 105 4 17 15 Individ al optimisations sepsy oiya epe ng oti EEA TORSE E E pa 106 4 17 16 Pr
108. a for creating a set of bindings that we want to repeatedly load into the GHCi session Prelude gt def readFil Prelude gt cmds ghci Notice that we named the command by analogy with the lt Unix shell com mand that does the same thing Opens an editor to edit the file file or the most recently loaded module if file is omitted The editor to invoke is taken from the EDITOR environment variable or a default editor on your system if EDITOR is not set You can change the editor us ing set editor Displays a list of the available commands Displays information about the given name s For example if name is a class then the class methods and their types will be printed if name is a type constructor then its definition will be printed if name is a function then its type will be printed If name has been loaded from a source file then GHCi will also display the location of its definition in the source Recursively loads the specified modules and all the modules they depend on Here each module must be a module name or filename but may not be the name of a module in a package All previously loaded modules except package modules are forgotten The new set of modules is known as the target set Note that Load can be used without any ar guments to unload all the currently loaded modules and bindings After a Load command the current context is set to e module if it was loaded successfully
109. a where class C a where Here neither D nor T is declared in module Orphan We call such modules orphan modules defined thus e An orphan module contains at least one orphan instance or at least one orphan rule e An instance declaration in a module M is an orphan instance if none of the type constructors or classes mentioned in the instance head the part after the gt are declared in M Only the instance head counts In the example above it is not good enough for C s declaration to be in module A it must be the declaration of D or T e A rewrite rule in a module M is an orphan rule if none of the variables type constructors or classes that are free in the left hand side of the rule are declared in M GHC identifies orphan modules and visits the interface file of every orphan module below the module being compiled This is usually wasted work but there is no avoiding it You should therefore do your best to have as few orphan modules as possible You can identify an orphan module by looking in its interface file M hi using the show i face If there is a on the first line GHC considers it an orphan module 4 7 Warnings and sanity checking GHC has a number of options that select which types of non fatal error messages otherwise known as warnings can be generated during compilation By default you get a standard set of warnings which are generally likely to indicate bugs in your program These are Ewarn overl
110. ack a less than useful error message an assertion failed but which and where One way out is to define an extended assert function which also takes a descriptive string to include in the error message and perhaps combine this with the use of a pre processor which inserts the source location where assert was used Ghce offers a helping hand here doing all of this for you For every use of assert in the user s source kelvinToC Double gt Double kelvinToC k assert k gt 0 0 k 273 15 Ghce will rewrite this to also include the source location where the assertion was made assert pred val gt assertError Main hs 15 pred val The rewrite is only performed by the compiler when it spots applications of Con trol Exception assert so you can still define and use your own versions of assert should you so wish If not import Control Exception to make use assert in your code GHC ignores assertions when optimisation is turned on with the O flag That is expressions of the form assert pred e will be rewritten to e You can also disable assertions using the fignore asserts option Assertion failures can be caught see the documentation for the Cont rol Exception library for the details Pragmas GHC supports several pragmas or instructions to the compiler placed in the source code Pragmas don t normally affect the meaning of the program but they might affect the efficiency of the generated code Pragmas a
111. ack to the C com piler dynamic 4 17 25 External core file options Section 4 15 Generating and compiling External Core Files Flag Description Static Dynamic Reverse Generate hcr external Core files fext core static 4 17 26 Compiler debugging options 110 Using GHC Section 4 16 Debugging the compiler Flag Description Static Dynamic Reverse dceore lint Turn on internal sanity dynamic checking ddump asm Dump assembly dynamic ddump bcos Dump interpreter byte code dynamic ddump cmm Dump C output dynamic ddump cpranal Dump output from CPR dynamic analysis ddump cse Dump CSE output dynamic ddump deriv Dump deriving output dynamic ddump ds Dump desugarer output dynamic ddump flatc Dump flat C dynamic ddump foreign Dump foreign export dynamic stubs ddump inlinings Dump inlining info dynamic ddump occur anal Dump occurrence analysis dynamic output ddump opt cmm Dump the results of C to dynamic C optimising passes ddump parsed Dump parse tree dynamic ddump prep Dump prepared core dynamic ddump rn Dump renamer output dynamic ddump rules Dump rules dynamic ddump simpl Dump final simplifier out put dynamic ddump sim pl iterations Dump output from each simplifier iteration dynamic dd
112. age collections and less promotion With only 1 generation G1 the A option specifies the minimum allocation area since the actual size of the allocation area will be resized according to the amount of data in the heap see F below Use a compacting algorithm for collecting the oldest generation By default the old est generation is collected using a copying algorithm this option causes it to be com pacted in place instead The compaction algorithm is slower than the copying al gorithm but the savings in memory use can be considerable For a given heap size using the H option compaction can in fact reduce the GC cost by allowing fewer GCs to be performed This is more likely when the ratio of live data to heap size is high say gt 30 NOTE compaction doesn t currently work when a single generation is requested us ing the G1 option Default 30 Automatically enable compacting collection when the live data exceeds n of the maximum heap size see the M option Note that the maximum heap size is unlimited by default so this option has no effect unless the maximum heap size is set with Msi ze Default 2 This option controls the amount of memory reserved for the older gener 85 Using GHC Ggenera tions Hsize Iseconds ksize Ksize ations and in the case of a two space collector the size of the allocation area as a factor of the amount of live data For example if there was 2M of li
113. aks can often be eliminated by forcing the relevant computations to be performed eagerly using seq or strictness an notations on data constructor fields Often a particular data structure is being retained by a chain of unevaluated closures only the nearest of which will be reported by retainer profiling for example A retains B B retains C and C retains a large structure There might be a large number of Bs but only a single A so A is really the one we re inter ested in eliminating However retainer profiling will in this case report B as the retainer of the large structure To move further up the chain of retainers we can ask for another retainer profile but this time restrict the profile to B objects so we get a profile of the retainers of B prog RTS hr hcB This trick isn t foolproof because there might be other B closures in the heap which aren t the retainers we are interested in but we ve found this to be a useful technique in most cases 5 4 3 Biographical Profiling A typical heap object may be in one of the following four states at each point in its lifetime e The lag stage which is the time between creation and the first use of the object e the use stage which lasts from the first use until the last use of the object and e The drag stage which lasts from the final use until the last reference to the object is dropped e An object which is never used is said to be in the void state for its whole lifetime A b
114. al Modules aynen a prne e EEEE pe ese gees NE ITERE 137 7 3 2 Pattem p ardS o ae E E E A S TC A ERTS 137 133s TNE recursive CO NOLATION merre n e a a EE R n E A EAT Rees toes 139 7 3 4 Parallel List Comprehensions sseseseseesseersrrerrsrrsrreresrrerssrerrsrreereresrreresreerereet 140 3 52 Rebindable syntax e cs 0d ss cts scvedesiss soins ds eE EE svesdsasebanias shoieodsres mages yore sess SaS EES 141 7 320 POSTAX op ratorS siyi renns a a ge geian ye sean ees desenes EFESE S EOE E EE 141 TA Type system eXtensionS orire n eie E E e o E A EE E E GE e 142 7 4 1 Data types and type Synonyms s ssesessesssserererestsrerestsrererrrrsesrererereresrsreseseereee 142 7 4 1 1 Data types with no constructors eesesesseeeeesesesesreresesrerrerrrsesreresesrerese 142 7 4 1 2 Infix type constructors classes and type variables ceeeeeeeeeereeeeereees 142 7 4 1 3 Liberalised type SYNONYMS sisesed ei e EEE E a E a SE 143 7 4 1 4 Existentially quantified data constructors s eeeseeeeseseerereeeresesreresesrereee 144 74 2 Class deClatavoms sensin once sae S E E Ey TEE basen E TENO SEAS 149 7 4 2 1 Multi parameter type classes pciesrsniie ensenen etsene nenien Es 149 7 4 2 2 The superclasses of a class declaration esseesseeerrseesrrrrerrerrsrrerrereseeee 149 TA2Z3 3 Class method tY PCS ea r Re EE EEE NS EENE EES 149 7 4 3 Functional dependencies sseeesseesrreeesrerrsrreersrrerrere
115. al unix environment and finding things like bin and usr include without ever explicitly bothering with their actual location on the windows system probably C cygwin bin and C cygwin usr include 11 4 2 The problem GHC by default no longer depends on cygwin but is a native windows program It is built using mingw and it uses mingw s ghc while compiling your Haskell sources even if you call it from cygwin s bash but what matters here is that just like any other normal windows program neither GHC nor the executables it produces are aware of cygwin s pretended unix hierarchy GHC will happily accept either or as path separators but it won t know where to find home joe Main hs or bin bash or the like This causes all kinds of fun when GHC is used from within cygwin s bash or in make sessions running under cygwin 11 4 3 Things to do e Don t use absolute paths in make configure amp co if there is any chance that those might be passed to GHC or to GHC compiled programs Relative paths are fine because cygwin tools are happy with them and GHC accepts as path separator And relative paths don t depend on where cygwin s root directory is located or on which partition or network drive your source tree happens to reside as long as you cd there first e If you have to use absolute paths beware of the innocent looking ROOT pwd in makefile hier archies or configure scripts cygwin provides a tool called cygpath
116. all inline f arranges that f is inlined regardless of its size More precisely the call inline f rewrites to the right hand side of f s definition This allows the programmer to control inlining from a particular call site rather than the definition site of the function c f INLINE pragmas Section 7 10 3 INLINE and NOINLINE pragmas This inlining occurs regardless of the argument to the call or the size of f s definition it is uncondition al The main caveat is that f s definition must be visible to the compiler That is must be let bound in the current scope If no inlining takes place the inline function expands to the identity function in Phase zero so its use imposes no overhead If the function is defined in another module GHC only exposes its inlining in the interface file if the function is sufficiently small that it might be inlined by the automatic mechanism There is currently no way to tell GHC to expose arbitrarily large functions in the interface file This shortcoming is something that could be fixed with some kind of pragma 7 12 2 The lazy function The lazy function restrains strictness analysis a little lazy a gt a The call lazy e means the same as e but Lazy has a magical property so far as strictness analysis is concerned it is lazy in its first argument even though its semantics is strict After strictness analysis has run calls to Lazy are inlined to be the identity function This
117. altered to make it more efficient There are also new functions mallocPlainForeignPtr and mallocPlainForeignPtrBytes which do not allow you to attach a finalizer to the ForeignPtr e The Text Regex and Text Regex Posix modules have been removed Instead use the new regex compat package for a drop in Text Regex replacement or the new library in the new regex posix package 1 4 4 2 Cabal e Version number 1 1 6 was 1 1 4 e Support for JHC symmetric to the support for the other implementations has been added throughout e Support for object splitting and building in place has been added throughout e Added a debianTemplate directory with templates for building Debian packages from Cabal packages e There are now modules Distribution Simple compiler for each of GHC NHC Hugs and JHC The Distribution Simple Build and Distribution Simple Install mod ules have shrunk correspondingly e Distribution GetOpt is no longer a visible module e Distribution Simple exports a function defaultMainArgs which is identical to de faultMain except that the arguments are given as a list of strings rather than being retrieved with getArgs e Distribution Simple Configure no longer exports LocalBuildiInfo but does now export configDependency and configCompilerAux e Distribution Simple LocalBuildInfo now exports mkHaddockDir distPref srcPref autogenModulesDir and mkIncludeDir e Distribution PackageDe
118. am that gobbles less heap space I think I have a space leak Re run your program with RTS Sstderr and remove all doubt You ll see the heap usage get bigger and bigger Hmmm this might be even easier with the G1 RTS option so a out RTS Sstderr G1 Once again the profiling facilities Chapter 5 Profiling are the basic tool for demystifying the space behaviour of your program Strict functions are good for space usage as they are for time as discussed in the previous section Strict functions get right down to business rather than filling up the heap with closures the system s notes to itself about how to evaluate something should it eventually be required 132 Chapter 7 GHC Language Features As with all known Haskell systems GHC implements some extensions to the language They are all en abled by options by default GHC understands only plain Haskell 98 Some of the Glasgow extensions serve to give you access to the underlying facilities with which we im plement Haskell Thus you can get at the Raw Iron if you are willing to write some non portable code at a more primitive level You need not be stuck on performance because of the implementation costs of Haskell s high level features you can always code under them In an extreme case you can write all your time critical code in C and then just glue it together with Haskell Before you get too carried away working a
119. ample invoke ghc c 01 Foo hs 02 Bar hs to apply different op timisation levels to the files Foo hs and Bar hs command line options in source files Sometimes it is useful to make the connection between a source file and the command line options it re quires quite tight For instance if a Haskell source file uses GHC extensions it will always need to be compiled with the fglasgow exts option Rather than maintaining the list of per file options in a Makefile it is possible to do this directly in the source file using the OPTIONS_GHC pragma OPTIONS_GHC fglasgow exts module X where OPTIONS_GHC pragmas are only looked for at the top of your source files upto the first non literate non empty line not containing OPTIONS_GHC Multiple OPTIONS_GHC pragmas are re cognised Do not put comments before or on the same line as the OPT IONS_GHC pragma Note that your command shell does not get to the source file options they are just included literally in the array of command line arguments the compiler maintains internally so you ll be desperately disap pointed if you try to glob etc inside OPTIONS_GHC NOTE the contents of OPTIONS_GHC are prepended to the command line options so you do have the ability to override OPTIONS_GHC settings via the command line It is not recommended to move all the contents of your Makefiles into your source files but in some cir cumstances the OPTIONS_GHC pragma is the Right Thing If you
120. and then we use seq to force the parent thread to evaluate nfib n 2 before going on to add together these two subexpressions In this divide and conquer approach we only spark a new thread for one branch of the computation leaving the parent to evaluate the other branch Also we must use seq to ensure that the parent will evaluate n2 before n1 in the expression n1 n2 1 Itis not sufficient to reorder the expression as n2 n1 1 because the compiler may not generate code to evaluate the addends from left to right When using par the general rule of thumb is that the sparked computation should be required at a later time but not too soon Also the sparked computation should not be too small otherwise the cost of forking it in parallel will be too large relative to the amount of parallelism gained Getting these factors right is tricky in practice More sophisticated combinators for expressing parallelism are available from the Con trol Parallel Strategies libraries base Control Parallel Strategies html module This module builds functionality around par expressing more elaborate patterns of parallel computation such as parallel map 204 Chapter 8 Foreign function interface FFI GHC mostly conforms to the Haskell 98 Foreign Function Interface Addendum 1 0 whose definition is available from http haskel1l org To enable FFI support in GHC give the ff fiflag or the fglasgow exts flag which implies E
121. apping patterns fwarn deprecations fwarn duplicate exports fwarn missing fields and fwarn missing methods The following flags are simple ways to select standard packages of warnings W Provides the standard warnings plus fwarn incomplete patterns fwarn unused matches fwarn unused imports and fwarn unused binds w Turns off all warnings including the standard ones 57 Using GHC Wall Turns on all warning options 58 Using GHC Makes any warning into a fatal error Useful so that you don t miss warnings when doing batch compilation The full set of warning options is described below To turn off any warning simply give the correspond ing fno warn option on the command line fwarn deprecations Causes a warning to be emitted when a deprecated function or type is used Entities can be marked as deprecated using a pragma see Section 7 10 1 DEPRECATED pragma 59 Using GHC fwarn hi shadowing fwarn incom plete patterns fwarn incom plete record updates fwarn missing fields fwarn missing methods Have the compiler warn about duplicate entries in export lists This is useful information if you maintain large export lists and want to avoid the continued export of a definition after you ve de leted one mention of it in the export list This option is on by default Causes the compiler to emit a warning
122. are not in cluded please tell us If you want to write your own good consumers or producers look at the Prelude definitions of the above functions to see how to do so 7 11 4 Specialisation Rewrite rules can be used to get the same effect as a feature present in earlier versions of GHC For ex ample suppose that genericLookup Ord a gt Table ab er el gt int Lookup Bee Table Int b gt Int gt b where int Lookup is an implementation of genericLookup that works very fast for keys of type Int You might wish to tell GHC to use int Lookup instead of genericLookup whenever the lat ter was called with type Table Int b gt Int gt b It used to be possible to write SPECIALIZE genericLookup Table Int b gt Int gt b intLookup This feature is no longer in GHC but rewrite rules let you do the same thing RULES genericLookup Int genericLookup intLookup This slightly odd looking rule instructs GHC to replace genericLookup by int Lookup whenever the types match What is more this rule does not need to be in the same file as genericLookup un like the SPECIALIZE pragmas which currently do so that they have an original definition available to specialise It is Your Responsibility to make sure that int Lookup really behaves as a specialised version of gen ericLookup An example in which using RULES for specialisation will Win Big toDouble Real a gt a g
123. are therefore in force in any module that im ports the module that defined the rule directly or indirectly That is if A imports B which imports C then C s rules are in force when compiling A The situation is very similar to that for instance de clarations 195 GHC Language Features 7 11 3 List fusion The RULES mechanism is used to implement fusion deforestation of common list functions If a good consumer consumes an intermediate list constructed by a good producer the intermediate list should be eliminated entirely The following are good producers e List comprehensions e Enumerations of Int and Char e g a z e Explicit lists e g True False e The cons constructor e g 3 4 e map e take filter e iterate repeat e zip zipWith The following are good consumers e List comprehensions e array on its second argument e length e on its first argument e foldr map e take filter concat e unzip unzip2 unzip3 unzip4 e zip zipWith but on one argument only if both are good producers zip will fuse with one but not the other e partition e head e and or any all s sequence _ 196 GHC Language Features e msum sortBy So for example the following should generate no intermediate lists array 1 10 i i i i lt map 1 0 9 This list could readily be extended if there are Prelude functions that you use a lot which
124. askell so you ve got to bootstrap it somehow We provide machine gen erated C files from Haskell for this purpose but it s really quite a pain to use them If you must build GHC from its sources using a binary distributed GHC to do so is a sensible way to proceed For the oth er fptools programs many are written in Haskell so binary distributions allow you to install them without having a Haskell compiler This guide is in several parts e Installing on Unix a likes Section 2 1 Installing on Unix a likes e Installing on Windows Section 2 2 Installing on Windows e The layout of installed files Section 2 3 The layout of installed files You don t need to know this to install GHC but it s useful if you are changing the implementation 2 1 Installing on Unix a likes 2 1 1 When a platform specific package is available 2 1 2 For certain platforms we provide GHC binaries packaged using the native package format for the plat form This is likely to be by far the best way to install GHC for your platform if one of these packages is available since dependencies will automatically be handled and the package system normally provides a way to uninstall the package at a later date We generally provide the following packages RedHat or SuSE Linux x86 RPM source amp binary packages for RedHat and SuSE Linux x86 only are available for most major releases Debian Linux x86 Debian packages for Linux x86 only
125. askell org ghc docs latest html libraries index html search for the type ExpQ Temporary many changes to the original design are described in ht tp research microsoft com simonpj tmp notes2 ps http research microsoft com simonpj tmp notes2 ps Not all of these changes are in GHC 6 6 The first example from that paper is set out below as a worked example to help get you started The documentation here describes the realisation in GHC It s rather sketchy just now Tim Sheard is going to expand it Syntax Template Haskell has the following new syntactic constructions You need to use the flag fthto switch these syntactic extensions on fth is no longer implied by fglasgow exts e A splice is written x where x is an identifier or where the is an arbitrary expres 176 GHC Language Features sion There must be no space between the and the identifier or parenthesis This use of over rides its meaning as an infix operator just as M x overrides the meaning of as an infix operator If you want the infix operator put spaces around it A splice can occur in place of e an expression the spliced expression must have type Q Exp e alist of top level declarations the spliced expression must have type Q Dec e Planned but not implemented yet a type the spliced expression must have type Q Typ Note that the syntax for a declaration splice uses not splice as in the
126. at case GHC will never discover a dependency on it so it has to be mentioned explicitly Ignore the exposed flag on installed packages and hide them all by default If you use this flag then any packages you require including base need to be explicitly exposed using package options This is a good way to insulate your program from differences in the glob ally exposed packages and being explicit about package dependencies is a Good Thing Cabal always passes the hide all packages flag to GHC for exactly this reason This option does the opposite of package it causes the specified pack age to be hidden which means that none of its modules will be available for import by Haskell import directives Note that the package might still end up being linked into the final pro gram if it is a dependency direct or indirect of another exposed package Causes the compiler to behave as if package P and any packages that de pend on P are not installed at all Saying ignore package P is the same as giving hide package flags for P and all the packages that depend on P Sometimes we don t know ahead of time which packages will be installed that depend on P which is when the ignore package flag can be useful Consequences of packages It is possible that by using packages you might end up with a program that contains two modules with 65 Using GHC 4 8 3 the same name perhaps you used a package P that has a hidden mod
127. at the moment see Section 11 5 1 Creating a DLL for the gory details Building a static library is done by using the ar tool like so ar cqs libHSfoo a A o B o C o where A o B o and so on are the compiled Haskell modules and 1ibHSfoo a is the library you wish to create The syntax may differ slightly on your system so check the documentation if you run into difficulties Versions of the Haskell libraries for use with GHCi may also be included GHCi cannot load a files directly instead it will look for an object file called HS foo o and load that On some systems the ghc pkg tool can automatically build the GHCi version of each library see Section 4 8 5 Package management the ghc pkg command To build these libraries by hand from the a archive it is possible to use GNU Id as follows ld r whole archive o HSfoo o libHSfoo a replace whole archive with all_load on MacOS X GHC does not maintain detailed cross package dependency information It does remember which modules in other packages the current module depends on but not which things within those impor ted things To compile a module which is to be part of a new package use the package name option package name foo This option is added to the command line when compiling a module that is destined to be part of package foo If this flag is omitted then the default 67 Using GHC 4 8 5 package main is assumed Note the argument
128. atching someTerm Term Bool someTerm IsZero arg Succ num Lit val 0 eval Term a gt a eval Lit val i i eval Succ num t eval t 1 eval Pred num t eval t 1 eval IsZero arg t eval t 0 eval Pair argl tl arg2 t2 eval t1 eval t2 eval t If if eval cnd t then eval tru t else eval fls t You can use strictness annotations in the obvious places in the constructor type data Term a where Lit 2 PINE gt Term Int TFE Term Bool gt Term a gt Term a gt Term a Pair Term a gt Term b gt Term a b You can use a deriving clause on a GADT style data type declaration but only if the data type could also have been declared in Haskell 98 syntax For example these two declarations are equival ent data Maybel a where Nothingl Maybel a Justl a gt Maybel a deriving Eq Ord data Maybe2 a Nothing2 Just2 a deriving Eq Ord This simply allows you to declare a vanilla Haskell 98 data type using the where form without los 175 GHC Language Features ing the deriving clause e Pattern matching causes type refinement For example in the right hand side of the equation eval Term a gt a eval Lit i the type a is refined to Int That s the whole point A precise specification of the type rules is beyond what this user manual aspires to but there is a paper about
129. ation checker may get confused at the least you won t get any recompilation avoidance We recommend using a combination of hidir and hisuf options instead if possible To avoid generating an interface at all you could use this option to redirect the in terface into the bit bucket ohi dev nu11 for example hidir dir Redirects all generated interface files into dir instead of the default stubdir dir Redirects all generated FFI stub files into dir Stub files are generated when the Haskell source contains a foreign export or foreign import swrapper declaration see Section 8 2 1 Using foreign export and foreign import ccall wrapper with GHC The stubdir option behaves in exactly the same way as odir and hidir with respect to hierarch ical modules osuf suffix The osuf suffix will change the o file suffix for object files to whatever hisuf suffix you specify We use this when compiling libraries so that objects for the profiling hesuf suf versions of the libraries don t clobber the normal ones fix Similarly the hisuf suffix will change the hi file suffix for non system interface files see Section 4 6 7 Other options related to interface files Finally the option hcsuf suffix will change the hc file suffix for compiler generated intermediate C files The hisuf osuf game is particularly useful if you want to compile a pro gram both with and without profiling in the same director
130. ations of class Storable for a given C struct see the For eign Storable module in the library documentation Similarly for poke It will have the type Storable b gt Ptr a gt b gt IO Makes a pointer to a field struct It will have the type Ptr a gt Ptr b Computes the offset in bytes of field in struct_type It will have type Int Computes the size in bytes of struct_type It will have type Int A shortcut for multiple definitions which use const Each value is a name of a C integer constant e g enumeration value The name will be translated to Haskell by making each letter fol lowing an underscore uppercase making all the rest lowercase and removing underscores You can supply a different translation by writing hs_name c_value instead of a value in which case c_value may be an arbitrary expression The hs_name will be defined as having the specified type Its definition is the specified constructor which in fact may be an expression or be empty applied to the appropriate integer value You can have multiple enum definitions with the same type this construct does not emit the type definition itself 10 3 3 Custom constructs type peek poke and ptr are not hardwired into the hse2hs but are defined in a C template that is included in the C program template hsc h Custom constructs and templates can be used too Any construct with unknown key is expected to be handle
131. auses can be included in the definition of classes to add in formation about dependencies between parameters as in the following examples a gt b where a gt b b gt a where class class m o ab ab The notation a gt b used here between the and where symbols not to be confused with a function type indicates that the a parameter uniquely determines the b parameter and might be read as a de termines b Thus D is not just a relation but actually a partial function Similarly from the two de pendencies that are included in the definition of E we can see that E represents a partial one one map ping between types More generally dependencies take the form x1 xn gt yl ym where xl xn and yl yn are type variables with n gt 0 and m gt 0 meaning that the y parameters are uniquely determined by the x parameters Spaces can be used as separators if more than one variable appears on any single side of a dependency asin t gt a b Note that a class may be annotated with multiple dependencies us ing commas as separators as in the definition of E above Some dependencies that we can write in this notation are redundant and will be rejected because they don t serve any useful purpose and may in stead indicate an error in the program Examples of dependencies like this include a gt a a gt a a a gt etc There can also be some redundancy if multiple dependencies are giv
132. ave the same length as the shortest branch We can define parallel list comprehensions by translation to regular comprehensions Here s the basic idea Given a parallel comprehension of the form e pl lt ell p2 lt e12 ql lt e21 q2 lt e22 This will be translated to e pl p2 ql q2 lt zipN pl p2 pl lt ell p2 lt el2 ql q2 ql lt e21 q2 lt e22 140 GHC Language Features 7 3 5 7 3 6 where zipN is the appropriate zip for the given number of branches Rebindable syntax GHC allows most kinds of built in syntax to be rebound by the user to facilitate replacing the Pre lude with a home grown version for example You may want to define your own numeric class hierarchy It completely defeats that purpose if the lit eral 1 means Prelude fromInteger 1 which is what the Haskell Report specifies So the fno implicit prelude flag causes the following pieces of built in syntax to refer to whatever is in scope not the Prelude versions e An integer literal 368 means fromInteger 368 Integer rather than Pre lude fromInteger 368 Integer e Fractional literals are handed in just the same way except that the translation is fromRational 3 68 Rational e The equality test in an overloaded numeric pattern uses whatever is in scope e The subtraction operation and the greater than or equal test in n k patterns use whateve
133. ax the types of functions used by sugar such as do notation numeric literal patterns need not match the types of the Prelude functions normally used The InstalledPackagelInfo syntax has changed Now instead of ext ra libs we have ex tra libraries instead of extra hugs opts we have hugs options instead of ex tra cc opts we have cc options instead of extra ld opts we have 1d options and instead of extra frameworks we have frameworks See Section 4 8 6 Installed PackagelInfo a package specification for details If you newt ype the IO monad e g newtype MyIO a MyIO IO a then GHC will now allow you to have FFI calls return MyIO t rather than just IO t See Sec tion 8 1 2 Newtype wrapping of the IO monad GHC s mechansim for deriving user defined classes for newtypes has been further generalised to multi parameter type classes and higher kinded types See Section 7 4 12 Generalised derived in stances for newtypes By default pattern bindings in GHC are now monomorphic This means that some valid Haskell 98 programs will get rejected but we believe they will be few in number To revert to the old behaviour use the fno mono pat binds flag More details are in Section 7 1 Language options 6 Introduction to GHC 1 4 2 e GHCi already does more defaulting than Haskell 98 so that for example reverse shows a result rather than giving an ambiguous type variable error There
134. ay Bb ao Pes Unit where a and b are type variables Furthermore all the type patterns for a single type constructor say must be identical they must use the same type variables So this is illegal class Foo a where op a gt Bool op a b Inl x True op p q Inr y False The type patterns must be identical even in equations for different methods of the class So this too is illegal 201 GHC Language Features class Foo a where opl a gt Bool Oph adorn be i OS es yy S Tieue op2 a gt Bool op2 p gq x y False The reason for this restriction is that we gather all the equations for a particular type consructor into a single generic instance declaration e A generic method declaration must give a case for each of the three type constructors e The type for a generic method can be built only from e Function arrows e Type variables e Tuples e Arbitrary types not involving type variables Here are some example type signatures for generic methods opl a gt Bool op2 Bool gt a Bool op3 Int gt a gt a op4 a gt Bool Here opl op2 op3 are OK but op4 is rejected because it has a type variable inside a list This restriction is an implementation restriction we just havn t got around to implementing the ne cessary bidirectional maps over arbitrary type constructors It would be relatively easy to add specif
135. b c contains the compiler generated definition of foo To invoke foo from C just include Foo_stub h and call foo The foo_stub c and foo_stub h files can be redirected using the stubdir option see Sec tion 4 6 4 Redirecting the compilation output s 8 2 1 1 Using your own main Normally GHC s runtime system provides a main which arranges to invoke Main main in the Haskell program However you might want to link some Haskell code into a program which has a main function written in another language say C In order to do this you have to initialize the Haskell runtime system explicitly Let s take the example from above and invoke it from a standalone C program Here s the C code include lt stdio h gt include HsFFI h ifdef _ GLASGOW_HASKELL _ include foo_stub h endif 206 Foreign function interface FFI ifdef _ GLASGOW_HASKELL _ extern void __stginit_Foo void endif int main int argc char argv int i hs_init amp argce amp argv ifdef _ GLASGOW_HASKELL _ hs_add_root __stginit_Foo endif for i O i lt 5 itt printf Sd n foo 2500 hs_exit return 0 We ve surrounded the GHC specific bits with ifdef __GLASGOW_HASKELL__ the rest of the code should be portable across Haskell implementations that support the FFI standard The call to hs_init initializes GHC s runtime system Do NOT try to invoke
136. c Finally note that the syntax of the type patterns in the class declaration uses and brackets curly braces alone would ambiguous when they appear on right hand sides an extension we anticipate wanting 7 13 3 Terminology and restrictions Terminology A generic default method in a class declaration is one that is defined using type patterns as above A polymorphic default method is a default method defined as in Haskell 98 A generic class declaration is a class declaration with at least one generic default method Restrictions e Alas we do not yet implement the stuff about constructor names and field labels e A generic class can have only one parameter you can t have a generic multi parameter class e A default method must be defined entirely using type patterns or entirely without So this is illegal class Foo a where op a gt a Bool op Unit Unit Unit True op x x False However it is perfectly OK for some methods of a generic class to have generic default methods and others to have polymorphic default methods e The type variable s in the type pattern for a generic method declaration scope over the right hand side So this is legal note the use of the type variable p in a type signature on the right hand side class Foo a where op a gt Bool Opt sp Bes qld Ge ep we Sep ke tee pp e The type patterns in a generic default method must take one of the forms
137. c expression which is a new sort of thing called a command The syntax of commands is as follows cmd exp lt exp exp lt lt exp cmd with cma up to cmd defined using infix operators as for expressions and 179 GHC Language Features 7 1 1 10 cmd apat apat gt cmd let decls in cmd if exp then cmd else cmd case exp of calts do cstmt cstmt cmd fcmd fecemd fomd aexp cmd aexp cmd cmd cstmt let decis pat lt cmd rec cstmt 3 2 cestmt 3 F cmd where calts are like alts except that the bodies are commands instead of expressions Commands produce values but like monadic computations may yield more than one value or none and may do other things as well For the most part familiarity with monadic notation is a good guide to using commands However the values of expressions even monadic ones are determined by the values of the variables they contain this is not necessarily the case for commands A simple example of the new notation is the expression proc x gt f lt x tl We call this a procedure or arrow abstraction As with a lambda expression the variable x is a new variable bound within the proc expression It refers to the input to the arrow In the above example lt is not an identifier but an new reserved symbol used for building commands from an expression of ar row type and an expression to be fed as input to that
138. c classes Independent of fglasgow exts GHC normally imports Prelude hi files for you If you d rather it didn t then give it a fno implicit prelude option The idea is that you can then import a Prelude of your own But don t call it Prelude the Haskell module namespace is flat and you must not conflict with any Prelude module Even though you have not imported the Prelude most of the built in syntax still refers to the built in Haskell Prelude types and val ues as specified by the Haskell Report For example the type Int still means Prelude Int tuples continue to refer to the standard Prelude tuples the translation for list comprehensions continues to use Prelude map etc However fno implicit prelude does change the handling of certain built in syntax see Section 7 3 5 Rebindable syntax Enables implicit parameters see Section 7 4 6 Implicit paramet ers Currently also implied by fglasgow exts Syntax stolen varid svarid Enables lexically scoped type variables see Section 7 4 10 Lexically scoped type variables 7 Implied by fglasgow exts Enables Template Haskell see Section 7 6 Template Haskell This flag must be given explicitly it is no longer implied by fglasgow exts Syntax stolen el pl dl tl Svarid 7 2 Unboxed types and primitive operations GHC is built on a raft of primitive data types and operations While you reall
139. ce ecn cena cena eeneeeneeeenees 185 7 8 2 Syntax and SOMANUCS sisis nisou ries errr e desu ye EE EE E EESE E EE E ai 186 EY ASSCLUONS cr rni a p E E a e e E Ea E e E EET sss 187 FAO Pragmas errete aaee aooe EE E EE EEEE ENE EEE EEEE EEEIEE EE 187 1 10 1 DEPRECATED pragma s 3foccscse gst oti esseossens syyiang decdwaciassotaads be sseeaesvbtesggevasieses 188 70 2 INCLUDE pragma sc css netos ena tegen guessssa sed O EEE R eccose espe SEIO EES 188 7 10 3 INLINE and NOINLINE pragmas 1 0 20 00 eee cece cece eeece ceca cena cena eeueeeneeennees 188 TAO 3 Vs INEINE pratima secs secishos ences Seder aE p E E ote pe Setes teced bees wualss Soot 189 7 10 3 2 NOINEINE pragma vorenn e E E bh E Er EE E EE EEEO RE S 189 T410 3 3 Phase Control secese e e e e a E E e ES 189 L104 LANGUAGE pragma x ce3 coteiss eednlcsevelivace des es bade sd EE EE Gaede y 190 TAOS SEINE pragma apeere ood ese eshiges soa Sud EEEE p OEE NE ANERE 190 7 10 6 OPTIONS_GHC pragma 1 2 0 0 cece cee cence eS EEn E E EEES EESE Eni 191 TFIO7 RULES pragma ik eere ss packadardottassabontesiacstas tsa bochadetdettasn ssoib sks aicag bes Ea a 191 7 10 8 SPECIALIZE pragma icc s ccbicseceses ees osvigecdde in TTE ETRE sss ETRE EKETE IE EE EEAS 191 7 10 9 SPECIALIZE instance pragma spess oid rera usb oseg asses basses E EE TER E OEE e io Eee 192 7 10 10 UNPACK pragma iensor E E EE AE EES nua O Ei 192 TAL R writ rules iiie ea a E EEE E e E REES 193 The Glori
140. ceeecceec ee ceeeceeeeeeeca seca seca eeaeeeaeeeueeeneeenees 163 7 4 8 Arbitrary rank polymorphism cece cece cece ee ceeeceeecaeeca ceca eeaeeeaeeeneeeeeeeenees 164 TAS el BX AMples cccsasseseis fangs cage a uethad Meeps vee tence E E E ET 165 TAS 2 VY Pe IUMLCLENCS erer ease se a of Seek Beh Wa See ate deh Reet a beh eee aie 166 T483 Implicit Quantification acces ensipainos peen Er Teon In IES EERENS Ep Aa 167 7 4 9 Impredicative polymorphism ssessseseesssrrsserrerrrrreresrrrrssrerrsrrerrereerreresreerereet 168 7 4 10 Lexically scoped type variables ss siseisssrisssssiseisesii a riese sesi sne 168 TATL OVERVIEW vicete o eo inc ee coed a EE EEEE EE NEE EEKE E EEEE EEE REER 168 7 4 10 2 Declaration type signatures 2 0 0 0 cee eesis isad p ir epes cena sssi Esas 169 7 4 10 3 Pattern type signatures s sseeesseerrrresrrrrsrrerrsrrerrsrerrsrrerreresrrererreerre 169 7 4 10 4 Class and instance declarations cece cece cece eee c cece cece cena cen eeneeeeeeeeees 170 7 4 11 Deriving clause for classes Typeable and Data oo eeeeececceeceeeneeeeeeeneeneenees 170 7 4 12 Generalised derived instances for newtypes c cece eeeeceeeceeeeeeceeeeeueeeeaeeceuneeeeees 170 7 4 12 1 Generalising the deriving Clause cee ceee cece eee ceeeee ceca eeceeeae eens eenseenees 171 74 12 2 A more precise Specification ccceeeceeeeeceeeceeeeeeeereeeereeeeereeesereeeeeneees 172 7 4
141. ceteeeeeee eee 222 12 11 72 InP Ke rude support aone e n Deneaiaaweeag ssa ved an eeast ans 222 12 1 2 GHC s interpretation of undefined behaviour in Haskell 98 20 0 0 eee 223 12 2 Known bugs or infelicities s is dca sgseciscuseavoin cd fos dha shle es ABAS n ATRASA PITSER SPUET EASA 224 12 2 1 Bugs Im GHC mern 5cscseety Sevens aeaaee e p ee ett aetna ETE p EE SERERE Mee eee ees 224 12 2 2 Bugs in GHCi the interactive GHC raria mii ent a a a 224 wood EOE E E py use evaphbee sae nee bis EE AE A E 226 The Glasgow Haskell Compiler License Copyright 2002 The University Court of the University of Glasgow All rights reserved Redistribution and use in source and binary forms with or without modification are permitted provided that the following conditions are met e Redistributions of source code must retain the above copyright notice this list of conditions and the following disclaimer e Redistributions in binary form must reproduce the above copyright notice this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution e Neither name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW AND THE CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WAR RANTIES INCLUDING B
142. checks GHC s sanity not yours Ditto for STG level NOTE currently doesn t work Ditto for C level 4 16 3 How to read Core syntax from some ddump 97 Using GHC flags Let s do this by commenting an example It s from doing ddump ds on this code skip2 m m skip2 m 2 Before we jump in a word about names of things Within GHC variables type constructors etc are identified by their Uniques These are of the form letter plus number both loosely interpreted The letter gives some idea of where the Unique came from e g _ means built in type variable t means from the typechecker s means from the simplifier and so on The number is printed fairly com pactly in a base 62 format which everyone hates except me WDP Remember everything has a Unique and it is usually printed out when debugging in some form or another So here we go Desugared Main skip2 r1L6 _forall_ a _4 gt Num aS_4 gt aS_4 gt aS _4 r1L6 is the Unique for Main skip2 _4 is the Unique for the type variable template a Num a _4 is a dictionary argument _NI_ means no pragmatic information yet it will later evolve into the GHC_PRAGMA info that goes into interface files Main skip2 r1lL6 _4 gt d Num t4Gt gt let CoRec toH ea Sr 4 aA _NI_ t4Hg r3JH _4
143. ction 4 4 3 Batch compiler mode ghc M Dependency generation mode In this mode GHC can be used to generate depend ency information suitable for use in a Makefile See Section 4 6 11 Dependency generation ghc mk dll DLL creation mode Windows only See Section 11 5 1 Creating a DLL Using ghc make When given the make option GHC will build a multi module Haskell program by following depend encies from a single root module usually Main For example if your Main module is in a file called Main hs you could compile and link the program like this ghc make Main hs The command line may contain any number of source file names or module names GHC will figure out all the modules in the program by following the imports from these initial modules It will then attempt to compile each module which is out of date and finally if there is a Main module the program will also be linked into an executable The main advantages to using ghc make over traditional Makefiles are e GHC doesn t have to be restarted for each compilation which means it can cache information between compilations Compiling a multi module program with ghc make can be up to twice as fast as running ghc individually on each source file e You don t have to write a Makefile e GHC re calculates the dependencies each time it is invoked so the dependencies never get out of sync with the source Any of the command line options describ
144. cumentation is due to Jeff Lewis Implicit parameter support is enabled with the option fimplicit params A variable is called dynamically bound when it is bound by the calling context of a function and static ally bound when bound by the callee s context In Haskell all variables are statically bound Dynamic binding of variables is a notion that goes back to Lisp but was later discarded in more modern incarna tions such as Scheme Dynamic binding can be very confusing in an untyped language and unfortu nately typed languages in particular Hindley Milner typed languages like Haskell only support static scoping of variables However by a simple extension to the type class system of Haskell we can support dynamic binding Basically we express the use of a dynamically bound variable as a constraint on the type These con straints lead to types of the form x t gt t which says this function uses a dynamically bound variable x of type t For example the following expresses the type of a sort function impli citly parameterized by a comparison function named cmp sort cmp a gt a gt Bool gt a gt a The dynamic binding constraints are just a new form of predicate in the type class system An implicit parameter occurs in an expression using the special form x where x is any valid identifier e g ord x is a valid expression Use of this construct also introduces a new dynamic binding con strain
145. d by GHC Most people only have a gen eral goal something like Compile quickly or Make my program run like greased lightning The fol lowing packages of optimisations or lack thereof should suffice Note that higher optimisation levels cause more cross module optimisation to be performed which can have an impact on how much of your program needs to be recompiled when you change something This is one reaosn to stick to no optimisation when developing code No O type option specified 00 O or 01 O2 This is taken to mean Please compile quickly I m not over bothered about compiled code quality So for example ghe c Foo hs Means turn off all optimisation reverting to the same settings as if no 0O options had been specified Saying O0 can be useful if eg make has inserted a O on the command line already Means Generate good quality code without taking too long about it Thus for example ghc c O Main lhs O currently also implies fvia C This may change in the future Means Apply every non dangerous optimisation even if it means signific antly longer compile times The avoided dangerous optimisations are those that can make runtime or space worse if you re unlucky They are normally turned on or off individu ally 73 Using GHC 4 9 2 At the moment O2 is unlikely to produce better code than O Ofile lt file gt NOTE n
146. d by a C template A C template should define a macro or function with name prefixed by hsc_ that handles the construct by emitting the expansion to stdout See template hsc h for examples Such macros can also be defined directly in the source They are useful for making a let macros Plain whose expansion uses other the definition ina printf call let like macro let prepends hsc_ to the macro name and wraps 215 Chapter 11 Running GHC on Win32 systems 11 1 Starting GHC on Win32 platforms The installer that installs GHC on Win32 also sets up the file suffix associations for hs and Ihs files so that double clicking them starts ghci Be aware of that ghc and ghci do require filenames containing spaces to be escaped using quotes e ghc bin ghci c Program Files Haskell Project hs If the quotes are left off in the above command ghci will interpret the filename as two c Program and Files Haskell Project hs 11 2 Interacting with the terminal By default GHC builds applications that open a console window when they start If you want to build a GUI only application with no console window use the flag opt 1 mwindows in the link step Warning Windows GUI only programs have no stdin stdout or stderr so using the ordinary Haskell in put output functions will cause your program to fail with an IO exception such as Fail lt stdout gt hPutChar failed Bad file
147. d results of foreign imports and exports will be auto matically unwrapped if they are newtypes Section 3 2 of the FFI addendum GHC extends the FFI by automatically unnwrapping any newtypes that wrap the IO monad itself More precisely wherever the FFI specification requires an IO type GHC will accept any newtype wrapping of an IO type For ex ample these declarations are OK foreign import foo Int gt MyIO Int foreign import dynamic baz Int gt MyIO Int gt CInt gt MyIO Int 205 Foreign function interface FFI 8 2 Using the FFI with GHC The following sections also give some hints and tips on the use of the foreign function interface in GHC 8 2 1 Using foreign export and foreign import ccall wrapper with GHC When GHC compiles a module say M hs which uses foreign export or foreign import wrapper it generates two additional files M_stub c and M_stub h GHC will automatically compile M_stub c to generate M_stub o at the same time For a plain foreign export the file M_stub h contains a C prototype for the foreign exported function and M_stub c contains its definition For example if we compile the following module module Foo where foreign export ccall foo Int gt IO Int foo Int gt IO Int foo n return length f n Int gt Int f F0 fon n f n 1 Then Foo_stub h will contain something like this include HsFFI h extern HsInt foo HsInt a0 and Foo_stu
148. d the fglasgow exts flag is specified Typeable and Data These classes are defined in the library modules Data Typeable and Data Generics respectively and the appropriate class must be in scope before it can be mentioned in the deriving clause An instance of Typeable can only be derived if the data type has seven or fewer type parameters all of kind The reason for this is that the Typeable class is derived using the scheme described in Scrap More Boilerplate Reflection Zips and Generalised Casts http research microsoft com 7Esimonpj papers hmap gmap2 ps Section 7 4 of the paper describes the multiple Typeable classes that are used and only Typeablel up to Typeable7 are provided in the library In other cases there is nothing to stop the programmer writing a TypablexX class whose kind suits that of the data type constructor and then writing the data type instance by hand 7 4 12 Generalised derived instances for newtypes When you define an abstract type using newt ype you may want the new type to inherit some instances from its representation In Haskell 98 you can inherit instances of Eq Ord Enum and Bounded by deriving them but for any other classes you have to write an explicit instance declaration For example if you define newtype Dollars Dollars Int 170 GHC Language Features and you want to use arithmetic on Dollars you have to explicitly define an instance of Num instance Num Dollars
149. ded modules For example to load and run a Haskell program containing a module Main we might say ghc e Main main Main hs or we can just use this mode to evaluate expressions in the context of the Prelude ghc e interact unlines map reverse lines hello olleh Batch compiler mode In batch mode GHC will compile one or more source files given on the command line The first phase to run is determined by each input file suffix and the last phase is determined by a flag If no relevant flag is present then go all the way through linking This table summarises Phase of the compilation Suffix saying start here Flag saying stop after suffix of output file system literate pre processor lhs hs C pre processor opt hs with cpp E hspp Haskell compiler sS he s C compiler opt heor c s assembler o linker a out Thus a common invocation would be ghc c Foo hs to compile the Haskell source file Foo hs to an object file Foo o Note What the Haskell compiler proper produces depends on whether a native code generator is used producing assembly language or not producing C See Section 4 10 6 Options affecting code gener ation for more details Note C pre processing is optional the cpp flag turns it on See Section 4 10 3 Options affecting the C pre processor for more details 44 Using GHC Note The option E runs just th
150. descriptor However using Debug Trace trace is alright because it uses Windows debugging output support rather than stderr For some reason Mingw ships with the readline library but not with the readline headers As a result GHC like Hugs does not use readline for interactive input on Windows You can get a close simulation by using an emacs shell buffer 11 3 Differences in library behaviour Some of the standard Haskell libraries behave slightly differently on Windows e On Windows the z character is interpreted as an end of file character so if you read a file con taining this character the file will appear to end just before it To avoid this use IO Exts openFileEx to open a file in binary untranslated mode or change an already opened file handle into binary mode using IOExts hSetBinaryMode The IOExts module is part of the lang package 11 4 Using GHC and other GHC compiled ex ecutables with cygwin 11 4 1 Background 216 Running GHC on Win32 systems The cygwin tools aim to provide a unix style API on top of the windows libraries to facilitate ports of unix software to windows To this end they introduce a unix style directory hierarchy under some root directory typically is C cygwin Moreover everything built against the cygwin API including the cygwin tools and programs compiled with cygwin s ghc will see as the root of their file system happily pretending to work in a typic
151. doubt just report it What to put in a bug report The name of the bug reporting game is facts facts facts Don t omit them because Oh they won t be interested What kind of machine are you running on and exactly what version of the operating system are you using on a Unix system uname a or cat etc motd will show the desired information In the bug tracker this information can be given in the Architecture and Operating system fields What version of GCC are you using gee v will tell you Run the sequence of compiles runs that caused the offending behaviour cut and paste the whole session into the bug report We d prefer to see the whole thing Add the v flag when running GHC so we can see exactly what was run what versions of things you have etc What is the program behaviour that is wrong in your opinion If practical please attach or send enough source files for us to duplicate the problem If you are a Hero and track down the problem in the compilation system sources please send us patches either darcs send plain patches or just whole files if you prefer 1 3 GHC version numbering policy As of GHC version 6 0 we have adopted the following policy for numbering GHC versions 3 Introduction to GHC Stable Releases These are numbered x y z where y is even and z is the patch level number the trailing z can be omitted if z is zero Patch levels are bug fix releases on
152. ds fast pre compiled library code and fast compile turnaround for the parts of your program being actively developed GHC supports numerous language extensions including concurrency a foreign function interface ex ceptions type system extensions such as multi parameter type classes local universal and existential quantification functional dependencies scoped type variables and explicit unboxed types These are all described in Chapter 7 GHC Language Features GHC has a comprehensive optimiser so when you want to Really Go For It and you ve got time to spare GHC can produce pretty fast code Alternatively the default option is to compile as fast as pos sible while not making too much effort to optimise the generated code although GHC probably isn t what you d describe as a fast compiler GHC s profiling system supports cost centre stacks a way of seeing the profile of a Haskell program in a call graph like structure See Chapter 5 Profiling for more details GHC comes with a large collection of libraries with everything from parser combinators to networking The libraries are described in separate documentation 1 1 Meta information Web sites mailing lists etc On the World Wide Web there are several URLs of likely interest e Haskell home page http www haskell org e GHC home page http www haskell org ghc e comp lang functional FAQ http www cs nott ac uk gmh faq html We run the following ma
153. dy in scope Indeed it cannot already be in scope because it is bound by the pattern match GHC s rule is that in this situation and only then a pattern type signature can mention a type variable that is not already in scope the effect is to bring it into scope standing for the existentially bound type variable If this seems a little odd we think so too But we must have some way to bring such type variables into scope else we could not name existentially bound type variables in subequent type signatures This is now the only situation in which a pattern type signature is allowed to mention a lexical variable that is not already in scope For example both f and g would be illegal if a was not already in scope 7 4 10 4 Class and instance declarations The type variables in the head of a class or instance declaration scope over the methods defined in the where part For example class C a where Op 2s fal gt ca op xs let ys a ys reverse xs in head ys 7 4 11 Deriving clause for classes Typeable and Data Haskell 98 allows the programmer to add deriving Eq Ord to a data type declaration to generate a standard instance declaration for classes specified in the deriving clause In Haskell 98 the only classes that may appear in the deriving clause are the standard classes Eq Ord Enum Ix Bounded Read and Show GHC extends this list with two more classes that may be automatically derived provide
154. e 71 Using GHC library dirs hs libraries extra libraries include dirs includes depends hugs options If the package contains profiling libraries then the interface files for those library modules should have the suffix p_hi So the package can contain both normal and profiling versions of the same library without conflict see also lib rary_dirs below string list A list of directories containing libraries for this package string list A list of libraries containing Haskell code for this package with the a or d11 suffix omitted When packages are built as libraries the 1 ib prefix is also omitted For use with GHCi each library should have an object file too The name of the object file does not have a 1ib prefix and has the normal object suffix for your platform For example if we specify a Haskell library as HSfoo in the package spec then the various flavours of library that GHC actually uses will be called libHSfoo a_ The name of the library on Unix and Windows mingw systems Note that we don t support building dynamic libraries of Haskell code on Unix systems HSfoo dll The name of the dynamic library on Windows systems optional HSfoo o The object version of the library used by GHCi HSfoo obj string list A list of extra libraries for this package The difference between hs libraries and extra libraries is that hs libraries normally have several versions to support profi
155. e data Set cxt a Set a Unused cxt a gt The only use for the Unused constructor was to force the correct kind for the type variable cxt GHC now instead allows you to specify the kind of a type variable directly wherever a type variable is explicitly bound Namely e data declarations data Set cxt 2 gt a Set al e type declarations type T E gt Int e class declarations class Eq a gt C f gt a where e forall s in type signatures Peis ROK AWS OXE en ok Soe e SSE Cx aie The parentheses are required Some of the spaces are required too to separate the lexemes If you write gt you will get a parse error because gt is a single lexeme in Haskell 163 GHC Language Features 7 4 8 As part of the same extension you can put kind annotations in types as well Thus Este CINE se HS ENE g ssn foradd Gita gt AGa se The syntax is atype ctype kind The parentheses are required Arbitrary rank polymorphism Haskell type signatures are implicitly quantified The new keyword forall allows us to say exactly what this means For example OG seb Seb means this Oe forall bs by 2b The two are treated identically However GHC s type system supports arbitrary rank explicit universal quantification in types For ex ample all the following types are legal fl forallab a gt b gt
156. e and run using the interpreter However interpreted code can also run alongside compiled code in GHCi indeed nor mally when GHCi starts it loads up a compiled copy of the base package which contains the Pre lude Why should we want to run compiled code Well compiled code is roughly 10x faster than interpreted code but takes about 2x longer to produce perhaps longer if optimisation is on So it pays to compile the parts of a program that aren t changing very often and use the interpreter for the code being actively developed When loading up source files with Load GHCi looks for any corresponding compiled object files and will use one in preference to interpreting the source if possible For example suppose we have a 4 module program consisting of modules A B C and D Modules B and C both import D only and A imports both B amp C We can compile D then load the whole program like this Prelude gt ghc c D hs Prelude gt load A Skipping D Compiling C Compiling B Compiling A Ok modules loaded A B C Main gt hs D o hs interpreted hs interpreted hs interpreted rwa In the messages from the compiler we see that it skipped D and used the object file D o The message Skipping module indicates that compilation for module isn t necessary because the source and everything it depends on is unchanged since the last compilation At any time you can use the command show modules to get
157. e een eeneeeneees 216 11 3 Differences in library behaviour 0 eee cee i e a E TEE aT EA E 216 11 4 Using GHC and other GHC compiled executables with cygwin 0 0 0 0 ese ceee eee ee eee e eee e ees 216 L4o Back ground necon rest scsScecuie veck it E EEE ERE a KE E E T aerate 216 IA 2 Theproblenmi aiaa a a a a totes Mundane eben E NE oe E 217 L143 THIN 810200 3s a ne Sk os eee a a E bate ek EE Gaels 217 11 5 Building and using Win32 DELS 0000 cece cence EE pe OS EEA E TORSE EE EE E 217 115 1 Creating a DLL ernsten corte ois ee o AE EE EIKE EEO EEEE EEEE 218 11 5 2 Making DLLs to be called from other languages nssseesseesrrseesrerrerrrrrerrerreresre 218 12 Known bugs andnfehicities sieru serene ken Ke EEE E EEEE EKETE KE ESEE ES RE Eee 221 12 1 Haskell 98 vs Glasgow Haskell language non compliance sseeeisseesrererrerrsrrerreresree 221 12 1 1 Divergence from Haskell 98 cicis eio eoa er E E 221 121A We XiCalSyNtax cirine E E a a E Ea 221 ix 6 6 12 112 Context ee Syntax sere se ssa o rN wee eth ives hbase id yee ee ee See 221 12 1 1 3 Expressions and patterns 20 0 0 eee cee eee aE cece cena een EAS 222 12 1 1 4 Declarations and bindings cece cece ee cece eee eeneceeeeeaeeeeeaeeeeaeeneeaeeneeas 222 12 1 1 5 Module system and interface files 1 0 0 0 cece eee ee cece ne eeneeneeeeneeeees 222 12 1 1 6 Numbers basic types and built in classes 20 0 0 cee cece ee eeceee
158. e gt 1 0 No instance for Show a gt a arising from use of print at lt interactive gt 1 0 1 Possible fix add an instance declaration for Show a gt a In the expression print it In a do expression print it The error message contains some clues as to the transformation happening internally If the expression was instead of type IO a for some a then it will be bound to the result of the IO computation which is of type a eg Prelude gt Time getClockTime Wed Mar 14 12 23 13 GMT 2001 Prelude gt print it Wed Mar 14 12 23 13 GMT 2001 The corresponding translation for an IO typed e is 33 Using GHCi 3 4 5 it lt e Note that it is shadowed by the new value each time you evaluate a new expression and the old value of it is lost Type defaulting in GHCi Consider this GHCi session ghci gt reverse What should GHCi do Strictly speaking the program is ambiguous show reverse which is what GHCi computes here has type Show a gt a and how that displays depends on the type a For example ghci gt reverse String ghci gt reverse Int However it is tiresome for the user to have to specify the type so GHCi extends Haskell s type defaulting rules Section 4 3 4 of the Haskell 98 Report Revised as follows The standard rules take each group of constraints C1 a C2 a Cn a for each type variable a and defaults the type variable if e The type
159. e installed use the ghc pkg command ghc pkg list usr lib ghc 6 4 package conf base 1 0 haskel198 1 0 template haskell 1 0 mt1 1 0 unix 1 0 Cabal 1 0 haskell src 1 0 parsec 1 0 network 1 0 QuickCheck 1 0 HUnit 1 1 fgl 1 0 X11 1 1 HGL 3 1 OpenGL 2 0 GLUT 2 0 stm 1 0 readline 1 0 lang 1 0 concurrent 1 0 posix 1 0 util 1 0 data 1 0 text 1 0 net 1 0 hssource 1 0 rts 1 0 Packages are either exposed or hidden Only modules from exposed packages may be imported by your Haskell code if you try to import a module from a hidden package GHC will emit an error message Each package has an exposed flag which says whether it is exposed by default or not Packages hidden by default are listed in parentheses eg lang 1 0 in the output from ghc pkg list To expose a package which is hidden by default use the package flag see below To see which modules are exposed by a package ghc pkg field network exposed modules xposed modules Network BSD Network CGI Network Socket Network URI Network In general packages containing hierarchical modules are usually exposed by default However it is pos sible for two packages to contain the same module in this case only one of the packages should be ex posed It is an error to import a module that belongs to more than one exposed package The GHC command line options that control packages are package P This option causes package P to be e
160. e of empty doesn t mention a Functional dependencies can make the type variable reachable class Coll s a s gt a where empty i s insert s gt a gt s Alternatively Co11 might be rewritten class Coll s a where empty i sa insert Ss a gt a gt sa which makes the connection between the type of a collection of a s namely s a and the element type a Occasionally this really doesn t work in which case you can split the class like this class CollE s where empty i s class CollE s gt Coll s a where insert s gt a gt s 7 4 3 2 Background on functional dependencies The following description of the motivation and use of functional dependencies is taken from the Hugs user manual reproduced here with minor changes by kind permission of Mark Jones Consider the following class intended as part of a library for collection types class Collects ce wher empty i ce insert gt c pit 150 GHC Language Features member gt Cc gt Bool The type variable e used here represents the element type while ce is the type of the container itself Within this framework we might want to define instances of this class for lists or characteristic func tions both of which can be used to represent collections of any equality type bit sets which can be used to represent collections of characters or hash tables which can be used to represent any collection whose elements have a hash f
161. e pre processing passes of the compiler dumping the result in a file Note that this differs from the previous behaviour of dumping the file to standard output 4 4 3 1 Overriding the default behaviour for a file As described above the way in which a file is processed by GHC depends on its suffix This behaviour can be overriden using the x option x Causes all files following this option on the command line to be processed as if they had the suffix su suffix For example to compile a Haskell module in the file M my hs use ghc c x hs ff M my hs ix 4 5 Help and verbosity options help n v vn V version numeric ver sion print libdi sa Cause GHC to spew a long usage message to standard output and then exit Does a dry run i e GHC goes through all the motions of compiling as normal but does not actually run any external commands The v option makes GHC verbose it reports its version number and shows on stderr exactly how it invokes each phase of the compilation system Moreover it passes the v flag to most phases each reports its version number and possibly some other information Please oh please use the v option when reporting bugs Knowing that you ran the right bits in the right order is always the first thing we want to verify To provide more control over the compiler s verbosity the v flag takes an op tional numeric argument Specifying v on its own is equivalent to
162. e to install the package properly at a later date you have to go through the installa tion steps that follow 18 Installing GHC To install a package you ll have to do the following 1 Edit the Makefile and check the settings of the following variables platform the platform you are going to install for bindir the directory in which to install user invokable binaries libdir the directory in which to install platform dependent support files datadir the directory in which to install platform independent support files infodir the directory in which to install Emacs info files htmldir the directory in which to install HTML documentation dvidir the directory in which to install DVI documentation The values for these variables can be set through invocation of the configure script that comes with the distribution but doing an optical diff to see if the values match your expectations is always a Good Idea Instead of running configure it is perfectly OK to copy Makefile in to Makefile and set all these variables directly yourself But do it right 2 Run make install This should work with ordinary Unix make no need for fancy stuff like GNU make 3 rehash t csh or zsh users so your shell will see the new stuff in your bin directory 4 Once done test your installation as suggested in Section 2 1 2 3 Testing that GHC seems to be working Be sure to use a v option so you can see exactly what pathna
163. eaeeeaees 50 4 6 7 Other options related to interface files 0 00 cece ce cece ence ence ence eeeeeeeeeeeaeeeaeeeaes 50 4 6 8 The recompilation checker smire cece cece cece E ceca seca cena cena R E EEVEE 31 4 6 9 How to compile mutually recursive modules eee ee cece ce ee ce eeceeeeeeeeeeeeeeeeaeeeaes 31 4 6 10 Using mak nseni i ieoa nesses rest eb este va bese eee sbes ests coe duende cabs bucedewente de 53 4 6 11 Dependency generation secer aieri a a a nea er E aE e T ka 54 4 6 12 Orphan modules and instance declarations eseeeeseeersseerrsresrrrrrrreresrrerrsreerreees 56 4 7 Warnings and Samity checking c s ssscossnsaeh geaadesesencaessotedaseedebesees EESE PEOS S avtessreedebesesh 57 AS Packages eira r E EE O EEE e gerbes E E ES E E e E E 64 4 8 1 Using Packages s 2 osi cstestsntlogs eA eE E E aE a ea r E E EE EES 64 6 6 43 2 Consequences of packages sssri ih Sea Maes E eda ee ended eed ele oie eee 65 4 8 3 Package Databases spices en e e a N e avb laces E A Bewetous as 66 4 8 3 1 The GHC_PACKAGE_PATH environment variable ccccceceeeeeeeeeeeeeeees 66 4 8 4 Building a package from Haskell source 0 e eee ee ce ence ence eee eeceeeceeeseeeeeeeeaeeeaes 67 4 8 5 Package management the ghc pkg command eeeeeeeeseeeeeeereeeeereeeereeenereeenes 68 4 8 6 InstalledPackageInfo a package specification eee ceee cece eee eeeeeeeeee teen es 70 4 9
164. eally in scope at the prompt cece eee eee c eee ceeeceeeceeeeeeeeeeeaeeeaeeea sean eeaee 31 3 4 3 TV Quialified Names is 25 sie in hatie E a E O E raed dassiess s 32 3 4 3 2 The main command 2 6555 wos ces coy ei e ie costes ETRE EEEE EEE EESTE RETES 32 BAA The itvarable iesea aan ae ERA eE PERET POE da saa saedes EPES RUS NEEE Es 33 3 4 5 Type defaulting in GHC sesssrssiioriis eio ereo EEEE E NEE EEE EE EEEE KE 34 3 5 Invoking GHC eirese nerea ea ea dea EE E sass eae s EE E h 34 3 1 Packages eotia erei e S TE nE EEE EEEE TEES EERE E EES ITEE kE 35 3 5 2 Extra libraries menn ao seed Ee E S EEN Sha petals S reed 35 3 6 GHE1 COMMANS a r e e EE A A E dey EE EAE AE EEE A 36 37s The SSE COMMANE 2 5 25 eI ele AL eR Ma Ay r A E A N 38 32713 GHEL OpuOns rags cena e e sowe ct shwtuade ova renee seed ate peswaess pune eaule ph AE A VEEE wane ss RSS 38 3 7 2 Setting GHC command line options in GHCi 00 0 eee cece ee ceeeee teen seen sean eeaes 39 B18 THE wo Ca Arle x aati cosh A west eneate leans ssetewet kes Aten Stent rabed A EET cea eann thant tabi ae E S 39 3 9 FAQ and Things To Watch Out For 00 0 0 cee cece cece ence cece cece ne eeeeeeeeeeeeeeeaeeeaeseaeeeaeeaneeans 40 A USING GHG soesadosvasescoscosanchs votes E ad fectnasa ses etes gs EE 41 4 1 OPUODS OVERVIEW n sape eise o eie EA EE E NE EE EEE SA EETA EEE EEE 41 4 11 scommand line areument seriinin aa E a a ERR 41 4 1 2 command line options in source files 2
165. ece eee ceee ca eeca cece een usen Ee Erio aE 115 9 1 2 Rules for attributing Costs sss errs o enia bethadeieettassabosvassemncag ses o EER 115 5 2 Compiler options for profiling 2 0 0 0 ee cece cee Ee e teen seca EEE EEEE SAYER ESS 116 5 3 Time and allocation profiling a sree cece cece ce a E E EEE AEE TEE S aS 117 5 4 Profiling memory usa feni a e E a EAEE E A EAA E OEE IRE ER 117 5 4 1 RTS options for heap profiling e seeseeeeesiessreesrresresrrrerrerrresrrrertesrterrterrrerrereee 117 5 4 2 Retainer ProfINE Srey enso oee aa E a E e S EE EER ERNE ESR ESEIST 119 5 4 2 1 Hints for using retainer profiling se sseessesresserrsrrerrsresrrrrsrrerrerrerreresreee 120 5 4 3 Biographical Profiling 2 sss cescessiesss sovcostantoae sss rae a aE E sae nieag sos NESS 120 5 4 4 Actual memory residency esssistovier snese e oeiee EEEE EEEE E SEE EEEE EEE EES 121 5 5 Graphical time allocation profile ssi rsss esseci rni sessnaseshvteasasSeanas s eiad eraros Esa PESE SERS nET 121 5 6 hp2ps heap profile to PostScript ee seeeeesesesreesiresrresressrrssresrrtsrresrrerrterrererteeereeeeeee 122 5 6 1 Manipulating the hp file s 200 cccastie gsc orice she enirere e E esa E ie 123 5 6 2 Zooming in on regions of your profile se sseessesresrerrsrrerrrrsrsrrerreresrreresrerrereet 123 5 6 3 Viewing the heap profile of a running program 0c cee eee cece e ence e eee eeeneeeneeeenees 124 5 6 4 Viewing a heap pro
166. eceeeceeeeaeeea ceca eesaecan eeu eeueeeeeeeenees 210 9 1 When the compiler does the wrong thing 2 0 0 e eee cece eee eee cece eeceeean eeu eeneeeneeeenees 210 9 2 When your program does the wrong thing 0 cee ceee cece cn ee cece eee eceeeee eeu eeneeneeennees 210 10 Other Haskell utility programs sses neni cece aE eee eeeeeeeeeeeaeeeaeeeaeeea seas eeu EEN o 212 10 1 Ctags and Etags for Haskell hasktags 0 0 ccc eee cence eece teen eecaeena sean eeneeeneeeenees 212 10 1 1 Using tags with your editor cece cece ence ee ceeeeaeeea seca sean eeaeeeueeeeeeeeeees 212 10 2 Yace for Haskell Nappy iiie Sas cab esten te wee E A Raves eel Reew tons 212 10 3 Writing Haskell interfaces to C code hse2hs 0 ee eeeeceeneeceeeeceececueeeeueeeeeeeceaueeeene ees 213 10 3 1 command line Syntax sss5 sees fsacacstssFeota ads ste saaceseviad gases sanas syoisadsresmauee yore sgsseeagusles 213 10 3 2 Input SYDtAX icasi unrin Suge sde ye gs Aenea cieseees cA E E EEEE EEES 214 10 3 3 Custom CONSUIUCtS cccsis snares ca csateste sss e E E ETE batwa she cota bes PEE Sens 215 11 Running GHC on Win32 systems 0 0 00 ce ce ee ce ee ce eece eee eeceeeeeeeeaeeeaeeeaeeea seas eeu eeaeeeneeeenees 216 11 1 Starting GHC on Win32 platforms 2 00 cscccsss susie sscossess sb viecaseedsacspssoteodssesssenssveceegssveaneses 216 11 2 Interacting with the terminal ee cece ce ee eens ce eeceeeeeeeeaeeea seca eeaaeea
167. ed A B C D Nothing happened Here s another lesson newly compiled modules aren t picked up by reload only load Main gt load A Skipping D Skipping C Compiling B Compiling A Ok modules loaded A B C RS D0 SG CO hs interpreted hs interpreted UPwWwaAD HINT since GHCi will only use a compiled object file if it can be sure that the compiled version is up to date a good technique when working on a large program is to occasionally run ghc make to compile the whole project say before you go for lunch then continue working in the interpreter As you modify code the new modules will be interpreted but the rest of the project will remain compiled 3 4 Interactive evaluation at the prompt When you type an expression at the prompt GHCi immediately evaluates and prints the result Prelude gt reverse hello olleh Prelude gt 5 5 10 29 Using GHCi 3 4 1 3 4 2 I O actions at the prompt GHCi does more than simple expression evaluation at the prompt If you type something of type IO a for some a then GHCi executes it as an IO computation Prelude gt hello hello Prelude gt putStrLn hello hello Furthermore GHCi will print the result of the I O action if and only if e The result type is an instance of Show e The result type is not For example remembering that putStrLn String gt IO Prelude gt putStrLn hello hello Prelude gt do
168. ed in the rest of this chapter can be used with make but note that any options you give on the command line will apply to all the source files compiled so if you want any options to apply to a single source file only you ll need to use an OPTIONS_GHC pragma see Section 4 1 2 command line options in source files If the program needs to be linked with additional objects say some auxiliary C code then the object files can be given on the command line and GHC will include them when linking the executable Note that GHC can only follow dependencies if it has the source file available so if your program in cludes a module for which there is no source file even if you have an object and an interface file for the module then GHC will complain The exception to this rule is for package modules which may or may not have source files The source files for the program don t all need to be in the same directory the i option can be used to add directories to the search path see Section 4 6 3 The search path 43 Using GHC 4 4 2 4 4 3 Expression evaluation mode This mode is very similar to interactive mode except that there is a single expression to evaluate which is specified on the command line as an argument to the e option ghc e expr Haskell source files may be named on the command line and they will be loaded exactly as in interact ive mode The expression is evaluated in the context of the loa
169. ed to the recursive call In the latter case because len_acc2 has a type signature the recursive call is made to the polymoprhic version which takes acc as an implicit parameter So we get the following results in GHCi Prog gt lenl hello 0 Prog gt len2 hello 3 Adding a type signature dramatically changes the result This is a rather counter intuitive phenomenon worth watching out for 7 4 6 4 Implicit parameters and monomorphism 162 GHC Language Features 7 4 7 GHC applies the dreaded Monomorphism Restriction section 4 5 5 of the Haskell Report to implicit parameters For example consider Trh ORGS CZNIE f v let x 0 in let y x v in let x 5 in y Since the binding for y falls under the Monomorphism Restriction it is not generalised so the type of y is simply Int not x Int gt Int Hence f 9 returns result 9 If you add a type signature for y then y will get type x Int gt Int so the occurrence of y in the body of the let will see the inner binding of x so 9 will return 14 Explicitly kinded quantification Haskell infers the kind of each type variable Sometimes it is nice to be able to give the kind explicitly as machine checked documentation just as it is nice to give a type signature for a function On some occasions it is essential to do so For example in his paper Restricted Data Types in Haskell Haskell Workshop 1999 John Hughes had to define the data typ
170. eda aeas sees 8 144 2 Cabal ce cess ccssceeeeth ses teen ere a ae E EE EE S EE S EO ERE S 10 14 43 haskell snc a aa esse Res ed Aa N a EER S 10 144 AW Parse da tnaa ee suawseshetantesy ites vedere tet eon R ARA Sone a Aa 11 VAS readline tie teats vee 2085 cha ae cet he eed T E Sock aay eee TE heal ee 11 VA ALO recer DASE nnen e deans den N vodd ae aun caou etude aes onbeddessruel sas cae NERS 11 1 4 4 57 TESEX COMP Al seis ccdek a eve deb cod E nee dee lop deet ee owcteub usd edotebenceddetebcceteeenees 11 144 8 regex POSIX pee enep n eee eE seu Sed obeb stewed gs A NEEE EE E ghepsvenepepbetens 11 PAA OSU a REEE EE S EEEE EER EEEE EE SEA 11 1 4 4 10 t emplat haskell s s 03 cse2sssehoots oun coach ss e E ETES 12 TAA TTUK e 22508 eeke eT EEE E R E E EE iat deed ees EEE O TEET E E eats 12 144 12 Win 26255 teed ss ves e ra Ed E OEE E EE EEE ERRE EE RT OENES 12 TAS Extra Librari ES esn enado nse a a E E E avd EEEO EEES E 13 VAS LOA LUT ree sata ps ease E e a E e E ba bata T EES 13 LAD ALLOWS ee enoi e E N E O E E pe nna E E E EET EE EIERS 13 VAD a A i EE S E A Moi edi E E iatwarguteces 13 E o e P TOL E A E EAT EE EA E E T ET EE E E SEAE 13 I Sokoa Ci E O KEE E is oa a E ESE 13 VASO haske ll SiC innet niea e aa de Sea E e E E a gee a oS 14 VAD Te LE E a E E E A A stats 14 VASO html a ea POON e a E e e T E AES 14 PAS Oe HUME nesir ee ebbe uk et slate best weecouvie tees ia geen bbl dee gesds Beevheteg STEI Sows 14 V
171. eft of the gt that is not uniquely determined either directly or indirectly by the variables on the right Dependencies also help to produce more accurate types for user defined functions and hence to provide earlier detection of errors and less cluttered types for programmers to work with Recall the previous definition for a function f f x y insert x y insert x insert y for which we originally obtained a type f Collects ac Collects b c gt Jae gt SP eS He 153 GHC Language Features Given the dependency information that we have for Collects however we can deduce that a and b must be equal because they both appear as the second parameter in a Collects constraint with the same first parameter c Hence we can infer a shorter and more accurate type for f E Collects ac gt a gt a gt c gt c In a similar way the earlier definition of g will now be flagged as a type error Although we have given only a few examples here it should be clear that the addition of dependency in formation can help to make multiple parameter classes more useful in practice avoiding ambiguity problems and allowing more general sets of instance declarations 7 4 4 Instance declarations 7 4 4 1 Relaxed rules for instance declarations An instance declaration has the form instance assertion Sooty assertion gt class type type where The part before the gt is the context while the par
172. elements to be removed from the profile ensuring that all the data will be displayed Generate colour output Ignore marks Print out us age information Manipulating the hp file Notes kindly offered by Jan Willhem Maessen The FOO hp file produced when you ask for the heap profile of a program FOO is a text file with a par ticularly simple structure Here s a representative example with much of the actual data omitted JOB EE oer PLE Dp hu UNIT I S LE S sam pl LE PLE E O PLE e ier OM FOO hC Dec 26 18 17 2002 seconds T bytes a 0 00 00 15 07 data Si Py N A P A a PLE SAMPL sam PL C SA PL E 30 23 ple data E 30 23 E 11695 47 PL E 11695 47 The first four lines JOB DATE SAMPLI starting with BEGIN_SAMPLI E UNIT VALUE_UNIT form a header Each block of lines E and ending with END_SAMPLE forms a single sample you can think of this as a vertical slice of your heap profile The hp2ps utility should accept any input with a properly formatted header followed by a series of complete samples Zooming in on regions of your profile You can look at particular regions of your profile simply by loading a copy of the hp file into a text editor and deleting the unwanted samples The resulting hp file can be run through hp2ps and viewed or printed 123 Profi
173. en as in a gt b b gt c a gt c and in which some subset implies the remaining dependencies Examples like this are not treated as errors Note that dependencies appear only in class declarations and not in any other part of the language In particular the syntax for instance declarations class constraints and types is com pletely unchanged By including dependencies in a class declaration we provide a mechanism for the programmer to spe cify each multiple parameter class more precisely The compiler on the other hand is responsible for ensuring that the set of instances that are in scope at any given point in the program is consistent with 152 GHC Language Features any declared dependencies For example the following pair of instance declarations cannot appear to gether in the same scope because they violate the dependency for D even though either one on its own would be acceptable instance D Bool Int where instance D Bool Char where Note also that the following declaration is not allowed even by itself instance D a b where The problem here is that this instance would allow one particular choice of a to be associated with more than one choice for b which contradicts the dependency specified in the definition of D More generally this means that in any instance of the form instance D t s where for some particular types t and s the only variables that can appear in s are the ones that appear in t a
174. er the directions above will install the whole lot in one go Note you must at least have the basic GHC binary distribution bundle these extra bundles won t install on their own 2 1 2 3 Testing that GHC seems to be working The way to do this is of course to compile and run this program in a file Main hs main putStr Hello world n Compile the program using the v verbose flag to verify that libraries etc are being found properly o ghc v o hello Main hs Now run it hello Hello world Some simple but profitable tests are to compile and run the notorious nfib program using different numeric types Start with nfib Int gt Int and then try Integer Float Double Ra tional and perhaps the overloaded version Code for this is distributed in ghc misc examples nfib in a source distribution For more information on how to drive GHC read on 2 2 Installing on Windows Getting the Glasgow Haskell Compiler post 5 02 to run on Windows platforms is a snap the Install shield does everything you need 2 2 1 Installing GHC on Windows To install GHC use the following steps e Download the Installshield setup exe from the GHC download page haskell org http www haskell org ghc e Run setup exe On Windows all of GHC s files are installed in a single directory If you choose Custom from the list of install options you will be given a choice about where this directory is otherwise
175. errereee 203 7 14 2 Monomorphic pattern bindings eseesssessrsseesresrssretrsrrerrrrsrsrrerreresrreresreerereet 203 71S Parallel Haskell seriene arie a EE EE E AAEE S EEEE GASE SEEE EPEE SARS 203 7 15 1 Running Concurrent Haskell programs in parallel cece cece ce ee ce eecetece ence ence 203 7 15 2 Annotating pure code for parallelism 2 0 0 0 eee cee eee ee cece cee c een cen eeneeeeneeeees 204 8 Foreign function interface FFI rneer noet reenen EEE EEEREN EER EREKE ES RES 205 8 1 GHC extensions to the FFI Addendum 00 0 cence nce eceenceneeeeeeeeeeeeeeeeeeaeseaeeenes 205 Sel Ls Unboxed types e n oda see davots othe daboedentendtacon dsb erea weed oe dau sedan sn AEE SRNR 205 8 1 2 Newtype wrapping of the IO monad 0 00 cece cece cece neceeee ener eeeeeeeeeeeeeeaees 205 8 2 Using the BEL with GHG eryr e nE paetacedpetaten E eatappeetess onluedtsh EAEE 206 8 2 1 Using foreign export and foreign import ccall wrapper with GHC 206 8 2 121s Using VOur own maim Y caus vk scagercsaceseee thas gous dae bee vathae poets cade weldan eau NEEN 206 8 2 1 2 Using foreign import ccall wrapper with GHC 207 8 2 2 Using function headers sisisi eee roai nss oe re oases RESES PISAR ESIS SERTE PESASSE RESSE 208 8 2 2 1 Finding Hedder files vso insniessns ns ee E E SE EEE 208 8 2 3 Memory Allocation 202554505 nietre ee aE E E A aE sateen 209 9 What to do when something goes Wrong 20 2 0 eee eee cece eee e ence e
176. es A hi A o ghc o foo A o B o Linking the program There are several points to note here e The file A hs boot is a programmer written source file It must live in the same directory as its parent source file A hs Currently if you use a literate source file A hs you must also use a liter ate boot file A Lhs boot and vice versa e Ahs boot file is compiled by GHC just like a hs file ghc c A hs boot When a hs boot file A hs boot is compiled it is checked for scope and type errors When its par ent module A hs is compiled the two are compared and an error is reported if the two are incon sistent e Just as compiling A hs produces an interface file A hi and an object file A o so compiling A hs boot produces an interface file A hi boot and an pseudo object file A o boot e The pseudo object file A o boot is empty don t link it but it is very useful when using a Makefile to record when the A hi boot was last brought up to date see Section 4 6 10 Using make e The hi boot generated by compiling a hs boot file is in the same machine generated binary format as any other GHC generated interface file e g B hi You can display its contents with ghc show iface If you specify a directory for interface files the ohidir flag then that af fects hi boot files too GI e If hs boot files are considered distinct from their parent source files and if a SOURCE import is considered
177. esepensees 82 4 12 1 Options to enable SMP parallelism 20 0 0 ce cece cc ee ce eecc nce eeeeeeeeeeeeeeeaeeeaeeenes 82 4 12 2 Hints for using SMP parallelism 0 ce cece ee cence ence eeceeeceeeeeeeeaeseaeseaeeeaes 82 4 13 Platform specific Flags se esnie i erein ieii aiaa Ta AREO EERE E S a S T SEA 82 4 14 Running a compiled program phren aa E cece cena cena eens E EEE 84 4 14 1 Setting global RTS options srini ce ceee cece cn E EEE SEE EEE E rE 84 4 14 2 Miscellaneous RTS Options songe eeose a EE a EEE E ae oTa Di 85 4 14 3 RTS options to control the garbage collector 2 0 0 0 cece cece cc eece ence eeceeeeeeeeneeeaes 85 4 14 4 RTS options for profiling and parallelism ceeeeeeeeeeeeceeeeeceeeceneeeeeeeeeeneeeene eres 87 4 14 5 RTS options for hackers debuggers and over interested souls 0 eceeeeeeeeeee ee ee 87 4 14 6 Hooks to change RTS behaviour ccc cece cence ence nce eeceeeeeeseeeeaeeeaeeenes 88 4 15 Generating and compiling External Core Files 2 0 0 0 cece eeece cece eee neececeeeeeeeeeeeeeeeaees 89 4 16 Debugging the compiler sissisota sha eactap E a a aa E E T yey 89 4 16 1 Dumping out compiler intermediate structures 2 0 0 0 eee cece cee ce ee ceeeceeeeeeeeaeeenes 89 4 16 2 Checking for COMSIStENCY ssiri irni ieoa iepa i REEE POTSER ES E ERTSE y EEEE e 97 4 16 3 How to read Core syntax from some ddump flags eeeeeeeeeeeeereeererrerrrerrrerrreeee 98 4 16 4
178. etailed information Make the interface loader be real chatty about what it is upto Make the type checker be real chatty about what it is upto Make the renamer be real chatty about what it is upto Print out summary of what kind of information the renamer had to bring in Show the output of the intermediate Core to Core and STG to STG passes respect ively Lots of output So when we re really desperate o ghc noC 0 ddump simpl dverbose simpl dcore lint Foo hs Print out each pass name as it happens Show statistics for the usage of fast strings by the compiler Debugging output is in one of several styles Take the printing of types for ex ample In the user style the default the compiler s internal ideas about types are presented in Haskell source level syntax insofar as possible In the debug style which is the default for debugging output the types are printed in with explicit for alls and variables have their unique id attached so you can check for things that look the same but aren t This flag makes debugging output appear in the more verb ose debug style In error messages expressions are printed to a certain depth with subexpressions beyond the depth replaced by ellipses This flag sets the depth Have the renamer report what imports does not contribute 4 16 2 hecking for consistency Turn on heavyweight intra pass sanity checking within GHC at Core level It
179. file in real time 2 0 0 0 cece eeeeeceeeceeeeeceueeeeeeecenaeeeeeeeeeeeseeaneeeees 124 5 7 Using ticky ticky profiling for implementors cece cece cece ee ee cece cece ceneeeneeeeneeeees 125 6 Advice on sooner faster smaller thriftier cc cece cececececececececeee eee en ease ea enenea ease ence eeeeeeeeenees 127 6 1 Sooner producing a program more QUICKLY 2 0 0 0 ee cence eee a eeeeeeceeeaeeeueeeneeeneeeenees 127 6 2 Faster producing a program that runs quicker cece cece cece ceee ee eece seca eeneeeueceusceneeeeeees 128 6 3 Smaller producing a program that is smaller 00 eeceee cece cece cece ne eeneeeneeeneeeeeeeeees 132 6 4 Thriftier producing a program that gobbles less heap space c esse cece eee eceeeeeeeeeeees 132 7GHE Language Features ccsice coos ccleds stabs csesv eds AES EEEE E eet duvecdove socom O EEES E TETES 133 TAs Lan GUase OPuON S252 sstashs asthas ea a sia seaas des E a T eaa ges Phat o 133 7 2 Unboxed types and primitive Operations 2 0 0 0 cece cece cece cece ca eece cena cena cca eeaneeueceaseeneeeenees 134 Tet Unboxed Ty Pes seis of aeee oa cect oaar pEi vad saasles voted saee denies ev olan ds vedgaeassvoteaeseeteasens 135 Tea Unboxed TUples iss iscressteaas hubs aE cheer so ydeaae thu EE E EE tous EENE ES 136 T3 Syntactic EXtensions iiss sei ceteris ahah aoa wy eased Losed ae evade hes eda wth eR eal ede hel Gees 137 Teel Hierarchic
180. filing To generate a time and allocation profile give one of the following RTS options to the compiled pro gram when you run it RTS options should be enclosed between RTS RTS as usual p or P The p option produces a standard time profile report It is written into the file pro gram prof The P option produces a more detailed report containing the actual time and alloca tion data as well Not used much px The px option generates profiling information in the XML format understood by our new profiling tool see Section 5 5 Graphical time allocation profile xC This option makes use of the extra information maintained by the cost centre stack profiler to provide useful information about the location of runtime errors See Sec tion 4 14 5 RTS options for hackers debuggers and over interested souls 5 4 Profiling memory usage 5 4 1 In addition to profiling the time and allocation behaviour of your program you can also generate a graph of its memory usage over time This is useful for detecting the causes of space leaks when your pro gram holds on to more memory at run time that it needs to Space leaks lead to longer run times due to heavy garbage collector activity and may even cause the program to run out of memory altogether To generate a heap profile from your program 1 Compile the program for profiling Section 5 2 Compiler options for profiling 2 Run it with one of the heap p
181. find a source file for each module in the dependency graph so that it can parse the import declarations and follow dependencies Any pre compiled modules without source files must therefore belong to a package By default ghc M generates all the dependencies and then concatenates them onto the end of make file or Makefile if makefile doesn t exist bracketed by the lines DO NOT DELETE Be ginning of Haskell dependencies and DO NOT DELETE End of Haskell dependencies If these lines already exist in the makefile then the old dependencies are deleted first Don t forget to use the same package options on the ghc M command line as you would when compiling this enables the dependency generator to locate any imported modules that come from pack ages The package modules won t be included in the dependencies generated though but see the include pkg deps option below The dependency generation phase of GHC can take some additional options which you may find useful For historical reasons each option passed to the dependency generator from the GHC command line This is a change in behaviour relative to 6 2 and earlier 55 Using GHC must be preceded by optdep For example to pass f depend to the dependency generator you say ghc M optdep f optdep depend The options which affect dependency generation are v2 f file s lt suf gt
182. finition clunky env varl var2 okl amp amp ok2 vall val2 otherwise varl var2 where ml lookup env varl m2 lookup env var2 okl maybeToBool ml ok2 maybeToBool m2 vall expectJust ml val2 expectJust m2 The auxiliary functions are maybeToBool Maybe a gt Bool maybeToBool Just x True maybeToBool Nothing False expectJust Maybe a gt a expectJust Just x x expectJust Nothing error Unexpected Nothing What is clunky doing The guard ok1 amp amp ok2 checks that both lookups succeed using maybeTo Bool to convert the Maybe types to booleans The lazily evaluated expectJust calls extract the values from the results of the lookups and binds the returned values to val1 and val2 respectively If either lookup fails then clunky takes the otherwise case and returns the sum of its arguments This is certainly legal Haskell but it is a tremendously verbose and un obvious way to achieve the de sired effect Arguably a more direct way to write clunky would be to use case expressions clunky env varl varl case lookup env varl of Nothing gt fail Just vall gt case lookup env var2 of Nothing gt fail Just val2 gt vall val2 where fail varl var2 This is a bit shorter but hardly better Of course we can rewrite any set of pattern matching guarded equations as case expressions that is precisely what the compiler does when compiling equations The reason that Haskell p
183. flown relocation fi ld The last time we looked this bug still wasn t fixed in the BFD codebase and there wasn t any notice able interest in fixing it when we reported the bug back in 2001 or so The workaround is to split up the o files that make up your package into two or more o s along the lines of how the base package does it 225 re Index Symbols r 39 RTS 84 s 39 t 31 39 include 79 RTS 84 show iface 51 45 69 A RTS option 85 A lt size gt option 127 A lt size gt RTS option 132 auto 113 116 auto all 113 116 B RTS option 87 C 43 44 c 43 44 80 RTS option 85 caf all 116 116 cpp 44 76 cpp option 76 cpp Vs string gaps 78 Cs RTS option 81 D 77 RTS option 87 demm lint 97 dcore lint 63 97 dcore lint option 210 ddump options 89 ddump asm 96 ddump bcos 97 ddump cmm 95 ddump cpranal 91 ddump cse 92 ddump deriv 90 ddump ds 90 ddump flatC 94 ddump foreign 97 ddump hi 50 ddump hi diffs 50 ddump if trace 97 ddump inlinings 91 ddump minimal imports 51 ddump occur anal 92 ddump opt cmm 95 ddump parsed 89 ddump prep 93 ddump rn 89 ddump rn trace 97 ddump rules 91 ddump simpl 91 ddump simpl iterations 97 ddump simpl stats option 97 ddump spec 91 ddump splices 89 ddump stg 94 ddump stranal 91 ddump tc 89 89 ddump tc trace 97 ddump types 9
184. foral1 brings into scope the expli citly quantified type variables in the definition of the named function s For example f forall a a gt a f x xs xs x a wow The forall a brings a into scope in the definition of f This only happens if the quantification in f s type signature is explicit For example g ies xs x a This program will be rejected because a does not scope over the definition of f so x a means x forall a a by Haskell s usual implicit quantification rules 7 4 10 3 Pattern type signatures A type signature may occur in any pattern this is a pattern type signature For example f and g assume that a is already in scope f x Int y a gt x g X a Xx h x y Int Bool y x In the case where all the type variables in the pattern type sigature are already in scope i e bound by the enclosing context matters are simple the signature simply constrains the type of the pattern in the ob vious way There is only one situation in which you can write a pattern type signature that mentions a type variable that is not already in scope namely in pattern match of an existential data constructor For example data T forall a MkT a Kent ot SR on TT k MKT t a MkT t3 169 GHC Language Features where t3 a t t t Here the pattern type signature t a mentions a lexical type variable that is not alrea
185. g Haskell input and pre processing C GHC output Since GHC from verion 4 00 now supports concurrent haskell by de fault this symbol is always defined Only defined when parallel is in use This symbol is defined when pre processing Haskell input and pre processing C GHC output This define allows conditional compilation based on the Operating System whereos is the name of the current Operating System eg Linux mingw32 for Windows solaris etc This define allows conditional compilation based on the host architecture wherearch is the name of the current architecture eg 1386 x86_64 powerpc sparc etc 77 Using GHC 4 10 3 1 CPP and string gaps A small word of warning cpp is not friendly to string gaps In other words strings such as the fol lowing strmod p Ve don t work with cpp usr bin cpp elides the backslash newline pairs However it appears that if you add a space at the end of the line then cpp at least GNU cpp and pos sibly other cpps leaves the backslash space pairs alone and the string gap works as expected 4 10 4 Options affecting a Haskell pre processor A custom pre processor is run over your Haskell source file only if the F option is given Running a custom pre processor at compile time is in some settings appropriate and useful The F option lets you run a pre processor as part of the overall GHC compil ation pipeline which has the advantage ove
186. g file user package conf 4 17 12 Language options Section 7 1 Language options Flag Description Static Dynamic Reverse fal Enable overlapping in dynamic low over stances fno al lapping instances low over lapping instances fal Enable incoherent dynamic low inco instances Implies fno al herent instances fallow over low inco lapping instances herent instances fai Enable undecidable in dynamic x low unde stances fno al cidable instances low unde cidable instances fcontext stack Nn set the limit for context re dynamic 20 duction farrows Enable arrow notation ex dynamic fno arrows tension ffior fffi Enable foreign function in dynamic fno ffi terface implied by fglasgow exts fgenerics Enable generic classes dynamic fno fgenerics fglasgow exts Enable most language ex dynamic fno glasgow exts tensions fimplicit params Enable Implicit Parameters dynamic Implied by fno impli fglasgow exts cit params fir Make tuple pattern match dynamic refutable tuples ing irrefutable fno irre 103 Using GHC Flag Description Static Dynamic Reverse futable tuples Don t implicitly import dynamic fimplicit prelude fno impli Prelude cit prelude Disable the monomorphism dynamic fno mono restriction fmonomorphism re morphism re striction stric
187. generator fasm It is not quite ready to be used yet for x86 Linux On Win dows position independent code is never used and on PowerPC64 Linux position independent code is always used so the flag is a no op on those platforms When generating code assume that entities imported from a different package will reside in a different shared library or binary This currently works on Mac OS X it works on PowerPC Linux when using the native code generator As with fPIC x86 Linux support is not quite ready yet Windows is not supported and it is a no op on PowerPC64 Linux Note that this option also causes GHC to use shared libraries when linking 4 10 7 Options affecting linking GHC has to link your code with various libraries possibly including user supplied GHC supplied and system supplied 1m math library for example llib Link in the 1ib library On Unix systems this will be in a file called Liblib a or 1iblib so which resides somewhere on the library directories path Because of the sad state of most UNIX linkers the order of such options does matter If library foo requires library bar then in general 1 foo should come before lbar on the command line There s one other gotcha to bear in mind when using external libraries if the library contains a main function then this will be linked in preference to GHC s own main function eg Libf2c and libl have their own main s This is be cause GHC s main c
188. gle field of the installed package description for P Additionally the following flags are accepted by ghc pkg au to ghei libs Automatically generate the GHCi 0 version of each a Haskell library using GNU ld if that is available Without this option ghc pkg will warn if GHCi versions of any Haskell libraries in the package don t exist GHCi o libraries don t necessarily have to live in the same directory as the corresponding a library However this option will cause the GHCi library to be created in the same directory as the a library Adds file to the stack of package databases Additionally file will also be the database modified by a register unregister expose or hide command unless it is overriden by a later package conf user or global option Causes ghc pkg to ignore missing dependencies directories and libraries when registering a package and just go ahead and add it anyway This might be useful if your package installation system needs to add the package to GHC before building and installing the files Operate on the global package database this is the default This flag affects the register update unregister expose and hide commands Outputs the command line syntax Operate on the current user s local package database This flag affects the register update unregister expose and hide commands 69 Using GHC 4 8 6 ve Output the ghc pkg version number rs
189. gs output of specialisation pass dumps all rewrite rules including those generated by the specialisa tion pass simplifier output Core to Core passes inlining info from the simplifier CPR analyser output strictness analyser output 91 Using GHC CSE pass output worker wrapper split output ddump workwrap occurrence analysis output ddump oc cur anal 92 Using GHC output of core preparation pass 93 Using GHC output of STG to STG passes flattened Abstract C ddump flatc 94 Using GHC Print the C code out Dump the results of C to C optimising passes ddump opt cmm 95 Using GHC assembly language from the native code generator 96 Using GHC ddump sim pl iterations byte code compiler output dump foreign export stubs ddump Show thmoutput of each iteration of the simplifier each run of the simplifier has a maximum number of iterations normally 4 Used when even dverbose simp1 doesn t cut it Dump statistics about how many of each kind of transformation too place If you add ddump sim pl stats ddump tf trace ddump te trace ddump rn trace ddump rn stats dverbose core2core dverbose stg2stg dshow passes dfast sdppagdebug stats dp pr user length dshow un used im dcore lint dstg lint demm lint dppr debug you get more d
190. h GHC and the preprocessor must observe some additional restrictions 184 GHC Language Features e The module must import Cont rol Arrow ibraries base Control Arrow html e The preprocessor cannot cope with other Haskell extensions These would have to go in separate modules e Because the preprocessor targets Haskell rather than Core 1et bound variables are monomorphic 7 8 Bang patterns 7 8 1 GHC supports an extension of pattern matching called bang patterns Bang patterns are under considera tion for Haskell Prime The Haskell prime feature description http hackage haskell org trac haskell prime wiki BangPatterns contains more discussion and ex amples than the material below Bang patterns are enabled by the flag fbang patterns Informal description of bang patterns The main idea is to add a single new production to the syntax of patterns pat pat Matching an expression e against a pattern p is done by first evaluating e to WHNF and then match ing the result against p Example fl x True This definition makes f1 is strict in x whereas without the bang it would be lazy Bang patterns can be nested of course 2 x y x y Here f2 is strict in x but not in y A bang only really has an effect if it precedes a variable or wild card pattern 35 L x y f4 x y x y x y Here f3 and f4 are identical putting a bang before a pattern that forces evaluation anyway does
191. he things it needed last time gleaned from the interface file of the module being compiled if they are all the same it stops compiling rather early in the process saying Compilation IS NOT required What a beautiful sight Patrick Sansom had a workshop paper about how all this is done though the details have changed quite a bit Ask him mailto sansom dcs gla ac uk if you want a copy How to compile mutually recursive modules GHC supports the compilation of mutually recursive modules This section explains how Every cycle in the module import graph must be broken by a hs boot file Suppose that modules A hs and B hs are Haskell source files thus module A where import B TB newtype TA MkTA Int f TB gt TA f MkTB x MkTA x module B where 51 Using GHC import SOURCE A TA data TB MkTB Int g TA gt TB g MkTA x MkTB x Here A imports B but B imports A with a SOURCE pragma which breaks the circular de pendency For every module A hs that is SOURCE imported in this way there must exist a souce file A hs boot This file contains an abbreviated version of A hs thus module A where newtype TA MkTA Int To compile these three files issue the following commands ghe c A hs boot Poduces A hi boot A o boot ghe c B hs Consumes A hi boot produces B hi B o ghe c A hs Consumes B hi produc
192. help 45 69 interactive 34 make 42 43 mk dll 218 numeric version 45 print libdir 45 user 69 version 45 70 A allocation area size 85 arguments command line 41 ASCII 47 Assertions 187 author package specification 71 auto package specification 71 Bang patterns 185 binary distribution layout 18 binary installations 17 binds unused 63 bug reports contents 3 bugs reporting 3 bundles of binary stuff 17 228 Index bundles binary 19 bundles gransim 19 bundles parallel 19 bundles profiling 19 bundles ticky ticky 20 C C calls function headers 208 C compiler options 78 C pre processor options 76 CAFs in GHCi 39 category package specification 71 cc options package specification 73 Char size of 223 command line arguments 41 compacting garbage collection 85 compiled code in GHCi 28 compiler problems 210 compiling faster 127 Concurrent Haskell using 81 configure 18 19 consistency checks 97 Constant Applicative Form see CAFs constructor fields strict 75 copyright package specification 71 CORE pragma 198 Core syntax how to read 98 core annotation 198 cost centres automatically inserting 116 cost centre profiling 113 cpp pre processing with 76 Creating a Win32 DLL 218 CTAGS for Haskell 212 D debugging options for GHC 89 defaulting mechanism warning 63 dependencies in Makefiles 54 dependency
193. hermore as sketched below you may have big problems run ning programs compiled using unstable interfaces Unlikely A useful be more paranoid option to give to GHC is dcore lint this causes a lint pass to check for errors notably type errors after each Core to Core transformation pass We run with dcore lint on all the time it costs about 5 in compile time If the linker complains about not finding _ lt something gt _fast then something is inconsistent you probably didn t compile modules in the proper dependency order On this score GHC usually does pretty well especially if you allow it to be off by one or two In the case of an instance or class declaration the line number may only point you to the de claration not to a specific method Please report line number errors that you find particularly unhelp ful 9 2 When your program does the wrong thing 210 What to do when something goes wrong For advice about overly slow or memory hungry Haskell programs please see Chapter 6 Advice on sooner faster smaller thriftier Help My program crashed My program entered an absent argument What s with this arithmetic or floating exception e g a segmentation fault or core dumped If your program has no foreign calls in it and no calls to known unsafe functions such as unsafePerformI0 then a crash is always a BUG in the G
194. hrashing and page faults are or you don t know the memory configuration of your ma chine don t try to be clever about memory use you ll just make your life a misery and for other people too probably Because Haskell objects and libraries tend to be large it can take many real seconds to slurp the bits to from a remote filesystem It would be quite sensible to compile on a fast machine using re motely mounted disks then link on a slow machine that had your disks directly mounted It s ugly and slow Deeply nested list comprehensions seem to be one such in the past very large constant tables were bad too 127 Advice on sooner faster smaller thriftier We d rather you reported such behaviour as a bug so that we can try to correct it The part of the compiler that is occasionally prone to wandering off for a long time is the strictness analyser You can turn this off individually with fno strictness To figure out which part of the compiler is badly behaved the v2 option is your friend If your module has big wads of constant data GHC may produce a huge basic block that will cause the native code generator s re gister allocator to founder Bring on fvia C not that GCC will be that quick about it either Explicit import declarations Instead of saying import Foo say import Foo stuff I want You can get GHC to tell you the minimal set of required imports by using the ddump minimal impo
195. ic 700 105 Using GHC Flag Description Static Dynamic Reverse level 1 On Set optimisation level n dynamic 00 4 17 15 Individual optimisations Section 4 9 2 f platform independent flags Flag Description Static Dynamic Reverse fcase merge Enable case merging dynamic fno case merg fdicts strict Make dictionaries strict static fno dicts strict fdo eta reduction Enable eta reduction dynamic fno do eta reduction fdo lambda eta expansion Enable lambda eta reduction dynamic fno do lambda eta expansion including rules generated by automatic specialisation of overloaded functions fexcess precision Enable excess intermediate dynamic precision fno ex cess precision frules off Switch off all rewrite rules static frules off fignore asserts Ignore assertions in the source dynamic fno ignore asserts fignore inter face pragmas Ignore pragmas in interface files dynamic fno ig nore inter face pragmas fliber ate case threshold Tweak the liberate case op timisation default 10 static fno liber ate case threshold fomit inter face pragmas Don t generate interface pragmas dynamic fno omit inter face pragmas fmax worker args If a worker has that many arguments none will be un packed anymore default 10 static fmax s
196. il address list archives lt cvs ghc haskell org gt ht tp www haskell org mai lman listinfo cvs ghc lt cvs ghc admin haskell o rg gt ht tp www haskell org pip ermail cvs ghc There are several other haskell and GHC related mailing lists served by www haskell org Go to http www haskell org mailman listinfo for the full list Some Haskell related discussion also takes in the Usenet newsgroup Introduction to GHC 1 2 Reporting bugs in GHC 1 2 1 1 2 2 Glasgow Haskell is a changing system so there are sure to be bugs in it To report a bug either Preferred Create a new bug http hackage haskell org trac ghc newticket type bug and enter your bug report You can also search the bug database here to make sure your bug hasn t already been reported if it has it might still help to add information from your experience to the existing re port Bug reports can also be emailed to lt glasgow haskell bugs haskell org gt How do tell if I should report my bug Take a look at the FAQ http hackage haskell org trac ghc wiki FAQ and Chapter 9 What to do when something goes wrong which will give you some guidance as to whether the behaviour you re seeing is really a bug or not If it is a bug then it might have been reported before try searching on the bug tracker http hackage haskell org trac ghc and failing that try Google http www google com If in
197. iling lists about Glasgow Haskell We encourage you to join as you feel is ap propriate glasgow haskell users This list is for GHC users to chat among themselves If you have a specific question about GHC please check the FAQ http hackage haskell org trac ghc wiki FAQ first list email address lt glasgow haskell users h askell org gt Introduction to GHC glasgow haskell bugs cvs ghe subscribe at admin email address list archives ht tp www haskell org mai lman listinfo glas gow haskell users lt glasgow haskell users a dmin haskell org gt ht tp www haskell org pip ermail glas gow haskell users Send bug reports for GHC to this address The sad and lonely people who subscribe to this list will muse upon what s wrong and what you might do about it list email address subscribe at admin email address list archives lt glasgow haskell bugs ha skell org gt ht tp www haskell org mai lman listinfo glas gow haskell bugs lt glasgow haskell bugs ad min haskell org gt ht tp www haskell org pip ermail glas gow haskell bugs The hardcore GHC developers hang out here This list also gets com mit message from the CVS repository There are several other simil ar lists for other parts of the CVS repository eg cvs hslibs cvs happy cvs hdirect etc list email address subscribe at admin ema
198. impli fier iterations Set the max iterations for the simplifier dynamic fno state hack Turn off the state hack whereby any lambda with a real world state token as ar gument is considered to be static 106 Using GHC Flag Description Static Dynamic Reverse single entry Hence OK to inline things inside it fno cse Turn off common sub dynamic expression Fno full laziness fno pre inlining Turn off full laziness floating bindings out wards Turn off pre inlining dynamic ffull laziness fno strictness Turn off strictness analysis fun box strict fields Flatten strict constructor fields static dynamic dynamic fno un box strict fields fun Tweak unfolding settings static date in place folding cre fno unfold ation threshold ing cre ation threshold fun Tweak unfolding settings static folding Fno unfold Fun discount ing fun discount fun Tweak unfolding settings _ static folding kee Fno unfold ness factor ing keeness factor fun Tweak unfolding settings _ static Folding up no unfold ng up ate in place Q H fun folding use threshold Tweak unfolding settings static Hh no unfold ng use threshold jie 4 17 16 Profiling options Chapter 5 Profiling Flag Descriptio
199. in GHC type foo 22 forall x x gt T3 GHC currently does kind checking before expanding synonyms though even that could be changed After expanding type synonyms GHC does validity checking on types looking for the following mal formedness which isn t detected simply by kind checking e Type constructor applied to a type involving for alls e Unboxed tuple on left of an arrow e Partially applied type synonym So for example this will be rejected type Pr Int Int As 23h Pr gt Int hx because GHC does not allow unboxed tuples on the left of a function arrow 7 4 1 4 Existentially quantified data constructors The idea of using existential quantification in data type declarations was suggested by Perry and imple mented in Hope Nigel Perry The Implementation of Practical Functional Programming Languages 144 GHC Language Features PhD Thesis University of London 1991 It was later formalised by Laufer and Odersky Polymorphic type inference and abstract data types TOPLAS 16 5 pp1411 1430 1994 It s been in Lennart Au gustsson s hbe Haskell compiler for several years and proved very useful Here s the idea Consider the declaration data Foo forall a MkFoo a a gt Bool Nil The data type Foo has two constructors with types MkFoo forall a a gt a gt Bool gt Foo Nil Foo Notice that the type variable a in the type of MkFoo does not appear in the data ty
200. ind and re turn functions from the MonadT data structure rather than using pattern matching 7 4 8 2 Type inference In general type inference for arbitrary rank types is undecidable GHC uses an algorithm proposed by Odersky and Laufer Putting type annotations to work POPL 96 to get a decidable algorithm by re quiring some help from the programmer We do not yet have a formal specification of some help but the rule is this For a lambda bound or case bound variable x either the programmer provides an explicit polymorphic type for x or GHC s type inference will assume that x s type has no foralls in it What does it mean to provide an explicit type for x You can do that by giving a type signature for x directly using a pattern type signature Section 7 4 10 Lexically scoped type variables thus 166 GHC Language Features f forall a a gt a gt f True f c Alternatively you can give a type signature to the enclosing context which GHC can push down to find the type for the variable f gt f True f c forall a a gt a gt Bool Char Here the type signature on the expression can be pushed inwards to give a type signature for f Similarly and more commonly one can give a type signature for the function itself forall a a gt a gt Bool Char miri ht E Trie fote You don t need to give a type signature if the lambda bound variable is a constructor argumen
201. ing Don t derive use Read unnecessar ily GHC compiles some program con structs slowly By using them you are telling GHC that you are willing to suffer longer compilation times for better quality code GHC is surprisingly zippy for normal compilations without oO Within reason more memory for heap space means less garbage collection for GHC which means less compilation time If you use the Rghc timing option you ll get a garbage collector re port Again you can use the cheap and nasty RTS Sstderr RTS option to send the GC stats straight to standard error If it says you re using more than 20 of total time in garbage col lecting then more memory would help If the heap size is approaching the maximum 64M by default and you have lots of memory try increasing the maximum with the M lt size gt option e g ghe c O M1024m Foo hs Increasing the default allocation area size used by the compiler s RTS might also help use the A lt size gt option If GHC persists in being a bad memory citizen please report it as a bug As soon as GHC plus its fellow citizens other processes on your machine start using more than the real memory on your ma chine and the machine starts thrashing the party is over Com pile times will be worse than terrible Use something like the csh builtin time command to get a report on how many page faults you re getting If you don t know what virtual memory t
202. io n When modifying the package database file a copy of the original file is saved in file old soin an emergency you can always restore the old settings by copying the old file back again InstalledPackageInfo a package specification A package specification is a Haskell record in particular it is the record InstalledPackageInfo libraries Cabal Distribution InstalledPackageInfo html tInstalledPackageInfo in the module Distri bution InstalledPackageInfo which is part of the Cabal package distributed with GHC An InstalledPackageInfo has a human readable writable syntax The functions parseIn stalledPackagelInfo and showInstalledPackageInfo read and write this syntax respect ively Here s an example of the InstalledPackageInfo for the unix package ghc pkg describe unix name unix version 1 0 license BSD3 copyright maintainer libraries haskell org stability homepage package url description category author exposed True xposed modules Syste Syste Syste Syste Syste Syste Syste Syste Syste Syste Syste Syste Syste Syste Posix Posix DynamicLinker Module Posix DynamicLinker Prim Posix Directory Posix DynamicLinker Posix Env Posix Error Posix Files Posix IO Posix Process Posix Resource Posix Temp Posix Terminal Posix Time System Posix Unistd System Posix User System Posix Signals Exts import dirs usr lib ghc 6 4 libraries
203. iographical heap profile displays the portion of the live heap in each of the four states listed above Usually the most interesting states are the void and drag states live heap in these states is more likely to be wasted space than heap in the lag or use states It is also possible to break down the heap in one or more of these states by a different criteria by re stricting a profile by biography For example to show the portion of the heap in the drag or void state by producer prog RTS hc hbdrag void Once you know the producer or the type of the heap in the drag or void states the next step is usually to find the retainer s prog RTS hr hccc 120 Profiling NOTE this two stage process is required because GHC cannot currently profile using both biographical and retainer information simultaneously 5 4 4 Actual memory residency How does the heap residency reported by the heap profiler relate to the actual memory residency of your program when you run it You might see a large discrepancy between the residency reported by the heap profiler and the residency reported by tools on your system eg ps or top on Unix or the Task Manager on Windows There are several reasons for this e There is an overhead of profiling itself which is subtracted from the residency figures by the pro filer This overhead goes away when compiling without profiling support of course The space over head is currently 2 extra wo
204. ion 7 4 1 1 Data types with no constructors but in an hi boot file it means I don t know or care what the constructors are This is the most common form of data type declaration because it s easy to get right You can also write out the constructors but if you do so you must write it out precisely as in its real definition If you do not write out the constructors you may need to give a kind annotation Section 7 4 7 Explicitly kinded quantification to tell GHC the kind of the type variable if it is not In source files this is worked out from the way the type variable is used in the constructors For ex ample data R xf bn w y You cannot use deriving on a data type declaration write in instance declaration instead Class declarations is exactly as in Haskell except that you may not put default method declarations You can also omit all the superclasses and class methods entirely but you must either omit them all or put them all in You can include instance declarations just as in Haskell but omit the where part 4 6 10 Using make It is reasonably straightforward to set up a Makefile to use with GHC assuming you name your source files the same as your modules Thus HC ghc HC_OPTS cpp EXTRA_HC_OPTS SRCS Main lhs Foo lhs Bar lhs OBJS Main o Foo o Bar o 53 Using GHC SUFFIXES o hs hi lhs hc s cool_pgm OBJS rm f S HC o HC_OPTS OBJS
205. ion from being inlined by the compiler You shouldn t ever need to do this unless you re very cautious about code size NOTINLINE is a synonym for NOINLINE NOINLINE is specified by Haskell 98 as the standard way to disable inlining so it should be used if you want your code to be portable 7 10 3 3 Phase control Sometimes you want to control exactly when in GHC s pipeline the INLINE pragma is switched on In lining happens only during runs of the simplifier Each run of the simplifier has a different phase number the phase number decreases towards zero If you use dverbose core2core you ll see the sequence of phase numbers for successive runs of the simplifier In an INLINE pragma you can option ally specify a phase number thus e INLINE k means do not inline f until phase k but from phase k onwards be very keen to inline it 189 GHC Language Features e INLINE k f means be very keen to inline f until phase k but from phase k onwards do not inline it e NOINLINE k f means do not inline f until phase k but from phase k onwards be willing to inline it as if there was no pragma e INLINE k f means be willing to inline f until phase k but from phase k onwards do not in line it The same information is summarised here Before phase 2 Phase 2 and later INLINE 2 No Yes INLINE 2 Yes No NOINLINE 2 f ee No Maybe
206. ion is weaker than saying that a is functionally dependent on a type variable free in type see Section 7 4 3 Functional dependencies The reason for this is there might be a hidden dependency in a superclass perhaps So reachable is a conservative approx imation to functionally dependent For example consider class Cab a gt b where class C a b gt Dab where f forall a b D a bD gt a gt This is fine because in fact a does functionally determine b but that is not immediately apparent from f s type 2 Every constraint ci must mention at least one of the universally quantified type variables tvi For example this type is OK because C a b mentions the universally quantified type variable b forall a C a b gt burble The next type is illegal because the constraint Eq b does not mention a forall a Eq b gt burble The reason for this restriction is milder than the other one The excluded types are never useful or necessary because the offending context doesn t need to be witnessed at this point it can be floated out Furthermore floating them out increases sharing Lastly excluding them is a conservative choice it leaves a patch of territory free in case we need it later 7 4 5 2 For all hoisting It is often convenient to use generalised type synonyms see Section 7 4 1 3 Liberalised type syn onyms at the right hand end of an arrow thus type Discard a forall b a gt
207. ions to Haskell 98 de scribed in Chapter 7 GHC Language Features except where other wise noted New reserved words forall only in types mdo Other syntax stolen varid char string integer float float 1 1 ffi and fffi This option enables the language extension defined in the Haskell 98 Foreign Function Interface Addendum New reserved words foreign 133 GHC Language Features ism restriction fno mono pat binds fexten ded default rules fal low overlap ping instances f4indwnengbese able instances fallow incoher egerrev ances fcontext stack N fgenerics fno implicit prelude fimplicit params fscoped type variables fth These two flags control how generalisation is done See Sec tion 7 14 Control over monomorphism Use GHCi s extended default rules in a regular module Sec tion 3 4 5 Type defaulting in GHCi Independent of the fglasgow exts flag See Section 7 4 4 Instance declarations Only relevant if you also use fglasgow exts See Section 7 11 Rewrite rules Only relevant if you also use fglasgow exts See Section 7 7 Arrow notation fglasgow exts Independent of New reserved words symbols rec proc lt gt lt lt gt gt Other syntax stolen See Section 7 13 Generi
208. is retained by one or more retainer objects collectively called its retainer set or its retainer set or its retainers When retainer profiling is requested by giving the program the hr option a graph is generated which is broken down by retainer set A retainer set is displayed as a set of cost centre stacks because this is usually too large to fit on the profile graph each retainer set is numbered and shown abbreviated on the graph along with its number and the full list of retainer sets is dumped into the file prog prof Retainer profiling requires multiple passes over the live heap in order to discover the full retainer set for each object which can be quite slow So we set a limit on the maximum size of a retainer set where all retainer sets larger than the maximum retainer set size are replaced by the special set MANY The maxim um set size defaults to 8 and can be altered with the R RTS option 119 Profiling si Restrict the number of elements in a retainer set to si ze default 8 ze 5 4 2 1 Hints for using retainer profiling The definition of retainers is designed to reflect a common cause of space leaks a large structure is re tained by an unevaluated computation and will be released once the computation is forced A good ex ample is looking up a value in a finite map where unless the lookup is forced in a timely manner the un evaluated lookup will cause the whole mapping to be retained These kind of space le
209. ise x y See Section 7 8 Bang patterns for more details The restriction that you cannot use two packages together if they contain a module with the same name has been removed In implementation terms the package name is now included in every ex ported symbol name in the object file so that modules with the same name in different packages do not clash See Section 4 8 2 Consequences of packages GHC now treats source files as UTF 8 ASCII is a strict subset of UTF 8 so ASCII source files will continue to work as before However invalid UTF 8 sequences are ignored in comments so ASCII code with comments in for example Latin 1 will also work A way to have Latin 1 source files pre processed by GHC is described in Section 4 10 4 Options affecting a Haskell pre processor GADTs can now use record syntax Also if the datatype could have been declared with Haskell 98 syntax then deriving clauses are permitted For more info see Section 7 5 Generalised Algebraic Data Types GADTs There is a new pragma LANGUAGE which allows extensions to be specified portably i e without having to resort to the OPTIONS_GHC pragma and giving GHC specific options The arguments to the pragma are the same extensions that Cabal knows about More info in Section 7 10 4 LANGUAGE pragma When you use ghe make GHC will now take the executable filename from the name of the file containing the Main module rather than
210. it will be installed in c ghc ghc version The executable binary for GHC will be installed in the bin sub directory of the installation directory you choose 20 Installing GHC If you have already installed the same version of GHC Installshield will offer to modify or re move GHC Choose remove then run setup exe a second time This time it should offer to in stall When installation is complete you should find GHCi and the GHC documentation are available in your Start menu under Start Programs Glasgow Haskell Compiler e The final dialogue box from the install process reminds you where the GHC binary has been in stalled usually c ghc ghc version bin If you want to invoke GHC from a command line add this to your PATH environment variable e GHC needs a directory in which to create and later delete temporary files It uses the standard Win dows procedure Get TempPath to find a suitable directory This procedure returns e The path in environment variable TMP if TMP is set e Otherwise the path in environment variable TEMP if TEMP is set e Otherwise there is a per user default which varies between versions of Windows On NT and XP ish versions it might be c Documents and Settings lt username gt Local Settings Temp The main point is that if you don t do anything GHC will work fine but if you want to control where the directory is you can do so by setting TMP or TEMP e To test the fruits
211. l U gt Bool russel u MkU p not S pu x Bool x russel MkU russel We have never found another class of programs other than this contrived one that makes GHC di verge and fixing the problem would impose an extra overhead on every compilation So the bug re mains un fixed There is more background in Secrets of the GHC inliner http research microsoft com simonpj Papers inlining e GHC does not keep careful track of what instance declarations are in scope if they come from other packages Instead all instance declarations that GHC has seen in other packages are all in scope everywhere whether or not the module from that package is used by the command line expression This bug affects only the make mode and GHCi 12 2 2 Bugs in GHCi the interactive GHC 224 Known bugs and infelicities GHCi does not respect the default declaration in the module whose scope you are in Instead for expressions typed at the command line you always get the default default type behaviour that is default Int Double It would be better for GHCi to record what the default settings in each module are and use those of the current module whatever that is On Windows there s a GNU Id BFD bug whereby it emits bogus PE object files that have more than Oxffff relocations When GHCi tries to load a package affected by this bug you get an error message of the form Loading package javavm linking WARNING Over
212. le where this is useful class C a where 60 Using GHC _simpleFn a gt String complexFn a gt a gt String complexFn x y _simpleFn The idea is that a users of the class will only call complexFn never _SimpleFn and b instance declarations can define either complexFn or _simpleFn 61 Using GHC fwarn name sh fwarn orphans fwarn overlap ping patterns adowing fwarn simpl patterns If you would like GHC to check that every top level function value has a type signature use the fwarn missing signatures option This option is off by default This option causes a warning to be emitted whenever an inner scope value has the same name as an outer scope value i e the inner value shadows the outer one This can catch typographical errors that turn into hard to find bugs e g in the inadvertent cyc lic definition let x x in Consequently this option does will complain about cyclic recurs ive definitions This option causes a warning to be emitted whenever the module contains an orphan instance declaration or rewrite rule An in stance declartion is an orphan if it appears in a module in which neither the class nor the type being instanced are declared in the same module A rule is an orphan if it is a rule for a function de clared in another module A module containing any orphans is called an orphan module The trouble with orphans is that GHC m
213. les which arise from pattern matches in cluding patterns consisting of a single variable For instance f x y would report x and y as unused The warning is sup pressed if the variable name begins with an underscore thus f _x True If you re feeling really paranoid the dcore lint option is a good choice It turns on heavyweight intra pass sanity checking within GHC It checks GHC s sanity not yours 63 Using GHC 4 8 4 8 1 Packages A package is a library of Haskell modules known to the compiler GHC comes with several packages see the accompanying library documentation libraries index html Using a package couldn t be simpler if you re using make or GHCi then most of the installed pack ages will be automatically available to your program without any further options The exceptions to this tule are covered below in Section 4 8 1 Using Packages Building your own packages is also quite straightforward we provide the Cabal http www haskell org cabal infrastructure which automates the process of configuring building in stalling and distributing a package All you need to do is write a simple configuration file put a few files in the right places and you have a package See the Cabal documentation Cabal index html for de tails and also the Cabal libraries Distribution Simple libraries Cabal Distribution Simple html for example Using Packages To see which packages ar
214. ling 5 6 3 5 6 4 Viewing the heap profile of a running program The hp file is generated incrementally as your program runs In principle running hp2ps on the in complete file should produce a snapshot of your program s heap usage However the last sample in the file may be incomplete causing hp2ps to fail If you are using a machine with UNIX utilities installed it s not too hard to work around this problem though the resulting command line looks rather Byz antine head fgrep n END_SAMPLE FOO hp tail 1 cut d f 1 FOO hp hp2ps gt FOO ps The command fgrep n END_SAMPLE FOO hp finds the end of every complete sample in FOO hp and labels each sample with its ending line number We then select the line number of the last complete sample using tail and cut This is used as a parameter to head the result is as if we deleted the final in complete sample from FOO hp This results in a properly formatted hp file which we feed directly to hp2ps Viewing a heap profile in real time The gv and ghostview programs have a watch file option can be used to view an up to date heap pro file of your program as it runs Simply generate an incremental heap profile as described in the previous section Run gv on your profile gv watch seascape FOO ps If you forget the watch flag you can still select Watch file from the State menu Now each time you generate a new profile FOO ps the view will update au
215. ling parallel and other build options The various versions are given different suffixes to distinguish them for example the profiling version of the standard prelude library is named 1ibHSbase_p a with the _p indicating that this is a profiling version The suffix is added automatically by GHC for hs libraries only no suffix is added for libraries in extra libraries The libraries listed in ext ra libraries may be any libraries supported by your system s linker including dynamic libraries so on Unix DLL on Win dows Also ext ra libraries are placed on the linker command line after the hs libraries for the same package If your package has dependencies in the other direction i e extra libraries depends on hs libraries and the lib raries are static you might need to make two separate packages string list A list of directories containing C includes for this package string list A list of files to include for via C compilations using this package Typically the include file s will contain function prototypes for any C functions used in the package in case they end up being called as a result of Haskell func tions from the package being inlined package name list Packages on which this package depends This field contains packages with explicit versions are required except that when submitting a pack age to ghc pkg register the versions will be filled in if they are unambigu ous string list Options to pass to
216. ll be exported This has various pessimising effects For example if a bit of code is actu ally unused perhaps because of unfolding effects GHC will not 130 lazy Advice on sooner faster smaller thriftier Look at the Core syntax Use strictness annotations Use unboxed types a GHC exten sion Use foreign import a GHC extension to plug into fast librar ies Don t use Floats be able to throw it away because it is exported and some other module may be relying on its existence GHC can be quite a bit more aggressive with pieces of code if it knows they are not exported The form in which GHC manipulates your code Just run your compilation with ddump simp1 don t forget the 0 If profiling has pointed the finger at particular functions look at their Core code lets are bad cases are good dictionaries d lt Class gt lt Unique gt or anything overloading ish are bad nested lambdas are bad explicit data constructors are good primitive operations e g eqInt are good Putting a strictness annotation on a constructor field helps in two ways it adds strictness to the program which gives the strict ness analyser more to work with and it might help to reduce space leaks It can also help in a third way when used with fun box strict fields see Section 4 9 2 f platform independent flags a strict field can be unpacked or unboxed in the constructor and
217. ll take the form word where word indicates the type of pragma and is fol lowed optionally by information specific to that type of pragma Case is ignored in word The various values for word that GHC understands are described in the following sections any pragma encountered with an unrecognised word is silently ignored 187 GHC Language Features 7 10 1 DEPRECATED pragma The DEPRECATED pragma lets you specify that a particular function class or type is deprecated There are two forms e You can deprecate an entire module thus module Wibble DEPRECATED Use Wobble instead wher When you compile any module that import Wibble GHC will print the specified message e You can deprecate a function class type or data constructor with the following top level declara tion DEPRECATED f C T Don t use thes W When you compile any module that imports and uses any of the specified entities GHC will print the specified message You can only depecate entities declared at top level in the module being compiled and you can only use unqualified names in the list of entities being deprecated A capitalised name such as T refers to either the type constructor T or the data constructor T or both if both are in scope If both are in scope there is currently no way to deprecate one without the other c f fixities Section 7 4
218. local GHC installation the following trick is useful ghc E optP dM cpp foo hs cat foo hspp you need a file foo hs but it isn t actually used __HASKELL98 __HASKELL 98 __HASKELL1 __GLASGOW_H ASKELL__ CONCURREN T_HASKELL __ PARALLEL HASKELL__ _HOST_OS osl _HOST_A archRCH 1 If defined this means that GHC supports the language defined by the Haskell 98 re port In GHC 4 04 and later the _ HASKELL__ macro is defined as having the value 98 If defined to n that means GHC supports the Haskell language defined in the Haskell report version n Currently 5 This macro is deprecated and will probably disappear in future versions For version x y z of GHC the value of __GLASGOW_HASKELL___ is the integer xyy if y is a single digit then a leading zero is added so for example in version 6 2 of GHC __GLASGOW_HASKELL___ 602 More information in Section 1 3 GHC version numbering policy With any luck _ GLASGOW_HASKELL___ will be undefined in all other implement ations that support C style pre processing For reference the comparable symbols for other systems are __HUGS___for Hugs __NHC__ for nhc98 and__ HBC__ for hbc NB This macro is set when pre processing both Haskell source and C source includ ing the C source generated from a Haskell module i e hs lhs c and he files This symbol is defined when pre processin
219. lts whose representations are identical to those of the compiler itself Template Haskell works in any mode make interactive or file at a time There used to be a restriction to the former two but that restriction has been lifted 7 6 3 A Template Haskell Worked Example 177 GHC Language Features To help you get over the confidence barrier try out this skeletal worked example First cut and paste the two modules below into Main hs and Printf hs Main hs module Main where Import our template pr import Printf pr The splice operator takes the Haskell source code generated at compile time by pr and splices it into the argument of putStrLn main putStrLn pr Hello Printf hs module Printf where Skeletal printf from the paper It needs to be in a separate module to the one where you intend to use it Import some Template Haskell syntax import Language Haskell TH Describe a format string data Format D S L String Parse a format string This is left largely to you as we are here interested in building our first ever Template Haskell program and not in building printf parse String gt Format parse s Ls Generate Haskell source code from a parsed representation of the format string This code will be spliced into the module which calls pr at compile time
220. ly and never change the program mer interface to any system supplied code However if you in stall a new patchlevel over an old one you will need to recompile any code that was compiled against the old libraries The value of __GLASGOW_HASKELL__ see Section 4 10 3 Options affecting the C pre processor for a major release x y z is the integer xyy if y is a single digit then a leading zero is added so for example in version 6 2 of GHC __GLASGOW_HASKELL___ 602 Snapshots unstable releases We may make snapshot releases of the current development sources from time to time and the current sources are always available via the CVS repository see the GHC web site http www haskell org ghc for details Snapshot releases are named x y YYYYMMDD where YYYYMM DD is the date of the sources from which the snapshot was built In theory you can check out the exact same sources from the CVS repository using this date If y is odd then this is a snapshot of the CVS HEAD the main development branch If y is even then it is a snapshot of the stable branch between patchlevel releases For example 6 3 20040225 would be a snapshot of the HEAD but 6 2 20040225 would be a snapshot of the 6 2 branch The value of __GLASGOW_HASKELL__ for a snapshot release is the integer xyy You should never write any conditional code which tests for this value however since interfaces change on a day to day basis and we don t have fi
221. ly a bug Optimise using 0O or 02 This is the most basic way to make your program go faster Com pilation time will be slower especially with 02 At present O2 is nearly indistinguishable from O Compile via C and crank up GCC The native code generator is designed to be quick not mind bogglingly clever Better to let GCC have a go as it tries much harder on register allocation etc 128 Advice on sooner faster smaller thriftier At the moment if you turn on O you get GCC instead This may change in the future So when we want very fast code we use O fvia C Overloaded functions are not your Haskell s overloading using type classes is elegant neat etc friend etc but it is death to performance if left to linger in an inner loop How can you squash it Give explicit type signatures Signatures are the basic trick putting them on ex ported top level functions is good software engin eering practice anyway Tip using fwarn miss ing signatures can help enforce good signa ture practice The automatic specialisa tion of overloaded func tions with O should take care of overloaded local and or unexported func tions Use SPECIALIZE pragmas Specialize the overloading on key functions in your program See Sec tion 7 10 8 SPECIALIZE pragma and Sec tion 7 10 9 SPECIALIZE instance pragma But how do I know where over A low tech wa
222. m pilation System User s Guide Version 2 2 1 Installing GHC on Windows Aspre i cece cece E e E sees eens eeaes 20 2 22 Moving GHC around oy sni ck Sh des og Gah ee de Cease te wt Side eda sens al aa dan sates eM astiesh ieee PONG 21 2 23 Installing ghe win32 FAO siuis ee east hs 5 iis eke dey es Se NG vase A a Ge a ee 21 2 3 The layout of installed TES ssis aaee Settee te Saee se pecan se le ole tila E A E OE AERES 22 2 3 1 Th binary directory sorses eoesesastoy Siva sie ee rE EEEE E EEES E E EEEE es 22 2 3 2 The library directory meiiens eies cheb sid aat pha E ds anon E AEE E TE EEES 22 3 Using GHUI a haerea res eene E E E EE AEE E EEEE E SEE EE EEE E EEEIEE E Eene 25 3 1 Introductionito GHCI snide ieres ere r sE Oa o SESE E ea PESE i PORTEE ESARET EERO PORER Er 25 3 2 Loading Source files scianie erie i EEE noes E E ETE EES EE EESE AT EEE E EE ds 26 3 2 1 M dules vs filenames esses mise ottiene a ea a E E Aea O EERTE 27 3 2 2 Making changes and recompilation sssesssesseeseesreressrerrsrrsrrrrrsrrresrrerrerreerereseree 27 3 3 Loading compiled code aoeeoe a bee E E lung E E E E EE E EEE RS ESS 28 3 4 Interactive evaluation at the prompt sessessessseresrserrsrrerrsresrrererrrrrsrrerrsrrerrsrerrrreerrrersreet 29 3 4 1 1 O actions at the prompt ssnsti vee eke ek E deh ede aceon E 30 3 4 2 Using do notation at the prompt 2 0 0 0 ce cece cece eece ence eeceeeeeeeeueeeeeeaeeea esau esas eeaes 30 3 4 3 What s r
223. main otherwise GHC will try to link to the Main Haskell module 8 2 1 2 Using foreign import ccall wrapper with GHC When foreign import ccall wrapper is used in a Haskell module The C stub file M_stub c generated by GHC contains small helper functions used by the code generated for the im ported wrapper so it must be linked in to the final program When linking the program remember to in The outermost hs_exit will actually de initialise the system NOTE that currently GHC s runtime cannot reliably re initialise after this has happened 207 Foreign function interface FFI 8 2 2 clude M_stub o in the final link command line or you ll get link errors for the missing function s this isn t necessary when building your program with ghc make as GHC will automatically link in the correct bits Using function headers When generating C using the fvia C directive one can assist the C compiler in detecting type er rors by using the include directive Section 4 10 5 Options affecting the C compiler if applic able to provide h files containing function headers For example finclude HsFFI h void initialiseEFS HsInt size HsInt terminateEFS void HsForeignObj emptyEFS void HsForeignObj updateEFS HsForeignObj a HsInt i HsInt x HsInt lookupEFS HsForeignObj a HsInt i The types Hs Int HsForeignObj etc are described in the H98 FFI Addendum Note that
224. matically exported from a module just as instance declarations are 7 11 2 Semantics From a semantic point of view e Rules are only applied if you use the O flag e Rules are regarded as left to right rewrite rules When GHC finds an expression that is a substitution instance of the LHS of a rule it replaces the expression by the appropriately substituted RHS By a substitution instance we mean that the LHS can be made equal to the expression by substituting for the pattern variables e The LHS and RHS of a rule are typechecked and must have the same type e GHC makes absolutely no attempt to verify that the LHS and RHS of a rule have the same meaning That is undecidable in general and infeasible in most interesting cases The responsibility is entirely 194 GHC Language Features the programmer s GHC makes no attempt to make sure that the rules are confluent or terminating For example Loop forall x y x ye fy x This rule will cause the compiler to go into an infinite loop If more than one rule matches a call GHC will choose one arbitrarily to apply GHC currently uses a very simple syntactic matching algorithm for matching a rule LHS with an expression It seeks a substitution which makes the LHS and expression syntactically equal modulo alpha conversion The pattern rule but not the expression is eta expanded if necessary Eta expanding the expression can lead to laziness bugs But not beta co
225. mber and filename of the original code for example LINE 42 Foo vhs if you d generated the current file from something called Foo vhs and this line corresponds to line 42 in the original GHC will adjust its error messages to refer to the line file named in the LINE pragma 190 GHC Language Features 7 10 6 OPTIONS GHC pragma The OPTIONS_GHC pragma is used to specify additional options that are given to the compiler when compiling this source file See Section 4 1 2 command line options in source files for details Previous versions of GHC accepted OPTIONS rather than OPTIONS_GHC but that is now deprecated 7 10 7 RULES pragma The RULES pragma lets you specify rewrite rules It is described in Section 7 11 Rewrite rules 7 10 8 SPECIALIZE pragma UK spelling also accepted For key overloaded functions you can create extra versions NB more code space specialised to particular types Thus if you have an overloaded function hammeredLookup Ord key gt key value gt key gt value If it is heavily used on lists with Widget keys you could specialise it as follows SPI A SPECIA ECIALIZE hammeredLookup Widget value gt Widget gt value IZE pragma for a function can be put anywhere its type signature could be put A SPECIA IZE has the effect of generating a a specialised version of the function and b
226. ments The operator must also not use any variable defined within the current arrow abstraction We could define our own operator untilA ArrowChoice a gt a Q gt a Bool gt ae untilA body cond proc x gt if cond x then returnA lt else do body lt x untilA body cond lt x and use it in the same way Of course this infix syntax only makes sense for binary operators there is also a more general syntax involving special brackets proc x gt do y lt ft S lt axel JuntilA increment lt x y within 0 5 lt x Primitive constructs Some operators will need to pass additional inputs to their subcommands For example in an arrow type supporting exceptions the operator that attaches an exception handler will wish to pass the exception that occurred to the handler Such an operator might have a type handleA gt a G gt a 6 Ex gt a ec where Ex is the type of exceptions handled You could then use this with arrow notation by writing a command body handleA ex gt handler so that if an exception is raised in the command body the variable ex is bound to the value of the ex ception and the command handler which typically refers to ex is entered Though the syntax here looks like a functional lambda we are talking about commands and something different is going on The input to the arrow represented by a command consists of values for the free local variables i
227. mes it s using If things don t work as expected check the list of known pitfalls in the building guide When installing the user invokable binaries this installation procedure will install GHC as ghc x xx where x xx is the version number of GHC It will also make a link in the binary installation directory from ghc to ghc x xx If you install multiple versions of GHC then the last one wins and ghc will invoke the last one installed You can change this manually if you want But regardless ghc x xx should always invoke GHC version x xx 2 1 2 2 What bundles there are There are plenty of non basic GHC bundles The files for them are called ghc x xx bundle platform tar gz where the platform is as above and bundle is one of these prof Profiling with cost centres You probably want this par Parallel Haskell features sits on top of PVM You ll want this if you re into that kind of thing gran The GranSim parallel Haskell simulator hmm mainly for implementors 19 Installing GHC Ticky ticky profiling very detailed information about what happened when I ran this pro gram really for implementors One likely scenario is that you will grab two binary bundles basic and profiling We don t usually make the rest although you can build them yourself from a source distribution The various GHC bundles are designed to be unpacked into the same directory then installing as p
228. n e Template Haskell used to have limited support for type signatures in patterns but since that design is in flux for Haskell let alone Template Haskell we ve removed type signatures in patterns from Template Haskell e GHC now supports postfix operators as a simple generalisation of left sections Section 7 3 6 Postfix operators e Parallel arrays as enabled by fparr no longer work They ll be coming back shortly in full glory GHCi changes e GHCi now allows tab completion of in scope names and modules on platforms that use readline i e not Windows e GHCi now has a main command that allows you to call the main function with command line ar guments See Section 3 6 GHCi commands for more information e GHCi now has ctags and etags commands to generate tags files for vi style and emacs style editors respectively See Section 3 6 GHCi commands for more information e GHCi now has an edit command which pops up an editor on the most recently loaded file or a specified file See Section 3 6 GHCi commands for more information e GHCi now invokes print by default on the result of IO actions and bindings at the prompt This is occasionally not what you want so it can be disabled at least for bindings with set fno print bind result See Section 3 4 2 Using do notation at the prompt Introduction to GHC 1 4 3 Libraries Libraries are now divided into core libra
229. n either way we d be interested to hear from you One significant limitation with the current implementation is that the garbage collector is still single threaded and all execution must stop when GC takes place This can be a significant bottleneck in a par allel program especially if your program does a lot of GC If this happens to you then try reducing the cost of GC by tweaking the GC settings Section 4 14 3 RTS options to control the garbage collector enlarging the heap or the allocation area size is a good start 4 13 Platform specific Flags Some flags only make sense for particular target platforms 82 Using GHC 83 Using GHC iX86 machines GHC tries to steal four registers from GCC for perform ance reasons it almost always works However when GCC is compiling some modules with four stolen registers it will crash probably saying Foo he 533 fixed or forbidden register was spilled This may be due to a compiler bug or to impossible asm statements or clauses Just give some registers back with monly N regs Try 3 first then 2 If 2 doesn t work please report the bug to us 4 14 Running a compiled program To make an executable program the GHC system compiles your code and then links it with a non trivial runtime system RTS which handles storage management profiling etc You have some control over the behaviour of the RTS by giving special command line arguments
230. n Static Dynamic Reverse auto Auto add _scc_s to all ex static no auto ported functions auto all Auto add _scc_s to all static no auto all top level functions caf all Auto add _scc_s to all static no caf all CAFs prof Turn on profiling static ticky Turn on ticky ticky profil ing static 4 17 17 Haskell pre processor options 107 Using GHC Section 4 10 4 Options affecting a Haskell pre processor Flag Description Static Dynamic Reverse F Enable the use of a pre dynamic processor set with pgmF 4 17 18 C pre processor options Section 4 10 3 Options affecting the C pre processor Flag Description Static Dynamic Reverse cpp Run the C pre processor on dynamic Haskell source files Dsymbol value Define a symbol in the C dynamic Usymbol pre processor search list for include files Usymbol Undefine a symbol in the C dynamic pre processor Idir Add dir to the directory dynamic 4 17 19 C compiler options Section 4 10 5 Options affecting the C compiler if applicable Flag Description Static Dynamic Reverse include file Include file when com dynamic piling the hc file 4 17 20 Code generation options Section 4 10 6 Options affecting code generation Flag Description Static Dynamic Reverse fasm Use the native code gener dynamic fvia C ator f
231. n bugs and infelicities 12 1 Haskell 98 vs Glasgow Haskell language non compliance This section lists Glasgow Haskell infelicities in its implementation of Haskell 98 See also the when things go wrong section Chapter 9 What to do when something goes wrong for information about crashes space leaks and other undesirable phenomena The limitations here are listed in Haskell Report order roughly 12 1 1 Divergence from Haskell 98 12 1 1 1 Lexical syntax The Haskell report specifies that programs may be written using Unicode GHC only accepts the ISO 8859 1 character set at the moment Certain lexical rules regarding qualified identifiers are slightly different in GHC compared to the Haskell report When you have module reservedop such as M GHC will interpret it as a single qualified operator rather than the two lexemes M and 12 1 1 2 Context free syntax GHC is a little less strict about the layout rule when used in do expressions Specifically the restric tion that a nested context must be indented further to the right than the enclosing context is relaxed to allow the nested context to be at the same level as the enclosing context if the enclosing context is a do expression For example the following code is accepted by GHC main do args lt getArgs if null args then return else do ps lt mapM process args mapM print ps GHC doesn t do fixity resolution in expressions during parsing
232. n function is A foo Strictly speaking main is is not a link phase flag at all it has no effect on the link step The flag must be specified when compiling the module containing the spe cified main function e g module A in the latter two items above It has no effect for other modules and hence can safely be given to ghc make However if all the modules are otherwise up to date you may need to force recompilation both of the module where the new main is and of the module where the main function used to be ghc is not clever enough to figure out that they both need recompiling You can force recompilation by removing the object file or by using the fforce recomp flag 80 Using GHC no hs main In the event you want to include ghc compiled code as part of another non Haskell program the RTS will not be supplying its definition of main at link time you will have to To signal that to the compiler when linking use no hs main See also Section 8 2 1 1 Using your own main Notice that since the command line passed to the linker is rather involved you prob ably want to use ghc to do the final link of your mixed language application This is not a requirement though just try linking once with v on to see what options the driver passes through to the linker The no hs main flag can also be used to persuade the compiler to do the link step in make mode when there is no Haskell Main module prese
233. n if the function is recurs ive The motivating example is this A GADT for arrays with type indexed representation data Arr e where ArriInt Int gt ByteArray gt Arr Int ArrPair Int gt Arr el gt Arr e2 gt Arr el e2 Arr e gt Int gt e SPECIALISE INLINE Arr Int gt Int gt Int SPECIALISE INLINE Arr a b gt Int gt a b ArrInt _ ba I i I indexIntArray ba i ArrPair _ al a2 i al i a2 i Here is a recursive function that indexes arrays of type Arr e Consider a call to at type Int Int The second specialisation will fire and the specialised function will be inlined It has two calls to both at type Int Both these calls fire the first specialisation whose body is also inlined The result is a type based unrolling of the indexing function T Warning you can make GHC diverge by using SPECIALISE INLINE on an ordinarily recursive function Note In earlier versions of GHC it was possible to provide your own specialised function for a given type SPECIALIZE hammeredLookup Int value gt Int gt value intLookup This feature has been removed as it is now subsumed by the RULES pragma see Section 7 11 4 Specialisation 7 10 9 SPECIALIZE instance pragma Same idea except for instance declarations For example gt Eq Foo a where
234. n t work on Windows at the moment however all the ma chinery is still there If you re interested contact the GHC team Note that building an entire Haskell ap plication as a single DLL is still supported it s just multi DLL Haskell programs that don t work The Windows distribution of GHC contains static libraries only 217 Running GHC on Win32 systems 11 5 1 Creating a DLL Sealing up your Haskell library inside a DLL is straightforward compile up the object files that make up the library and then build the DLL by issuing a command of the form ghc mk dll o foo dll bar o baz o wibble a lfooble By feeding the ghc compiler driver the option mk d11 it will build a DLL rather than produce an executable The DLL will consist of all the object files and archives given on the command line A couple of things to notice e By default the entry points of all the object files will be exported from the DLL when using mk d11 Should you want to constrain this you can specify the module definition file to use on the command line as follows ghe mk dll o optdll def optdllMyDef def See Microsoft documentation for details but a module definition file simply lists what entry points you want to export Here s one that s suitable when building a Haskell COM server DLL EXPORTS D11CanUnloadNow D11CanUnloadNow 0 D11GetClassObject D1llGetClassObject 12 DllRegisterServer DllRegisterSe
235. n the command plus a stack of anonymous values In all the prior examples this stack was empty In the second argument to hand1eA this stack consists of one value the value of the exception The com mand form of lambda merely gives this value a name More concretely the values on the stack are paired to the right of the environment So operators like handleA that pass extra inputs to their subcommands can be designed for use with the notation by 183 GHC Language Features 7 1 5 7 7 6 pairing the values with the environment in this way More precisely the type of each argument of the operator and its result should have the form a N E E S N E E Rn He where e is a polymorphic variable representing the environment and t i are the types of the values on the stack with t 1 being the top The polymorphic variable e must not occur in a ti or t However the arrows involved need not be the same Here are some more examples of suitable operators bracketA gt a p gt a e b c gt a e c d gt aed runReader gt a e c gt a e State c runState gt a e c gt a e State c State We can supply the extra input required by commands built with the last two by applying them to ordin ary expressions as in proc x gt do s lt 3 runReader do s which adds s to the stack of inputs to the command built using runReader The command versions of lambda abstracti
236. n32 only Support files for gcc specs gcc configuration cpp0 exe gcc support binaries as exe ld exe crt0 o Standard etc binaries libmingw32 a Standard etc libraries ces Include files imports GHC interface files std hi std library lang hi lang library EEC include C header files StgMacros h GHC specific etc header files mingw h Win32 only Mingwin header files libHSrts a GHC library archives libHSstd a libHSlang a a SCR C HSstdl o GHC library linkables HSstd2 o0 used by ghci which does HSlang o not grok a files yet Note that e libdir also contains support binaries These are not expected to be on the user s PATH but 23 Installing GHC and are invoked directly by GHC In the Makefile system this directory is also called libexecdir but you are not free to change it It must be the same as libdir We distribute gcc with the Win32 distribution of GHC so that users don t need to install gcc nor need to care about which version it is All gcc s support files are kept in libdir gcc lib Similarly we distribute per1 and a touch replacement touchy exe with the Win32 distribu tion of GHC The support programs ghc split and ghc asm are Perl scripts The first line says bin per1 on Unix the script is indeed invoked as a shell script which invokes Perl on Win dows GHC invokes libdir perl exe directly which treats the bin perl as a com me
237. nce C a gt C a where For example these are OK instance C Int a Multiple parameters instance Eq S a Structured type in head Repeated type variable in head 154 GHC Language Features instance C4 a a gt C4 a a instance Stateful ST s MutVar s Head can consist of type variables only instance Ca instance Eq a Show b gt C2 ab Non type variables in context instance Show s a gt Show Sized s a nstance C2 Int a gt C3 Bool a instance C2 Int a gt C3 a b But these are not Context assertion no smaller than head instance C a gt C a where C b b has more more occurrences of b than the head instance C b b gt Foo b where The same restrictions apply to instances generated by deriving clauses Thus the following is accep ted data MinHeap h a H a h a deriving Show because the derived instance instance Show a Show h a gt Show MinHeap h a conforms to the above rules A useful idiom permitted by the above rules is as follows If one allows overlapping instance declara tions then it s quite convenient to have a default instance declaration that applies if something more specific does not instance C a where op Default You can find lots of background material about the reason for these restrictions in the paper Understand ing functional dependencies via Constraint Handling Rules http re
238. ncies on any imported package modules including Prelude and all other standard Haskell libraries Dependen cies are not traced recursively into packages dependencies are only generated for home package modules on external package modules directly imported by the home package module This op tion is normally only used by the various system libraries 4 6 12 Orphan modules and instance declarations Haskell specifies that when compiling module M any instance declaration in any module below M is visible Module A is below M if A is imported directly by M or if A is below a module that M im ports directly In principle GHC must therefore read the interface files of every module below M just in case they contain an instance declaration that matters to M This would be a disaster in practice so GHC tries to be clever In particular if an instance declaration is in the same module as the definition of any type or class men 56 Using GHC tioned in the head of the instance declaration then GHC has to visit that interface file anyway Example module A where instance C a gt D T a where data T a The instance declaration is only relevant if the type T is in use and if so GHC will have visited A s in terface file to find T s definition The only problem comes when a module contains an instance declaration and GHC has no other reason for visiting the module Example module Orphan where instance C a gt D T
239. nd hence if the type t is known then s will be uniquely determined The benefit of including dependency information is that it allows us to define more general multiple parameter classes without ambiguity problems and with the benefit of more accurate types To illus trate this we return to the collection class example and annotate the original definition of Collects with a simple dependency class Collects ee gt wher empty i ce insert gt G ae member SFE gt Bool The dependency ce gt e here specifies that the type e of elements is uniquely determined by the type of the collection ce Note that both parameters of Collects are of kind there are no constructor classes here Note too that all of the instances of Collects that we gave earlier can be used together with this new definition What about the ambiguity problems that we encountered with the original definition The empty func tion still has type Collects e ce gt ce but it is no longer necessary to regard that as an ambiguous type Although the variable e does not appear on the right of the gt symbol the dependency for class Collects tells us that it is uniquely determined by ce which does appear on the right of the gt symbol Hence the context in which empty is used can still give enough information to determine types for both ce and e without ambiguity More generally we need only regard a type as ambiguous if it contains a variable on the l
240. nd after a statement is entered at the prompt If the statement is a single expression then the only variable binding will be for the variable it Setting GHC command line options in GHCi Normal GHC command line options may also be set using set For example to turn on fglasgow exts you would say Prelude gt set fglasgow exts Any GHC command line option that is designated as dynamic see the table in Section 4 17 Flag refer ence may be set using set To unset an option you can set the reverse option Prelude gt set fno glasgow exts Section 4 17 Flag reference lists the reverse for each option where applicable Certain static options package I i and 1 in particular will also work but some may not take effect until the next reload 3 8 The ghci file When it starts GHCi always reads and executes commands from HOME ghci followed by ghci The ghci in your home directory is most useful for turning on favourite options eg set s and defining useful macros Placing a ghci file in a directory with a Haskell project is a useful way to set certain project wide options so you don t have to type them everytime you start GHCi eg if your project uses GHC extensions and CPP and has source files in three subdirectories A B and C you might put the following lines in ghci set fglasgow exts cpp set iA B C Note that strictly speaking the i flag is
241. nd more bindings will typecheck For example consider f Eq a gt a gt Bool f x x x g True g Yes gy y lt y True This is rejected by Haskell 98 but under Jones s scheme the definition for g is typechecked first separ ately from that for f because the reference to f in g s right hand side is ingored by the dependency ana lysis Then g s type is generalised to get g Ord a gt a gt Bool Now the defintion for f is typechecked with this type for g in the type environment The same refined dependency analysis also allows the type signatures of mutually recursive functions to have different contexts something that is illegal in Haskell 98 Section 4 5 2 last sentence With fglasgow exts GHC only insists that the type signatures of a refined group have identical type sig natures in practice this means that only variables bound by the same pattern binding must have the same context For example this is fine f Eq a gt a gt Bool f x x x g True g Ord a gt a gt Bool gy Cy lt oy Ue CE True 173 GHC Language Features 7 5 Generalised Algebraic Data Types GADTs Generalised Algebraic Data Types generalise ordinary algebraic data types by allowing you to give the type signatures of constructors explicitly For example data Term a where Lit Int gt Term Int Succ Term Int gt Term Int IsZero Term Int gt Term Bool
242. ned above us dcore lint to amp my_prog KEEL Yes Of course if you have foreign calls in your program then all bets are off because you can trash the heap the stack or whatever This is definitely caused by a bug in GHC Please report it see Section 1 2 Reporting bugs in GHC Int Float and Double arithmetic is unchecked Overflows underflows and loss of precision are either silent or reported as an exception by the operating system depending on the platform Divide by zero may cause an untrapped exception please report it if it does 211 Chapter 10 Other Haskell utility programs 10 1 10 1 1 10 2 This section describes other program s which we distribute that help with the Great Haskell Program ming Task Ctags and Etags for Haskell hasktags hasktags is a very simple Haskell program that produces ctags tags and etags TAGS files for Haskell programs When loaded into an editor such an NEdit Vim or Emacs this allows one to easily navigate around a multi file program finding definitions of functions types and constructors Invocation Syntax hasktags files This will read all the files listed in files and produce a ctags tags file and an etags TAGS file in the current directory Example usage find name hs xargs hasktags This will find all haskell source files in the current directory and below and create tags files indexing them in the current direct
243. ner granularity in the values of __GLASGOW_HASKELL__ you should only conditionally compile using predicates which test whether __GLASGOW_HASKELL___ is equal to later than or earlier than a given major release The version number of your copy of GHC can be found by invoking ghc with the version flag see Section 4 5 Help and verbosity options 1 4 Release notes for version 6 6 1 4 1 User visible compiler changes GHC now supports SMP when you compile with threaded you now get an RTS flag N that allows you to specify the number of OS threads that GHC should use Defaults to 1 See Sec tion 4 12 Using SMP parallelism and Section 7 15 Parallel Haskell GHC now handles impredicative polymorphism see Section 7 4 9 Impredicative polymorphism There are significant changes to the way scoped type variables work and some programs that used to compile may no longer do so The new story is documented in Section 7 4 10 Lexically scoped type variables Simon s e mail http www haskell org pipermail glasgow haskell users 2006 January 009565 html gives some 4 Introduction to GHC background but the user manual should be complete tell us if not and certainly takes precedence if there is any conflict GHC now supports bang patterns to require a function is strict in a given argument e g f x y x y is equivalent to f x y x gt seq False undefined otherw
244. ng yourself with numer ical errors 131 Advice on sooner faster smaller thriftier One time when Float might be a good idea is if you have a lot of them say a giant array of Floats They take up half the space in the heap compared to Doubles However this isn t true on a 64 bit machine Use unboxed arrays UArray GHC supports arrays of unboxed elements for several basic arith metic element types including Int and Char see the Data Array Unboxed library for details These arrays are likely to be much faster than using standard Haskell 98 arrays from the Data Array library Use a bigger heap If your program s GC stats S RTS option indicate that it s do ing lots of garbage collection say more than 20 of execution time more memory might help with the M lt size gt or A lt size gt RTS options see Section 4 14 3 RTS options to con trol the garbage collector 6 3 Smaller producing a program that is smal ler Decrease the go for it threshold for unfolding smallish expressions Give a fun folding use thresholdo0 option for the extreme case Only unfoldings with zero cost should proceed Warning except in certain specialised cases like Happy parsers this is likely to actually in crease the size of your program because unfolding generally enables extra simplifying optimisations to be performed Avoid Read Use st rip on your executables 6 4 Thriftier producing a progr
245. ning of the module for example the module A B C must begin module A B C It is a common strategy to use the as keyword to save some typing when using qualified names with hierarchical modules For example import qualified Control Monad ST Strict as ST For details on how GHC searches for source and interface files in the presence of hierarchical modules see Section 4 6 3 The search path GHC comes with a large collection of libraries arranged hierarchically see the accompanying library documentation There is an ongoing project to create and maintain a stable set of core libraries used by several Haskell compilers and the libraries that GHC comes with represent the current status of that project For more details see Haskell Libraries http www haskell org simonmar libraries libraries html 7 3 2 Pattern guards 137 GHC Language Features The discussion that follows is an abbreviated version of Simon Peyton Jones s original proposal http research microsoft com simonpj Haskell guards html Note that the proposal was written be fore pattern guards were implemented so refers to them as unimplemented Suppose we have an abstract data type of finite maps with a lookup operation lookup FiniteMap gt Int gt Maybe Int The lookup returns Nothing if the supplied key is not in the domain of the mapping and Just v otherwise where v is the value that the key maps to Now consider the following de
246. nst v Similarly in Figure 4 of Section 3 17 3 http haskell org onlinereport exps html sect3 17 3 add a new case t case v of pat gt e _ gt e v seq case v of pat gt e _ gt e That leaves let expressions whose translation is given in Section 3 12 http haskell org onlinereport exps html sect3 12 of the Haskell Report In the translation box first apply the following transformation for each pattern pi that is of form qi ei transform it to xi qi ei and and replace e0 by xi seq e0 Then when none of the left hand side patterns have a bang at the top apply the rules in the existing box The effect of the let rule is to force complete matching of the pattern qi before evaluation of the body is begun The bang is retained in the translated form in case qi is a variable thus let y fx inb The let binding can be recursive However it is much more common for the let binding to be non recursive in which case the following law holds let p rhs in body is equivalent to case rhs of p gt body 186 GHC Language Features A pattern with a bang at the outermost level is not allowed at the top level of a module 7 9 Assertions 7 10 If you want to make use of assertions in your standard Haskell code you could define a function like the following assert Bool gt a gt a assert False x error assertion failed assert _ x x which works but gives you b
247. nstance Eq Foo Int Bar instance Eq a SPECIALIZI usual stuff CI H The pragma must occur inside the where part of the instance declaration Compatible with HBC by the way except perhaps in the placement of the pragma 7 10 10 UNPACK pragma The UNPACK indicates to the compiler that it should unpack the contents of a constructor field into the constructor itself removing a level of indirection For example UNPACK Float UNPACK Float data T T will create a constructor T containing two unboxed floats This may not always be an optimisation if the T constructor is scrutinised and the floats passed to a non strict function for example they will have to 192 GHC Language Features 7 11 7 11 1 be reboxed this is done automatically by the compiler Unpacking constructor fields should only be used in conjunction with O in order to expose unfoldings to the compiler so the reboxing can be removed as often as possible For example foie sa gt BbOatk f T f1 2 f1 2 The compiler will avoid reboxing f1 and f2 by inlining on floats but only when O is on Any single constructor data is eligible for unpacking for example data T T UNPACK Int Int will store the two Ints directly in the T constructor by flattening the pair Multi level unpacking is also supported data T data S UNPACK S UNPACK Int
248. nt Reason on Windows we want to invoke the Perl distributed with GHC rather than assume some installed one 24 Chapter 3 Using GHCi GHCi is GHC s interactive environment in which Haskell expressions can be interactively evaluated and programs can be interpreted If you re familiar with Hugs http www haskell org hugs then you ll be right at home with GHCi However GHCi also has support for interactively loading compiled code as well as supporting all the language extensions that GHC provides 3 1 Introduction to GHCi Let s start with an example GHCi session You can fire up GHCi with the command ghci ghci DREE ET EES E SNS fap fk il GHC Interactive version 6 6 for Haskell 98 _ http www haskell org ghc _ AIZ Type for help Loading package base linking done Prelude gt There may be a short pause while GHCi loads the prelude and standard libraries after which the prompt is shown If we follow the instructions and type for help we get Commands available from the prompt lt stmt gt evaluate run lt stmt gt add lt filename gt add module s to the current target set browse lt module gt display the names defined by lt module gt cd lt dir gt change directory to lt dir gt def lt cmd gt lt expr gt define a command lt cmd gt edit lt file gt edit file edit edit last module shel
249. nt normally the compiler will not attempt linking when there is no Main debug Link the program with a debugging version of the runtime system The debugging runtime turns on numerous assertions and sanity checks and provides extra options for producing debugging output at runtime run the program with RTS to see a list threaded Link the program with the threaded version of the runtime system The threaded runtime system is so called because it manages multiple OS threads as opposed to the default runtime system which is purely single threaded Note that you do not need threaded in order to use concurrency the single threaded runtime supports concurrency between Haskell threads just fine The threaded runtime system provides the following benefits e Parallelism on a multiprocessor or multicore machine See Section 4 12 Using SMP parallelism The ability to make a foreign call that does not block all other Haskell threads The ability to invoke foreign exported Haskell functions from multiple OS threads With threadedad calls to foreign functions are made using the same OS thread that created the Haskell thread if it was created by a call to a foreign exported Haskell function or an arbitrary OS thread otherwise if the Haskell thread was created by forkIoO More details on the use of bound threads in the threaded runtime can be found in the Control Concurrent libraries base Control Concurrent html m
250. ntextl gt C Int a where A instance context2 gt Ca Bool where B instance context3 gt C Int a where C instance context4 gt C Int Int where D The instances A and B match the constraint C Int Bool but C and D do not When matching GHC takes no account of the context of the instance declaration context1 etc GHC s default beha viour is that exactly one instance must match the constraint it is trying to resolve It is fine for there to be a potential of overlap by including both declarations A and B say an error is only reported if a par ticular constraint matches more than one The fallow overlapping instances flag instructs GHC to allow more than one instance to match provided there is a most specific one For example the constraint C Int Int matches in stances A C and D but the last is more specific and hence is chosen If there is no most specific match the program is rejected However GHC is conservative about committing to an overlapping instance For example f f Suppose that from the RHS of f we get the constraint C Int b But GHC does not commit to in stance C because in a particular call of f b might be instantiate to Int in which case instance D would be more specific still So GHC rejects the program If you add the flag fal low incoherent instances GHC will instead pick C without complaining about the problem of subsequent ins
251. nversion that s called high er order matching Matching is carried out on GHC s intermediate language which includes type abstractions and ap plications So a rule only matches if the types match too See Section 7 11 4 Specialisation be low GHC keeps trying to apply the rules as it optimises the program For example consider let s map f t map g in s t xs The expressions t xs does not match the rule map map but GHC will substitute for s and t giving an expression which does match If s or t was a used more than once and b large or a redex then it would not be substituted and the rule would not fire In the earlier phases of compilation GHC inlines nothing that appears on the LHS of a rule because once you have substituted for something you can t match against it given the simple minded match ing So if you write the rule map map forall f g map f map g map f g this won t match the expression map f map g xs It will only match something written with explicit use of Well not quite It will match the expression wibble f g xs where wibble is defined wibble f g map f map g because wibble will be inlined it s small Later on in compilation GHC starts inlining even things on the LHS of rules but still leaves the rules enabled This inlining policy is controlled by the per simplification pass flag finline phasen All rules are implicitly exported from the module and
252. o the C program too The inline struct or typedef keyword must come just after def Note A foreign import of aC function may be inlined across a module boundary in which case you must arrange for the import ing module to include the C header file generated by hsc2hs see Section 8 2 2 Using function headers For this reason we avoid using def in the libraries 214 Other Haskell utility programs error message warning message const C_expression const_str C_expression type C_type peek struct_type field poke struct_type field ptr struct_type field offset struct_type field size struct_type enum type constructor value value const Conditional compilation directives are passed unmodified to the C program C file and C header Putting them in the C program means that appropriate parts of the Haskell file will be skipped The expression must be convertible to long or unsigned long Its value literal or negated literal will be output The expression must be convertible to const char pointer Its value string literal will be output A Haskell equivalent of the C numeric type will be output It will be one of Int Word 8 16 32 64 Float Double LDouble A function that peeks a field of a C struct will be output It will have the type Storable b gt Ptr a gt IO b The inten tion is that peek and poke can be used for implementing the oper
253. odule 4 11 Using Concurrent Haskell GHC supports Concurrent Haskell by default without requiring a special option or libraries compiled in a certain way To get access to the support libraries for Concurrent Haskell just import Con trol Concurrent libraries base Control Concurrent html More information on Concurrent Haskell is provided in the documentation for that module The following RTS option s affect the behaviour of Concurrent Haskell programs Cs Sets the context switch interval to s seconds A context switch will occur at the next heap block allocation after the timer expires a heap block allocation occurs every 4k of allocation With 81 Using GHC CO or C context switches will occur as often as possible at every heap block allocation By default context switches occur every 20ms 4 12 Using SMP parallelism GHC supports running Haskell programs in parallel on an SMP symmetric multiprocessor There s a fine distinction between concurrency and parallelism parallelism is all about making your program run faster by making use of multiple processors simultaneously Concurrency on the other hand is a means of abstraction it is a convenient way to structure a program that must respond to mul tiple asynchronous events However the two terms are certainly related By making use of multiple CPUs it is possible to run con current threads in parallel and this is exactly what GHC s SMP parallelism
254. of your labour try now to compile a simple Haskell program bash cat main hs module Main main where main putStrLn Hello world bash ghc o main main hs bash main Hello world bash You do not need the Cygwin toolchain or anything else to install and run GHC An installation of GHC requires about 140M of disk space To run GHC comfortably your machine should have at least 64M of memory 2 2 2 Moving GHC around At the moment GHC installs in a fixed place c ghc ghc x yy but once it is installed you can freely move the entire GHC tree just by copying the ghc x yy directory You may need to fix up the links in Start Programs Glasgow Haskell Compiler if you do this It is OK to put GHC tree in a directory whose path involves spaces However don t do this if you use want to use GHC with the Cygwin tools because Cygwin can get confused when this happens We havn t quite got to the bottom of this but so far as we know it s not a problem with GHC itself Never theless just to keep life simple we usually put GHC in a place with a space free path 2 2 3 Installing ghc win32 FAQ 21 Installing GHC I m having trouble with symlinks Symlinks only work under Cygwin Section 2 1 2 1 Installing so binaries not linked to the Cygwin DLL in particular those built for Mingwin will not work with symlinks I m getting permission denied This can have various causes trying to rename a director
255. ofiling options secs sisicsss ess eeseees Goede cobs send eeN EE EE EO ERSE KEES EE E E 107 4 17 17 Haskell pre processor Options e sseessseseessessrrerssrerrsrrerrrrerrerrerreresrrererrrerert 107 4 17 18 C pre processor Options eisssscebusereries tisso stesti SCi EEA on EEE ECEE IDEES EKETE raS 108 4 17 19 C compiler Opt ons nsien aeoea danas ved a Eo a n baled EESE RER R REE ii Raees 108 4 17 20 Code generation Options ksss ee eae aa EE EV O EEEa E EAE 108 4 17 21 Linking Options pessi oeeaaeaii 108 vi The Glorious Glasgow Haskell Com pilation System User s Guide Version 4 17 22 Replacing phases minea heey ees homed eed E once eediev eee hoe 109 4 17 23 Forcing options to particular phases 0 cee eee ccee cece eeceeeeeeceeeeaeeeaeeea sean eeaes 110 4 17 24 Platform specific Options ceeeeeeeceeseeceeeeeceeecenaeeeeueeeeeeeceaueseeueeeeaeeeeaaeeeeees 110 4 17 23 External core Ae Options wise chosaaseyssuteesdesoa tts by uae sas yedus dey A EE E Gute 110 4 17 26 Compiler debugging Options 2 0 0 0 cece cece cence nece eee eeeeeeceeeeeeeaeeea seen sean eeaes 110 4 17 27 Misc COMP EL Options sssi resa was Vota aapEGTaane EE O E EEA 112 Ds PrOfIINE soient eS E EE Mee celeb E A O EEEE EEEE EE AE E E EEIE EE 113 35 1 Cost centres and Cost centre Stacks is c5scos cs sscsseesssistetgsscasseas seeteogpesssenpseosaetesvetesgssvesbesss 113 5 1 1 Inserting cost centres by hand 0 0 0 0 cee c
256. olymorphism This means that you can call a polymorphic function at a polymorphic type and parameterise data structures over polymorphic types For example f Maybe forall a a gt a gt Maybe Int Char f Just g Just g 3 g hello f Nothing Nothing Notice here that the Maybe type is parameterised by the polymorphic type forall a a gt a The technical details of this extension are described in the paper Boxy types type inference for higher rank types and impredicativity http research microsoft com 7Esimonpj papers boxy which ap peared at ICFP 2006 7 4 10 Lexically scoped type variables GHC supports lexically scoped type variables without which some type signatures are simply im possible to write For example f forall a a gt a f xs ys ys where ys a ys reverse xs The type signature for f brings the type variable a into scope it scopes over the entire definition of f In particular it is in scope at the type signature for ys In Haskell 98 it is not possible to declare a type for ys a major benefit of scoped type variables is that it becomes possible to do so Lexically scoped type variables are enabled by Eglasgow exts Note GHC 6 6 contains substantial changes to the way that scoped type variables work compared to earlier releases Read this section carefully 7 4 10 1 Overview The design follows the following principles e A scoped type variable
257. omes from the HSrts library which is normally included after all the other libraries on the linker s command line To force GHC s main to 79 Using GHC package name framework name Ldir framework pathdir split objs static dynamic main is thing be used in preference to any other main s from external libraries just add the op tion 1HSrts before any other libraries on the command line Omits the link step This option can be used with make to avoid the automatic linking that takes place if the program contains a Main module If you are using a Haskell package see Section 4 8 Packages don t forget to add the relevant package option when linking the program too it will cause the appropriate libraries to be linked in with the program Forgetting the package op tion will likely result in several pages of link errors On Darwin MacOS X only link in the framework name This option corresponds to the framework option for Apple s Linker Please note that frameworks and pack ages are two different things frameworks don t contain any haskell code Rather they are Apple s way of packaging shared libraries To link to Apple s Carbon API for example you d use framework Carbon Where to find user supplied libraries Prepend the directory dir to the library dir ectories path On Darwin MacOS X only prepend the directory dir to the framework directories path This opti
258. ompilation output s Flag Description Static Dynamic Reverse hesuf suffix set the suffix to use for in dynamic termediate C files hidir dir set directory for interface dynamic files hisuf suffix set the suffix to use for in dynamic terface files o filename set output filename dynamic odir dir set output directory dynamic ohi filename set the filename in which to dynamic put the interface osuf suffix set the output file suffix dynamic stubdir dir redirect FFi stub files dynamic 4 17 5 Keeping intermediate files Section 4 6 5 Keeping Intermediate Files Flag Description Static Dynamic Reverse keep hc fil retain intermediate hc dynamic files keep s file retain intermediate s files dynamic keep raw s fil retain intermediate dynamic caw_s files keep tmp files retain all intermediate tem dynamic porary files 4 17 6 Temporary files Section 4 6 6 Redirecting temporary files Flag Description Static Dynamic Reverse tmpdir set the directory for tempor dynamic ary files 101 Using GHC 4 17 7 Finding imports Section 4 6 3 The search path Flag Description Static Dynamic Reverse idirl idir2 add dir dir2 etc toim dynamic port path i Empty the import directory dynamic list 4 17 8 Interface file options Section 4 6 7 Othe
259. ompile the program starting with module Main and put the executable in the file prog Note on Windows if the result is an executable file the extension exe is ad ded if the specified filename does not already have an extension Thus ghc o foo Main hs will compile and link the module Main hs and put the resulting executable in foo exe not foo If you use ghc make and you don t use the o the name GHC will choose for the executable will be based on the name of the file containing the module Main Note that with GHC the Main module doesn t have to be put in file Main hs Thus both 48 Using GHC ghc make Prog and ghc make Prog hs will produce Prog or Prog exe if you are on Windows odir dir Redirects object files to directory dir For example ghc c parse Foo hs parse Bar hs gurgle Bumble hs odir arch The object files Foo o Bar o and Bumble o would be put into a subdirect ory named after the architecture of the executing machine x86 mips etc Note that the odir option does not affect where the interface files are put use the hidir option for that In the above example they would still be put in parse Foo hi parse Bar hi and gurgle Bumble hi ohi file The interface output may be directed to another file bar2 Wurble iface with the option ohi bar2 Wurble iface not recommended WARNING if you redirect the interface file somewhere that GHC can t find it then the recompil
260. on and application are analogous to the expression versions In particular the beta and eta rules describe equivalences of commands These three features operators lambda abstraction and application are the core of the notation everything else can be built using them though the results would be somewhat clumsy For example we could simulate do notation by defining bind Arrow a gt a b gt a e b c gt aec u bind f returnA amp amp amp u gt gt gt f bind_ Arrow a gt a b gt a G gt oa u bind f u bind arr fst gt gt gt f We could simulate if by defining cond ArrowChoice a gt a b gt a b gt a e Bool b cond f g arr e b gt if b then Left lse Right e gt gt gt f lll g Differences with the paper e Instead of a single form of arrow application arrow tail with two translations the implementation provides two forms lt first order and lt lt higher order e User defined operators are flagged with banana brackets instead of a new form keyword Portability Although only GHC implements arrow notation directly there is also a preprocessor available from the arrows web page http www haskell org arrows that translates arrow notation into Haskell 98 for use with other Haskell systems You would still want to check arrow programs with GHC tracing type er rors in the preprocessor output is not easy Modules intended for bot
261. on causes all constructor fields which are marked strict i e to be unboxed or unpacked if possible It is equivalent to adding an UNPACK pragma to every strict constructor field see Section 7 10 10 UNPACK pragma This option is a bit of a sledgehammer it might sometimes make things worse Selectively unboxing fields by using UNPACK prag mas might be better Switches on an experimental optimisation Switching it on makes the compiler a little keener to inline a function that returns a constructor if the context is that of a thunk x plusInt ab If we inlined plusInt we might get an opportunity to use update in place for the thunk x Default 45 Governs the maximum size that GHC will allow a function unfolding to be An unfolding has a size that reflects the cost in terms of code bloat of expanding that unfolding at at a call site A bigger function would be assigned a bigger cost Consequences a nothing larger than this will be inlined unless it has an INLINE pragma b nothing larger than this will be spewed into an interface file Increasing this figure is more likely to result in longer compile times than faster code The next option is more useful Default 8 This is the magic cut off figure for unfolding below this size a function definition will be unfolded at the call site any bigger and it won t The size computed for a function depends on two things the actual size of
262. on corresponds to the F option for Apple s Linker F already means something else for GHC Tell the linker to split the single object file that would normally be generated into multiple object files one per top level Haskell function or type in the module This only makes sense for libraries where it means that executables linked against the lib rary are smaller as they only link against the object files that they need However as sembling all the sections separately is expensive so this is slower than compiling normally We use this feature for building GHC s libraries warning don t use it un less you know what you re doing Tell the linker to avoid shared Haskell libraries if possible This is the default Tell the linker to use shared Haskell libraries if available this option is only suppor ted on Mac OS X at the moment and also note that your distribution of GHC may not have been supplied with shared libraries Note that this option also has an effect on code generation see above The normal rule in Haskell is that your program must supply a main function in module Main When testing it is often convenient to change which function is the main one and the main is flag allows you to do so The thing can be one of e A lower case identifier foo GHC assumes that the main function is Main foo e An module name A GHC assumes that the main function is A main e An qualified name A foo GHC assumes that the mai
263. one or more levels of indirection may be re moved Unpacking only happens for single constructor datatypes Int is a good candidate for example Using funbox strict fields is only really a good idea in conjunction with O because otherwise the extra packing and un packing won t be optimised away In fact it is possible that funbox strict fields may worsen performance even with O but this is unlikely let us know if it happens to you When you are really desperate for speed and you want to get right down to the raw bits Please see Section 7 2 1 Unboxed types for some information about using unboxed types Before resorting to explicit unboxed types try using strict con structor fields and funbox strict fields first see above That way your code stays portable This may take real work but There exist piles of massively tuned library code and the best thing is not to compete with it but link with it Chapter 8 Foreign function interface FFI describes the foreign function interface If you re using Complex definitely use Complex Double rather than Complex Float the former is specialised heavily but the latter isn t Floats probably 32 bits are almost always a bad idea any way unless you Really Know What You Are Doing Use Doubles There s rarely a speed disadvantage modern ma chines will use the same floating point unit for both With Doubles you are much less likely to ha
264. or e the most recently successfully loaded module if any other modules were loaded as a result of the current load or e Prelude otherwise When a program is compiled and executed it can use the getArgs function to ac cess the command line arguments However we cannot simply pass the arguments to the main function while we are testing in ghci as the main function doesn t take its directly Instead we can use the main command This runs whatever main is in scope with any arguments being treated the same as command line arguments e g Prelude gt let main System Environment getArgs gt gt print Prelude gt main foo bar Y foo bar Sets or modifies the current context for statements typed at the prompt See Sec tion 3 4 3 What s really in scope at the prompt for more details Quits GHCi You can also quit by typing a control D at the prompt 37 Using GHCi reload set option set args arg set editor cmd set prog prog set prompt prompt show bind ings show mod ules ctags file name etags filename type ex pression kind type undef name sunset op tion command The Attempts to reload the current target set see load if any of the modules in the set or any dependent module has changed Note that this may entail loading new modules or dropping modules which are no longer indirectly required by the target Sets
265. or example if the current scope is Prelude then we can bring into scope the exports from the module TO like so Prelude gt module 10 Prelude IO gt hPutStrLn stdout hello n hello Prelude IO gt Note module can be shortened to m The full syntax of the module command is smodule mod oe mod Using the form of the module commands adds modules to the current scope and removes them Without either or the current scope is replaced by the set of modules specified Note that if you use this form and leave out Prelude GHCi will assume that you really wanted the Prelude and add it in for you if you don t want the Prelude then ask to remove it with m Prelude The scope is automatically set after a load command to the most recently loaded target module in a form if possible For example if you say load foo hs bar hs and bar hs contains module Bar then the scope will be set to Bar if Bar is interpreted or if Bar is compiled it will be set to Prelude Bar GHCi automatically adds Prelude if it isn t present and there aren t any form modules With multiple modules in scope especially multiple form modules it is likely that name clashes will occur Haskell specifies that name clashes are only reported when an ambiguous identifier is used and GHCi behaves in the same way for expressions typed at the prompt Hint GHCi will tab complete names that are in scope for example if you run
266. ors it is quite separate from the main cost centre profiling system intended for all users everywhere To be able to use ticky ticky profiling you will need to have built appropriate libraries and things when you made the system See Customising what libraries to build in the installation guide To get your compiled program to spit out the ticky ticky numbers use a r RTS option See Sec tion 4 14 Running a compiled program Compiling your program with the ticky switch yields an executable that performs these counts Here is a sample ticky ticky statistics file generated by the invocation foo RTS rfoo ticky foo RTS rfoo ticky ALLOCATIONS 3964631 11330900 words total 3999476 admin 6098829 goods 1232595 total words 2 3 4 5 6 69647 1 8 function values 500 500 0 0 0 0 0 0 2382937 60 1 thunks 0 0 83 9 16 1 0 0 0 0 1477218 37 3 data values 66 8 33 2 0 0 0 0 0 0 O 0 0 big tuples 2 0 0 black holes 0 0 100 0 0 0 0 0 0 0 O 0 0 prim things 34825 0 9 partial applications 0 0 0 0 0 0 100 0 0 0 2 0 0 thread state objects 0 0 0 0 0 0 0 0 100 0 Total storage manager allocations 3647137 11882004 words 551104 words lost to speculative heap checks STACK USAGE ENTERS 9400092 of which 2005772 21 3 direct to the entry code the rest indirected via Node s info ptr thunks data values function values of which 1999880 63 5 bypassed arg satisfaction chk
267. orted GHC requires that the name of the module in the import statement ex actly matches the name of the module in the interface file or source file found using the strategy spe cified in Section 4 6 3 The search path This means that for most modules the source file name should match the module name However note that it is reasonable to have a module Main ina file named foo hs but this only works because GHC never needs to search for the interface for module Main because it is never imported It is therefore possible to have several Main modules in separate source files in the same directory and GHC will not get confused In batch compilation mode the name of the object file can also be overridden using the o option and the name of the interface file can be specified directly using the ohi option 4 6 3 The search path 47 Using GHC 4 6 4 In your program you import a module Foo by saying import Foo In make mode or GHCi GHC will look for a source file for Foo and arrange to compile it first Without make GHC will look for the interface file for Foo which should have been created by an earlier compilation of Foo GHC uses the same strategy in each of these cases for finding the appropriate file This strategy is as follows GHC keeps a list of directories called the search path For each of these dir ectories it tries appending basename extension to the directory and checks whether the file ex ists
268. ory hasktags is a simple program that uses simple parsing rules to find definitions of functions construct ors and types It isn t guaranteed to find everything and will sometimes create false index entries but it usually gets the job done fairly well In particular at present functions are only indexed if a type signa ture is given for them Before hasktags there used to be fptags and hstags which did essentially the same job however neither of these seem to be maintained any more Using tags with your editor With NEdit load the tags file using File Load Tags File Use Ctrl D to search for a tag With XEmacs load the TAGS file using visit tags table Use M to search for a tag Yacc for Haskell happy Andy Gill and Simon Marlow have written a parser generator for Haskell called happy Happy is to Haskell what Yacc is to C You can get happy from the Happy Homepage http www haskell org happy Happy is at its shining best when compiled by GHC 212 Other Haskell utility programs 10 3 Writing Haskell interfaces to C code hsc2hs 10 3 1 The hsc2hs command can be used to automate some parts of the process of writing Haskell bindings to C code It reads an almost Haskell source with embedded special constructs and outputs a real Haskell file with these constructs processed based on information taken from some C headers The extra con structs deal with accessing C data from Haskell
269. ositions are repor ted as in the original source file 4 10 5 Options affecting the C compiler if applicable If you are compiling with lots of foreign calls you may need to tell the C compiler about some 78 gt 3 Using GHC include files The Right Way to do this is to add an INCLUDE pragma to the top of your source file Section 7 10 2 INCLUDE pragma INCLUD E lt X Xlib h gt Sometimes this isn t convenient In those cases there s an equivalent command line option ghc c include lt X Xlib h gt Xstuff lhs 4 10 6 Options affecting code generation fasm fvia C fno code ERITC dynamic Use GHC s native code generator rather than compiling via C This will compile faster up to twice as fast but may produce code that is slightly slower than compil ing via C fasm is the default when optimisation is off see Section 4 9 Optimisation code improvement Compile via C instead of using the native code generator This is default for optim ised compilations and on architectures for which GHC doesn t have a native code generator Omit code generation and all later phases altogether Might be of some use if you just want to see dumps of the intermediate compilation phases Generate position independent code code that can be put into shared libraries This currently works on Mac OS X it works on PowerPC Linux when using the native code
270. ost GHC command line flags can also be set here eg v2 fglasgow exts etc We ll explain most of these commands as we go along For Hugs users many things work the same as in Hugs so you should be able to get going straight away Haskell expressions can be typed at the prompt Prelude gt 1 2 3 Prelude gt let x 42 in x 9 4 666666666666667 Prelude gt GHCi interprets the whole line as an expression to evaluate The expression may not span several lines as soon as you press enter GHCi will attempt to evaluate it 3 2 Loading source files Suppose we have the following Haskell source code which we place in a file Main hs main print fac 20 fac 0 1 fac n n fac n 1 You can save Main hs anywhere you like but if you save it somewhere other than the current direct ory then we will need to change to the right directory in GHCi Prelude gt cd dir where dir is the directory or folder in which you saved Main hs To load a Haskell source file into GHC1 use the load command Prelude gt load Main Compiling Main Main hs interpreted ig you started up GHCi from the command line then GHCi s current directory is the same as the current directory of the shell from which it was started If you started GHCi from the Start menu in Windows then the current directory is probably something like C Documents and Settings user name 26 Using GHCi Ok modules loaded Main Main g
271. ot supported since GHC 4 x Please ask if you re interested in this For those who need absolute control over exactly what options are used e g compiler writers sometimes a list of options can be put in a file and then slurped in with Ofile In that file comments are of the to end of line variety blank lines and most whitespace is ignored Please ask if you are baffled and would like an example of Ofile We don t use a O flag for day to day work We use O to get respectable speed e g when we want to measure something When we want to go for broke we tend to use 02 fvia C and we go for lots of coffee breaks The easiest way to see what O etc really mean is to run with v then stand back in amazement f platform independent flags These flags turn on and off individual optimisations They are normally set via the O options described above and as such you shouldn t need to set any of them explicitly indeed doing so could lead to un expected results However there are one or two that may be of interest fexcess precision When this option is given intermediate floating point values can have a greater precision range than the final type Generally this is a good thing but some programs may rely on the exact preci sion range of F loat Doub1le values and should not use this op tion for their compilation fignore asserts Causes GHC to ignore uses of the function Excep
272. ous Glasgow Haskell Com pilation System User s Guide Version PAI LES yntaxs Girar oa edit ebsites Col E E E a abuase ui oes Schade ia lige ee AR 193 TAD 2 SCMANUUCS co 55 casey iaesten te E EE E store s cb ster te wth cata reed goers EEEE ESE 194 TAVBELAStTUSION ya 500d 2 Be E AAP Sa Ss eat ec he Tee ae aoe ia eg 196 TAA Specialisation sce wrasse Sevntes vsvan oes ve age gh Seaton ge neds bp vege sede ede ase eetassdeced eb AES 197 7 11 5 Controlling what s going ON 0 00 ee eee cece ence ee ceeeee teen occa ceca eeaaeeaeeeueeeneeeenees 197 PALO CORE pragma oo tiss sessis se sete aatestesses cicbsseeinzs ies E AE nea ek E E 198 7 12 Special built in functions srei esirin cece teer pner E EEES E EEN EEEIEE E EEEn 199 T121 The inline functioNs sereis mi stags or Esin Oe PONE PREPO ISERE PEEPS TIREE 199 RIZ Th Ta ZY UNCON resser a aa e ea a e Tei 199 712 3 The unsafeCoerceF function s id anien e a E r e 199 TAB Generic CLASSES ay anen r RE E N E E E RN E EENES EAE SEI YEE pa RENAE 200 TASS USimg PENELICStist esis a E E E E T E EE E AE EAE ERRIA 200 T132 Changes wit the paper ssiri e E E a e a Ea Eae 201 7 13 3 Terminology and restrictions necis e e E E E EE E EER SS 201 TIIA Another xample esen a E Ee EEEE E EE NEEE ETDE 202 7 14 Control over monomorphism sessseseesseesereresrerrsrrerrsrerrreresrrrresrerrsrrerreresrrseesreeesreeeen 203 7 14 1 Switching off the dreaded Monomorphism Restriction sesseeseeerrsresrrrrerr
273. p display this list of commands info lt name gt display information about the given names load lt filename gt load module s and their dependents module lt mod gt set the context for expression evaluation main lt arguments gt run the main function with the given arguments reload reload the current module set set lt option gt set options set args lt arg gt set the arguments returned by System getArgs set prog lt progname gt set the value returned by System getProgName set prompt lt prompt gt set the prompt used in GHCi set editor lt cmd gt set the command used for edit show modules show the currently loaded modules show bindings show the current bindings made at the prompt ctags lt file gt create tags file for Vi default tags etags lt file gt create tags file for Emacs defauilt TAGS The i stands for Interactive except foreign export at the moment 25 Using GHCi type lt expr gt show the type of lt expr gt kind lt type gt show the kind of lt type gt undef lt cmd gt undefine user defined command lt cmd gt sunset lt option gt unset options quit exit GHCi lt command gt run the shell command lt command gt Options for set and unset r revert top level expressions after each evaluation s print timing memory stats after each evaluation t print type after evaluation lt flags gt m
274. pe itself which is plain Foo For example the following expression is fine MkFoo 3 even MkFoo c isUpper Foo Here MkFoo 3 even packages an integer with a function even that maps an integer to Bool and MkFoo c isUpper packages a character with a compatible function These two things are each of type Foo and can be put in a list What can we do with a value of type Foo In particular what happens when we pattern match on Mk Foo f MkFoo val fn Since all we know about val and fn is that they are compatible the only useful thing we can do with them is to apply fn to val to get a boolean For example f Foo gt Bool f MkFoo val fn fn val What this allows us to do is to package heterogenous values together with a bunch of functions that ma nipulate them and then treat that collection of packages in a uniform manner You can express quite a bit of object oriented like programming this way 7 4 1 4 1 Why existential What has this to do with existential quantification Simply that MkF oo has the nearly isomorphic type MkFoo exists a a a gt Bool gt Foo But Haskell programmers can safely think of the ordinary universally quantified type given above 145 GHC Language Features thereby avoiding adding a new existential quantification construct 7 4 1 4 2 Type classes An easy extension is to allow arbitrary contexts before the constructor For example data Ba
275. peration op of C to mention D It would not be OK for D to be a superclass of C 7 4 2 3 Class method types Haskell 98 prohibits class method types to mention constraints on the class type variable thus class Seq s a where fromList a gt sa elem Eq a gt a gt s a gt Bool The type of elem is illegal in Haskell 98 because it contains the constraint Eq a constrains only the class type variable in this case a GHC lifts this restriction 7 4 3 Functional dependencies 149 GHC Language Features Functional dependencies are implemented as described by Mark Jones in Type Classes with Functional Dependencies http www cse ogi edu mpj pubs fundeps html Mark P Jones In Proceedings of the 9th European Symposium on Programming ESOP 2000 Berlin Germany March 2000 Springer Ver lag LNCS 1782 Functional dependencies are introduced by a vertical bar in the syntax of a class declaration e g class Monad m gt MonadState s m m gt s where class Foo abc a b gt c where There should be more documentation but there isn t yet Yell if you need it 7 4 3 1 Rules for functional dependencies In a class declaration all of the class type variables must be reachable in the sense mentioned in Sec tion 7 4 5 Type signatures from the free variables of each method type For example class Coll s a where empty i s insert i s gt a gt s is not OK because the typ
276. perators The extension is this the left section e is equivalent from the point of view of both type checking and execution to the expression 141 GHC Language Features e for any expression e and operator The strict Haskell 98 interpretation is that the section is equi valent to y gt e y That is the operator must be a function of two arguments GHC allows it to take only one argument and that in turn allows you to write the function postfix Since this extension goes beyond Haskell 98 it should really be enabled by a flag but in fact it is en abled all the time No Haskell 98 programs change their behaviour of course The extension does not extend to the left hand side of function definitions you must define such a func tion in prefix form 7 4 Type system extensions 7 4 1 Data types and type synonyms 7 4 1 1 Data types with no constructors With the fglasgow exts flag GHC lets you declare a data type with no constructors For example data S ere Ieee A data T a aS A a AES N Syntactically the declaration lacks the constrs part The type can be parameterised over types of any kind but if the kind is not then an explicit kind annotation must be used see Section 7 4 7 Explicitly kinded quantification Such data types have only one value namely bottom Nevertheless they can be useful when defining phantom types 7 4 1 2 Infix type constructors cla
277. pha3D hs RedBook DOF hs RedBook FogIndex hs Red Book Multisamp hs RedBook PointP hs RedBook PolyOff hs RedBook Sten 13 Introduction to GHC cil hs RedBook Stroke hs and RedBook Torus hs and the examples in RedBook Font hs and RedBook Histogram hs have been improved 1 4 5 6 haskell src No change version 1 0 1 4 5 7 HGL No change version 3 1 1 4 5 8 html Version 1 0 html is a new package developing on what used to be Text Html and Text Html BlockTable in the base package Text Html BlockTable exports a new function empty 1 4 5 9 HUnit 1 4 5 10 1 4 5 11 No change version 1 1 mtl No change version 1 0 network Version number 2 0 was 1 0 Network CGI has been removed use the cgi package instead Network BSD no longer exports symlink or readlink use Sys tem Posix Files createSymbolicLink and Sys tem Posix Files readSymbolicLink instead Network BSD now exports defaultProtocol Network Socket SocketStatus now has a constructor ConvertedToHandle for sockets that have been converted to handles Network Socket Family now has the following additional constructors AF_NETROM AF_BRIDGE AF_ATMPVC AF_ROSE AF_NETBEUI AF_SECURITY AF_PACKET AF_ASH 14 Introduction to GHC AF_ECONET AF_ATMSVC AF_IRDA AF_PPPOX AF_WANPIPE and AF_BLUETOOTH e In Network URI parseabsoluteURI has
278. pkg update file The same as register except that if a package of the same name is already installed it is replaced by the new one ghc pkg unregister P Remove the specified package from the database 68 Using GHC ghc pkg expose P Sets the exposed flag for package P to True ghc pkg hide P Sets the exposed flag for package P to False ghe pkg list P This option displays the currently installed packages for each of simple output the databases known to ghc pkg That includes the global data base the user s local database and any further files specified us ing the f option on the command line Hidden packages those for which the exposed flag is False are shown in parentheses in the list of packages If an optional package identifier P is given then only packages matching that identifier are shown If the option simple output is given then the packages are listed on a single line separated by spaces and the database names are not included This is intended to make it easier to parse the output of ghc pkg list using a script ghc pkg latest P Prints the latest available version of package P ghc pkg describe P Emit the full description of the specified package The description is in the form of an InstalledPackagelInfo the same as the input file format for ghc pkg register See Section 4 8 6 InstalledPackagelInfo a package specification for de tails ghc pkg field P field Show just a sin
279. r and gt are in scope e Negation e g x means negate f x bothin numeric patterns and expressions e Do notation is translated using whatever functions gt gt gt gt and fail are in scope not the Prelude versions List comprehensions mdo Section 7 3 3 The recursive do notation and par allel array comprehensions are unaffected e Arrow notation see Section 7 7 Arrow notation uses whatever arr gt gt gt first app i and loop functions are in scope But unlike the other constructs the types of these func tions must match the Prelude types very closely Details are in flux if you want to use this ask In all cases apart from arrow notation the static semantics should be that of the desugared form even if that is a little unexpected For emample the static semantics of the literal 368 is exactly that of fro minteger 368 Integer it s fine for fromInteger to have any of the types fromInteger Integer gt Integer fromInteger forall a Foo a gt Integer gt a fromInteger Num a gt a gt Integer fromInteger Integer gt Bool gt Bool Be warned this is an experimental facility with fewer checks than usual Use dcore lint to typecheck the desugared program If Core Lint is happy you should be all right Postfix operators GHC allows a small extension to the syntax of left operator sections which allows you to define postfix o
280. r x gt x x gt gt gt first arr x gt xt 1 gt gt gt f gt gt gt arr y x gt y x y gt gt gt first arr y gt 2 y gt gt gt g gt gt gt arr snd gt gt gt arr x y gt let z x y in x z z gt gt gt first arr x z gt x z gt gt gt h gt gt gt arr t z gt t z gt gt gt returnA Note that variables not used later in the composition are projected out After simplification using rewrite rules see Section 7 11 Rewrite rules defined in the Control Arrow libraries base Control Arrow html module this reduces to arr x gt xt l x gt gt gt first f gt gt gt arr y x gt 2 y y gt gt gt first g gt gt gt arr _ x y gt let z x y in x z z gt gt gt first h gt gt gt arr t Z gt tt z which is what you might have written by hand With arrow notation GHC keeps track of all those tuples of variables for you Note that although the above translation suggests that 1et bound variables like z must be monomorph ic the actual translation produces Core so polymorphic variables are allowed It s also possible to have mutually recursive bindings using the new rec keyword as in the following example counter ArrowCircuit a gt a Bool Int counter proc reset gt do rec output lt returnA lt if reset then 0 else next next lt delay 0 lt outp
281. r a Bound at test hs 5 4 test hs 6 7 In the binding group for a b a Note that line numbers start counting at one but column numbers start at zero This choice was made to follow existing convention i e this is how Emacs does it Hsize Set the minimum size of the heap to size This option is equivalent to RTS Hsize see Section 4 14 3 RTS options to control the garbage collect or Rghc timing Prints a one line summary of timing statistics for the GHC run This option is equivalent to RTS tstderr see Section 4 14 3 RTS options to control the garbage collector 4 6 Filenames and separate compilation 4 6 1 This section describes what files GHC expects to find what files it creates where these files are stored and what options affect this behaviour Note that this section is written with hierarchical modules in mind see Section 7 3 1 Hierarchical Modules hierarchical modules are an extension to Haskell 98 which extends the lexical syntax of module names to include a dot Non hierarchical modules are thus a special case in which none of the module names contain dots Pathname conventions vary from system to system In particular the directory separator is on Unix systems and on Windows systems In the sections that follow we shall consistently use as the directory separator substitute this for the appropriate character for your system Haskell source files
282. r database or package conf can be used to act on another data base entirely When multiple of these options are given the rightmost one is used as the database to act upon If the environment variable GHC_PACKAGE_PATH is set and its value does not end in a separator on Unix on Windows then the last database is considered to be the global database and will be modi fied by default by ghc pkg The intention here is that GHC_PACKAGE_PATH can be used to create a virtual package environment into which Cabal packages can be installed without setting anything other than GHC_PACKAGE_ PATH The ghc pkg program may be run in the ways listed below Where a package name is required the package can be named in full including the version number e g net work 1 0 or without the ver sion number Naming a package without the version number matches all versions of the package the specified action will be applied to all the matching packages A package specifier that matches all ver sion of the package can also be written pkg to make it clearer that multiple packages are being matched 6 ghce pkg register fil Reads a package specification from file which may be to indicate standard input and adds it to the database of installed packages The syntax of file is given in Section 4 8 6 In stalledPackagelInfo a package specification The package specification must be a package that isn t already in stalled ghc
283. r options related to interface files Flag Description Static Dynamic Reverse ddump hi Dump the new interface to dynamic and dump it as text to stdout stdout ddump hi diffs Show the differences vs dynamic the old interface Dump a minimal set of im dynamic ddump minim ports al imports show iface file Read the interface in file mode 4 17 9 Recompilation checking Section 4 6 8 The recompilation checker Flag Description Static Dynamic Reverse fforce recomp Turn off recompilation dynamic fno force recomp checking implied by any ddump X option 4 17 10 Interactive mode options Section 3 8 The ghci file Flag Description Static Dynamic Reverse ignore dot ghci Disable reading of ghci_ static files read dot ghci Enable reading of g ci static files 4 17 11 Packages 102 Using GHC Section 4 8 Packages Flag Description Static Dynamic Reverse package name P Compile to be part of pack dynamic age P package P Expose package P dynamic hide all packages Hide all packages by de static fault hide package name Hide package P dynamic ignore package Ignore package P dynamic name package conf file Load more packages from dynamic file Don t load the user s pack dynamic no age confi
284. r running a Haskell pre processor separ ately in that it works in interpreted mode and you can continue to take reap the bene fits of GHC s recompilation checker The pre processor is run just before the Haskell compiler proper processes the Haskell input but after the literate markup has been stripped away and possibly the C pre processor has washed the Haskell input Use pgmF cmd to select the program to use as the preprocessor When invoked the cmd pre processor is given at least three arguments on its command line the first argument is the name of the original source file the second is the name of the file holding the input and the third is the name of the file where cmd should write its out put to Additional arguments to the pre processor can be passed in using the optF option These are fed to cmd on the command line after the three standard input and output arguments An example of a pre processor is to convert your source files to the input encoding that GHC expects i e create a script convert sh containing the lines bin sh echo LINE 1 S2 iconv f 11 t utf 8 2 and pass F pgmF convert sh to GHC The f 11 option tells iconv to con vert your Latin 1 file supplied in argument 2 while the t utf 8 options tell iconv to return a UTF 8 encoded file The result is redirected into argument 3 The echo mf LINE 1 S2 just makes sure that your error p
285. r than the overlapped one Perhaps overlap at a usage site should be permitted regardless of how the instance declarations are com piled if the fallow overlapping instances flag is used at the usage site Mind you the ex act usage site can occasionally be hard to pin down We are interested to receive feedback on these points 157 GHC Language Features The fallow incoherent instances flag implies the fal low overlapping instances flag but not vice versa 7 4 4 4 Type synonyms in the instance head Unlike Haskell 98 instance heads may use type synonyms The instance head is the bit after the gt in an instance decl As always using a type synonym is just shorthand for writing the RHS of the type synonym definition For example type Point Int Int instance C Point where instance C Point where is legal However if you added instance C Int Int where as well then the compiler will complain about the overlapping actually identical instance declarations As always type synonyms must be fully applied You cannot for example write type P a a instance Monad P where This design decision is independent of all the others and easily reversed but it makes sense to me 7 4 5 Type signatures 7 4 5 1 The context of a type signature Unlike Haskell 98 constraints in types do not have to be of the form class type variable or class type variable type variable Thus these type
286. rce code Syntactically core annotations are attached to expressions and take a Haskell string literal as an argument The following function definition shows an example f x CORE foo show CORE bar x Semantically this is equivalent to g x show x However when external for is generated via fext core there will be Notes attached to the ex pressions show and x The core function declaration for f is f Sforall a GHCziShow ZCTShow a gt a gt GHCziBase ZMZN GHCziBase Char a zddShow GHCziShow ZCTShow a eta a gt Snote foo scase zddShow tof tpl GHCziShow ZCTShow a GHCziShow ZCDShow tpll GHCziBase Int gt a gt GHCziBase ZMZN GHCziBase Char gt GHCziBase ZMZN GHCziBase Cha Char gt GHCziBase ZMZN GHCziBase Cha r tpl2 a gt GHCziBase ZMZN GHCziBase Char tpl3 GHCziBase ZMZN a gt GHCziBase ZMZN GHCziBase r gt tp12 Snote bar eta 198 GHC Language Features Here we can see that the function show which has been expanded out to a case expression over the Show dictionary has a note attached to it as does the expression eta which used to be called x 7 12 Special built in functions GHC has a few built in funcions with special behaviour described in this section All are exported by GHC Exts 7 12 1 The inline function The inline function is somewhat experimental inline a gt a The c
287. rds per heap object which probably results in about a 30 overhead e Garbage collection requires more memory than the actual residency The factor depends on the kind of garbage collection algorithm in use a major GC in the standard generation copying collector will usually require 3L bytes of memory where L is the amount of live data This is because by default see the RTS F option we allow the old generation to grow to twice its size 2L before collect ing it and we require additionally L bytes to copy the live data into When using compacting collec tion see the RTS c option this is reduced to 2L and can further be reduced by tweaking the F option Also add the size of the allocation area currently a fixed 512Kb e The stack isn t counted in the heap profile by default See the RTS xt option e The program text itself the C stack any non heap data eg data allocated by foreign libraries and data allocated by the RTS and mmap d memory are not counted in the heap profile 5 5 Graphical time allocation profile You can view the time and allocation profiling graph of your program graphically using gheprof This is a new tool with GHC 4 08 and will eventually be the de facto standard way of viewing GHC profiles To run gheprof you need uDraw Graph installed which can be obtained from uDraw Graph http www informatik uni bremen de uDrawGraph en uDrawGraph uDrawGraph html Install one of the binary distrib
288. rguments etc is still inlined across modules So if you want the foreign call itself to be inlined across modules use the command line and package configuration include mechanism 8 2 2 1 Finding Header files Header files named by the include option or ina foreign import declaration are searched for using the C compiler s usual search path You can add directories to this search path using the I option see Section 4 10 3 Options affecting the C pre processor Note header files are ignored unless compiling via C If you had been compiling your code using the native code generator the default and suddenly switch to compiling via C then you can get unexpected errors about missing include files Compiling via C is enabled automatically when certain options are 208 Foreign function interface FFI given eg O and prof both enable fvia C 8 2 3 Memory Allocation The FFI libraries provide several ways to allocate memory for use with the FFI and it isn t always clear which way is the best This decision may be affected by how efficient a particular kind of allocation is on a given compiler platform so this section aims to shed some light on how the different kinds of alloc ation perform with GHC alloca and friends Useful for short term allocation when the allocation is intended to scope over a given IO computation This kind of allocation is commonly used when marshalling data to and from FFI functions
289. ries those that are necessary to build GHC and extra libraries Decoupling the extra libraries means that they can release independently of GHC releases and makes development builds of GHC quicker as they no longer need to build unnecessary libraries The hslibs libraries have finally been removed 1 4 4 Core Libraries 1 4 4 1 base e Version number 2 1 was 1 0 e We now have Read and Show instances for up to 15 tuples used to be up to 5 tuples e New module Control Applicative that describes a structure intermediate between a functor and a monad it provides pure expressions and sequencing but no binding e Control Exception now exports bracketOnError which behaves like bracket but only runs the final action if the main action raised an error e There isa new module Control Monad Instances which provides Monad and Functor in stances for gt r were in mtl s Control Monad Reader a Functor instance for Either a was inmtl s Control Monad Error anda Functor instance for a new e The MonadFix instance for gt r is now in Control Monad Fix was in mt1 s Con trol Monad Reader e Control Monad ST now exports unsafeSTToIO e The HasBounds class has been removed from Data Array Base and its bounds method is now in the Array class The MArray class has also gained a method get Bounds e Data Array Base now provides anMArray STArray s e Lazy ST s instance e Data Array Storable now exports
290. rised trademark of Peyton Jones Enterprises plc 4 16 4 Unregisterised compilation The term unregisterised really means compile via vanilla C disabling some of the platform specific tricks that GHC normally uses to make programs go faster When compiling unregisterised GHC simply generates a C file which is compiled via gcc Unregisterised compilation can be useful when porting GHC to a new machine since it reduces the pre requisite tools to gcc as and Id and nothing more and furthermore the amount of platform specific code that needs to be written in order to get unregisterised compilation going is usually fairly small unreg Compile via vanilla ANSI C only turning off platform specific optimisations NOTE in order to use unreg you need to have a set of libraries including the RTS built for unregisterised compilation This amounts to building GHC with way u enabled 4 17 Flag reference This section is a quick reference for GHC s command line flags For each flag we also list its static dynamic status see Section 4 2 Static Dynamic and Mode options and the flag s opposite if avail able 4 17 1 Help and verbosity options Section 4 5 Help and verbosity options Flag Description Static Dynamic Reverse help mode help help mode 99 Using GHC Flag Description Static Dynamic Reverse n do a dry run dynamic
291. rofiling options described below eg hc for a basic producer pro file This generates the file prog hp 3 Run hp2ps to produce a Postscript file prog ps The hp2ps utility is described in detail in Sec tion 5 6 hp2ps heap profile to PostScript 4 Display the heap profile using a postscript viewer such as Ghostview or print it out on a Postscript capable printer RTS options for heap profiling There are several different kinds of heap profile that can be generated All the different profile types yield a graph of live heap against time but they differ in how the live heap is broken down into bands The following RTS options select which break down to use hc Breaks down the graph by the cost centre stack which produced the data hm Break down the live heap by the module containing the code which produced the 117 Profiling hd hy hb data Breaks down the graph by closure description For actual data the description is just the constructor name for other closures it is a compiler generated string identifying the closure Breaks down the graph by type For closures which have function type or un known polymorphic type the string will represent an approximation to the actual type Break down the graph by retainer set Retainer profiling is described in more de tail below Section 5 4 2 Retainer Profiling Break down the graph by biography Biographical profiling is described in
292. roperty in Graphics X11 Xlib Misc Region in Graphics X11 Xlib Region Display Screen Visual GC GCValues SetWindowAttributes Point Rectangle Arc Segment and Color in Graph ics X11 Xlib Types 1 4 5 18 xhtml 1 4 6 1 4 7 e Version 2006 8 14 e xhtml is a new package developing on what used to be Text Html and Text Html BlockTable in the base package GHC As A Library Version number 6 6 The internal modules of GHC are now available as a library package name ghc The interface has not been designed with use by other programs in mind so expect the API to vary radically in future releases An introduction to using the library can be found on the wiki http haskell org haskellwiki GHC As_a_library Internal changes e GHC development now has its own integrated wiki and bug tracker http hackage haskell org trac ghc e GHC has now moved to darcs See the wiki http hackage haskell org trac ghc wiki GhcDarcs for more details The sources have moved around a bit within the tree as a result most notably the GHC sources are no longer kept within a ghc subdirectory e The native code generator is now capable of compiling loops which gets us a big step closer to be ing able to compile entirely without gcc on well supported arches 16 Chapter 2 Installing GHC Installing from binary distributions is easiest and recommended Why binaries Because GHC is a Haskell compiler written in H
293. rovides guarded equations is because they allow us to write down the cases we want to consider one at a time independently of each other This structure is hidden in the case version Two of the right hand sides are really the same fail and the whole expression tends to become more and more indented Here is how I would write clunky 138 GHC Language Features 7 3 3 clunky env varl varl Just vall lt lookup env varl Just val2 lt lookup env var2 vall val2 other equations for clunky The semantics should be clear enough The qualifiers are matched in order For a lt qualifier which I call a pattern guard the right hand side is evaluated and matched against the pattern on the left If the match fails then the whole guard fails and the next equation is tried If it succeeds then the appropriate binding takes place and the next qualifier is matched in the augmented environment Unlike list com prehensions however the type of the expression to the right of the lt is the same as the type of the pat tern to its left The bindings introduced by pattern guards scope over all the remaining guard qualifiers and over the right hand side of the equation Just as with list comprehensions boolean expressions can be freely mixed with among the pattern guards For example Haskell s current guards therefore emerge as a special case in which the qualifier list has just one ele ment a boolean expression
294. rrent STM module has been updated to re export all the new modules 1 4 4 10 template haskell 1 4 4 11 1 4 4 12 Version number 2 0 was 1 0 A Show instance is now derived for Info Fixity and FixityDirection in Lan guage Haskell TH Syntax In Language Haskell TH Syntax there is a type PkgName and functions mkPkgName and pkgString for dealing with package names The patGE function in Language Haskell TH Lib now takes the final expression separately to the list of statements rather than splitting it off itself unix No change version 1 0 Win32 Version number 2 1 was 1 0 Now maintained by Esa Ilari Vuokko There is a new module System Win32 Console providing an interface to the Windows Con sole API There is a new module System Win32 DebugApi providing an interface to the Windows De bugApi There is anew module System Win32 FileMapping for working with memory mapped files There is anew module System Win32 SimpleMAPT for using the Windows mail API There is anew module System Win32 Time for using the Windows time API iNVALID_HANDLE_VALUE has moved from Graphics Win32 Misc to Sys tem Win32 Types System Win32 File has a new function getFileInformationByHandle and associated data types System Win32 Info has a new function get SystemInfo and associated data types System Win32 Process now has many more exports System Win32 Types has new types LARGE_INTEG
295. rror if it can t find the library GHCi can also load plain object files o or ob 4 depending on your platform from the command line Just add the name the object file to the command line Ordering of 1 options matters a library should be mentioned before the libraries it depends on see 35 Using GHCi Section 4 10 7 Options affecting linking 3 6 GHCi commands GHCi commands all begin with and consist of a single command name followed by zero or more parameters The command name may be abbreviated with ambiguities being resolved in favour of the more commonly used commands add module Add module s to the current target set and perform a reload browse Displays the identifiers defined by the module module which must be either module loaded into GHCi or be a member of a package If the symbol is placed before the module name then all the identifiers defined in module are shown otherwise the list is limited to the exports of module The form is only available for modules which are interpreted for compiled modules including modules from packages only the non form of browse is available cd dir Changes the current working directory to dir A symbol at the beginning of dir will be replaced by the contents of the environment variable HOME GI NOTE changing directories causes all currently loaded modules to be unloaded This is because the search path is usually expressed
296. rs in the same office An RTS debugging flag varying quantities of output depending on which bits are set in num Only works if the RTS was compiled with the DEBUG option Produce ticky ticky statistics at the end of the program run The file business works just like on the S RTS option above Ticky ticky statistics are counts of various program actions updates enters etc The program must have been compiled using ticky a k a ticky ticky profil 87 Using GHC ing and for it to be really useful linked with suitable system libraries Not a trivial undertaking consult the installation guide on how to set things up for easy ticky ticky profiling For more information see Section 5 7 Using ticky ticky profiling for implementors xC Only available when the program is compiled for profiling When an exception is raised in the program this option causes the current cost centre stack to be dumped to stderr This can be particularly useful for debugging if your program is complaining about a head error and you haven t got a clue which bit of code is causing it compiling with prof auto all and running with RTS xc RTS will tell you exactly the call stack at the point the error was raised The output contains one line for each exception raised in the program the program might raise and catch several exceptions during its execution where each line is of the form
297. rts option see Section 4 6 7 Other options related to interface files Truthfully the reduction on compilation time will be very small However judicious use of import declarations can make a pro gram easier to understand so it may be a good idea anyway 6 2 Faster producing a program that runs quicker The key tool to use in making your Haskell program run faster are GHC s profiling facilities described separately in Chapter 5 Profiling There is no substitute for finding where your program s time space is really going as opposed to where you imagine it is going 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 Another extremely efficient way to make your program snappy is to use library code that has been Seri ously Tuned By Someone Else You might be able to write a better quicksort than the one in Data List but it will take you much longer than typing import Data List Please report any overly slow GHC compiled programs Since GHC doesn t have any credible competi tion 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 NHC or Hugs then it s definite
298. rver 0 D1llUnregisterServer DllUnregisterServer 0 e In addition to creating a DLL the mk d11 option also creates an import library The import lib rary name is derived from the name of the DLL as follows DLL HScool dll gt import lib libHScool_imp a The naming scheme may look a bit weird but it has the purpose of allowing the co existence of im port libraries with ordinary static libraries e g LibHSfoo a and libHSfoo_imp a Addition ally when the compiler driver is linking in non static mode it will rewrite occurrence of 1HS foo on the command line to 1HSfoo_imp By doing this for you switching from non static to static linking is simply a question of adding static to your command line 11 5 2 Making DLLs to be called from other languages If you want to package up Haskell code to be called from other languages such as Visual Basic or C there are some extra things it is useful to know The dirty details are in the Foreign Function Interface definition but it can be tricky to work out how to combine this with DLL building so here s an example e Use foreign export declarations to export the Haskell functions you want to call from the out side For example module Adder wher 218 Running GHC on Win32 systems adder Int gt Int gt IO Int gratuitous use of IO adder x y return x y foreign export stdcall adder Int gt Int gt IO Int Compile it up
299. ry Alternatively use your TMPDIR environment variable Set it to the name of the directory where temporary files should be put GCC and other programs will hon our the TMPDTR variable as well Even better idea Set the DEFAULT_TMPDIR make variable when building GHC and never worry about TMPDIR again see the build documentation Other options related to interface files ddump hi Dumps the new interface to standard output The compiler does not overwrite an existing hi interface file if the new one is ddump the same as the old one this is friendly to make When an interface does change hi diffs it is often enlightening to be informed The ddump hi diffs option will make GHC run diff on the old and new hi files 50 Using GHC 4 6 8 4 6 9 ddump min Dump to the file M imports where M is the module being compiled a minim imal imports al set of import declarations You can safely replace all the import declarations in M hs with those found in M imports Why would you want to do that Be cause the minimal imports a import everything explicitly by name and b im port nothing that is not required It can be quite painful to maintain this property by hand so this flag is intended to reduce the labour show iface Where file is the name of an interface file dumps the contents of that interface file in a human readable ish format The recompilation checker ms Turn off recompilation
300. s This example used an underscore in the fields for which record selectors will not be defined but that is only programming style GHC ig nores them To make use of these hidden fields we need to create some helper functions inc Counter a gt Counter a inc NewCounter x idt NewCounter _this i x _inc i _display d tag t 146 GHC Language Features display Counter a gt IO display NewCounter _this x _display d dx Now we can define counters with different underlying implementations counterA Counter String counterA NewCounter this 0 inc 1 _display print tag A counterB Counter String counterB NewCounter _this _inc _display putStrLn tag B main do display inc counterA prints 1 display inc inc counterB prints In GADT declarations see Section 7 5 Generalised Algebraic Data Types GADTs the explicit forall may be omitted For example we can express the same Counter a using GADT data Counter a where NewCounter _this self ring self gt self _Gisplay self gt IO tag foe ca Counter a At the moment record update syntax is only supported for Haskell 98 data types so the following func tion does not work This is invalid use explicit NewCounter instead for now setTag Counter a gt a gt Counter a setTag obj t obj tag t 7
301. s and Monoid instances for them There is a new module Data Sequence for finite sequences The Data Queue module is now deprecated in favour of this faster more featureful replacement Data Tree now has Data Typeable Traversable and Foldable instances for the Tree datatype Data Typeable now uses fallow overlapping instances so the generic instances can be overriden for your own datatypes Debug Trace now exports t raceShow which is the same as trace except its first argument can be any showable thing rather than being required to be a string Foreign C Types now also defines CIntPtr CUIntPtr CIntMax and CUIntMax Foreign ForeignPtr now exports FinalizerEnvPtr newForeignPtrEnv and add ForeignPtrFinalizerEnv Together these allow the use of finalizers which are passed an ad ditional environment parameter Foreign Marshal Utils no longer exports the withObject function deprecated since Introduction to GHC 5 04 use with instead e Foreign Ptr now also defines IntPtr ptrToIntPtr intPtrToPtr WordPtr ptrTo WordPtr and wordPtrToPtr e There are now Bounded instances for up to 15 tuples used to be up to 4 tuples e The Text Html and Text Html BlockTable modules have now been removed with the new html and xhtml packages providing replacements e Text Read now exports a function parens which parses a value in an arbitrary number of paren theses e The ForeignPtr datatype has been
302. s to insert each of the two values one after the other into the same collection If we re trying to model collections that contain only one type of value then this is clearly an inaccurate type Worse still the definition for g is accepted without causing a type error As a result the error in this code will not be flagged at the point where it appears Instead it will show up only when we try to use g which might even be in a different module 7 4 3 2 1 An attempt to use constructor classes Faced with the problems described above some Haskell programmers might be tempted to use something like the following version of the class declaration class Collects c wher empty i ce insert gt Q Pus member 2G gt Bool The key difference here is that we abstract over the type constructor c that is used to form the collection type c e and not over that collection type itself represented by ce in the original class declaration This 151 GHC Language Features avoids the immediate problems that we mentioned above empty has type Collects c gt ce which is not ambiguous The function f from the previous section has a more accurate type f Collects c gt gt gt c gt ec The function g from the previous section is now rejected with a type error as we would hope because the type of f does not allow the two arguments to have different types This then is an example of a mul tiple parameter class
303. scription now exports haddockName e Distribution Simple Utils now exports copyDirectoryRecursiveVerbose dirOf distPref haddockPref and srcPref It no longer exports mk GHCiLibName 1 4 4 3 haskell98 10 Introduction to GHC e No change version 1 0 1 4 4 4 parsec e Version number 2 0 was 1 0 e No other change 1 4 4 5 readline e No change version 1 0 1 4 4 6 regex base e Version 0 71 e New library that provides common functions for different regex backends 1 4 4 7 regex compat e Version 0 71 e New package providing a replacement Text Regex module 1 4 4 8 regex posix e Version 0 71 e Anew package providing POSIX regexes 1 4 4 9 stm e Version number 2 1 was 1 0 e A new module Co ntrol Monad STM contains the MonadP lus instance for STM and the func tion check both used to be in Cont rol Concurrent STM It also re exports STM atomic ally retry or Else and catchSTM e A new module Control Concurrent STIM TArray defines TArray a transactional array and makes it an instance of MArray e Control Concurrent STM TChan now provides a function newTChanIO which allows TChans to be created in the IO monad Similarly Control Concurrent STM TMVar provides newTMVarIO and newEmptyTMVarI0 and Control Concurrent STM TVar exports newTVarIO 11 Introduction to GHC Control Concurrent STM TVar exports registerDelay The Control Concu
304. se bindings are neither polymorphic or recursive e You may not mix implicit parameter bindings with ordinary bindings in a single let expression use two nested lets instead In the case of where you are stuck since you can t nest where clauses e You may put multiple implicit parameter bindings in a single binding group but they are not treated as a mutually recursive group as ordinary let bindings are Instead they are treated as a non recursive group simultaneously binding all the implicit parameter The bindings are not nested and may be re ordered without changing the meaning of the program For example consider ft let x t Py xt l Int in x y The use of x in the binding for y does not see the binding for x so the type of f is for 2x Int gt Int gt Int 7 4 6 3 Implicit parameters and polymorphic recursion Consider these two definitions lenl a gt Int lenl xs let acc 0 in len_accl xs len_accl acc len_accl x xs let Pacc acc 1 Int in len_accl xs len2 a gt Int len2 xs let acc 0 in len_acc2 xs len_acc2 acc Int gt a gt Int len_acc2 acc len_acc2 x xs let Pacc acc 1 Int in len_acc2 xs The only difference between the two groups is that in the second group len_acc is given a type signa ture In the former case Len_acc1 is monomorphic in its own right hand side so the implicit paramet er acc is not pass
305. search microsoft com 7Esimonpj papers fd 2Dchr 7 4 4 2 Undecidable instances Sometimes even the rules of Section 7 4 4 1 Relaxed rules for instance declarations are too onerous For example sometimes you might want to use the following to get the effect of a class synonym class Cl a C2 a C3 a gt C a where instance Cl a C2 a C3 a gt C a where This allows you to write shorter signatures 155 GHC Language Features f C a gt instead of f Cl a C2 a C3 a gt The restrictions on functional dependencies Section 7 4 3 Functional dependencies are particularly troublesome It is tempting to introduce type variables in the context that do not appear in the head something that is excluded by the normal rules For example class HasConverter ab a gt b where convert a gt b data Foo a MkFoo a instance HasConverter a b Show b gt Show Foo a where show MkFoo value show convert value This is dangerous territory however Here for example is a program that would make the typechecker loop class D a class F a b a gt b instance F a a instance D c F a c gt D a c is not mentioned in the head Similarly it can be tempting to lift the coverage condition class Mul a b c a b gt c where Cos CRE a Re AE instance Mul Int Int Int where instance Mul Int Float Float where x y fromIntegral x y instance M
306. ser These are the files that must end up in your path contains platform specific support files for the installation Typically there is a subdirectory for each fptools project whose name is the name of the project with its version number For example for GHC there would be a sub directory ghc x xx where x xx is the version number of GHC in the bundle These sub directories have the following general structure libHSstd aetc supporting library archives ghc iface prl_ support scripts etc import hi for the prelude include A few C include files contains platform independent support files for the installation Again there is a sub directory for each fptools project contains HTML documentation files one sub directory per project OK so let s assume that you have unpacked your chosen bundles What next Well you will at least need to run the configure script by changing directory into the top level directory for the bundle and typing configure That should convert Makefile into Makefile You can now either start using the tools in situ without going through any installation process just type make in place to set the tools up for this You ll also want to add the path which make will now echo to your PATH environment variable This option is useful if you simply want to try out the package and or you don t have the necessary privileges or inclination to properly install the tools locally Note that if you do decid
307. ser tok m Notice that since Monad is a constructor class the instance is a partial application of the new type not the entire left hand side We can imagine that the type declaration is eta converted to generate the context of the instance declaration We can even derive instances of multi parameter classes provided the newtype is the last class paramet er In this case a partial application of the class appears in the deriving clause For example given the class class StateMonad s m m gt s where 171 GHC Language Features instance Monad m gt StateMonad s State s m where then we can derive an instance of Stat eMonad for Parsers by newtype Parser tok m a Parser State tok Failure m a deriving Monad StateMonad tok The derived instance is obtained by completing the application of the class to the new type instance StateMonad tok State tok Failure m gt StateMonad tok Parser tok m As a result of this extension all derived instances in newtype declarations are treated uniformly and im plemented just by reusing the dictionary for the representation type except Show and Read which really behave differently for the newtype and its representation 7 4 12 2 A more precise specification Derived instance declarations are constructed as follows Consider the declaration after expansion of any type synonyms newtype T vl vn T t vk l vn deriving cl cm where e The
308. serts fignore interface pragmas SL framework framework path 1 L main is no hs main split objs pgmL pgmP pome pgma pgml pgmdll pgmF optl optdll optdep fno asm mangling See Section 4 2 Static Dynamic and Mode options for more on the meaning of static and dynamic flags and Section 4 17 Flag reference for more on the flags them selves There is a new flag x for overriding the default behaviour for source files see Section 4 4 3 1 Overriding the default behaviour for a file details The no recomp option is now called fforce recomp the old name is still accepted for backwards compatibility but will be removed in the future The fglobalise toplev names flag has been removed The fallow overlapping instances flag is implied by the fal low incoherent instances flag The directory that the foo_stub c and foo_stub h files are put in can now be controlled with the stubdir flag See Section 4 6 4 Redirecting the compilation output s for more details When the fno implicit prelude is given the equality test performed when pattern match ing against an overloaded numeric literal now uses the in scope rather than the one from Prelude Likewise the subtraction and inequality test performed when pattern matching against n k patterns uses the and gt in scope Another change to fno implicit prelude with the exception of the arrow synt
309. signatures are perfectly OK g Eq a gt g Ord Ta gt GHC imposes the following restrictions on the constraints in a type signature Consider the type forall tvl tvn cl cn gt type Here we write the foralls explicitly although the Haskell source language omits them in Haskell 98 all the free type variables of an explicit source language type signature are universally quantified except for the class type variables in a class declaration However in GHC you can give the foralls if you want See Section 7 4 8 Arbitrary rank polymorphism 1 Each universally quantified type variable tvi must be reachable from type A type variable a is reachable if it it appears in the same constraint as either a type variable free in in t ype or anoth er reachable type variable A value with a type that does not obey this reachability restriction can not be used without introducing ambiguity that is why the type is rejected Here for example is an illegal type 158 GHC Language Features forall a Eq a gt Int When a value with this type was used the constraint Eq tv would be introduced where tv is a fresh type variable and in the dictionary translation implementation the value would be applied to a dictionary for Eq tv The difficulty is that we can never know which instance of Eq to use be cause we never get any more information about tv Note that the reachability condit
310. sn t fit this model then increasing this option may help performance The main thread is normally started with a slightly larger heap to cut down on unne cessary stack growth while the program is starting up Default 8M Set the maximum stack size for an individual thread to size bytes This option is there purely to stop the program eating up all the available memory in the machine if it gets into an infinite loop 86 Using GHC A Msize sfile Sfile tfile Minimum n of heap which must be available for allocation The default is 3 Default unlimited Set the maximum heap size to size bytes The heap normally grows and shrinks according to the memory requirements of the program The only reason for having this option is to stop the heap growing without bound and filling up all the available swap space which at the least will result in the program being sum marily killed by the operating system The maximum heap size also affects other garbage collection parameters when the amount of live data in the heap exceeds a certain fraction of the maximum heap size compacting collection will be automatically enabled for the oldest generation and the F parameter will be reduced in order to avoid exceeding the maximum heap size Write modest s or verbose S garbage collector statistics into file file The default file is program stat The file stderr is treated specially with the output really being sent to
311. so all operations on Int happen modulo 2n where n is the size in bits of the Int type The fromIntegerfunction and hence also fromIntegral is a special case when converting to Int The value of fromIn tegral x Int is given by taking the lower n bits of abs x multiplied by the sign of x in 2 s complement n bit arithmetic This behaviour was chosen so that for example writ ing Oxffffffff Int preserves the bit pattern in the res ulting Int Negative literals such as 3 are specified by a careful reading of the Haskell Report as meaning Prelude negate Prelude fromInteger 3 So 2147483648 means negate fromInteger 2147483648 Since fromIn teger takes the lower 32 bits of the representation fromIn teger 2147483648 Integer computed at type Int is 2147483648 Int The negate operation then overflows but it is unchecked so negate 2147483648 Int is just 2147483648 In short one can write minBound Int as a literal with the expected meaning but that is not in general guaranteed The fromIntegral function also preserves bit patterns when converting between the sized integral types Int 8 Int16 Int32 Int 64 and the unsigned Word variants see the mod ules Data Int and Data Word in the library documentation Operations on Float and Double numbers are unchecked for overflow underflow and other sad occurrences note however that some architectures trap floating point overflow and loss of precision
312. srrrrssrerrerrerreresrrreesreeeereet 149 7 4 3 1 Rules for functional dependencies 2 0 0 0 cece cece cece ence eee eeneeeneceneeennees 150 7 4 3 2 Background on functional dependencies 0 2 00 0 cce eee ee cece eeceeece eee eeeeeeeee es 150 TAA Anstance declarations siernpre Ea E OS E Disb EE ETER E PESEE ER aip poses 154 7 4 4 1 Relaxed rules for instance declarations ccc ceee cece cece eee eeeeeeeneeeeeeeees 154 TAA2 Undecidable instances sssri a sss veh aastwacs seas bonadh steppe sees ie 155 Vil 6 6 7 4 4 3 Overlapping instantes siisi en eee cece tec e ee E E E EEES EEN 156 7 4 4 4 Type synonyms in the instance head sesneeeesseeeseeerrsresrrrrerrerrsrrerreresren 158 LAS LY pe S AES a a Taree al eR eT ale das 158 7 4 5 1 The context of a type Signature meresi p a ect a E E E E O 158 T43 2 FOt all Hoisting sinies ar oe a a E Ee a dls E EE E ert 159 TA 6 Implicit parameters siipeni esr esy asne n e E EE TE E eS 160 7 4 6 1 Implicit parameter type Constraints 2 2 0 0 ce eee cece cence cece cena ceneeeneeennees 161 7 4 6 2 Implicit parameter bindings 2 0 0 0 eee eee eee cece ee cece ceca eeee eens eeueeeneeeenees 161 7 4 6 3 Implicit parameters and polymorphic recursion 1 1 0 0 0 0 cee eee eeeeceeeeeeeee eee 162 7 4 6 4 Implicit parameters and monomorphism ccc cee eeeeee ences ce eee ece nena es 162 7 4 7 Explicitly kinded quantification ce
313. sses and type variables GHC allows type constructors classes and type variables to be operators and to be written infix very much like expressions More specifically e A type constructor or class can be an operator beginning with a colon e g The lexical syntax is the same as that for data constructors e Data type and type synonym declarations can be written infix parenthesised if you want further ar guments E g data a b Foo ab type a b Either a b class a b where Baz a b x Either a b y 142 GHC Language Features Types and class constraints can be written infix For example Teimas BOOL a b gt a gt b mh x A type variable can be an unqualified operator e g The lexical syntax is the same as that for variable operators excluding and In a binding position the operator must be paren thesised For example type T Int Int Eos Ee Bacher f Left 3 1liftA2 Arrow gt gt a gt b gt c gt e gt a gt e gt b gt e c liftA2 Back quotes work as for expressions both for type constructors and type variables e g Int Either Bool or Int a Bool Similarly parentheses work the same e g Int Bool Fixities may be declared for type constructors or classes just as for data constructors However one cannot distinguish between the two in a fixity declaration a fixity
314. stands for a type variable and not for a type This is a change from GHC s earlier design e Furthermore distinct lexical type variables stand for distinct type variables This means that every programmer written type signature includin one that contains free scoped type variables denotes a rigid type that is the type is fully known to the type checker and no inference is involved e Lexical type variables may be alpha renamed freely without changing the program A lexically scoped type variable can be bound by 168 GHC Language Features e A declaration type signature Section 7 4 10 2 Declaration type signatures e A pattern type signature Section 7 4 10 3 Pattern type signatures e Class and instance declarations Section 7 4 10 4 Class and instance declarations In Haskell a programmer written type signature is implicitly quantifed over its free type variables Sec tion 4 1 2 http haskell org onlinereport decls html sect4 1 2 of the Haskel Report Lexically scoped type variables affect this implicit quantification rules as follows any type variable that is in scope is not universally quantified For example if type variable a is in scope then e a gt a means e a gt a e b gt b means e forall b b gt b e a gt b means e forall b a gt b 7 4 10 2 Declaration type signatures A declaration type signature that has explicit quantification using
315. t GHCi has loaded the Main module and the prompt has changed to Main gt to indicate that the cur rent context for expressions typed at the prompt is the Main module we just loaded we ll explain what the means later in Section 3 4 3 What s really in scope at the prompt So we can now type expres sions involving the functions from Main hs Main gt fac 17 355687428096000 Loading a multi module program is just as straightforward just give the name of the topmost module to the load command hint Load can be abbreviated to 1 The topmost module will normally be Main but it doesn t have to be GHCi will discover which modules are required directly or indirectly by the topmost module and load them all in dependency order 3 2 1 Modules vs filenames Question How does GHC find the filename which contains module M Answer it looks for the file M hs or M lhs This means that for most modules the module name must match the filename If it doesn t GHCi won t be able to find it There is one exception to this general rule when you load a program with load or specify it when you invoke ghci you can give a filename rather than a module name This filename is loaded if it ex ists and it may contain any module you like This is particularly convenient if you have several Main modules in the same directory and you can t call them all Main hs The search path for finding source files is specified with the
316. t Double toDouble fromRational toRational RULES toDouble Int toDouble i2d i2d I i D int2Double i uses Glasgow prim op directly The i2d function is virtually one machine instruction the default conversion via an intermediate Ra tional is obscenely expensive by comparison 7 11 5 Controlling what s going on 197 GHC Language Features e Use ddump rules to see what transformation rules GHC is using e Use ddump simpl stats to see what rules are being fir a more detailed listing ed If you add dppr debug you get e The definition of say buildin GHC Base lhs looks llike this build so forati as forari be ta gt br gt b gt b gt b gt a INLINE build build g g Notice the INLINE That prevents from being inlined when compiling PrelBase so that an importing module will see the and can match it on the LHS of a rule INLINE prevents any inlining happening in the RHS of the INLINE thing I regret the delicacy of this e In libraries base GHC Base hs look at the rules for map to see how to write rules that will do fusion and yet give an efficient program even if fusion doesn t happen More rules in GHC Eist Liis 7 11 6 CORE pragma The external core format supports Note annotations the CORI E pragma gives a way to specify what these should be in your Haskell sou
317. t Here is an example we saw earlier E oertain van gt ay Oar f Tl w k x w k x w c d Here we do not need to give a type signature to w because it is an argument of constructor T1 and that tells GHC all it needs to know 7 4 8 3 Implicit quantification GHC performs implicit quantification as follows At the top level only of user written types if and only if there is no explicit forall GHC finds all the type variables mentioned in the type that are not already in scope and universally quantifies them For example the following pairs are equivalent f a a f forall a a gt a g x a let he st sap gt b gt b hxy y in g x a let h forall b a gt b gt b hxey y Eiee Notice that GHC does not find the innermost possible quantification point For example f a gt a gt Int MEANS f lt forall a a gt ay gt Int NOT f forall a a gt a gt Int g Ord a gt a gt a gt Int MEANS the illegal type g forall a Ord a gt a gt a gt Int NOT g forall a Ord a gt a gt a gt Int The latter produces an illegal type which you might think is silly but at least the rule is simple If you 167 GHC Language Features 7 4 9 want the latter type you can write your for alls explicitly Indeed doing so is strongly advised for rank 2 types Impredicative polymorphism GHC supports impredicative p
318. t before any RTS flags from the GHCRTS environment variable and any flags on the command line You can also change the messages printed when the runtime system blows up e g on stack overflow The hooks for these are as follows 88 Using GHC void OutOf The heap overflow message HeapHook vyvensigtedk The stack overflow message Ovag un fognutak The message printed if malloc fails 1eaggilHepk long int For examples of the use of these hooks see GHC s own versions in the file ghc com piler parser hschooks c ina GHC source tree 4 15 Generating and compiling External Core Files 4 16 4 16 1 GHC can dump its optimized intermediate code said to be in Core format to a file as a side effect of compilation Core files which are given the suffix hcr can be read and processed by non GHC back end tools The Core format is formally described in An External Representation for the GHC Core Lan guage http www haskell org ghc docs papers core ps gz and sample tools in Haskell for manipulat ing Core files are available in the GHC source distribution directory fptools ghc utils ext core Note that the format of hcr files is different though similar to the Core output format generated for debugging purposes Section 4 16 Debugging the compiler The Core format natively supports notes which you can add to your source code using the CORE pragma see Section 7 10 Pragmas
319. t after the gt is the head of the instance declara tion In Haskell 98 the head of an instance declaration must be of the form C T al an where C is the class T is a type constructor and the al an are distinct type variables Furthermore the as sertions in the context of the instance declaration must be of the form C a where a is a type variable that occurs in the head The fglasgow exts flag loosens these restrictions considerably Firstly multi parameter type classes are permitted Secondly the context and head of the instance declaration can each consist of ar bitrary well kinded assertions C t1 tn subject only to the following rules 1 For each assertion in the context a No type variable has more occurrences in the assertion than in the head b The assertion has fewer constructors and variables taken together and counting repetitions than the head 2 The coverage condition For each functional dependency tvs t T EVS ishe of the class every e x t right type variable in S tvs must appear in S tvs _ where is the substitution mapping each 4 right left 7 y type variable in the class declaration to the corresponding type in the instance declaration These restrictions ensure that context reduction terminates each reduction step makes the problem smal ler by at least one constructor For example the following would make the type checker loop if it wasn t excluded insta
320. t in the type of the expression For example the following definition shows how we can define an implicitly parameterized sort function in terms of an explicitly parameterized sort By function sortBy a gt a gt Bool gt a gt a 160 GHC Language Features sort cmp a gt a gt Bool gt a gt a sort sortBy cmp 7 4 6 1 Implicit parameter type constraints Dynamic binding constraints behave just like other type class constraints in that they are automatically propagated Thus when a function is used its implicit parameters are inherited by the function that called it For example our sort function might be used to pick out the least value in a list least 2 cmp a gt a gt Bool gt a gt a least xs head sort xs Without lifting a finger the cmp parameter is propagated to become a parameter of least as well With explicit parameters the default is that parameters must always be explicit propagated With impli cit parameters the default is to always propagate them An implicit parameter type constraint differs from other type class constraints in the following way All uses of a particular implicit parameter must have the same type This means that the type of x x is X a gt a a andnot x a x b gt a b as would be the case for type class constraints You can t have an implicit parameter in the context of a class or instance declaration For
321. t preceded by a backslash Backslash newline pairs are stripped In addition stuff is equivalent to stuff except that it s self delimited and thus needs not to be placed at the end of line or in some brackets Meanings of specific keywords include lt file h gt The specified file gets included into the C program the compiled include file h Haskell file and the C header lt HsFFI h gt is included automat ically define name defin Similar to include Note that includes and defines name value undef nam may be put in the same file twice so they should not assume oth erwise let name parameters Defines a macro to be applied to the Haskell source Parameter definition names are comma separated not inside parens Such macro is in voked as other constructs starting with name The definition will be put in the C program inside parens as arguments of printf To refer to a parameter close the quote put a parameter name and open the quote again to let C string literals concatenate Or use print f s format directives Values of arguments must be given as strings unless the macro stringifies them itself using the C preprocessor s parameter syntax def C_definition The definition of a function variable struct or typedef is written to the C file and its prototype or extern declaration to the C head er Inline functions are handled correctly struct definitions and ty pedefs are written t
322. t s just as if you had written this data T a MkT forall b Either ab forall b b gt b That is since the type variable b isn t in scope it s implicitly universally quantified Arguably it would be better to require explicit quantification on constructor arguments where that is what is wanted Feed back welcomed 165 GHC Language Features You construct values of types T1 MonadT Swizzle by applying the constructor to suitable val ues just as usual For example al T Int al T1 xy gt x 3 a2 a3 Swizzle a2 MkSwizzle sort a3 MkSwizzle reverse a4 MonadT Maybe a4 let r x Just x bm k case m of Just y gt k y Nothing gt Nothing in MkMonad r b mkTs forall b b gt b gt b gt a gt T a mkTs f x y Tl f x Tl f y The type of the argument can as usual be more general than the type required as MkSwizzle re verse shows reverse does not need the Ord constraint When you use pattern matching the bound variables may now have polymorphic types For example fF SP a Shan gt ay Char f Tl w k x w k x w c d g Ord a Ord b gt Swizzle gt a gt a gt b gt b g MkSwizzle s xs f s map f s xs h MonadT m gt m a gt m a hm return m h m x xs bind m x y gt bind m h m xs ys gt return m y ys In the function h we use the record selectors return and bind to extract the polymorphic b
323. t the lowest level e g sloshing MutableByteArray s around your program you may wish to check if there are libraries that provide a Haskellised veneer over the features you want The separate libraries documentation libraries index html describes all the libraries that come with GHC 7 1 Language options These flags control what variation of the language are permitted Leaving out all of them gives you standard Haskell 98 NB turning on an option that enables special syntax might cause working Haskell 98 code to fail to compile perhaps because it uses a variable name which has become a reserved word So together with each option below we list the special syntax which is enabled by this option We use notation and nonterminal names from the Haskell 98 lexical syntax see the Haskell 98 Report There are two classes of special syntax e New reserved words and symbols character sequences which are no longer available for use as iden tifiers in the program e Other special syntax sequences of characters that have a different meaning when this particular op tion is turned on We are only listing syntax changes here that might affect existing working programs i e stolen syn tax Many of these extensions will also enable new context free syntax but in all cases programs writ ten to use the new syntax would not be compilable without the option enabled fglasgow exts This simultaneously enables all of the extens
324. table for a laser printer Normally a profile is limited to 20 bands with additional identifiers being grouped into an OTHER band The 1 flag removes this 20 band and lim it producing as many bands as necessary No key is produced as it won t fit It is useful for creation time profiles with many bands Normally a profile is limited to 20 bands with additional identifiers being grouped into an OTHER band The m flag specifies an alternative band limit the maximum is 20 m0 requests the band limit to be removed As many bands as necessary are produced However no key is produced as it won t fit It is useful for displaying creation time profiles with many bands Use previous parameters By default the PostScript graph is automatic 122 Profiling 5 6 1 5 6 2 S t lt float gt ally scaled both horizontally and vertically so that it fills the page However when preparing a series of graphs for use in a presentation it is often usefu dering as a 1 to draw a new graph using the same scale shading and or previous one The p flag causes the graph to be drawn using the parameters determined by a previous run of hp2ps on file These are extracte d from file aux Use a small box for the title Normally trace elements which sum to a total of less than 1 of the pro file are removed from the profile The t option allows this percentage to be modified maximum 5 t0 requests no trace
325. tantiations The willingness to be overlapped or incoherent is a property of the instance declaration itself controlled by the presence or otherwise of the fallow overlapping instances and fal low incoherent instances flags when that mdodule is being defined Neither flag is required in a module that imports and uses the instance declaration Specifically during the lookup process e An instance declaration is ignored during the lookup process if a a more specific match is found and b the instance declaration was compiled with fallow overlapping instances The flag setting for the more specific instance does not matter e Suppose an instance declaration does not matche the constraint being looked up but does unify with it so that it might match when the constraint is further instantiated Usually GHC will regard this as a reason for not committing to some other constraint But if the instance declaration was compiled with fallow incoherent instances GHC will skip the does it unify check for that declaration These rules make it possible for a library author to design a library that relies on overlapping instances without the library client having to know If an instance declaration is compiled without fallow overlapping instances then that in stance can never be overlapped This could perhaps be inconvenient Perhaps the rule should instead say that the overlapping instance declaration should be compiled in this way rathe
326. tatic Dynamic Reverse pgmL cmd Use cmd as the literate pre dynamic processor pgmP cmd Use cmd as the C pre dynamic processor with cpp only pgmc cmd cmd as the C compiler dynamic pgmm cmd cmd as the mangler dynamic pgms cmd cmd as the splitter dynamic pgma cmd cmd as the assembler dynamic pgml cmd cmd as the linker dynamic pgmdll cmd cmd as the DLL gener dynamic ator pgmF cmd Use cmd as the pre dynamic 109 Using GHC Flag Description processor with F only Static Dynamic 4 17 23 Forcing options to particular phases Section 4 10 2 Forcing options to a particular phase pendency generator Flag Description Static Dynamic Reverse optL option pass option to the literate dynamic pre processor optP option pass optiontocpp with dynamic cpp only optF option pass option to the custom dynamic pre processor optc option pass option to the C dynamic compiler optm option pass option to the man dynamic gler opta option pass option to the assem dynamic bler optl option pass option to the linker dynamic optdll option pass option tothe DLL dynamic generator optdep option pass option to the de dynamic 4 17 24 Platform specific options Section 4 13 Platform specific Flags Flag Description Static Dynamic monly 432 regs x86 only give some re gisters b
327. th prof and additionally use osuf p_o to name the object files dif ferentliy you can choose any suffix that isn t the normal object suffix here GHC will automatic ally load the object files built in the first step when executing splice expressions If you omit the osuf flag when building with prof and Template Haskell is used GHC will emit an error mes sage 7 7 Arrow notation Arrows are a generalization of monads introduced by John Hughes For more details see e Generalising Monads to Arrows John Hughes in Science of Computer Programming 37 pp67 111 May 2000 e A New Notation for Arrows http www soi city ac uk ross papers notation html Ross Pater son in ICFP Sep 2001 e Arrows and Computation http www soi city ac uk ross papers fop html Ross Paterson in The Fun of Programming Palgrave 2003 and the arrows web page at http www haskell org arrows With the farrows flag GHC supports the arrow notation described in the second of these papers What follows is a brief intro duction to the notation it won t make much sense unless you ve read Hughes s paper This notation is translated to ordinary Haskell using combinators from the Control Arrow libraries base Control Arrow html module The extension adds a new kind of expression for defining arrows expl proc apat gt cmd where proc is a new keyword The variables of the pattern are bound in the body of the pro
328. that can convert cygwin s unix style paths to their actual windows style counterparts Many cygwin tools actually accept absolute windows style paths remember though that you either need to escape or convert to so you should be fine just using those everywhere If you need to use tools that do some kind of path mangling that depends on unix style paths one fun example is trying to interpret as a separator in path lists you can still try to convert paths using cygpath just before they are passed to GHC and friends e If you don t have cygpath you probably don t have cygwin and hence no problems with it unless you want to write one build process for several platforms Again relative paths are your friend but if you have to use absolute paths and don t want to use different tools on different platforms you can simply write a short Haskell program to print the current directory thanks to George Russell for this idea compiled with GHC this will give you the view of the file system that GHC depends on which will differ depending on whether GHC is compiled with cygwin s gcc or mingw s gcc or on a real unix system that little program can also deal with escaping in paths Apart from the banner and the startup time something like this would also do echo Directory getCurrentDirectory gt gt putStrLn init tail show g 11 5 Building and using Win32 DLLs Making Haskell libraries into DLLs does
329. the expression minus any discounts that apply see funfolding con discount 4 10 Options related to a particular phase 4 10 1 Replacing the program for one or more phases You may specify that a different program be used for one of the phases of the compilation system in place of whatever the ghc has wired into it For example you might want to try a different assembler The following options allow you to change the external program used for a given compilation phase pgmL cmd Use cmd as the literate pre processor 75 Using GHC PY PI PY PY PY PI PY PI mP cmd mc cmd mm cmd ms cmd ma cmd ml cmd mdll cmd mE cmd Use cmd as the C pre processor with cpp only Use cmd as the C compiler Use cmd as the mangler Use cmd as the splitter Use cmd as the assembler Use cmd as the linker Use cmd as the DLL generator Use cmd as the pre processor with F only 4 10 2 Forcing options to a particular phase Options can be forced through to a particlar compilation phase using the following flags op tion op tion op tion op tion op tion op tion 0p tio 0p tio op tio tL op tP op CF op tc op tm op ta op tl op n tdll op n tdep op n Pass option to the literate pre processor Pass option to CPP makes sense only if cpp is also on Pass option to the custom pre processor
330. the ideas Wobbly types practic al type inference for generalised algebraic data types on Simon PJ s home page The general principle is this type refinement is only carried out based on user supplied type annota tions So if no type signature is supplied for eval no type refinement happens and lots of obscure error messages will occur However the refinement is quite general For example if we had eval Term a gt a gt a eval Lit i j i j the pattern match causes the type a to be refined to Int because of the type of the constructor Lit and that refinement also applies to the type of j and the result type of the case expression Hence the addition i j is legal Notice that GADTs generalise existential types For example these two declarations are equivalent data T a forall b MkT b b gt a data T a where MKT b gt b gt a gt T a 7 6 Template Haskell 7 6 1 Template Haskell allows you to do compile time meta programming in Haskell The background to the main technical innovations is discussed in Template Meta programming for Haskell http research microsoft com simonpj papers meta haskell Proc Haskell Workshop 2002 There is a Wiki page about Template Haskell at _ http www haskell org th http haskell org haskellwiki Template_Haskell and that is the best place to look for further details You may also consult the online Haskell library reference material http www h
331. the inlining of function definitions 7 10 3 1 INLINE pragma GHC with O as always tries to inline or unfold functions values that are small enough thus avoiding the call overhead and possibly exposing other more wonderful optimisations Normally if GHC decides a function is too expensive to inline it will not do so nor will it export that unfolding for other modules to use The sledgehammer you can bring to bear is the INLINE pragma used thusly key_function Int gt String gt Bool Double ifdef _ GLASGOW_HASKELL _ INLINE key_function endif You don t need to do the C pre processor carry on unless you re going to stick the code through HBC it doesn t like INLINE pragmas The major effect of an INLINE pragma is to declare a function s cost to be very low The normal un folding machinery will then be very keen to inline it Syntactically an INLINE pragma for a function can be put anywhere its type signature could be put INLINE pragmas are a particularly good idea for the then return or bind unit functions in a monad For example in GHC s own UniqueSupply monad code we have ifdef _ GLASGOW_HASKELL _ INLINE thenUs INLINE returnUs endif See also the NOINLINE pragma Section 7 10 3 2 NOINLINE pragma 7 10 3 2 NOINLINE pragma The NOINLINE pragma does exactly what you d expect it stops the named funct
332. the options may be combined with one excep tion GHC doesn t currently support mixing the hr and hb options There are two more options which relate to heap profiling isecs Set the profiling sampling interval to secs seconds the default is 0 1 second Fractions are allowed for example i0 2 will get 5 samples per second This only affects heap profiling time profiles are always sampled on a 1 50 second fre quency 118 Profiling xt Include the memory occupied by threads in a heap profile Each thread takes up a small area for its thread state in addition to the space allocated for its stack stacks normally start small and then grow as necessary This includes the main thread so using xt is a good way to see how much stack space the program is using Memory occupied by threads and their stacks is labelled as TSO when display ing the profile by closure description or type description 5 4 2 Retainer Profiling Retainer profiling is designed to help answer questions like why is this data being retained We start by defining what we mean by a retainer A retainer is either the system stack or an unevaluated closure thunk In particular constructors are not retainers An object B retains object A if i B is a retainer object and ii object A can be reached by recursively following pointers starting from object B but not meeting any other retainer objects on the way Each live object
333. this approach is only essential for returning floats or if sizeof int sizeof int on your architecture but is a Good Thing for anyone who cares about writing solid code You re crazy not to do it What if you are importing a module from another package and a cross module inlining exposes a for eign call that needs a supporting include If the imported module is from the same package as the module being compiled you should supply all the include that you supplied when compiling the imported module If the imported module comes from another package you won t necessarily know what the appropriate include options are but they should be in the package configuration which GHC knows about So if you are building a package remember to put all those include options in to the package configuration See the c_includes field in Section 4 8 5 Package management the ghc pkg command It is also possible according the FFI specification to put the include option in the foreign import declaration itself foreign import foo h Int gt IO Int When compiling this module GHC will generate a C file that includes the specified include However GHC disables cross module inlining for such foreign calls because it doesn t transport the include information across module boundaries There is no fundamental reason for this it was just tiresome to implement The wrapper which unboxes the a
334. time they are needed For non auto packages however you need to request the package be loaded by using the package flag ghci package readline Po NPs TRY N s f fs _ f ANNA NI _ GHC Interactive version 6 6 for Haskell 98 http www haskell org ghc Type for help Loading package base linking done Loading package readline 1 0 linking done Prelude gt The following command works to load new packages into a running GHCi Prelude gt set package name But note that doing this will cause all currently loaded modules to be unloaded and you ll be dumped back into the Prelude Extra libraries Extra libraries may be specified on the command line using the normal 1 1 ib option The term library here refers to libraries of foreign object code for using libraries of Haskell source code see Sec tion 3 2 1 Modules vs filenames For example to load the m library ghci 1m On systems with so style shared libraries the actual library loaded will the liblib so GHCi searches the following places for libraries in this order e Paths specified using the Lpath command line option e the standard library search path for your system which on some systems may be overridden by set ting the LD_LIBRARY_PATH environment variable On systems with d11 style shared libraries the actual library loaded will be 1ib d11 Again GHCi will signal an e
335. tion Make pattern bindings dynamic fmono pat binds fno mono pat binds polymorphic fexten Use GHCi s extended de dynamic ded default rules fault rules in a normal mod fno exten ule ded default rules Enable lexically scoped dynamic fscoped type variables Implied by fno type variables fglasgow exts scoped type variables fth Enable Template Haskell dynamic fno th No longer implied by fglasgow exts fbang patterns Enable bang patterns dynamic fno bang patterns 4 17 13 Warnings Section 4 7 Warnings and sanity checking Flag Description Static Dynamic Reverse W enable normal warnings dynamic w wW disable all warnings dynamic Wall enable all warnings dynamic wW Werror make warnings fatal dynamic warn about uses of func dynamic fwarn deprecations tions amp types that are de fno precated warn deprecations warn when an entity is ex dynamic fwarn duplic ported multiple times fno ate exports warn duplic ate exports warn when a hi file in the dynamic fwarn hi shadowing current directory shadows a fno library warn hi shadowing warn when a pattern match dynamic fwarn incom could fail fno plete patterns warn incom plete patterns a warn when a record update dynamic ai could fail fwarn incom plete re fno warn incom 104 Using GHC Flag Description cord updates Static Dynamic Reverse plete re cord updates
336. to package name should be the full package identi fier for the package that is it should include the version number For ex ample package mypkg 1 2 Failure to use the package name option when compiling a package will probably result in disaster but you will only discover later when you attempt to import modules from the package At this point GHC will complain that the package name it was expecting the module to come from is not the same as the package name stored in the hi file It is worth noting that on Windows when each package is built as a DLL since a reference to a DLL costs an extra indirection intra package references are cheaper than inter package references Of course this applies to the main package as well Package management the ghc pkg command The ghc pkg tool allows packages to be added or removed from a package database By default the system wide package database is modified but alternatively the user s local package database or another specified file can be used To see what package databases are in use say ghc pkg list The stack of databases that ghc pkg knows about can be modified using the GHC_LPACKAGE_PATH environment variable see Sec tion 4 8 3 1 The GHC_PACKAGE_PATH environment variable and using package conf op tions on the ghc pkg command line When asked to modify a database ghc pkg modifies the global database by default Specifying user causes it to act on the use
337. to get the following additional information ticks The raw number of time ticks which were attributed to this cost centre from this we get the Stime figure mentioned above bytes Number of bytes allocated in the heap while in this cost centre again this is the raw number from which we get the alloc figure mentioned above What about recursive functions and mutually recursive groups of functions Where are the costs attrib uted Well although GHC does keep information about which groups of functions called each other re cursively this information isn t displayed in the basic time and allocation profile instead the call graph is flattened into a tree The XML profiling tool described in Section 5 5 Graphical time allocation pro file will be able to display real loops in the call graph Inserting cost centres by hand Cost centres are just program annotations When you say auto a11 to the compiler it automatically inserts a cost centre annotation around every top level function in your program but you are entirely free to add the cost centre annotations yourself The syntax of a cost centre annotation is SCC name lt expression gt where name is an arbitrary string that will become the name of your cost centre as it appears in the profiling output and lt expression gt is any Haskell expression An SCC annotation extends as far to the right as possible when parsing Rules for attributing costs
338. tomatically This can all be encapsulated in a little script bin sh head fgrep n END_SAMPLE FOO hp tail 1 cut d f 1 FOO hp hp2ps gt FOO ps gv watch seascape FOO ps amp while 1 do sleep 10 We generate a new profile every 10 seconds head fgrep n END_SAMPLE FOO hp tail 1 cut d f 1 FOO hp hp2ps gt FOO ps done Occasionally gv will choke as it tries to read an incomplete copy of FOO ps because hp2ps is still run ning as an update occurs A slightly more complicated script works around this problem by using the fact that sending a SIGHUP to gv will cause it to re read its input file bin sh head fgrep n END_SAMPLE FOO hp tail 1 cut d f 1 FOO hp hp2ps gt FOO ps gv FOO ps amp gvpsnum while 1 do sleep 10 head fgrep n END_SAMPLE FOO hp tail 1 cut d f 1 FOO hp hp2ps gt FOO ps kill HUP S gvpsnum done 124 Profiling 5 7 Using ticky ticky profiling for imple mentors ToDo document properly It is possible to compile Glasgow Haskell programs so that they will count lots and lots of interesting things e g number of updates number of data constructors entered etc etc We call this ticky ticky profiling because that s the sound a Sun4 makes when it is running up all those counters slowly Ticky ticky profiling is mainly intended for implement
339. uated thunks no indirections nothing can be at the other end of the point er than the primitive value A numerically intensive program using unboxed types can go a lot faster than its standard counterpart we saw a threefold speedup on one example There are some restrictions on the use of primitive types e The main restriction is that you can t pass a primitive value to a polymorphic function or store one in a polymorphic data type This rules out things like Int i e lists of primitive integers The reason for this restriction is that polymorphic arguments and constructor fields are assumed to be pointers if an unboxed integer is stored in one of these the garbage collector would attempt to fol low it leading to unpredictable space leaks Or a seq operation on the polymorphic component may attempt to dereference the pointer with disastrous results Even worse the unboxed value might be larger than a pointer Doub1e for instance e You cannot bind a variable with an unboxed type in a top level binding 135 GHC Language Features e You cannot bind a variable with an unboxed type in a recursive binding e You may bind unboxed variables in a non recursive non top level pattern binding but any such variable causes the entire pattern match to become strict For example data Foo Foo Int Int f x let Foo a b w rhs in body Since b has type Int the entire pattern
340. ude gt x 42 Prelude gt Another important difference between the two types of binding is that the monadic bind p lt e is Strict it evaluates e whereas with the let form the expression isn t evaluated immediately Prelude gt let x error help Prelude gt print x xxx Exception help Prelude gt Note that let bindings do not automatically print the value bound unlike monadic bindings Any exceptions raised during the evaluation or execution of the statement are caught and printed by the GHCi command line interface for more information on exceptions see the module Con trol Exception in the libraries documentation Every new binding shadows any existing bindings of the same name including entities that are in scope in the current module context WARNING temporary bindings introduced at the prompt only last until the next load or reload command at which time they will be simply lost However they do survive a change of context with module the temporary bindings just move to the new location HINT To get a list of the bindings currently in scope use the show bindings command Prelude gt show bindings xo INE Prelude gt HINT if you turn on the t option GHCi will show the type of each variable bound by a statement For example Prelude gt set t Prelude gt let x xs 1 x Integer xs Integer What s really in scope at the prompt When you type an expression
341. ul a b c gt Mul a b c where x v map x v The third instance declaration does not obey the coverage condition and indeed the somewhat strange definition f bxy gt if b then x y else y makes instance inference go into a loop because it requires the constraint Mul a b b Nevertheless GHC allows you to experiment with more liberal rules If you use the experimental flag fallow undecidable instances you can use arbitrary types in both an instance context and instance head Termination is ensured by having a fixed depth recursion stack If you exceed the stack depth you get a sort of backtrace and the opportunity to increase the stack depth with fcontext stack N 7 4 4 3 Overlapping instances In general GHC requires that that it be unambiguous which instance declaration should be used to re solve a type class constraint This behaviour can be modified by two flags fal low overlapping instances and fallow incoherent instances as this section dis cusses Both these flags are dynamic flags and can be set on a per module basis using an OP TIONS_GHC pragma if desired Section 4 1 2 command line options in source files 156 GHC Language Features When GHC tries to resolve say the constraint C Int Bool it tries to match every instance declara tion against the constraint by instantiating the head of the instance declaration For example consider these declarations instance co
342. ule M and there is also a module M in your program Or perhaps the dependencies of packages that you used contain some overlapping modules Perhaps the program even contains multiple versions of a certain package due to dependencies from other packages None of these scenarios gives rise to an error on its own but they may have some interesting con sequences For instance if you have a type M T from version 1 of package P then this is not the same as the type M T from version 2 of package P and GHC will report an error if you try to use one where the other is expected Formally speaking in Haskell 98 an entity function type or class in a program is uniquely identified by the pair of the module name in which it is defined and its name In GHC an entity is uniquely defined by a triple package module and name Package Databases A package database is a file normally called package conf which contains descriptions of installed packages GHC usually knows about two package databases e The global package database which comes with your GHC installation e A package database private to each user On Unix systems this will be SHOME ghc arch os version package conf and on Windows it will be something like C Documents And Settings user ghc The ghc pkg tool knows where this file should be located and will create it if it doesn t exist see Section 4 8 5 Package management the ghc pkg command When GHC starts up
343. ump spec ddump splices Dump specialiser output Dump TH splided expres sions and what they evalu ate to dynamic dynamic ddump stg Dump final STG dynamic ddump stranal Dump strictness analyser output dynamic ddump tc Dump typechecker output dynamic ddump types Dump type signatures dynamic ddump work er wrapper Dump worker wrapper out put dynamic ddump if trac ddump tc trac Trace interface files Trace typechecker dynamic dynamic ddump rn trac Trace renamer dynamic ddump rn stats Renamer stats dynamic 111 Using GHC Flag ddump simpl stats Description Dump simplifier stats Static Dynamic dynamic dppr debug Turn on debug printing more verbose static dppr noprags Don t output pragma info in dumps static dppr user length Set the depth for printing expressions in error msgs static dsource stats Dump haskell source stats dynamic demm lint C pass sanity checking dynamic dstg lint STG pass sanity checking dynamic dstg stats Dump STG stats dynamic dverb Show output from each dynamic ose core2core core to core pass dverbose stg2stg Show output from each STG to STG pass dynamic dshow passes Print out each pass name as it happens dynamic dfaststring stats
344. unction Omitting standard implementation details this would lead to the following declarations instance Eq e gt Collects e wher instance Eq e gt Collects e e gt Bool where instance Collects Char BitSet where instance Hashable e Collects a ce gt Collects e Array Int ce where All this looks quite promising we have a class and a range of interesting implementations Unfortu nately there are some serious problems with the class declaration First the empty function has an am biguous type empty Collects ce gt C By ambiguous we mean that there is a type variable e that appears on the left of the gt symbol but not on the right The problem with this is that according to the theoretical foundations of Haskell over loading we cannot guarantee a well defined semantics for any term with an ambiguous type We can sidestep this specific problem by removing the empty member from the class declaration However although the remaining members insert and member do not have ambiguous types we still run into problems when we try to use them For example consider the following two functions f x y insert x insert y g f True a for which GHC infers the following types ss Collects ac Collects bc S ac 2S bi SSE Se g Collects Bool c Collects Char c gt c gt c Notice that the type for f allows the two parameters x and y to be assigned different types even though it attempt
345. unix library dirs usr lib ghc 6 4 libraries unix hs libraries HSunix extra libraries HSunix_cbits dl include dirs usr lib ghc 6 4 libraries unix include includes HsUnix h depends base 1 0 3333333333383838888 The full Cabal documentation Cabal index html is still in preparation at time of writing so in the meantime here is a brief description of the syntax of this file A package description consists of a number of field value pairs A field starts with the field name in the oe left hand column followed by a and the value continues until the next line that begins in the left 70 Using GHC hand column or the end of file The syntax of the value depends on the field The various field types are freeform string string list Any arbitrary string no interpretation or parsing is done A sequence of non space characters or a sequence of arbitrary characters surrounded by quotes A sequence of strings separated by commas The sequence may be empty In addition there are some fields with special syntax e g package names version dependencies The allowed fields with their types are name version license license file copyright maintainer stability homepage package url description category author exposed exposed mod ules hidden mod ules import dirs The package s name without the version The
346. unning GHC executable lives and derives libdir from that Unix lacks such a system call That is why bindir must be libdir bin The binary directory The binary directory bindir contains user visible executables notably ghc and ghci You should add it to your SPATH On Unix the user invokable ghc invokes libdir ghc version passing a suitable B flag to tell ghc version where libdir is Similarly ghci except the extra flag interactive is passed On Win32 the user invokable ghc binary is the Real Thing no intervening shell scripts or bat files Reason we sometimes invoke GHC with very long command lines and cmd exe which executes bat files truncates them Similarly ghci is a C wrapper program that invokes ghc interactive passing on all other arguments not a bat file 2 3 2 The library directory 22 Installing GHC The layout of the library directory libdir is almost identical on Windows and Unix as follows Differences between Windows and Unix are noted thus Win32 only and are commented below S libdir package conf ghc usage txt GHC package configuration Message displayed by ghc help bin Win32 only User visible binaries ghc exe ghci exe unlit Remove literate markup touchy exe Win32 only perl exe Win32 only gcc exe Win32 only ghc x xx GHC executable Unix only ghe split Asm code splitter ghc asm Asm code mangler gcec lib Wi
347. use keep hc file too and have OPTION flags in your module the OPTIONS_GHC will get put into the generated hc file Setting options in GHCi Options may also be modified from within GHCi using the set command See Section 3 7 The set command for more details 41 Using GHC 4 2 Static Dynamic and Each of GHC s command line options is classified Mode options as either static or dynamic or mode Mode flags For example make or 1 mand line The available mo E There may be only a single mode flag on the com des are listed in Section 4 4 Modes of operation Dynamic Flags Most non mode flags fall into this category A dynamic flag may be used on the command line ina GHC_OPTIONS pragma in a source file or set using set in GHCi Static Flags A few flags are static which means they can only be used on the command line and remain in force over the entire GHC GHCi run The flag reference tables Section 4 17 Flag reference lists the status of each flag 4 3 Meaningful file suffixes File names with meaningful suffixes e g 1hs or o cause the right thing to happen to those files hs A Haskell module lhs A literate Haskell module hi A Haskell interface file probably compiler generated she Intermediate C file produced by the Haskell compiler ie A C file not produced by the Haskell compiler S An assembly language source file usually
348. using relative directories and changing the search path in the middle of a session is not supported def name ex The command def name expr defines a new GHCi command name imple pr mented by the Haskell expression expr which must have type String gt IO String When name args is typed at the prompt GHCi will run the expres sion name args take the resulting String and feed it back into GHCi as a new sequence of commands Separate commands in the result must be separated by Nis That s all a little confusing so here s a few examples To start with here s a new GHCi command which doesn t take any arguments or produce any results it just outputs the current date amp time Prelude gt let date _ Time getClockTime gt gt print gt gt return Prelude gt def date dat Prelude gt date Fri Mar 23 15 16 40 GMT 2001 Here s an example of a command that takes an argument It s a re implementation of cd Prelude gt let mycd d Directory setCurrentDirectory d gt gt return Prelude gt def mycd mycd Prelude gt mycd Or I could define a simple way to invoke ghc make Main in the current dir ectory Prelude gt def mak _ gt return ghc make Main We can define a command that reads GHCi input from a file This might be useful 36 Using GHCi edit file shelp info name Load module main arg r arg module modi Lapib
349. ust pro actively read the interface files for all orphan modules just in case their instances or rules play a role whether or not the module s interface would otherwise be of any use Other things being equal avoid orphan modules By default the compiler will warn you if a set of patterns are overlapping i e String gt Int 222s Tan Hh Fh Fh Fh 1 2 where the last pattern match in won t ever be reached as the second pattern overlaps it More often than not redundant pat terns is a programmer mistake error so this option is enabled by default Causes the compiler to warn about lambda bound patterns that can fail eg x xs gt Normally these aren t treated as incomplete patterns by fwarn incomplete patterns Lambda bound patterns includes all places where there is a single pattern including list comprehensions and do notation In these cases a pattern match failure is quite legitimate and trig gers filtering list comprehensions or the monad fail operation monads For example f Maybe a gt a 62 Using GHC f xs y Just y lt xs Switching on fwarn simple patterns will elicit warn ings about these probably innocent cases which is why the flag is off by default The deriving Read mechanism produces monadic code with pattern matches so you will also get misleading warnings about the compiler generated code This is arguably a Bad Thing b
350. ut 1 returnA lt output The translation of such forms uses the loop combinator so the arrow concerned must belong to the ArrowLoop class Conditional commands In the previous example we used a conditional expression to construct the input for an arrow Some times we want to conditionally execute different commands as in proc x y gt 181 GHC Language Features 7 7 3 if f x y then g lt x 1 else h lt y 2 which is translated to arr x y gt if f x y then Left x else Right y gt gt gt arr x gt x 1 gt gt gt f I arr y gt y 2 gt gt gt g Since the translation uses the arrow concerned must belong to the ArrowChoice class There are also case commands like case input of lt gt m lt x gt g lt xtl X1 x2 xs gt do y lt h lt xl x2 ys lt k lt xs returnA lt y ys The syntax is the same as for case expressions except that the bodies of the alternatives are commands rather than expressions The translation is similar to that of i f commands Defining your own control structures As we re seen arrow notation provides constructs modelled on those for expressions for sequencing value recursion and conditionals But suitable combinators which you can define in ordinary Haskell may also be used to build new commands out of existing ones The basic idea is that a command defines an arrow from environments to values These en
351. ut it s awkward to fix fwarn type defaults Have the compiler warn inform you where in your source the Haskell defaulting mechanism for numeric types kicks in This is useful information when converting code from a context that as sumed one default into one with another e g the default default for Haskell 1 4 caused the otherwise unconstrained value 1 to be given the type Int whereas Haskell 98 defaults it to Integer This may lead to differences in performance and behaviour hence the usefulness of being non silent about this This warning is off by default fwarn unused binds Report any function definitions and local bindings which are un used For top level functions the warning is only given if the binding is not exported A definition is regarded as used if a it is exported or b it is mentioned in the right hand side of another definition that is used or c the function it defines begins with an underscore The last case provides a way to suppress unused binding warnings select ively Notice that a variable is reported as unused even if it appears in the right hand side of another unused binding fwarn unused imports Report any modules that are explicitly imported but never used However the form import M is never reported as an unused import because it is a useful idiom for importing instance declar ations which are anonymous in Haskell fwarn unused matches Report all unused variab
352. utions and set your UDG_HOME environment variable to point to the installation direct ory ghcprof uses an XML based profiling log format and you therefore need to run your program with a different option px The file generated is still called lt prog gt prof To see the profile run gheprof like this ghcprof lt prog gt prof which should pop up a window showing the call graph of your program in glorious detail More inform ation on using gheprof can be found at The Cost Centre Stack Profiling Tool for GHC http www dcs warwick ac uk people academic Stephen Jarvis profiler index html Actually this isn t true any more we are working on a new tool for displaying heap profiles using Gtk HS so gheprof may go away at some point in the future 121 Profiling 5 6 hp gt 2ps heap profile to PostScript Usage hp2ps flags lt file gt hp The program hp2ps converts a heap profile as produced by the h lt break down gt runtime option into a PostScript graph of the heap profile By convention the file to be processed by hp2ps has a hp ex tension The PostScript output is written to lt file gt ps If lt file gt is omitted entirely then the pro gram behaves as a filter hp2ps is distributed in ghc utils hp2ps in a GHC source distribution It was originally developed by Dave Wakeling as part of the HBC LML heap profiler The flags are d e lt float gt in mm pt m lt int gt In
353. values of that type are represented by a pointer to a heap object The representation of a Haskell Int for example is a two word heap object An unboxed type however is represented by the value itself no pointers or heap allocation are involved Unboxed types correspond to the raw machine types you would use in C Int long int Double double Addr void etc The primitive operations PrimOps on these types are what you might expect e g is addition on Int s and is the machine addition that we all know and love usually one instruction Primitive unboxed types cannot be defined in Haskell and are therefore built into the language and compiler Primitive types are always unlifted that is a value of a primitive type cannot be bottom We use the convention that primitive types values and operations have a suffix Primitive values are often represented by a simple bit pattern such as Int Float Double But this is not necessarily the case a primitive value might be represented by a pointer to a heap allocated object Examples include Array the type of primitive arrays A primitive array is heap allocated be cause it is too big a value to fit in a register and would be too expensive to copy around in a sense it is accidental that it is represented by a pointer If a pointer represents a primitive value then it really does point to that value no uneval
354. variable a appears in no other constraints e All the classes Ci are standard e At least one of the classes Ci is numeric At the GHCi prompt the second and third rules are relaxed as follows differences italicised e All of the classes Ci are single parameter type classes e At least one of the classes Ci is numeric or is Show Eq or Ord The same type default behaviour can be enabled in an ordinary Haskell module using the flag fextended default rules 3 5 Invoking GHCi GHCi is invoked with the command ghci or ghc interactive One or more modules or file names can also be specified on the command line this instructs GHCi to load the specified modules or filenames and all the modules they depend on just as if you had said load modules at the GHCi prompt see Section 3 6 GHCi commands For example to start GHCi and load the program whose topmost module is in the file Main hs we could say ghci Main hs Most of the command line options accepted by GHC see Chapter 4 Using GHC also make sense in in 34 Using GHCi 3 5 1 3 5 2 teractive mode The ones that don t make sense are mostly obvious for example GHCi doesn t generate interface files so options related to interface file generation won t have any effect Packages Most packages see Section 4 8 1 Using Packages are available without needing to specify any extra flags at all they will be automatically loaded the first
355. ve data in the old est generation when we last collected it then by default we ll wait until it grows to 4M before collecting it again The default seems to work well here If you have plenty of memory it is usually bet ter to use Hsi ze than to increase F factor The F setting will be automatically reduced by the garbage collector when the max imum heap size the Ms i ze setting is approaching Default 2 Set the number of generations used by the garbage collector The default of 2 seems to be good but the garbage collector can support any number of genera tions Anything larger than about 4 is probably not a good idea unless your program runs for a Jong time because the oldest generation will hardly ever get collected Specifying 1 generation with RTS G1 gives you a simple 2 space collector as you would expect In a 2 space collector the A option see above specifies the min imum allocation area size since the allocation area will grow with the amount of live data in the heap In a multi generational collector the allocation area is a fixed size unless you use the H option see below Default 0 This option provides a suggested heap size for the garbage collector The garbage collector will use about this much memory until the program residency grows and the heap size needs to be expanded to retain reasonable performance By default the heap will start small and grow and shrink as necessary This can be bad
356. ve distinct types and so can t be compared It s just about possible to imagine ex amples in which the derived instance would make sense but it seems altogether simpler simply to prohibit such declarations Define your own instances 148 GHC Language Features 7 4 2 Class declarations This section and the next one documents GHC s type class extensions There s lots of background in the paper Type classes exploring the design space http research microsoft com simonpj Papers type class design space Simon Peyton Jones Mark Jones Erik Meijer All the extensions are enabled by the Eglasgow exts flag 7 4 2 1 Multi parameter type classes Multi parameter type classes are permitted For example class Collection c a where union ca gt ca gt ca ETE ORS 7 4 2 2 The superclasses of a class declaration There are no restrictions on the context in a class declaration which introduces superclasses except that the class hierarchy must be acyclic So these class declarations are OK class Functor m k gt FiniteMap m k where class Monad m Monad t m gt Transform t m where lift ma gt t m a As in Haskell 98 The class hierarchy must be acyclic However the definition of acyclic involves only the superclass relationships For example this is OK class C a where op Db gt a gt b gt b class C a gt D a where Here C is a superclass of D but it s OK for a class o
357. via C Compile via C dynamic fasm fno code Omit code generation mode 4 17 21 Linking options Section 4 10 7 Options affecting linking Flag Description Static Dynamic Reverse f PICc Generate position inde static pendent code where avail 108 Using GHC link in the framework name This option corres ponds to the framework option for Apple s Linker Flag Description Static Dynamic Reverse able dynamic Use dynamic Haskell lib _ static raries if available framework name On Darwin MacOS X only dynamic framework path On Darwin MacOS X only name add dir to the list of dir ectories searched for frame works This option corres ponds to the F option for Apple s Linker dynamic llib Link in library 1ib dynamic Ldir Add dir to the list of dir dynamic ectories searched for librar ies main is Set main function dynamic mk d11 DLL creation mode dynamic Windows only no hs main Don t assume this program dynamic contains main no link Omit linking dynamic split objs Split objects for libraries dynamic static Use static Haskell libraries static threaded Use the threaded runtime static debug Use the debugging runtime static 4 17 22 Replacing phases Section 4 10 1 Replacing the program for one or more phases Flag Description S
358. vironments assign values to the free local variables of the command Thus combinators that produce arrows from arrows may also be used to build commands from commands For example the Ar rowChoice class includes a combinator ArrowChoice a gt lt gt a ca c gt a e so we can use it to build commands expr proc x gt do returnA lt x lt gt do symbol Plus lt y lt term lt expr lt x y lt gt do symbol Minus lt y lt term lt expr lt x y The do on the first line is needed to prevent the first lt gt from being interpreted as part of the ex pression on the previous line This is equivalent to expr proc x gt returnA lt x lt gt proc x gt do symbol Plus lt y lt term lt expr lt x y 182 GHC Language Features 7 7 4 lt gt proc x gt do symbol Minus lt y lt term lt expr lt x y It is essential that this operator be polymorphic in e representing the environment input to the command and thence to its subcommands and satisfy the corresponding naturality property arr k gt gt gt f lt gt g arr k gt gt gt lt gt arr k gt gt gt g at least for strict k This should be automatic if you re not using seq This ensures that environments seen by the subcommands are environments of the whole command and also allows the translation to safely trim these environ
359. warning 62 overloading death to 129 191 192 P package url package specification 71 packages 64 building 67 management 68 using 64 with GHCi 35 parallel bundles 19 parallel list comprehensions 140 parallelism 81 82 203 parser generator for Haskell 212 Pattern guards Glasgow extension 138 patterns incomplete 60 patterns overlapping 62 phases changing 75 platform specific options 82 postscript from heap profiles 122 pragma 187 LANGUAGE 190 LINE 190 OPTIONS_GHC 191 pragma CORE 198 pragma RULES 193 pragma SPECIALIZE 191 pre processing cpp 76 pre processing custom 78 Pre processor options 78 problems 210 problems running your program 210 problems with the compiler 210 profiling 113 options 116 ticky ticky 87 with Template Haskell 179 profiling bundles 19 profiling ticky ticky 125 prompt GHCi 26 R reading Core syntax 98 recompilation checker 46 51 record updates incomplete 60 recursion between modules 51 redirecting compilation output 48 reporting bugs 3 rewrite rules 193 RTS behaviour changing 88 RTS hooks 88 RTS options 84 from the environment 84 garbage collection 85 RTS options concurrent 81 RTS options hacking debugging 87 RULES pragma 193 running compiled program 84 runtime control of Haskell programs 84 S sanity checking options 57 search path 47 segmentation fault 211 separate compilation 43 46 shadowing
360. wing 60 fwarn incomplete patterns 60 fwarn incomplete record updates 60 fwarn missing fields 60 fwarn missing methods 60 fwarn missing signatures 62 fwarn missing signatures option 129 fwarn name shadowing 62 fwarn orphans 62 fwarn overlapping patterns 62 fwarn simple patterns 62 fwarn type defaults 63 fwarn unused binds 63 fwarn unused imports 63 fwarn unused matches 63 G RTS option 86 G RTS option 132 H 46 RTS option 86 h lt break down gt 122 hb RTS option 118 118 hc RTS option 117 118 hC RTS option 118 hcsuf 49 hd RTS option 118 118 hi diffs option 211 hide package 65 65 hidir 49 hisuf 49 hm RTS option 117 118 hr RTS option 118 118 hy RTS option 118 118 I 77 RTS option 86 i 118 idirs 48 ignore dot ghci 40 ignore package 65 ignore scc 117 k RTS option 86 K RTS option 86 keep hc files 50 keep raw s files 50 keep s files 50 keep tmp files 50 1 79 L 80 m RTS option 87 M RTS option 87 m options 82 M lt size gt option 127 M lt size gt RTS option 132 main is 80 monly N regs option 1X86 only 84 n 45 no hs main 81 207 no recomp 6 no user package conf 66 Nx RTS option 82 O 40 187 0 48 O option 73 O not specified 73 O0 73 O1 option 73 O2 option 73 odir 49 Ofile lt file gt option 74 ohi 49 opta 76 optc 76 optdep 76 optdll 76
361. with GHCi is because the bytecode compiler can t deal with unboxed tuples No they don t This is because the Haskell binding to the GNU readline library doesn t support reading from the terminal in a non blocking way which is required to work properly with GHC s concurrency model This is the defined behaviour of getContents it puts the stdin Handle in a state known as semi closed wherein any further I O operations on it are forbidden Because I O state is retained between computations the semi closed state persists until the next Load or reload command You can make st din reset itself after every evaluation by giving GHCi the command set r This works because stdin is just a top level expression that can be reverted to its unevaluated state in the same way as any other top level expression CAF 40 Chapter 4 Using GHC 4 1 Options overview 4 1 1 4 1 2 4 1 3 GHC s behaviour is controlled by options which for historical reasons are also sometimes referred to as command line flags or arguments Options can be specified in three ways command line arguments An invocation of GHC takes the following form ghc argument command line arguments are either options or file names command line options begin with They may not be grouped vO is different from v O Options need not precede filenames e g ghc o o foo All options are processed and then applied to all files you cannot for ex
362. without existentials complicating the picture Also an existential pattern binding at the top level of a module doesn t make sense because it s not clear how to prevent the existentially quantified type escaping So for now there s a simple to state restriction We ll see how annoying it is You can t use existential quantification for newt ype declarations So this is illegal newtype T forall a Ord a gt MkT a Reason a value of type T must be represented as a pair of a dictionary for Ord t and a value of type t That contradicts the idea that newt ype should have no concrete representation You can get just the same efficiency and effect by using data instead of newt ype If there is no overloading involved then there is more of a case for allowing an existentially quantified newt ype because the data version does carry an implementation cost but single field existentially quantified construct ors aren t much use So the simple restriction no existential stuff on newt ype stands unless there are convincing reasons to change it You can t use deriving to define instances of a data type with existentially quantified data con structors Reason in most cases it would not make sense For example data T forall a MkT a deriving Eq To derive Eq in the standard way we would need to have equality between the single component of two MkT constructors instance Eq T where MkT a MKT b But a and b ha
363. xposed The package P can be spe cified in full with its version number e g network 1 0 or the version number can be omitted if there is only one version of the package installed 64 Using GHC 4 8 2 hide all packages hide package P ignore package P If there are multiple versions of P installed then all other versions will be come hidden The package P option also causes package P to be linked into the res ulting executable In make mode and GHCi the compiler normally de termines which packages are required by the current Haskell modules and links only those In batch mode however the dependency information isn t available and explicit package options must be given when linking For example to link a program consisting of objects Foo o and Main o where we made use of the net work package we need to give GHC the package flag thus ghc o myprog Foo o Main o package network The same flag is necessary even if we compiled the modules from source because GHC still reckons it s in batch mode ghc o myprog Foo hs Main hs package network In make and interactive modes Section 4 4 Modes of opera tion however GHC figures out the packages required for linking without further assistance The one other time you might need to use package to force linking a package is when the package does not contain any Haskell modules it might contain a C library only for example In th
364. y You can say ghc 49 Using GHC to get the ordinary version and ghe osuf prof o hisuf prof hi prof auto all to get the profiled version 4 6 5 Keeping Intermediate Files 4 6 6 4 6 7 The following options are useful for keeping certain intermediate files around when normally GHC would throw these away after compilation Keep intermediate hc files when doing hs to o compilations via C NOTE keep hc files hc files aren t generated when using the native code generator you may need to use fvia C to force them to be produced keep s files Keep intermediate s files Keep intermediate raw s files These are the direct output from the C compiler keep before GHC does assembly mangling to produce the s file Again these are raw s files not produced when using the native code generator Instructs the GHC driver not to delete any of its temporary files which it normally keep keeps in tmp or possibly elsewhere see Section 4 6 6 Redirecting temporary tmp files files Running GHC with v will show you what temporary files were generated along the way Redirecting temporary files tmpdir If you have trouble because of running out of space in tmp or wherever your in stallation thinks temporary files should go you may use the tmpdir lt dir gt option to specify an alternate directory For example tmpdir says to put temporary files in the current working directo
365. y grep loading is creeping in search your interface files for overloaded type signa tures You can view inter face files using the show iface option see Section 4 6 7 Other options related to interface files ghc show iface Foo hi egr Strict functions are your dear and among other things lazy pattern matching is your enemy friends If you don t know what a strict function is please consult a functional programming textbook A sentence or two of explana tion here probably would not do much good Consider these two code fragments 129 Advice on sooner faster smaller thriftier GHC loves single constructor data types Newtypes are better than datatypes How do I find out a function s strictness Force key functions to be INLINEd esp monads Explicit export list f Wibble x y strict let f arg Wibble x y arg in The former will result in far better code A less contrived example shows the use of cases instead of lets to get stricter code a good thing f Wibble x y beautiful but slow let al bl cl unpackFoo x a2 b2 c2 unpackFoo y LA ok f Wibble x y ugly and proud of it of of bl b2 el gt c2 gt case case unpackFoo x unpackFoo y al a2 a It s all the better if a function is strict in a single constructor type a
366. y can use this stuff to write 134 GHC Language Features 7 2 1 fast code we generally find it a lot less painful and more satisfying in the long run to use higher level language features and libraries With any luck the code you write will be optimised to the efficient un boxed version in any case And if it isn t we d like to know about it We do not currently have good up to date documentation about the primitives perhaps because they are mainly intended for internal use There used to be a long section about them here in the User Guide but it became out of date and wrong information is worse than none The Real Truth about what primitive types there are and what operations work over those types is held in the file fptools ghc compiler prelude primops txt pp This file is used directly to generate GHC s primitive operation definitions so it is always correct It is also intended for processing into text Indeed the result of such processing is part of the description of the External Core language http haskell cs yale edu ghc docs papers core ps gz So that document is a good place to look for a type set version We would be very happy if someone wanted to volunteer to produce an SGML back end to the program that processes primops txt so that we could include the results here in the User Guide What follows here is a brief summary of some main points Unboxed types Most types in GHC are boxed which means that
367. y when messages from the rm or mv an Explorer window is open on it tends to fail Closing the win dow generally cures the problem but sometimes its cause is more mysterious and logging off and back on or rebooting may be the quickest cure 2 3 The layout of installed files 2 3 1 This section describes what files get installed where You don t need to know it if you are simply in stalling GHC but it is vital information if you are changing the implementation GHC is installed in two directory trees Library directory known as libdir holds all the support files needed to run GHC On Unix this directory is usually something like usr lib ghc ghc 5 02 Binary directory known as bindir holds executables that the user is expected to in voke Notably it contains ghc and ghci On Unix this directory can be anywhere but is typically something like usr local bin On Win dows however this directory must be libdir bin When GHC runs it must know where its library directory is It finds this out in one of two ways e libdir is passed to GHC using the B flag On Unix but not Windows the installed ghc is just a one line shell script that invokes the real GHC passing a suitable B flag All the user supplied flags follow and a later B flag overrides an earlier one so a user supplied one wins e On Windows but not Unix if no B flag is given GHC uses a system call to find the directory in which the r
368. z forall a Eq a gt Bazlaa forall b Show b gt Baz2 b b gt b The two constructors have the types you d expect Bazl forall a Eq a gt a gt a gt Baz Baz2 forall b Show b gt b gt b gt b gt Baz But when pattern matching on Baz1 the matched values can be compared for equality and when pattern matching on Baz2 the first matched value can be converted to a string as well as applying the function to it So this program is legal f Baz gt String f Bazl p q p Yes otherwise No f Baz2 v fn show fn v Operationally in a dictionary passing implementation the constructors Baz1 and Baz2 must store the dictionaries for Eq and Show respectively and extract it on pattern matching Notice the way that the syntax fits smoothly with that used for universal quantification earlier 7 4 1 4 3 Record Constructors GHC allows existentials to be used with records syntax as well For example data Counter a forall self NewCounter _this sp gelf z ine self gt self _Gisplay self gt IO tag aware Here tag is a public field with a well typed selector function tag Counter a gt a The self type is hidden from the outside any attempt to apply _this _inc or _output as functions will raise a compile time error In other words GHC defines a record selector function only for fields whose type does not mention the existentially quantified variable
Download Pdf Manuals
Related Search
Related Contents
CDX-GT373A "取扱説明書" ARIVA T60 - Satsklep McompactL-864 español uni manual de instrucciones para equipo de corte en plasma Car0ー&Life DIAGNOSTIC - AutoZonePro.com PDF - BioMedSearch maTrixx SC-50 LED Effect LED scanner user manual Copyright © All rights reserved.
Failed to retrieve file