51,816 questions
Best practices
1
vote
6
replies
22
views
Terminology for a value that has a particular type in Haskell
In English, I might talk about the category or property or type tree, and then refer to specific trees as tree "instances" of the property tree. (In philosophical writing, sometimes it's ...
-1
votes
0
answers
19
views
How to write Monad for class MyPair[+A, +B]
I have no idea how to realize MonadError for case class MyPair[+A, +B](a: A, b: B), since I don't understand how to deal with 2 types in Monad. I've been trying to write it for 2 weeks already, no ...
Best practices
0
votes
2
replies
44
views
How to refer to heterogeneous list of any size?
I want to make a function call with two arguments: function and its arguments. Arguments are provided as heterogeneous list (Data.HList). Function should have a signature matching types of arguments. ...
Best practices
1
vote
2
replies
82
views
How to get a type of parameter from parametrized type?
How could I get a type of parameter from parametrized type? For example, for type T = Maybe String I want to get String, for type T = [Bool] I want to get Bool. It should be a function of T. Thanks in ...
0
votes
2
answers
80
views
Why is this instance overlapping/how to make interacting type classes work together?
I have the following sample code which shows the problem
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# ...
0
votes
1
answer
102
views
How can I map on a list that returns a tuple?
MRE:
MyType = MyDataCtor [MyType] | MyDataCtor2 MyType MyType | ... other cases (none of which require the map operation)
foo :: Type1 -> Type2 -> (Type1, Type2)
For example, for the ...
2
votes
1
answer
38
views
How to make the entire Parsec parsing process fail upon certain conditions?
I'm creating a toy language in Haskell, and using Text.Parsec to parse everything, So far it's worked great, but there's a certain feature that I don't know how to implement:
What I want to implement: ...
Advice
0
votes
4
replies
55
views
How to construct a signature of a function from arguments and return type?
I can get types of arguments of the function, I can collect it in some form, for example type T1 = String -> Int and I have return type of the function, for example type T2 = Bool. How can I ...
1
vote
3
answers
58
views
How would I use a custom function for some of the data constructors and the default class method for all others?
I am programming a Typechecker for a language that extends System F. This is my definition of Types (MRE):
data SType = SInt | SBool | SVar string | <9 other data constructors> |
...
4
votes
0
answers
59
views
Ensuring specialization while preventing inlining
disjointCollisions :: Eq k => Word -> Array (Leaf k a) -> Word -> Array (Leaf k b) -> Bool
I would like this function to be specialized (to arbitrary unknown key types k) but never ...
Advice
3
votes
4
replies
85
views
Confused about associativity of >>=
In Wayne Snyder's slides on the State monad, slide 11 includes the following Haskell code. There's not a lot of explanatory text on this slide, but it seems clear that these two snippets are supposed ...
Best practices
0
votes
1
replies
101
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 :: (...
3
votes
1
answer
83
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
144
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
64
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
139
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
128
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
219
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
135
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
112
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
152
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
155
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
119
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
187
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
95
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
121
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
108
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
87
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
87
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
91
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
68
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
170
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
131
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
113
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
71
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
71
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
74
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 ...