https://wiki.haskell.org/api.php?action=feedcontributions&user=AlfonsoAcosta&feedformat=atomHaskellWiki - User contributions [en]2024-03-29T04:46:41ZUser contributionsMediaWiki 1.35.5https://wiki.haskell.org/index.php?title=Applications_and_libraries/Hardware_verification&diff=23183Applications and libraries/Hardware verification2008-09-30T12:00:38Z<p>AlfonsoAcosta: </p>
<hr />
<div>== Applications ==<br />
<br />
;[http://www.ict.kth.se/org/ict/ecs/sam/projects/forsyde/www/ ForSyDe] <br />
: The ForSyDe (Formal System Design) methodology is implemented as a Haskell-based DSL. ForSyDe's embedded compiler is capable of translating system models to synthesizable VHDL. Moreover it is able to interface with third party EDA tools such as Altera's [http://www.model.com/ Modelsim] and [http://www.altera.com/products/software/quartus-ii/subscription-edition/qts-se-index.html Quartus II].<br />
<br />
;[http://funhdl.org/wiki/doku.php/atom Atom]<br />
:Atom is a hardware description language embedded in Haskell. Atom compiles conditional term rewriting systems to Verilog and VHDL for IC simulation, verification, and synthesis.<br />
<br />
;[http://www.macs.hw.ac.uk/~gnik/apset/circuit_simulator.lhs Circuit Simulator]<br />
:David J. King and John O'Donnell's digital circuit simulator<br />
<br />
;Hawk---Specifying and Prototyping Microprocessors<br />
:The goal of the Hawk project is to develop a language for expressing highly abstracted specifications of modern microprocessor designs, to provide design teams with the ability to dynamically explore a wide range of design choices. The Hawk language is Haskell plus the Hawk library. The original link at http://www.cse.ogi.edu/PacSoft/projects/Hawk/ seems to be no longer live.<br />
<br />
;[http://wiki.cs.pdx.edu/bartforge/chortl.html Chortl]<br />
:Chortl is an extremely simple Haskell EDSL for hardware description. Chortl supports multiple backends. The current backend generates Verilog, and is intended for FPGA designs; near-future intent is to implement backends to generate C, and to provide graphical circuit diagrams. Chortl features include simplicity, a notation reasonably comprehensible by HW folks, the ability to define custom "components" such as adder trees in full Haskell, simplicity, and simplicity. Chortl is in a very early stage of development, so feedback and help are greatly appreciated.<br />
<br />
;[http://www.cs.chalmers.se/~koen/Lava/ Lava], [http://raintown.org/lava/ Lava for Xilinx]<br />
:Lava is a hardware description language based upon Haskell.<br />
<br />
{{LibrariesPage}}</div>AlfonsoAcostahttps://wiki.haskell.org/index.php?title=Template_Haskell&diff=23182Template Haskell2008-09-30T11:51:29Z<p>AlfonsoAcosta: </p>
<hr />
<div>'''[http://www.haskell.org/th/ Template Haskell]''' is a [[GHC]] extension to Haskell that adds compile-time metaprogramming facilities. The original design can be found here: http://research.microsoft.com/~simonpj/papers/meta-haskell/ . It is [http://haskell.cs.yale.edu/ghc/docs/6.2/html/users_guide/template-haskell.html included] in GHC version 6. If you start working with Template Haskell you'll probably want to join the [http://haskell.org/mailman/listinfo/template-haskell Template Haskell mailing list].<br />
<br />
This page hopes to be a more central and organized repository of TH related things. However, at this point most things should probably go to/through the mailing list first.<br />
<br />
=What is Template Haskell?=<br />
Template Haskell is an extension to Haskell 98 that allows you to do type-safe compile-time meta-programming, with Haskell both as the manipulating language and the language being manipulated. <br />
<br />
Intuitively Template Haskell provides new language features that allow us to convert back and forth between concrete syntax, i.e. what you would type when you write normal Haskell code, and abstract syntax trees. These abstract syntax trees are represented using Haskell datatypes and, at compile time, they can be manipulated by Haskell code. This allows you to reify (convert from concrete syntax to an abstract syntax tree) some code, transform it and splice it back in (convert back again), or even to produce completely new code and splice that in, while the compiler is compiling your module. <br />
<br />
There is a [http://haskell.org/mailman/listinfo/template-haskell mailing list specifically for Template Haskell]. It's worth joining if you start to use TH.<br />
<br />
= Template Haskell specification =<br />
<br />
Template Haskell is only documented rather informally at the moment. Here are the main resources:<br />
<br />
* [http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html The user manual section]<br />
* [http://research.microsoft.com/~simonpj/papers/meta-haskell/ The original Template Haskell paper]<br />
* [http://research.microsoft.com/~simonpj/tmp/notes2.ps Notes on Template Haskell version 2], which describes changes since the original paper.<br />
* [http://www.haskell.org/ghc/docs/latest/html/libraries/template-haskell/Language-Haskell-TH.html The Template Haskell API]<br />
<br />
= Template Haskell tutorials and papers =<br />
<br />
* Bulat's tutorials:<br />
** http://www.haskell.org/bz/thdoc.htm<br />
** http://www.haskell.org/bz/th3.htm<br />
: One reader said "These docs are *brilliant* ! Exactly what I need to get an understanding of TH."<br />
<br />
* Mark Snyder's Template Haskell chapter on the Software Generation and Configuration Report<br />
** http://nix.cs.uu.nl/dist/courses/sgc-report-unstable-latest/manual/chunk-chapter/templatehaskell.html<br />
<br />
* jethr0's Template Haskell tutorial: ** http://www.haskell.org/tmrwiki/TemplateHaskell<br />
<br />
* Papers about Template Haskell<br />
** Template metaprogramming for Haskell, by Tim Sheard and Simon Peyton Jones, May 2002. [http://haskell.org/th/papers/meta-haskell.ps [A4 ps]] [http://haskell.org/th/papers/meta-haskell.bib [bib]]<br />
** Template Haskell: A Report From The Field, by Ian Lynagh, May 2003.[http://haskell.org/th/papers/Template_Haskell-A_Report_From_The_Field.ps [A4 ps]] [http://haskell.org/th/papers/Template_Haskell-A_Report_From_The_Field.bib [bib]]<br />
** Unrolling and Simplifying Expressions with Template Haskell, by Ian Lynagh, May 2003.[http://haskell.org/th/papers/Unrolling_and_Simplifying_Expressions_with_Template_Haskell.ps [A4 ps]][http://haskell.org/th/papers/Unrolling_and_Simplifying_Expressions_with_Template_Haskell.bib [bib]]<br />
** Automatic skeletons in Template Haskell, by Kevin Hammond, Jost Berthold and Rita Loogen, June 2003. [http://haskell.org/th/papers/hlpp.ps [A4 ps]][http://haskell.org/th/papers/hlpp.bib [bib]]<br />
** Optimising Embedded DSLs using Template Haskell, by Sean Seefried, Manuel Chakravarty, Gabriele Keller, March 2004. [http://haskell.org/th/papers/th-pan.ps [A4 ps]] [http://haskell.org/th/papers/th-pan.bib [bib]]<br />
** Typing Template Haskell: Soft Types, by Ian Lynagh, August 2004.[http://haskell.org/th/papers/Typing_Template_Haskell__Soft_Types.ps [A4 ps]][http://haskell.org/th/papers/Typing_Template_Haskell__Soft_Types.bib [bib]]<br />
<br />
<br />
<br />
= Other useful resources =<br />
<br />
* [http://www.haskell.org/th/ The old Template Haskell web page]. Would someone feel like moving this material into the HaskellWiki?<br />
* Old and probably not too useful for most but maybe... http://www.cse.unsw.edu.au/~chak/haskell/ghc/comm/exts/th.html<br />
*[http://web.comlab.ox.ac.uk/oucl/work/ian.lynagh/Fraskell/ Fraskell documentation] & explanation of how Template Haskell is used to vastly speed it up.<br />
Feel free to update our Wikipedia entry<br />
http://en.wikipedia.org/wiki/Template_Haskell<br />
<br />
= Projects =<br />
<br />
What are you doing/planning to do/have done with Template Haskell?<br />
<br />
* The [http://www.ict.kth.se/org/ict/ecs/sam/projects/forsyde/www ForSyDe methodology] is currently implemented as a Haskell-based DSL which makes extensive use of Template Haskell.<br />
<br />
* I have written a primitive (untyped) binding to the Objective-C runtime system on Mac OS X. It needs just TH, no "stub files" are created, no seperate utilities are required. Initial snapshot is at http://www.kfunigraz.ac.at/imawww/thaller/wolfgang/HOC020103.tar.bz2 -- WolfgangThaller<br />
<br />
* I am writing Template Greencard - a reimplementation of GreenCard using TH. Many bits work out really nicely. A few bits didn't work so nicely - once I get some time to think, I'll try to persuade the TH folk to make some changes to fix some of these. -- AlastairReid<br />
<br />
* I'm writing Hacanon - a framework for automatic generation of C++ bindings. Read "automated Template Greencard for C++" (-: Darcs repo: http://www.ScannedInAvian.org/repos/hacanon - You'll need gccxml (http://www.gccxml.org/) to compile the exmples. - 27 Dec Lemmih.<br />
<br />
* Following other FFI tools developers, I see some future for Template HSFFIG, especially when it comes to autogenerate peek and poke methods for structures defined in C; may be useful for implementation of certain network protocols such as X11 where layout of messages is provided as C structure/union declaration. - 16 Dec 2005 DimitryGolubovsky<br />
<br />
* I am using Template Haskell as a mechanism to get parsed, typechecked code into an Ajax based Haskell Equational Reasoning tool [[Haskell Equational Reasoning Assistant]], as well as simplify the specification of equational relationships between pieces of code. There was a quicktime movie of the tool being used on http://www.gill-warbington.com/home/andy/share/hera1.html - AndyGill <br />
<br />
* I am working on functional metaprogramming techniques to enhance programming reliability and productivity, by reusing much of the existing compiler technology. Template Haskell is especially interesting for me because it permits to check size information of structures by the compiler, provided this information is available at compile time. This approach is especially appropriate for hardware designs, where the structures are fixed before the circuit starts operating. See our metaprogramming web page at http://www.infosun.fmi.uni-passau.de/cl/metaprog/ -- ChristophHerrmann(http://www.cs.st-and.ac.uk/~ch)<br />
<br />
* I am using Template Haskell to do type safe database access. I initially [http://www.nabble.com/Using-Template-Haskell-to-make-type-safe-database-access-td17027286.html proposed this on haskell-cafe]. I connect to the database at compile-time and let the database do SQL parsing and type inference. The result from parsing and type inference is used to build a type safe database query which can executed at run-time. [[MetaHDBC | You can find the project page here]] -- [mailto:mads_lindstroem@yahoo.dk Mads Lindstrøm]<br />
<br />
= Utilities =<br />
<br />
Helper functions, debugging functions, or more involved code e.g. a monadic fold algebra for TH.Syntax.<br />
<br />
* http://www.haskell.org/pipermail/template-haskell/2003-September/000176.html<br />
<br />
= Known Bugs =<br />
<br />
Take a look at the [http://cvs.haskell.org/trac/ghc/query?status=new&status=assigned&status=reopened&component=Template+Haskell&order=priority open bugs against Template Haskell] on the GHC bug tracker.<br />
<br />
The bug you're most likely to run into is [http://cvs.haskell.org/trac/ghc/ticket/651 Template Haskell does not work with profiling].<br />
<br />
= Wish list =<br />
<br />
Things that Ian Lynagh (Igloo) mentioned in his paper ''Template Haskell: A Report From The Field'' in May 2003 (available [http://www.haskell.org/th/papers.html here]), by section:<br />
<br />
* Section 2 (curses)<br />
** The ability to splice names (into "foreign import" declarations, in particular)<br />
** The ability to add things to the export list from a splice(?)<br />
** The ability to use things defined at the toplevel of a module from splices in that same module (would require multi-stage compilation, as opposed to the current approach of expanding splices during typechecking)<br />
<br />
* Section 3 (deriving instances of classes)<br />
** <strike>First-class reification</strike> (the <hask>reify</hask> function)<br />
** A way to discover whether a data constructor was defined infix or prefix (which is necessary to derive instances for <hask>Read</hask> and <hask>Show</hask> as outlined in [http://www.haskell.org/onlinereport/derived.html The Haskell 98 Report: Specification of Derived Instances]) (if there is a way, [http://www-users.cs.york.ac.uk/~ndm/derive/ Derive] seems ignorant of it)<br />
** Type/context splicing (in <hask>instance</hask> headers in particular)<br />
<br />
* Section 4 (printf)<br />
** He says something to the effect that a pattern-matching form of the quotation brackets would be cool if it was expressive enough to be useful, but that this would be hard. (Don't expect this anytime soon.)<br />
<br />
* Section 5 (fraskell)<br />
** Type information for quoted code (so that simplification can be done safely even with overloaded operations, like, oh, <hask>(+)</hask>)<br />
<br />
* Section 6 (pan)<br />
** Type info again, and strictnes info too (this one seems a bit pie-in-the-sky...)<br />
<br />
(Please leave the implemented ones here, but crossed off.)<br />
<br />
Any other features that may be nice, and TH projects you'd want to see.<br />
<br />
* A TH tutorial (mainly a distillation and update of ''Template Meta-programming in Haskell'' at this point)<br />
* Write Haddock documentation for the Template Haskell library.<br />
* Make `reify` on a class return a list of the instances of that class (http://www.haskell.org/pipermail/template-haskell/2005-December/000503.html). (See also [http://hackage.haskell.org/trac/ghc/ticket/1577 GHC ticket #1577].)<br />
* A set of simple examples on this wiki page<br />
* A TH T-shirt with new logo to wear at conferences<br />
* (Long-term) Unify Language.Haskell.Syntax with Language.Haskell.TH.Syntax so there's just one way to do things<br />
<br />
---------------<br />
<br />
= Tips and Tricks =<br />
<br />
== What to do when you can't splice that there ==<br />
<br />
When you try to splice something into the middle of a template and find that you just can't, instead of getting frustrated about it, why not use the template to see what it would look like in longhand? <br />
<br />
First, an excerpt from a module of my own. I, by the way, am SamB.<br />
<haskell><br />
{-# OPTIONS_GHC -fglasgow-exts -fth #-}<br />
<br />
module MMixMemory where<br />
<br />
import Data.Int<br />
import Data.Word<br />
<br />
class (Integral int, Integral word)<br />
=> SignConversion int word | int -> word, word -> int where<br />
<br />
toSigned :: word -> int<br />
toSigned = fromIntegral<br />
toUnsigned :: int -> word<br />
toUnsigned = fromIntegral<br />
<br />
</haskell><br />
<br />
Say I want to find out what I need to do to splice in the types for an instance declaration for the SignConversion class, so that I can declare instances for Int8 with Word8 through Int64 with Word64. So, I start up good-ol' GHCi and do the following:<br />
<br />
<haskell><br />
$ ghci -fth -fglasgow-exts<br />
Prelude> :l MMixMemory<br />
*MMixMemory> :m +Language.Haskell.TH.Syntax<br />
*MMixMemory Language.Haskell.TH.Syntax> runQ [d| instance SignConversion Int Word where |] >>= print<br />
[InstanceD [] (AppT (AppT (ConT MMixMemory.SignConversion) (ConT GHC.Base.Int)) (ConT GHC.Word.Word)) []]<br />
</haskell><br />
<br />
== Why does <tt>runQ</tt> crash if I try to reify something? ==<br />
<br />
This program will fail with an error message when you run it:<br />
<haskell><br />
main = do info <- runQ (reify (mkName "Bool"))<br />
putStrLn (pprint info)<br />
</haskell><br />
Reason: <tt>reify</tt> consults the type environment, and that is not available at run-time. The type of <tt>reify</tt> is <br />
<haskell><br />
reify :: Quasi m => Q a -> m a<br />
</haskell><br />
The IO monad is a poor-man's instance of <tt>Quasi</tt>; it can allocate unique names and gather error messages, but it can't do <tt>reify</tt>. This error should really be caught statically.<br />
<br />
Here's an [http://www.haskell.org/pipermail/glasgow-haskell-users/2006-August/010844.html email thread with more details].<br />
<br />
-----------------<br />
= Examples =<br />
== Select from a tuple ==<br />
<br />
An example to select an element from a tuple of arbitrary size. Taken from [http://www.haskell.org/th/papers/meta-haskell.ps this paper].<br />
<br />
Use like so:<br />
<br />
> $(sel 2 3) ('a','b','c')<br />
'b'<br />
> $(sel 3 4) ('a','b','c','d')<br />
'c'<br />
<br />
<br />
<haskell><br />
sel :: Int -> Int -> ExpQ<br />
sel i n = [| \x -> $(caseE [| x |] [alt]) |]<br />
where alt :: MatchQ<br />
alt = match pat (normalB rhs) []<br />
<br />
pat :: Pat<br />
pat = tupP (map varP as)<br />
<br />
rhs :: ExpQ<br />
rhs = varE(as !! (i -1)) -- !! is 0 based<br />
<br />
as :: [String]<br />
as = ["a" ++ show i | i <- [1..n] ]<br />
</haskell><br />
<br />
Alternately:<br />
<br />
<haskell><br />
sel' i n = lamE [pat] rhs<br />
where pat = tupP (map varP as)<br />
rhs = varE (as !! (i - 1))<br />
as = [ "a" ++ show j | j <- [1..n] ]<br />
</haskell><br />
<br />
== Convert the first n elements of a list to a tuple ==<br />
<br />
This example creates a tuple by extracting elemnts from a list. Taken from<br />
[http://www.xoltar.org/2003/aug/13/templateHaskellTupleSample.html www.xoltar.org]<br />
<br />
Use like so:<br />
<br />
> $(tuple 3) [1,2,3,4,5]<br />
(1,2,3)<br />
> $(tuple 2) [1,2]<br />
(1,2)<br />
<br />
<haskell><br />
tuple :: Int -> ExpQ<br />
tuple n = [|\list -> $(tupE (exprs [|list|])) |]<br />
where<br />
exprs list = [infixE (Just (list))<br />
(varE "!!")<br />
(Just (litE $ integerL (toInteger num)))<br />
| num <- [0..(n - 1)]]<br />
</haskell><br />
<br />
== Printf ==<br />
This example taken from: http://haskell.cs.yale.edu/ghc/docs/6.0/html/users_guide/template-haskell.html<br />
<br />
Build it using a command similar to one of the following (depending on your environment):<br />
<br />
ghc/compiler/stage3/ghc-inplace --make -fglasgow-exts -package haskell-src main.hs -o main.exe<br />
ghc --make -fth Main.hs -o printfTest<br />
<br />
Main.hs:<br />
<br />
<haskell><br />
module Main where<br />
<br />
-- Import our template "pr"<br />
import Printf ( pr )<br />
<br />
-- The splice operator $ takes the Haskell source code<br />
-- generated at compile time by "pr" and splices it into<br />
-- the argument of "putStrLn".<br />
main = putStrLn ( $(pr "Hello") )<br />
</haskell><br />
<br />
Printf.hs:<br />
<br />
<haskell><br />
module Printf where<br />
<br />
-- Skeletal printf from the paper.<br />
-- It needs to be in a separate module to the one where<br />
-- you intend to use it.<br />
<br />
-- Import some Template Haskell syntax<br />
import Language.Haskell.TH.Syntax<br />
<br />
-- Describe a format string<br />
data Format = D | S | L String<br />
<br />
-- Parse a format string. This is left largely to you<br />
-- as we are here interested in building our first ever<br />
-- Template Haskell program and not in building printf.<br />
parse :: String -> [Format]<br />
parse s = [ L s ]<br />
<br />
-- Generate Haskell source code from a parsed representation<br />
-- of the format string. This code will be spliced into<br />
-- the module which calls "pr", at compile time.<br />
gen :: [Format] -> ExpQ<br />
gen [D] = [| \n -> show n |]<br />
gen [S] = [| \s -> s |]<br />
gen [L s] = stringE s<br />
<br />
-- Here we generate the Haskell code for the splice<br />
-- from an input format string.<br />
pr :: String -> ExpQ<br />
pr s = gen (parse s)<br />
</haskell><br />
<br />
== Handling Options with Templates ==<br />
A common idiom for treating a set of options, e.g. from GetOpt, is to define a datatype with all the flags and using a list over this datatype:<br />
<br />
<haskell><br />
data Options = B1 | B2 | V Integer<br />
<br />
options = [B1, V 3]<br />
</haskell><br />
<br />
While it's simple testing if a Boolean flag is set (simply use "elem"), it's harder to check if an option with an argument is set. It's even more tedious writing helper-functions to obtain the value from such an option since you have to explicitely "un-V" each. Here, Template Haskell can be (ab)used to reduce this a bit. The following example provides the module "OptionsTH" which can be reused regardless of the constructors in "Options". Let's start with showing how we'd like to be able to program. Notice that the resulting lists need some more treatment e.g. through "foldl".<br />
<br />
Options.hs:<br />
<br />
<haskell><br />
module Main where<br />
<br />
import OptionsTH<br />
import Language.Haskell.TH.Syntax<br />
<br />
data Options = B1 | B2 | V Int | S String deriving (Eq, Read, Show)<br />
<br />
options = [B1, V 3]<br />
<br />
main = do<br />
print foo -- test if B1 set: [True,False]<br />
print bar -- test if V present, w/o value: [False,True]<br />
print baz -- get value of V if available: [Nothing,Just 3]<br />
<br />
foo :: [Bool]<br />
-- Query constructor B1 which takes no arguments<br />
foo = map $(getopt (THNoArg (mkArg "B1" 0))) options<br />
<br />
bar :: [Bool]<br />
-- V is a unary constructor. Let mkArg generate the required<br />
-- wildcard-pattern "V _".<br />
bar = map $(getopt (THNoArg (mkArg "V" 1))) options<br />
<br />
-- Can't use a wildcard here!<br />
baz :: [(Maybe Int)]<br />
baz = map $(getopt (THArg (conP "V" [varP "x"]))) options<br />
</haskell><br />
<br />
OptionsTH.hs<br />
<br />
<haskell><br />
module OptionsTH where<br />
<br />
import Language.Haskell.TH.Syntax<br />
<br />
-- datatype for querying options:<br />
-- NoArg: Not interested in value (also applies to Boolean flags)<br />
-- Arg: Grep value of unary(!) constructor<br />
data Args = THNoArg Pat | THArg Pat<br />
<br />
getopt :: Args -> ExpQ<br />
getopt (THNoArg pat) = lamE [varP "y"] (caseE (varE "y") [cons0, cons1])<br />
where<br />
cons0 = match pat (normalB [| True |]) []<br />
cons1 = match wildP (normalB [| False |]) []<br />
<br />
-- bind "var" for later use!<br />
getopt (THArg pat@(ConP _ [VarP var])) = lamE [varP "y"] (caseE (varE "y") [cons0, cons1])<br />
where<br />
cons0 = match pat (normalB (appE [|Just|] (varE var))) []<br />
cons1 = match wildP (normalB [|Nothing|]) []<br />
<br />
mkArg :: String -> Int -> Pat<br />
mkArg k c = conP k (replicate c wildP)<br />
</haskell><br />
<br />
While the example might look contrived for the Boolean options which could have been tested much easier, it shows how both types of arguments can be treated in a similar way.<br />
<br />
=== Limitations ===<br />
<tt>getopt (THArg pat)</tt> is only able to treat unary constructors. See the pattern-binding: It matches exactly a single VarP.<br />
<br />
=== Improvements ===<br />
The following reduces things even a bit more, though I still don't know if I like it. It only works since <tt>c</tt> is either 0 or 1.<br />
<br />
<haskell><br />
mkArg k c = conP k (replicate c (varP "x"))<br />
<br />
baz = map $(getopt (THArg (mkArg "V" 1)))<br />
</haskell><br />
-- VolkerStolz<br />
<br />
== Generic constructor for records ==<br />
<br />
I have a large number of record types like this, of different length:<br />
<br />
<haskell><br />
data PGD = PGD {<br />
pgdXUnitBase :: !Word8,<br />
pgdYUnitBase :: !Word8,<br />
pgdXLUnitsperUnitBase :: !Word16<br />
}<br />
</haskell><br />
<br />
Currently I use GHC's Binary module to read them from files; it can handle<br />
types like <tt>(Word8, (Word8, Word16))</tt>, but there was no easy way to generate<br />
the correct amount of "uncurry" calls for automatically grabbing each element.<br />
<br />
With Template Haskell, the instance declarations are now written as such:<br />
<br />
<haskell><br />
instance Binary PGD where<br />
get bh = do a <- get bh ; return $ $(constrRecord PGD) a<br />
</haskell><br />
<br />
Here the trick lies in constrRecord, which is defined as:<br />
<br />
<haskell><br />
constrRecord x = reify exp where<br />
reify = \(Just r) -> appE r $ conE $ last args<br />
exp = foldl (dot) uncur $ replicate terms uncur<br />
terms = ((length args) `div` 2) - 2<br />
dot x y = (Just $ infixE x (varE ".") y)<br />
uncur = (Just [|uncurry|])<br />
args = words . show $ typeOf x<br />
</haskell><br />
<br />
-- AutrijusTang<br />
<br />
==[[Template haskell/Instance deriving example|Instance deriving example]]==<br />
An example using a 'deriving function' to generate a method instance <br />
per constructor of a type. The deriving function provides the body of the<br />
method.<br />
<br />
Note that this example assumes that the functions of the class take a parameter that is the same type as instance is parameterized with. <br />
<br />
The message [http://www.haskell.org/pipermail/template-haskell/2006-August/000581.html email message] contains the full source ([http://www.iist.unu.edu/~vs/haskell/TH_render.hs extracted file]).<br />
<br />
[[Category:Language extensions]]</div>AlfonsoAcostahttps://wiki.haskell.org/index.php?title=Cabal/How_to_install_a_Cabal_package&diff=22541Cabal/How to install a Cabal package2008-08-20T15:32:21Z<p>AlfonsoAcosta: People might not have found the package as of yet</p>
<hr />
<div>[[Category:How to]]<br />
There's an interesting Haskell package on [http://hackage.haskell.org/packages/archive/pkg-list.html HackageDB] you'd like to try. How do you install it on your system?<br />
<br />
# Check whether the package came with your Haskell implementation.<br />
# If your [[:Category:OS|operating system]] has a packaging system (e.g. most Linux or BSD distributions), check whether it is already packaged there.<br />
# Otherwise, you'll have to build and install the package. A program to automate this process, called [http://hackage.haskell.org/trac/hackage/wiki/CabalInstall ''cabal-install''], is under development. In the meantime, you'll have to do it manually:<br />
## First, ensure that all the packages it depends on are installed (by following these instructions recursively).<br />
## Unpack the tar file (yes, this assumes a Unix system; sorry about that ([[how to unpack a tar file in windows]])):<br />
##:<code>tar xzf PACKAGE-VERSION.tar.gz</code><br />
## Move into the directory this creates:<br />
##:<code>cd PACKAGE-VERSION</code><br />
## This directory should contain a file <code>Setup.hs</code> or <code>Setup.lhs</code>. In order to install a package globally, perform the following commands for the appropriate file (see the [http://www.haskell.org/ghc/docs/latest/html/Cabal/builders.html Cabal documentation] for more details):<br />
##:<code>runghc Setup.hs configure</code><br />
##:<code>runghc Setup.hs build</code><br />
##:<code>runghc Setup.hs install</code><br />
#: If instead of installing globally, you just wish to install a package for your normal user account, you could instead use the following <code>configure</code> command, which would register the install in the user-specific database and install binaries and libraries in $HOME/bin, $HOME/lib, and so forth:<br />
#::<code>runghc Setup.hs configure --user --prefix=$HOME</code><br />
#: You can get more information about any of these commands by adding <code>--help</code> after the command. For example, to see all the options available for the <code>configure</code> step, you could use the following command:<br />
#::<code>runghc Setup.hs configure --help</code><br />
#: Lastly, if you encounter unsatisfied dependencies when you run the configure step, that is when you recurse and first install the missing package.<br />
<br />
'''Note''': If you have more than one Haskell compiler on your system,<br />
use the <code>--with-compiler</code> option for the<br />
<code>configure</code> step. That will ensure that Cabal<br />
uses the correct compiler during the entire installation process.<br />
For example:<br />
<br />
<code><br />
runghc Setup.hs configure --with-compiler=ghc-6.8.2<br />
runghc Setup.hs build<br />
runghc Setup.hs install<br />
</code><br />
<br />
'''Note 2''': If you have missing dependencies when running Setup.hs configure, you will get the following message:<br />
<br />
<code><br />
...<br />
Setup.hs: At least the following dependencies are missing:<br />
zlib -any, plugins >=1.0, oeis -any<br />
</code><br />
<br />
You may want to use [http://hackage.haskell.org/trac/hackage/wiki/CabalInstall cabal-install] OR on debian systems (debian, ubuntu, etc.), alternatively you can try the following "hack" - please run this ONLY IF YOU KNOW WHAT YOU ARE DOING (understand the command, especially the "-y --force-yes" flags):<br />
<code><br />
runghc Setup.hs configure 2>&1 | tail -n +3 | awk '{print $1}' | xargs -ijoe sudo apt-get -y --force-yes install libghc6-joe-dev<br />
</code></div>AlfonsoAcosta