51,805 questions
Best practices
0
votes
2
replies
21
views
How can I simplify my Foldable instance for a rose tree?
I currently have this implementation:
data RoseTree a = RoseTree a [RoseTree a]
preorder :: Tree a -> [a]
preorder (Tree x ts) = x : concatMap preorder ts
instance Foldable Tree where
foldr :: (...
2
votes
1
answer
56
views
How to import a Haskell function in Python
I cannot fix this error. build.sh is trying to create a shared library but it fails:
~/Desktop/deepseek$ ./build.sh
Building Haskell shared library...
Loaded package environment from /home/success/....
-3
votes
1
answer
115
views
How to embed data equality information in Haskell's types?
I need help with some haskell syntax. I have a really long type signature and I need a quick way of making sure that the parts of it which are equal are represented in the signature itself.
Signature:
...
2
votes
1
answer
54
views
ghc gives error for something that's allowed in GHC.Internal.System.Posix.Internals
The following program gets the error:
"Illegal term-level use of the type constructor or class ‘IOError’"
even though it's right out of the source to ghc:
{-# LANGUAGE Trustworthy #-}
{-# ...
1
vote
2
answers
119
views
Replacing the element in the list with the specific index using `foldr` —— Exercise 1.19 of Essentials of Programming Language
Here is the question from Mitchell Wand's Essentials of Programming Language:
Exercise 1.19 [⋆ ⋆] (list-set lst n x) returns a list like lst, except that the n-th element, using zero-based indexing, ...
-1
votes
1
answer
120
views
Is there a way to write a linear monad transformer?
I need a function of type
linearJoin :: (ctx,Functor n,Functor m,Functor x) => (forall a. n (m a) -> x a) -> t n (t m a) -> t x a
A bind version would look like this
linearBind bindfun f k ...
2
votes
4
answers
201
views
Combining two associated type families
I'm looking into associated type families. As an own learning example, I try to model a small part of the x86 assembly language.
This compiles:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ...
Advice
0
votes
4
replies
126
views
Test Equality of Infinite Lists
In Haskell, how should one test the equality of two infinite lists?
With finite lists, one might try:
listEqual :: Eq a => [a] -> [a] -> Bool
listEqual l0 l1 = and $ zipWith (==) l0 l1
But ...
4
votes
2
answers
111
views
GHC can't avoid module cycle with class definitions
Problem: Can't avoid a module cycle with class definitions in Haskell.
Foo.hs contains:
module Foo where
import {-# SOURCE #-} Bar
class FooClass a where
...
myBar :: BarClass -> a -- use a ...
0
votes
1
answer
79
views
Haskell Foldr ADT from a List of Expressions into a single expression
So this is the data definition and my function.
Task 3
Define addAll, which adds a list of expressions together into a single expression without introducing any 'junk'.
You could use foldr since it ...
Best practices
5
votes
3
replies
150
views
Is there a way to tuck in Ord inside Applicative?
I had a nice idea of using applicative for nondeterministic financial modelling. Or maybe it is a simple case of sentization. So the basic example is to define newtype ValueRange.
newtype ValueRange a ...
5
votes
2
answers
152
views
Ambigous type variable in tagless final mini language
Fur fun and education I'm trying to write a mini compiler with the final tagless method as described by
Oleg Kiselyov in his paper Typed Tagless Final Interpreters.
My grammar has expressions and ...
5
votes
1
answer
140
views
What is the formal name for GHC automatically adapting less-constrained functions to more-constrained rank-2 arguments? [closed]
Consider this Haskell code that compiles successfully:
{-# LANGUAGE RankNTypes #-}
-- Applies a polymorphic function to different types, expecting 3 constraints
applyToMany :: (forall a. (Show a, Eq ...
4
votes
1
answer
115
views
Understanding usage of withFileBlocking with named pipes
The following program
assumes that /path/to/mypipe is a named pipe, e.g. created via mkfifo /path/to/mypipe, with no readers/writers waiting yet,
runs two threads, of which
the main thread keeps ...
0
votes
1
answer
154
views
How do I make the "return" function from the Monad class return a phantom parameter?
MRE:
module Temp where
data Some r a = Thing r a
instance Monad (Some r) where
return :: a -> Some r a
return a = Thing r a -- <- The is a phantom argument (somewhat like the s in ...
2
votes
2
answers
178
views
Merge every element of 2 lists of lists
Suppose, we have 2 lists of lists:
a = [[1,2], [3,4]]
b = [[5,6], [7,8]]
We want to merge every element of the first list with every element of the second list and get:
c = [[1,2,5,6], [1,2,7,8], [3,...
3
votes
1
answer
93
views
Is there a way to check for an instance of a class (with Haskell extensions)?
I am currently writing common theorems of intuitionistic logic in Haskell using the Curry-Howard isomorphism:
import Data.Void
type a :> b = a -> b -- implies
type a :+ b = Either a b -- or
data ...
2
votes
1
answer
94
views
On implementing map in terms of fold for a binary tree in Haskell
I've been making progress with Chris Allen's Haskell book and I am stumped in an exercise as the title suggests.
First of all, the binary tree is defined as such:
data BinTree a = Leaf | Node (...
5
votes
2
answers
134
views
Haskell ghci ocupying too much memory
I encountered a problem where my ram explodes and I really don't know why.
the code to read a csv is:
--module
splitComma _ [] = [[]]
splitComma False (',':t) = [[]]++splitComma False t
splitComma ...
4
votes
1
answer
120
views
How does the point-free expression ((*) .) . (*) work in Haskell?
I'm learning about Haskell and came across this concise, but weird, definition for a function that multiplies three numbers:
volume :: Float -> Float -> Float -> Float
volume = ((*) .) . (*)
...
3
votes
2
answers
104
views
Why doesn't readFile block on unix pipe in which no write has happened yet?
If in a terminal I enter
mkfifo /tmp/pipe
echo hello > /tmp/pipe
(which blocks) and in another I run the haskell program
main = readFile "/tmp/foobar" >>= putStr
then I see it ...
3
votes
1
answer
84
views
Polymorphic function in class and polymorphic instance: how to bind parameter types?
I want to recursively collect arguments of a function to a tuple with nesting (the general idea is taken from here).
I have a class with a function with polymorphic parameter (a, b). I want to make an ...
2
votes
1
answer
86
views
What does it mean that the arguments to <*> and their associated effects are known statically?
I'm reading the paper Selective Applicative Functors. So far I've read from page 16 out 29, and I think I've understood the gist of this abstraction, but I'm having some trouble with some basic ...
1
vote
1
answer
90
views
Understanding behavior of DBus with respect to killing the process that used DBus to register a name
Take this simple program in Haskell
{-# LANGUAGE OverloadedStrings #-}
import Control.Exception (finally)
import Control.Monad (forever)
import Xmobar (tenthSeconds)
import DBus.Client
startServer' :...
1
vote
1
answer
81
views
Under what circumstances can a write to a Haskell unsafeThaw-ed array/vector result in a segfault (via lost GC root)?
This question tries to collect the full picture if/when a stale object reference can happen from an old-gen (immutable) array referring a newer-gen object, from fragments of information.
Preface: was ...
1
vote
1
answer
65
views
Any memory safety concerns when using Haskell's vector unsafeFreeze / unsafeThaw on unpinned vector?
Apart from the known safety concerns documented for unsafeFreeze / unsafeThaw, is there any issue using these functions with non-pinned backing data (that is, as I understand everything except Vector....
3
votes
2
answers
107
views
Does GHC specialize data constructors, or how to design for such a usecase
I'm familiar with the SPECIALIZE pragma, or INLINE/INLINABLE that might enable automatic specialization at the use-site. But those are about specializing functions for type variables taking special ...
1
vote
2
answers
169
views
Execution or evalution: any rules?
Function fmap has two arguments: function and its argument(s). When I execute fmap for IO monad, it executes the second argument, but not the first:
main = fmap (\_ -> print "def") (print ...
5
votes
3
answers
126
views
Parse Conduit of Contiguous Either Values
Let's say I am receiving a streaming response from a web service. I get the response in the shape of a ConduitT () (Either a b) m (). Now, let's say I know from the API documentation that the response ...
3
votes
1
answer
111
views
Which test inputs cause my QuickCheck property to hang? (`QC.within` is insufficient to break the loop)
I have a testcase for a complicated function that for unclear reasons tends to hang on certain platforms. I would like to know which test inputs cause the hanging behaviour.
I have tried to use ...
0
votes
0
answers
94
views
Why are we passing --work-dir in the ghci dev command suggested in Yesod's scaffolding?
I started a new Yesod project with stack new blog yesodweb/postgres. The template generates an app/DevelMain.hs with a comment at the top that says that we can get faster dev reloads via stack ghci ...
0
votes
0
answers
70
views
How to configure brittany in Visual Studio Code Windows?
I'm trying to set up Haskell in VSCode Windows by following this Better Programming Guide
Everything worked fine except for the code formatting.
I tried installing brittany using stack install ...
0
votes
0
answers
70
views
Integer is fast, but Integral a is slow [duplicate]
Why is this slow
fibonacciSequence :: Integral a => [a]
fibonacciSequence = 0 : 1 :
zipWith (+) fibonacciSequence (tail fibonacciSequence)
-- in repl
take 10000 fibonacciSequence
but this ...
2
votes
2
answers
140
views
Adding a single variable to the function signature makes code significantly slower
This pertains to Emily's answer here: https://stackoverflow.com/a/13850560/2026752
ansMap :: M.Map Integer Int
ansMap = M.fromAscList [(i, collatz i) | i <- [1..1000000]]
where collatz 1 = 0
...
2
votes
1
answer
140
views
Haskell parameter function that itself has a type parameter
I'm trying to write a function that performs a binary operation on either floating-point numbers or integers. My attempt:
data MyNum = F Float | I Int
performBinaryOperation :: (Num a) => (a -> ...
0
votes
1
answer
80
views
Haskell JSON Web API Generalization
I am trying to write a Twitch chat bot that will receive messages. The messages will be in JSON and be an object of two other objects, metadata (which appears to be mostly standardized across all ...
0
votes
0
answers
48
views
What is the correct way to use the Eval monad in haskell? [duplicate]
I am learning parallel programming in haskell.
My example code:
module Quicksort where
import Control.Parallel.Strategies
import Control.Parallel
qsort :: [Int] -> [Int]
qsort [] = []
qsort lst@(...
3
votes
2
answers
114
views
How to use Haskell `do` notation with multiple monad constraints
I'm still quite new to Haskell and don't have a great grasp of monads intuitively. I'm trying to write the function below:
applyPandocFilters :: (MonadIO m, PandocMonad m) => Pandoc -> m Pandoc
...
1
vote
1
answer
73
views
Adding constraints to deriving declarations
I have this datatype
newtype Dimension a b = MkDimension b
Whenever it is properly formatted (e.g. satisfies the format constraint), I want to derive a set of classes. I do not want a Dimension which ...
0
votes
2
answers
196
views
How is Maybe a monad in Haskell?
A monad, I've been told, is a monoid of X in the category of endofunctors of X, where X is some category.
Maybe is supposedly then a monoid, which means that it is: an object of a category, some ...
2
votes
0
answers
81
views
Why large number of sparks are GC’d? [duplicate]
I have function (RSA encryption) where end result m depend on m1 and m2 where m1 and m2 can be computed in parallel. I tried to use par and pseq but the result is weak.
Total time 20.062s ( 18....
1
vote
1
answer
132
views
Why can't I print from chain of functions?
I want to use print from chain of functions, but it doesn't work. getNext is called from inside app1, but print is not called from inside app2. Why? The output is just Just (39,40,41,"zzz") ...
1
vote
2
answers
91
views
Conditional typeclass in GADT
I have a GADT like
data GADT where
Data :: a -> GADT
and I want to implement a Show instance depending on where Show a exists.
Intuitively, something like
instance Show GADT where
show (Data a)...
3
votes
0
answers
46
views
Merging hpc coverage from multiple architectures
I have a program that does slightly different things on different OS/architectures. I'll use this example:
main = do
output <- readProcess "uname" [] ""
print $ case ...
2
votes
2
answers
100
views
Cabal not recognizing Include/Header File Directories for FFI project
So I have a project in Haskell, and I was using make to build it, but then I switched the project over to use Cabal as someone recommended to me on another Stack Overflow post. It is an FFI project, ...
3
votes
2
answers
92
views
Is there a way to use a kind level class with type families?
I was trying to define an integer type, and a typeclass that would allow operations on all Numberlike types.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# ...
2
votes
1
answer
128
views
Problems setting up haskell-tools.nvim on NixOS
I'm trying to setup the Neovim plugin https://github.com/mrcjkb/haskell-tools.nvim
for a while now and I don't see any progress.
I mostly want this plugin for Haskell CodeLenses, so that I can write a ...
4
votes
3
answers
127
views
Why can't Haskell unify my types when using a constraint as class parameter?
I am trying to split my code into nice modules. However, while doing this, one of the classes I have in the imported module (Outer in Module 1 below) needs a constraint that is only known in the ...
2
votes
1
answer
56
views
GHC(Haskell) not picking up imports from makefile
I have been making a Haskell project that I want to continue on in C at some point through the FFI. I wanted to create a makefile to compile all the source with Clang for C and GHC for Haskell.
The ...
1
vote
1
answer
97
views
State Monad in Haskell . How to adapt functors, applicatives and monads to characteristics and Die Rolls
I'm trying to use State Monad in a personal section of code in order to have a more natural code . The problem and the code section are the following ones . I'm trying to ask interactively a user to ...