haskell

How to put constraints on type variable of kind `Constraint`?

人走茶凉 提交于 2021-02-07 05:02:20
问题 I'm playing around with the ConstraintKinds extension of GHC. I have the following data type, which is just a box for things fulfilling some one parameter constraint c : data Some (c :: * -> Constraint) where Some :: forall a. c a => a -> Some c For example, I could construct a box with some kind of number (arguably not very useful). x :: Some Num x = Some (1 :: Int) Now, as long as c includes the constraint Show , I could provide an instance of Show (Some c) . instance ??? => Show (Some c)

Are monad laws enforced in Haskell?

懵懂的女人 提交于 2021-02-07 04:53:05
问题 From the Haskell wiki: Monads can be viewed as a standard programming interface to various data or control structures, which is captured by the Monad class. All common monads are members of it: class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a In addition to implementing the class functions, all instances of Monad should obey the following equations, or Monad Laws: return a >>= k = k a m >>= return = m m >>= (\x -> k x >>=

Are monad laws enforced in Haskell?

三世轮回 提交于 2021-02-07 04:53:05
问题 From the Haskell wiki: Monads can be viewed as a standard programming interface to various data or control structures, which is captured by the Monad class. All common monads are members of it: class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a In addition to implementing the class functions, all instances of Monad should obey the following equations, or Monad Laws: return a >>= k = k a m >>= return = m m >>= (\x -> k x >>=

Why constraints on data are a bad thing?

丶灬走出姿态 提交于 2021-02-07 04:51:28
问题 I know this question has been asked and answered lots of times but I still don't really understand why putting constraints on a data type is a bad thing. For example, let's take Data.Map k a . All of the useful functions involving a Map need an Ord k constraint. So there is an implicit constraint on the definition of Data.Map . Why is it better to keep it implicit instead of letting the compiler and programmers know that Data.Map needs an orderable key. Also, specifying a final type in a type

How to resolve overlapping instance

本小妞迷上赌 提交于 2021-02-06 20:47:44
问题 I have the following code (transform is similar to convert) instance {-# OVERLAPS #-} Transformable a a where transform x = x instance {-# OVERLAPPABLE #-} (Transformable l l', Transformable r r' ) => Transformable (Either l r) (Either l' r') where transform = bimap transform transform Of course, those instances overlap in the case where I'm trying to transform Either a b to Either a b and get the following error message ( ParsingError is a type alias for Either something somethingElse )

How to resolve overlapping instance

只谈情不闲聊 提交于 2021-02-06 20:45:02
问题 I have the following code (transform is similar to convert) instance {-# OVERLAPS #-} Transformable a a where transform x = x instance {-# OVERLAPPABLE #-} (Transformable l l', Transformable r r' ) => Transformable (Either l r) (Either l' r') where transform = bimap transform transform Of course, those instances overlap in the case where I'm trying to transform Either a b to Either a b and get the following error message ( ParsingError is a type alias for Either something somethingElse )

Stripping out let in Haskell

我的梦境 提交于 2021-02-06 19:57:09
问题 I should probably first mention that I'm pretty new to Haskell. Is there a particular reason to keep the let expression in Haskell? I know that Haskell got rid of the rec keyword that corresponds to the Y-combinator portion of a let statement that indicates it's recursive. Why didn't they get rid of the let statement altogether? If they did, statements will seem more iterative to some degree. For example, something like: let y = 1+2 z = 4+6 in y+z would just be: y = 1+2 z = 4+6 y+z Which is

Stripping out let in Haskell

余生长醉 提交于 2021-02-06 19:56:47
问题 I should probably first mention that I'm pretty new to Haskell. Is there a particular reason to keep the let expression in Haskell? I know that Haskell got rid of the rec keyword that corresponds to the Y-combinator portion of a let statement that indicates it's recursive. Why didn't they get rid of the let statement altogether? If they did, statements will seem more iterative to some degree. For example, something like: let y = 1+2 z = 4+6 in y+z would just be: y = 1+2 z = 4+6 y+z Which is

Special runtime representation of [] type?

社会主义新天地 提交于 2021-02-06 10:42:09
问题 Consider the simple definition of a length-indexed vector: data Nat = Z | S Nat infixr 5 :> data Vec (n :: Nat) a where V0 :: Vec 'Z a (:>) :: a -> Vec n a -> Vec ('S n) a Naturally I would at some point need the following function: vec2list :: Vec n a -> [a] However, this function is really just a fancy identity. I believe that the runtime representations of these two types are the same, so vec2list :: Vec n a -> [a] vec2list = unsafeCoerce should work. Alas, it does not: >vec2list ('a' :>

Is there an object-identity-based, thread-safe memoization library somewhere?

一个人想着一个人 提交于 2021-02-06 10:14:45
问题 I know that memoization seems to be a perennial topic here on the haskell tag on stack overflow, but I think this question has not been asked before. I'm aware of several different 'off the shelf' memoization libraries for Haskell: The memo-combinators and memotrie packages, which make use of a beautiful trick involving lazy infinite data structures to achieve memoization in a purely functional way. (As I understand it, the former is slightly more flexible, while the latter is easier to use