News aggregator

Deriving Show instance with ADT that has phantom type

haskell-cafe - Sat, 05/16/2015 - 12:45am
Going through the Data Types Ala Carte paper I found this definition: data Expr f = In (f (Expr f)) I tried to use "deriving Show"... data Expr f = In (f (Expr f)) deriving Show I found out that doesn't work with ADT's that have phantom types. I tried StandaloneDeriving too since GHC recommended it: {-# LANGUAGE StandaloneDeriving #-} data Expr f = In (f (Expr f)) deriving Show instance deriving Show (Expr f) Got this error: src/Main.hs:32:1-31: No instance for (Show (f (Expr f))) … arising from a use of ‘showsPrec’ In the second argument of ‘(.)’, namely ‘(showsPrec 11 b1)’ In the second argument of ‘showParen’, namely ‘((.) (showString "In ") (showsPrec 11 b1))’ In the expression: showParen ((a >= 11)) ((.) (showString "In ") (showsPrec 11 b1)) When typechecking the code for ‘showsPrec’ in a standalone derived instance for ‘Show (Expr f)’: To see the code I am typ
Categories: Offsite Discussion

cast to specific Typeclass

haskell-cafe - Fri, 05/15/2015 - 7:59pm
Hello everyone, I'm trying to implement traversal over data structures and found Data.Typeable very useful for that. But it seems to be impossible to cast to specific typeclass, so that we can cast Typeable to something that is Show'able for example. It'd be usefull to have something like: data CanShow = forall a. CanShow a toConcrete :: (Typeable a) => a -> Maybe CanShow Implementing such datatypes for needed classes would allow to do some kind of class casting. Is it possible to implement this somehow? Is it a limitation of the implementation or a principal one? Thanks! Regards, Teodor.
Categories: Offsite Discussion

Transforming ASTs

haskell-cafe - Fri, 05/15/2015 - 5:08pm
This might be a duplicated message. The first time I posted it I had not subscribed to haskell cafe. Suppose the following data type for encoding Boolean expressions: data BExpr a = BTrue | BFalse | Id String | Not a | And a a | Or a a | BEq a a deriving (Functor) type Expr = Fix BExpr It is easy to produce a string representation of an expression or evaluate it: estr :: BExpr String -> String eval :: BExpr Bool -> Bool with the cata function from Data.Functor.Fixedpoint. Could you suggest a solution for transforming trees encoded as Exp into equivalent Expr (e.g Not Not a ~> a)? cata does not work since it expects a function f a -> a while a transformation would be f a -> f a. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Call for concurrency bugs!

haskell-cafe - Fri, 05/15/2015 - 2:14pm
Hi cafe, I've been working on a little library for testing concurrent Haskell programs, and would really like some test cases not constructed by myself to throw it at. If anyone has any examples of buggy concurrency (preferably where it's tricky to provoke the bugs with conventional testing techniques) that I could use: either open bugs, or things which have been fixed but were awkward to do so, that would be really useful. This is leading up to a paper, hopefully, so examples may go into that: anonymously, if preferred. Thank you. -- Michael Walker ( _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Haskell Weekly News

General haskell list - Fri, 05/15/2015 - 1:07pm
*Top Picks:* - Is Servant <>the most type-safe HTTP server library ever? Are the type signatures hard to read <>? In addition to its utmost relevance as a web library, Servant is also an awesome case study in the type safety vs type readability trade-off spectrum, brought to you by Alp Mestanogullari and Julian Arni. HN <> and /r/haskell <> - Ozan Sener compiles Pandoc into JS via GHCJS and creates a web interface to it using the Reflex FRP library <>. Markup.Rocks <> is much loved on /r/haskell <>. See also HN <>. - Is Haskell a "Startup Secret Weapon"? Ale
Categories: Incoming News

Parametricity and the difference between <*> and >>=

Haskell on Reddit - Fri, 05/15/2015 - 12:27pm

Can parametricity be used to explain the difference between (<*>) :: f (a -> b) -> f a -> f b and (>>=) :: m a -> (a -> m b) -> m b? More concretely, can it be proven that the function in f (a -> b) is always independent of the other term, while the lifted function (a -> m b) can always depend on the other term?

I want to use this to say something about the context-free-ishness of applicative parser combinators, and why it's so hard to optimize the grammar of a monadic parser combinator.

submitted by vincentrevelations
[link] [5 comments]
Categories: Incoming News

cabal is giving a weird error when attempting to install pandoc on Ubuntu 14.04

Haskell on Reddit - Fri, 05/15/2015 - 12:24pm

Hi. I am attempting to install Hakyll on my Chromebook's Ubuntu 14.04 chroot and I keep running into issues. I also ran into this problem when attempting to install on my 14.04 DigitalOcean droplet, but did not have this issue on my Manjaro install on my big PC.

My order of operations was to apt-get install haskell-platform and then cabal update and cabal install hakyll. The last command has a lot of dependencies that it attempts to install and it does work successfully, but when it gets to pandoc I get this error:

$ cabal install hakyll Resolving dependencies... [1 of 1] Compiling Main ( /tmp/pandoc-, /tmp/pandoc- ) Linking /tmp/pandoc- ... /usr/lib/ghc/unix- In function `pPrPr_disableITimers': (.text+0x300): multiple definition of `pPrPr_disableITimers' /root/.cabal/lib/unix- first defined here collect2: error: ld returned 1 exit status Failed to install pandoc- cabal: Error: some packages failed to install: hakyll- depends on pandoc- which failed to install. pandoc- failed during the configure step. The exception was: ExitFailure 1 pandoc-citeproc-0.7.1 depends on pandoc- which failed to install.

This error seems to be similar to the error in this post, but I am not really sure how to fix it. I am still getting my feet wet with Haskell in general and this is my first time using cabal to install anything (and use anything outside of the default packages).

I would appreciate it if someone could guide me on how to resolve this error. Thanks in advance.

EDIT: So I gave up on trying to fiddle around with the haskell-platform and ghc 7.6.* and whatever version of cabal that it comes with. I found this Gist describing how to install ghc 7.10.1 and cabal from source. I followed the instructions (just ran all the commands in order) and they worked on Ubuntu 14.04 perfectly.

EDIT 2: So because this whole compilation/installation process required over 1 GB of RAM, to get this running on my DigitalOcean droplet I had to shut it down and scale it up from 500 MB to 2 GB. When the process was done I scaled it back down and it appears that doing so broke ghc (I tested it right before and right after scaling it back down). I assume it will still work on my Chromebook, so I am fine if that is the case.

submitted by AIDS_Pizza
[link] [6 comments]
Categories: Incoming News

Can I specify the a in a phantom type to be limitedto a sum type?

haskell-cafe - Fri, 05/15/2015 - 7:47am
How can I create Answers of type Gender, Race, or Age? These should be possible: λ> Answer Male λ> Answer White λ> Answer Black λ> Answer 28 Others such as using a string should not be possible: λ> Answer "a string" -- should throw type error {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE StandaloneDeriving #-} module Tutorial where data Gender = Male | Female deriving (Show) data Race = White | Black deriving (Show) type Age = Int data Answer a where Answer :: Gender -> Answer Gender deriving instance Show (Answer w)
Categories: Offsite Discussion

Equality Contstraint?

haskell-cafe - Fri, 05/15/2015 - 2:17am
I am poking around in Arrow to understand it better. There is the following definition: second :: a b c -> a (d,b) (d,c) second f = arr swap >>> first f >>> arr swap where swap :: (x,y) -> (y,x) swap ~(x,y) = (y,x) Can someone explain what the ~ is? Searching the net resulted in frustration. I kind of think it is some kind of equality constraint but can’t find documentation. Perhaps if I knew what it was called I might succeed in finding something. If it is an equality constraint, please provide a reference if you have one. I did not find anything in a search including GHC, etc. Thanks
Categories: Offsite Discussion

Comparing Functions for Equality?

Haskell on Reddit - Thu, 05/14/2015 - 8:20pm

I read in A Book of Abstract Algebra, by Prof. Charles Pinter, that:

If f and g are functions from Real to Real, then f and g are equal if and only if f(x) = g(x) for every real number x.

When I tried to compare functions in ghci, I saw this compile-time error:

Prelude> let f x = "foo" Prelude> let g x = "foo" Prelude> f == g <interactive>:7:3: No instance for (Eq (t0 -> [Char])) arising from a use of `==' In the expression: f == g In an equation for `it': it = f == g

Is it simply too much work, not possible, to compare every input for the compiler? Or it's out of the compiler's scope?

submitted by kevin_meredith
[link] [19 comments]
Categories: Incoming News

Data constructor ‘Minus’ comes from an un-promotable type ‘Ints’

haskell-cafe - Thu, 05/14/2015 - 5:36pm
Hello, I clearly don’t really know what I’m doing…but at least I know it…. Here we defined the Naturals…and then attempt to construct the Integers…. naturals I can now define + and * and prove things about them…1 * x == x etc….nice..but lets put that on one side. Borrow bits and bobs from singletons Borrow bits and bobs from singletons..i.e. the isomorphic values…my proofs in nat now map to SNat…double nice. Create the integers by following my nose……(the integers are the equivalence class of pairs of naturals….) i.e. we have “positive” or “negative” or “zero”… Ok….this works as a set of values….but…. I can’t prove anything about these because the data constructors for my integers aren’t “promotable”… I cant do the same trick I did with Nat. “:k Zero” ….. Data constructor ‘Zero’ comes from an un-promotable type ‘Ints’ In a type in a GHCi command: Zero I’ve tried rejigging this in various futile and ignorant m
Categories: Offsite Discussion

Why does the inferred type of this expression change when I bind it to a varid?

Haskell on Reddit - Thu, 05/14/2015 - 4:03pm

I'd like a function that takes an Integer and gives back the number of digits in it. Here's the output from my ghci session:

GHCi, version 7.6.3: :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> :t (length . show) (length . show) :: Show a => a -> Int Prelude> let digits = length . show Prelude> :t digits digits :: () -> Int Prelude>

So, length . show has the right type on its own, but when I bind it to the name digits, it suddenly gets a much more restrictive type. What causes this, and how can I avoid it?

Sure, it works if I just specify the type I want, but I'm surprised that the type inference gets it right some of the time and wrong other times.

submitted by penguinland
[link] [5 comments]
Categories: Incoming News

Question: What's the most Haskell way to implement this? (Card game)

Haskell on Reddit - Thu, 05/14/2015 - 3:14pm

So I basicly have two implementations in mind and im not sure about which of them is the proper Haskell implementation. I have differents cards, entities of those cards, players who can be the owner of those cardentities, and locations on a board where the cardentities can be.

My first idea was:

data Player = Player {name :: String, ..} data Card = Card {name :: String, desc :: String, uri :: String} data CardEntity = CardEntity {card :: Card, rotation :: Direction, orientation :: Orientation, ..} data Location = Location {id :: Int}

But i wasn't sure where to put the information where a cardentity is and who it owns. Should an entity have an attribute who it owner is and where it is, or should a Location and a Player have a List of cardentities they have/own?

Then i came up with another idea. Maybe I could just give an ID to all players, entities and cards and make a function for each of their attributes:

data Player = Player {id :: Int} (or just type Player = Int) data CardEntity = CardEntity {id :: Int}


cardEntityRotation :: CardEntity -> Direction cardEntityOrientation :: CardEntity -> Orientation playerName :: Player -> String


that way i could easily give cardentities the owner attribute and players the cards attribute:

playerCardEntities :: Player -> [CardEntity] cardEntityOwner :: CardEntity -> Player cardName :: Card -> String

but cards are getting loaded from disk in the beginning so I'd probably had to make a hashmap for every single attribute..

Which one is better, or is there an better implementation than those two?

Thanks in advance.

submitted by Ecyoph
[link] [9 comments]
Categories: Incoming News

Which free monad?

Haskell on Reddit - Thu, 05/14/2015 - 2:57pm

It seems that there are two free-like monads.

data Free f r = Pure r | Free (f (Free f r)) data Free' f r = Done r | forall s. s :>>= (s -> f (Free' f r))

since s is existentially quantified, it can't be observed, so they should act the same. Is there ever a reason to use the second version instead of the (more popular) first? I'm not sure how haskell handles implicit state in closures, but I imagine it might be more efficient to reify the state in some cases.

submitted by dogodel
[link] [12 comments]
Categories: Incoming News

mightybyte: LTMT Part 3: The Monad Cookbook

Planet Haskell - Thu, 05/14/2015 - 11:52am

The previous two posts in my Less Traveled Monad Tutorial series have not had much in the way of directly practical content. In other words, if you only read those posts and nothing else about monads, you probably wouldn't be able to use monads in real code. This was intentional because I felt that the practical stuff (like do notation) had adequate treatment in other resources. In this post I'm still not going to talk about the details of do notation--you should definitely read about that elsewhere--but I am going to talk about some of the most common things I have seen beginners struggle with and give you cookbook-style patterns that you can use to solve these issues.

Problem: Getting at the pure value inside the monad

This is perhaps the most common problem for Haskell newcomers. It usually manifests itself as something like this:

main = do lineList <- lines $ readFile "myfile.txt" -- ... do something with lineList here

That code generates the following error from GHC:

Couldn't match type `IO String' with `[Char]' Expected type: String Actual type: IO String In the return type of a call of `readFile'

Many newcomers seem puzzled by this error message, but it tells you EXACTLY what the problem is. The return type of readFile has type IO String, but the thing that is expected in that spot is a String. (Note: String is a synonym for [Char].) The problem is, this isn't very helpful. You could understand that error completely and still not know how to solve the problem. First, let's look at the types involved.

readFile :: FilePath -> IO String lines :: String -> [String]

Both of these functions are defined in Prelude. These two type signatures show the problem very clearly. readFile returns an IO String, but the lines function is expecting a String as its first argument. IO String != String. Somehow we need to extract the String out of the IO in order to pass it to the lines function. This is exactly what do notation was designed to help you with.

Solution #1 main :: IO () main = do contents <- readFile "myfile.txt" let lineList = lines contents -- ... do something with lineList here

This solution demonstrates two things about do notation. First, the left arrow lets you pull things out of the monad. Second, if you're not pulling something out of a monad, use "let foo =". One metaphor that might help you remember this is to think of "IO String" as a computation in the IO monad that returns a String. A do block lets you run these computations and assign names to the resulting pure values.

Solution #2

We could also attack the problem a different way. Instead of pulling the result of readFile out of the monad, we can lift the lines function into the monad. The function we use to do that is called liftM.

liftM :: Monad m => (a -> b) -> m a -> m b liftM :: Monad m => (a -> b) -> (m a -> m b)

The associativity of the -> operator is such that these two type signatures are equivalent. If you've ever heard Haskell people saying that all functions are single argument functions, this is what they are talking about. You can think of liftM as a function that takes one argument, a function (a -> b), and returns another function, a function (m a -> m b). When you think about it this way, you see that the liftM function converts a function of pure values into a function of monadic values. This is exactly what we were looking for.

main :: IO () main = do lineList <- liftM lines (readFile "myfile.txt") -- ... do something with lineList here

This is more concise than our previous solution, so in this simple example it is probably what we would use. But if we needed to use contents in more than one place, then the first solution would be better.

Problem: Making pure values monadic

Consider the following program:

import Control.Monad import System.Environment main :: IO () main = do args <- getArgs output <- case args of [] -> "cat: must specify some files" fs -> liftM concat (mapM readFile fs) putStrLn output

This program also has an error. GHC actually gives you three errors here because there's no way for it to know exactly what you meant. But the first error is the one we're interested in.

Couldn't match type `[]' with `IO' Expected type: IO Char Actual type: [Char] In the expression: "cat: must specify some files"

Just like before, this error tells us exactly what's wrong. We're supposed to have an IO something, but we only have a String (remember, String is the same as [Char]). It's not convenient for us to get the pure result out of the readFile functions like we did before because of the structure of what we're trying to do. The two patterns in the case statement must have the same type, so that means that we need to somehow convert our String into an IO String. This is exactly what the return function is for.

Solution: return return :: a -> m a

This type signature tells us that return takes any type a as input and returns "m a". So all we have to do is use the return function.

import Control.Monad import System.Environment main :: IO () main = do args <- getArgs output <- case args of [] -> return "cat: must specify some files" fs -> liftM concat (mapM readFile fs) putStrLn output

The 'm' that the return function wraps its argument in, is determined by the context. In this case, main is in the IO monad, so that's what return uses.

Problem: Chaining multiple monadic operations import System.Environment main :: IO () main = do [from,to] <- getArgs writeFile to $ readFile from

As you probably guessed, this function also has an error. Hopefully you have an idea of what it might be. It's the same problem of needing a pure value when we actually have a monadic one. You could solve it like we did in solution #1 on the first problem (you might want to go ahead and give that a try before reading further). But this particular case has a pattern that makes a different solution work nicely. Unlike the first problem, you can't use liftM here.

Solution: bind

When we used liftM, we had a pure function lines :: String -> [String]. But here we have writeFile :: FilePath -> String -> IO (). We've already supplied the first argument, so what we actually have is writeFile to :: String -> IO (). And again, readFile returns IO String instead of the pure String that we need. To solve this we can use another function that you've probably heard about when people talk about monads...the bind function.

(=<<) :: Monad m => (a -> m b) -> m a -> m b (=<<) :: Monad m => (a -> m b) -> (m a -> m b)

Notice how the pattern here is different from the first example. In that example we had (a -> b) and we needed to convert it to (m a -> m b). Here we have (a -> m b) and we need to convert it to (m a -> m b). In other words, we're only adding an 'm' onto the 'a', which is exactly the pattern we need here. Here are the two patterns next to each other to show the correspondence.

writeFile to :: String -> IO () a -> m b

From this we see that "writeFile to" is the first argument to the =<< function. readFile from :: IO String fits perfectly as the second argument to =<<, and then the return value is the result of the writeFile. It all fits together like this:

import System.Environment main :: IO () main = do [from,to] <- getArgs writeFile to =<< readFile from

Some might point out that this third problem is really the same as the first problem. That is true, but I think it's useful to see the varying patterns laid out in this cookbook style so you can figure out what you need to use when you encounter these patterns as you're writing code. Everything I've said here can be discovered by carefully studying the Control.Monad module. There are lots of other convenience functions there that make working with monads easier. In fact, I already used one of them: mapM.

When you're first learning Haskell, I would recommend that you keep the documentation for Control.Monad close by at all times. Whenever you need to do something new involving monadic values, odds are good that there's a function in there to help you. I would not recommend spending 10 hours studying Control.Monad all at once. You'll probably be better off writing lots of code and referring to it whenever you think there should be an easier way to do what you want to do. Over time the patterns will sink in as form new connections between different concepts in your brain.

It takes effort. Some people do pick these things up more quickly than others, but I don't know anyone who just read through Control.Monad and then immediately had a working knowledge of everything in there. The patterns you're grappling with here will almost definitely be foreign to you because no other mainstream language enforces this distinction between pure values and side effecting values. But I think the payoff of being able to separate pure and impure code is well worth the effort.

Categories: Offsite Blogs

CFP : Extended deadline : Functional Art, Music,Modelling and Design (FARM 2015)

General haskell list - Thu, 05/14/2015 - 11:12am
************************************************************ Call for Papers and Demos : FARM 2015 The 3rd ACM SIGPLAN International Workshop on Functional Art, Music, Modelling and Design Vancouver, Canada, 5 September, 2015 affiliated with ICFP 2015 EXTENTED Submission Deadline : 27 May, 2015 (optional abstract submission : 17 May, 2015) ************************************************************ The ACM SIGPLAN International Workshop on Functional Art, Music, Modelling and Design (FARM) gathers together people who are harnessing functional techniques in the pursuit of creativity and expression. Functional Programming has emerged as a mainstream software development paradigm, and its artistic and creative use is booming. A growing number of software toolkits, frameworks and environments for art, music and design now employ functional programming languages and techniques. FARM is a forum for expl
Categories: Incoming News

Suggestion: "Sizable" super class for Storable

haskell-cafe - Thu, 05/14/2015 - 9:50am
Storable instances have a size, given by sizeOf. In many cases, we're not interested in peeking/poking data but only passing it opaquely via the FFI. A common use case is when the C API offers an "init" function such as: void mycontext_init(mycontext *context); For these cases it would be useful to know the size of "mycontext", so we could malloc it and pass a pointer to mycontext_init. Also, it allows Haskell-side code to decide how it wants to allocate the data, perhaps using some other (external) mechanism not related to the specific API that the FFI bindings are wrapping. c2hs would benefit by allowing users to use the '+' notation in function parameters (which generate malloc-and-pass style code), without having to guess the size of the structure. Instead, it could simply use the Sizable (TM) instance to get the size, and the user will define Sizable in any way they want (for example, using the {#sizeof#} macro, which is somewhat unreliable, or by hard-coding or manually entering the size or b
Categories: Offsite Discussion