template-haskell

How can I programatically produce this datatype from the other?

谁说胖子不能爱 提交于 2019-12-22 09:48:29
问题 I'd like to use DSum for something. To work with DSum , you need to have a 'tag' type which takes one type argument, e.g. data Tag a where AFirst :: Tag Int ASecond :: Tag String However, I'd like to use this internally in a library. I want the interface that I expose to users to take any old datatype, e.g. data SomeUserType1 = Foo Int | Bar String it's clearly quite mechanical to go from this to the Tag a type given above. So, is it possible to do this in code, with some sort of generic

How to get the declaration of a function using `reify`?

前提是你 提交于 2019-12-21 03:55:20
问题 Function reify allows me to look up information about a given name. For a function the returned value is VarI: data Info = ... | VarI Name Type (Maybe Dec) Fixity | ... Here I can examine the function's type, and I'd also like to examine its declaration. However, in the 3rd argument to VarI I always see Nothing . Is there a way to get the function's declaration? 回答1: From the template haskell docs on the VarI Info contructor: A "value" variable (as opposed to a type variable, see TyVarI ).

A workaround for the “Template Haskell + C” bug?

一笑奈何 提交于 2019-12-20 20:23:14
问题 I've got the following situation: Library X is a wrapper over some code in C. Library A depends on library X. Library B uses Template Haskell and depends on library A. GHC bug #9010 makes it impossible to install library B using GHC 7.6. When TH is processed, GHCi fires up and tries to load library X, which fails with a message like Loading package charsetdetect-ae-1.0 ... linking ... ghc: ~/.cabal/lib/x86_64-linux-ghc-7.6.3/charsetdetect-ae-1.0/ libHScharsetdetect-ae-1.0.a: unknown symbol `

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

天大地大妈咪最大 提交于 2019-12-20 03:20:38
问题 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

Function to output function name

孤人 提交于 2019-12-18 04:53:14
问题 Is it possible in Haskell to implement a function which returns its own function name? A possible type could be (a -> b) -> String . 回答1: You want a function that takes a function argument, and returns the definition site variable name that corresponds to the name of that function? This isn't possibly without meta-programming, which is usually a sign you're doing something wrong :). But assuming you're not, one way to achieve something in the right direction is via Template Haskell, which can

Generating a type annotation using DataKinds within a TH QuasiQuote

你离开我真会死。 提交于 2019-12-13 15:29:19
问题 In a haskell project using template haskell, I am trying to generate an expression that has a type annotation as a phantom type. A simple example would be a situation with DataKinds and KindSignatures like: {-# LANGUAGE DataKinds, KindSignatures #-} data Foo = A | B data GenMe (w :: Foo) = GenMe Int [| $(generate some code) :: GenMe $(genType someCompileTimeData) |] How can I write a function, like genType such that genType :: Foo -> Q Type lifting just lifts the variable holding the compile

Lenses and TypeFamilies

孤者浪人 提交于 2019-12-12 15:07:20
问题 I've encountered a problem of using Control.Lens together with datatypes while using the -XTypeFamilies GHC pragma. {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} import Control.Lens (makeLenses) class SomeClass t where data SomeData t :: * -> * data MyData = MyData Int instance SomeClass MyData where data SomeData MyData a = SomeData {_a :: a, _b :: a} makeLenses ''SomeData The error message is: reifyDatatype: Use a value constructor to reify a data family instance . Is there

How to circumvent GHC Stage Restriction?

断了今生、忘了曾经 提交于 2019-12-12 10:33:27
问题 I am writing a code generator whose output depends on datatype fields description which is stored in their class instances. However, I cannot find how to run a function with a TH-generated argument. {-# LANGUAGE TemplateHaskell, ScopedTypeVariables #-} module Generator where import Language.Haskell.TH import Language.Haskell.TH.Syntax data Description = Description String [Description] deriving Show class HasDescription a where getDescription :: a -> Description instance HasDescription Int

How to replicate the behaviour of 'name in a TH splice

我们两清 提交于 2019-12-11 04:08:32
问题 Consider this Haskell file: {-# LANGUAGE TemplateHaskell #-} {-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} module Text (main) where import Test.Inspection import Data.Text as T import Data.Text.Encoding as E import Data.ByteString (ByteString) import Language.Haskell.TH toUpperString :: String -> String toUpperString = T.unpack . T.toUpper . T.pack toUpperBytestring :: ByteString -> String toUpperBytestring = T.unpack . T.toUpper . E.decodeUtf8 do Just n <- lookupValueName

Adding an Ord instance to 'singleton' package generated naturals

南笙酒味 提交于 2019-12-10 17:45:18
问题 I am using very simple type-level naturals generated with the singletons package. I am now trying to add an Ord instance to them. {-# LANGUAGE MultiParamTypeClasses, TemplateHaskell, KindSignatures, DataKinds, ScopedTypeVariables, GADTs, TypeFamilies, FlexibleInstances, TypeOperators, UndecidableInstances, InstanceSigs #-} module Functions where import Data.Singletons import Data.Singletons.TH import Data.Singletons.Prelude import Data.Promotion.Prelude singletons [d| data Nat = Z | S Nat