haskell

Type Constructor as Return Type

佐手、 提交于 2020-07-05 07:21:29
问题 In Scala, I can define an Algebraic Data Type: scala> sealed trait Maybe[A] defined trait Maybe scala> case class Just[A](x: A) extends Maybe[A] defined class Just scala> case object NothingHere extends Maybe[Nothing] defined object NothingHere It's possible to return a function, f , with a return type of Maybe[A] . scala> def f[A](x: A): Maybe[A] = Just(x) f: [A](x: A)Maybe[A] However, it's also possible to specify that a Just[A] is returned. scala> def f[A](x: A): Just[A] = Just(x) f: [A](x

How to view the generated code for derived instances / deriving in Haskell

泄露秘密 提交于 2020-07-02 11:34:27
问题 So, in Haskell, it's really easy to do this: data Foo = Bar | Baz deriving (Read, Show) This is great, but I'd like to be able to pass some data as a string from Haskell to the Elm language. The languages are similar enough that, if I had a Haskell implementation of Read, I could easily convert it to Elm by hand. The problem is, when I use deriving, the function is automatically generated, but I can't actually see what it does. I'm wondering, is there a way to automatically generate the code

How to pattern-match an intermediate value in Haskell

此生再无相见时 提交于 2020-06-27 18:54:43
问题 Question In the book Category Theory for Programmers by Bartosz Milewski, chapter 4.3. You must code a Kleisli category where morphisms are partial functions. Here is my attempt which does not compile: data Optional a = Valid a | Invalid deriving (Show) return :: a -> Optional a return x = Valid x (>=>) :: (a -> Optional b) -> (b -> Optional c) -> (a -> Optional c) f (>=>) g = \x -> let s = f x in | s == Valid v = g v | s == Invalid = Invalid In the >=> operator definition, I want to pattern

How to pattern-match an intermediate value in Haskell

不想你离开。 提交于 2020-06-27 18:54:30
问题 Question In the book Category Theory for Programmers by Bartosz Milewski, chapter 4.3. You must code a Kleisli category where morphisms are partial functions. Here is my attempt which does not compile: data Optional a = Valid a | Invalid deriving (Show) return :: a -> Optional a return x = Valid x (>=>) :: (a -> Optional b) -> (b -> Optional c) -> (a -> Optional c) f (>=>) g = \x -> let s = f x in | s == Valid v = g v | s == Invalid = Invalid In the >=> operator definition, I want to pattern

Haskell Cabal v2 and Sandbox

霸气de小男生 提交于 2020-06-27 11:58:39
问题 We are told that this is now legacy mode of cabal, to manage a user-defined sandbox: cabal init sandbox cabal install <some stuff> Which is later loaded at your discretion using cabal exec bash Question: How is an equivalent operation performed using the new implementation of Cabal? The documentation is (as it currently stands) very cryptic with zero usage example. That would be helpful to facilitate migration. Currently contemplating Cabal 2.4.0.0 with GHC 8.6.5. 回答1: There are no sandboxes.

Haskell overlapping instances and type functions

梦想的初衷 提交于 2020-06-27 07:24:28
问题 I have the following typeclass which models a SQL-like query optimization: class OptimizableQuery q where type Optimized q :: * optimize :: q -> Optimized q instance Query q => OptimizableQuery q where type Optimized q = q optimize q = q instance (Query q, OptimizableQuery q) => OptimizableQuery (Select (Select q p) p) where type Optimized (Select (Select q p) p) = Select (Optimized q) p optimize (Select (Select q _) p) = Select (optimize q) p the problem is that I get the error "Conflicting

Making Read-Only functions for a State in Haskell

倖福魔咒の 提交于 2020-06-27 07:07:48
问题 I often end up in a situation where it's very convenient to be using the State monad, due to having a lot of related functions that need to operate on the same piece of data in a semi-imperative way. Some of the functions need to read the data in the State monad, but will never need to change it. Using the State monad as usual in these functions works just fine, but I can't help but feel that I've given up Haskell's inherent safety and replicated a language where any function can mutate