template-haskell

What are the differences between inline-c and language-c-inline?

柔情痞子 提交于 2019-12-04 02:24:25
I've been briefly looking into quasi-quotation libraries for Haskell. These libraries allow Haskell to integrate with other languages. For integrating with C, there appears to be two packages with similar functionality: inline-c language-c-inline (which uses language-c-quote ) As I'm looking to build a quasi-quotation library of my own, I'm interested in design choices, API differences, performance etc. The only difference I'm aware of is that language-c-quote supports C and Objective-C, whereas inline-c supports C. How would you distinguish these packages? What are the fundamental differences

Performing type equality in template haskell

梦想的初衷 提交于 2019-12-03 21:03:41
I have a function in Template Haskell that extracts the type information for sum of record constructors as below: listFields :: Name -> Q ([[(String,Name,Type)]]) listFields name = do TyConI (DataD _ _ _ cons _) <- reify name let showClause (RecC conName fields) = (map (\(x,_,t) -> (nameBase $ x,x,t)) fields) return $ map showClause cons Given the type in there for a field, how do you compare equality of that type with a particular type like GHC.Base.String or Data.Text.Internal.Text ? I see TypeQ in TH documentation. It builds type expression. However, I can't find any documentation on how to

TemplateHaskell and IO

不羁的心 提交于 2019-12-03 15:06:56
Is there any proper way to make TH's functions safe if they use side effects? Say, I want to have a function that calls git in compile time and generates a version string: {-# LANGUAGE TemplateHaskell #-} module Qq where import System.Process import Language.Haskell.TH version = $( [| (readProcess "git" ["rev-parse", "HEAD"] "") |] ) the type of version is IO String. But version is completely free of side effects in runtime, it has side effects only in compile time. Is there any way to make it pure in runtime without using unsafePerformIO ? First: normally, the runtime type of the generated

Get a Haskell record's field names as a list of strings?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 10:31:10
Say I have the following: data Rec = Rec { alpha :: Int, beta :: Double, phi :: Float } sample = Rec 1 2.3 4.5 I understand Template Haskell & the reify function can get me the record's field names. That is: print $(f sample) --> ["alpha", "beta", "phi"] There is also a claim that this can be done without Template Haskell. Can someone provide an example implementation for this can be accomplished? It can be done with a Data (most GHC versions) or Generic (7.2.x and up) instance, which GHC can derive for you. Here's an example of how to dump record fields with the Data typeclass: {-# LANGUAGE

Force pre-computation of a constant

喜你入骨 提交于 2019-12-03 10:03:51
问题 I have a constant declaration in Haskell -- can I force this to be evaluated ahead of time? I'm seeing some code that looks roughly like, myList = [(a, b), (c, d)] ... map (f . fst) myList take time in the fst call when I profile it (it does have 168M calls). The binary representation of myList is quite small, and could be, for example, copied into global memory [if this were a C program]. I'm compiling with -O3 -optc-O3 of course. Thanks very much! Generating Lift instances for a custom type

Is there any Template Haskell tutorial for someone who doesn't know Lisp?

允我心安 提交于 2019-12-03 08:35:28
问题 I wanted to learn Template Haskell but all tutorials I find either assume that you learned lisp and know what lisp macros are, or that you know some cs theory jargon - things as splices, quasiquotations, etc... - or some theoretical results about macros. I can't code a single line of lisp (and, though I intend to do this some day, I don't have the time to learn it right now). Haskell is my very first functional language and I learned it to the point that I can regularly code in it, use monads

Is it possible to use a bracketing syntactic sugar for an applicative functor?

自作多情 提交于 2019-12-03 04:49:56
In McBride and Paterson's 'Applicative programming with effects' they introduce some lovely syntactic sugar for lifting a pure function: [| f x y z |] for f <$> x <*> y <*> z and I recall someone somewhere else using li f w x y z il or il f v w x y z li , and I thought/hoped that might be because it could be defined using some existing language feature and cunning definition of li and il . I can't find any reference to this beyond the paper, and assuming that [| and |] aren't likely to turn up in ghc any time soon, is it possible to implement li and il somehow? I can't think of a sensible type

Is there any Template Haskell tutorial for someone who doesn't know Lisp?

二次信任 提交于 2019-12-03 00:05:38
I wanted to learn Template Haskell but all tutorials I find either assume that you learned lisp and know what lisp macros are, or that you know some cs theory jargon - things as splices, quasiquotations, etc... - or some theoretical results about macros. I can't code a single line of lisp (and, though I intend to do this some day, I don't have the time to learn it right now). Haskell is my very first functional language and I learned it to the point that I can regularly code in it, use monads, applicative, understand the type system, etc... but I don't know much (also want to learn but I'm too

Force pre-computation of a constant

女生的网名这么多〃 提交于 2019-12-02 23:19:39
I have a constant declaration in Haskell -- can I force this to be evaluated ahead of time? I'm seeing some code that looks roughly like, myList = [(a, b), (c, d)] ... map (f . fst) myList take time in the fst call when I profile it (it does have 168M calls). The binary representation of myList is quite small, and could be, for example, copied into global memory [if this were a C program]. I'm compiling with -O3 -optc-O3 of course. Thanks very much! Generating Lift instances for a custom type Any expression given to the lift call in sclv's answer must be an instance of Lift. There's a library

Haskell, Gen instance of B when class A provides enough info for class B

故事扮演 提交于 2019-12-01 23:08:12
While writing a class for a Collection/Container type (btw point me towards existing types if i'm reinventing the wheel) to provide a general interface for adding and removing elements from any 'Collection' type. class (Eq (c a), Monoid (c a)) => Collection c a where emptyColl :: c a -> Bool splitColl :: c a -> (a, c a) toColl :: a -> c a size :: c a -> Int combineColl :: a -> c a -> c a (>|<) :: a -> c a -> c a a >|< c = combineColl a c I noticed instances of Collection could also be instances of Foldable. Using splitColl and emptyColl. So you don't need to write a Foldable as well, if you