haskell

Parsing haskell preserving comments / formatting

只愿长相守 提交于 2020-01-01 12:02:58
问题 I want to do some source code transformation (automatic import list cleanup) and I'd like to preserve comments and formatting. I heard some stuff on and off about parsers that do this, I think for the ghc parser. It looks like I might be able to do this with hs-src-exts Language.Haskell.Exts.Annotate and its SrcSpans by pulling things out of the file. I think the SrcsSpanInfo only covers the parsed parts, but I could theoretically figure out the comments by looking at what's in between. But

What are the difference in usage between Either and Except in Haskell?

▼魔方 西西 提交于 2020-01-01 11:58:08
问题 I have a class that can be created from several arguments in Haskell which requires some complex validation of those arguments. Currently I have something like makeAThingExcept :: String -> String -> ... String -> Except ThingError AThing makeAThingExcept s1 s2 ... = do unless (s1CheckPasses s1) (throwError (BadS1 s1)) ... data ThingError = BadS1 String ... instance Show ThingError where show (BadS1 s) = "Bad S1: " ++ s makeAThing :: String -> String -> ... String -> AThing makeAThing s1 s2 .

scripts to automatically manage imports or refactor modules

人走茶凉 提交于 2020-01-01 10:49:08
问题 Does anyone know of tools to manage Haskell imports? Sometimes, I want to refactor things by moving them to another file, and a script to generate the minimal set of imports [for the new file] would be nice. 回答1: GHC can tell you which imports are required; simply add the -fwarn-unused-imports flag. 回答2: There's a program called fix-imports that can generate a minimal set of imports for you. It only works for qualified imports, though. 来源: https://stackoverflow.com/questions/6756477/scripts

Haskell calculate time of function performing

霸气de小男生 提交于 2020-01-01 10:19:11
问题 i tried to code to calculate time that a function costs list <- buildlist 10000 10000 starttime <- getClockTime let sortedlist = quicksort list endtime <- getClockTime let difftime = diffClockTimes endtime starttime function buildlist: buildlist :: Int -> Int -> IO [Int] buildlist n m = do seed <- getStdGen let l = randomRs (0, m) seed let list = take n l return list function quicksort: quicksort [] = [] quicksort (x:xs) = let head = [a|a<-xs,a<=x] tail = [a|a<-xs,a>x] in quicksort head ++ [x

How can I derive a Data instance for a GADT in Haskell?

怎甘沉沦 提交于 2020-01-01 10:16:34
问题 I have a GADT which is only ever used with two different parameters, ForwardPossible and (): -- | Used when a forward definition is possible. data ForwardPossible = ForwardPossible deriving (Eq, Ord, Typeable, Data, Show) -- | GADT which accepts forward definitions if parameter is ForwardPossible. data OrForward t forward where OFKnown :: t -> OrForward t forward OFForward :: NamespaceID -> SrcSpan -> BS.ByteString -> OrForward t ForwardPossible deriving instance Eq t => Eq (OrForward t

How can I derive a Data instance for a GADT in Haskell?

℡╲_俬逩灬. 提交于 2020-01-01 10:16:27
问题 I have a GADT which is only ever used with two different parameters, ForwardPossible and (): -- | Used when a forward definition is possible. data ForwardPossible = ForwardPossible deriving (Eq, Ord, Typeable, Data, Show) -- | GADT which accepts forward definitions if parameter is ForwardPossible. data OrForward t forward where OFKnown :: t -> OrForward t forward OFForward :: NamespaceID -> SrcSpan -> BS.ByteString -> OrForward t ForwardPossible deriving instance Eq t => Eq (OrForward t

Reentrant caching of “referentially transparent” IO calls

这一生的挚爱 提交于 2020-01-01 09:47:12
问题 Assume we have an IO action such as lookupStuff :: InputType -> IO OutputType which could be something simple such as DNS lookup, or some web-service call against a time-invariant data. Let's assume that: The operation never throws any exception and/or never diverges If it wasn't for the IO monad, the function would be pure, i.e. the result is always the same for equal input parameters The action is reentrant, i.e. it can be called from multiple threads at the same time safely. The

Parse expression right-to-left

醉酒当歌 提交于 2020-01-01 09:36:09
问题 I'm building a parser for expression. Here's my grammar rule: expr ::= term (+ expr | - expr | null) term ::= expo (* term | / term | null) expo ::= factor (^ expo | null) factor ::= (expr) | int and corresponding code: expr :: Parser Int expr = do t <- term do _ <- symbol "+" e <- expr return (t + e) <|> do _ <- symbol "-" e <- expr return (t - e) <|> return t term :: Parser Int term = do ep <- expo do _ <- symbol "*" t <- term return (ep * t) <|> do _ <- symbol "/" t <- term return (ep `div

indexing list with Control.Lens requires Monoid constraint

若如初见. 提交于 2020-01-01 09:19:36
问题 The following code doesn't compile: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data MyType = MyType Int data Outer = Outer { _inners :: [ Inner ] } data Inner = Inner { _val :: MyType } $(makeLenses ''Outer) $(makeLenses ''Inner) i1 = Inner (MyType 1) i2 = Inner (MyType 2) o = Outer [i1, i2] x = o ^. inners . ix 0 . val giving this error Toy.hs:17:23: No instance for (Data.Monoid.Monoid MyType) arising from a use of `ix' Possible fix: add an instance declaration for (Data.Monoid

Conduit - Multiple output file within the pipeline

▼魔方 西西 提交于 2020-01-01 09:09:11
问题 I'm writing a programme where an input file is split into multiple files (Shamir's Secret Sharing Scheme). Here's the pipeline I'm imagining: source: use Conduit.Binary.sourceFile to read from the input conduit: Takes a ByteString, produces [ByteString] sink: Takes [ByteString] from the conduit, and write each ByteString (in [ByteString]) to their corresponding file. (say if our input [ByteString] is called bsl, then bsl !! 0 will be written to file 0, bsl !! 1 to file 1 and so on) I found a