Imants Cekusins | 12 Feb 12:43 2016
Picon

Unequal types constraint

class C a b

instance C Int a    -- when a /= Int

instance C a a     -- when a == a

how would you do this without IncoherentInstances?
Thomas Jakway | 12 Feb 02:14 2016
Picon

Random Numbers with the State Monad

I'm having a bad time using the State monad to generate random numbers 
without carrying around a lot of StdGens manually.
I have this snippet in the IO monad:

     ... IO stuff ...
     gen <- getStdGen
     let (numPlayers, numMatches) = (evalState genRandVariables gen) :: 
(Integer, Integer)
     ... More IO stuff ...

     where maxRandPlayers = 10 :: Integer
           minRandMatches = 10 :: Integer
           maxRandMatches = 100 :: Integer
           genRandVariables = (do
                 np <- randomR (1, maxRandPlayers) --minimum 1 other player
                 nm <- randomR (minRandMatches, maxRandMatches)
                 return (np, nm)) :: State StdGen (Integer, Integer)

I get this error message:
test/Jakway/Blackjack/Tests/IntegrationTests/MatchTests.hs:53:23:
     Couldn't match expected type ‘StateT
                                     StdGen 
Data.Functor.Identity.Identity Integer’
                 with actual type ‘g0 -> (Integer, g0)’
     Probable cause: ‘randomR’ is applied to too few arguments
     In a stmt of a 'do' block: np <- randomR (1, maxRandPlayers)
     In the expression:
         (do { np <- randomR (1, maxRandPlayers);
               nm <- randomR (minRandMatches, maxRandMatches);
               return (np, nm) }) ::
(Continue reading)

PICCA Frederic-Emmanuel | 11 Feb 12:02 2016
Picon

while loop

Hello,

I am playing with FFI and I need to extract from a  C API a list of pointers.

I have two methods available

c_get_first_item :: Ptr List -> IO (Ptr Item)
c_get_next_item :: Ptr List -> Ptr Item -> IO (Ptr Item)

I would like to obtain a [Ptr Item]

I try to used whileM but I did not find how to inject the first item in the loop.

whileM (return . ( /= nullPtr)) (c_get_next_item list item)

the c_get_next_item return a nullPtr when there is no remaining item.

what is the haskell way in order to extract a list of pointer using these C methods ?

thanks for your help

Frederic
Ben Rogalski | 9 Feb 05:20 2016
Picon
Gravatar

Understanding wrap function in Connor McBride's Faking It

Hello,

I am working on a Vector class, heavily based on the paper "Faking It: Simulating Dependent Types in Haskell" by Connor McBride.

Here is a stripped down version of my code:

---

{-# LANGUAGE RankNTypes #-}
                           
import Control.Applicative
import Data.Functor

infixr 5 :.
data Cons u t = (:.) t (u t) deriving (Show)

data Nil t = Nil deriving (Show)

class (Applicative v, Functor v) => Vector v where
  wrap :: (forall u. Vector u => s -> f (u t) -> f (Cons u t)) -> f (Nil t) -> v s -> f (v t)

instance (Vector u) => Vector (Cons u) where
  wrap f n (x:.xs) = f x (wrap f n xs)

instance Vector Nil where
  wrap _ n Nil = n

instance (Applicative u) => Applicative (Cons u) where
  pure x = x:.(pure x)
  (f:.fs) <*> (x:.xs) = (f x):.(fs <*> xs)

instance Applicative Nil where
  pure _ = Nil
  _ <*> _ = Nil

instance (Functor u) => Functor (Cons u) where
  fmap f (x:.xs) = (f x):.(fmap f xs)

instance Functor Nil where
  fmap _ Nil = Nil

transpose :: (Vector w, Vector u) => w (u t) -> u (w t)
transpose v = wrap (\x xs -> (:.) <$> x <*> xs) (pure Nil) v

---

I am having trouble understanding the type signature of 'wrap'. This is how I interpret it:
(function that takes an 's' and container of 'u t' and returns a container of 'Cons u t') -> 
(container of 'Nil') -> 
(a vector of 's') -> 
(a container with the same type as the container of Nil, but containing v t instead)

What doesn't make sense to me is how the signature seems to imply that the structure of the container in the return type is the same as the structure of the container holding the Nils, but in 'transpose' the container holding Nil seems to be infinite.

Can someone help me to understand what is going on here?

Thanks,
Ben
<div><div dir="ltr">Hello,<div><br></div>
<div>I am working on a Vector class, heavily based on the paper "Faking It: Simulating Dependent Types in Haskell" by Connor McBride.</div>
<div><br></div>
<div>Here is a stripped down version of my code:</div>
<div><br></div>
<div>---</div>
<div><br></div>
<div>
<div>{-# LANGUAGE RankNTypes #-}</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>import Control.Applicative</div>
<div>import Data.Functor</div>
<div><br></div>
<div>infixr 5 :.</div>
<div>data Cons u t = (:.) t (u t) deriving (Show)</div>
<div><br></div>
<div>data Nil t = Nil deriving (Show)</div>
<div><br></div>
<div>class (Applicative v, Functor v) =&gt; Vector v where</div>
<div>&nbsp; wrap :: (forall u. Vector u =&gt; s -&gt; f (u t) -&gt; f (Cons u t)) -&gt; f (Nil t) -&gt; v s -&gt; f (v t)</div>
<div><br></div>
<div>instance (Vector u) =&gt; Vector (Cons u) where</div>
<div>&nbsp; wrap f n (x:.xs) = f x (wrap f n xs)</div>
<div><br></div>
<div>instance Vector Nil where</div>
<div>&nbsp; wrap _ n Nil = n</div>
<div><br></div>
<div>instance (Applicative u) =&gt; Applicative (Cons u) where</div>
<div>&nbsp; pure x = x:.(pure x)</div>
<div>&nbsp; (f:.fs) &lt;*&gt; (x:.xs) = (f x):.(fs &lt;*&gt; xs)</div>
<div><br></div>
<div>instance Applicative Nil where</div>
<div>&nbsp; pure _ = Nil</div>
<div>&nbsp; _ &lt;*&gt; _ = Nil</div>
<div><br></div>
<div>instance (Functor u) =&gt; Functor (Cons u) where</div>
<div>&nbsp; fmap f (x:.xs) = (f x):.(fmap f xs)</div>
<div><br></div>
<div>instance Functor Nil where</div>
<div>&nbsp; fmap _ Nil = Nil</div>
<div><br></div>
<div>transpose :: (Vector w, Vector u) =&gt; w (u t) -&gt; u (w t)</div>
<div>transpose v = wrap (\x xs -&gt; (:.) &lt;$&gt; x &lt;*&gt; xs) (pure Nil) v</div>
</div>
<div><br></div>
<div>---</div>
<div><br></div>
<div>I am having trouble understanding the type signature of 'wrap'. This is how I interpret it:</div>
<div>(function that takes an 's' and container of 'u t' and returns a container of 'Cons u t') -&gt;&nbsp;</div>
<div>(container of 'Nil') -&gt;&nbsp;</div>
<div>(a vector of 's') -&gt;&nbsp;</div>
<div>(a container with the same type as the container of Nil, but containing v t instead)</div>
<div><br></div>
<div>What doesn't make sense to me is how the signature seems to imply that the structure of the container in the return type is the same as the structure of the container holding the Nils, but in 'transpose' the container holding Nil seems to be infinite.</div>
<div><br></div>
<div>Can someone help me to understand what is going on here?</div>
<div><br></div>
<div>Thanks,</div>
<div>Ben</div>
</div></div>
wizard | 8 Feb 17:24 2016
Picon

Got "Non type-variable argument in the constraint" error for a simple function

Dear all,

I just started to learn Haskell with learnyouahaskell.com and at the very beginning, I met a strange issue with following simple function:

-- why does work with "toZero 10" but not for "toZero -10"? toZero :: (Integral t) => t -> [t] toZero 0 = [0] toZero x = if x > 0 then x : toZero (x - 1) else x : toZero (x + 1)

This function works as expected for positive arguments, e.g., "toZero 10" gives me [10,9,8,7,6,5,4,3,2,1,0]. However, GHCI will raise following error if I give it a negative argument, e.g., "toZero -10":

*Main> toZero -10

<interactive>:12:1:
    Non type-variable argument in the constraint: Num (t -> [t])
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall t. (Integral t, Num (t -> [t])) => t -> [t]

This seems strange to me as 10 and -10 has exactly the same type "Num a => a". I've done with chapter 1~10 of learnyouahaskell.com but still has no idea on why this error. Anybody can help to explain this?
Thanks a lot.

Regards
Zhiyi Xie
<div><div dir="ltr">Dear all,<div><br></div>
<div>I just started to learn Haskell with&nbsp;<a href="http://learnyouahaskell.com">learnyouahaskell.com</a>&nbsp;and at the very beginning, I met a strange issue with following simple function:</div>
<div><br></div>
<blockquote><div><div>
<span>-- why does work with "toZero 10" but not for "toZero -10"?</span>
<span>toZero</span> <span>::</span> (<span>Integral</span> t) <span>=&gt;</span> t <span>-&gt;</span> [t]
<span>toZero</span> <span>0</span> <span>=</span> [<span>0</span>]
<span>toZero</span> x <span>=</span> <span>if</span> x <span>&gt;</span> <span>0</span> <span>then</span> x <span>:</span> toZero (x <span>-</span> <span>1</span>)
                    <span>else</span> x <span>:</span> toZero (x <span>+</span> <span>1</span>)</div></div></blockquote>
<div><br></div>
<div>This function works as expected for positive arguments, e.g., "toZero 10" gives me [10,9,8,7,6,5,4,3,2,1,0]. However, GHCI will raise following error if I give it a negative argument, e.g., "toZero -10":</div>
<div><br></div>
<blockquote>
<div><div>*Main&gt; toZero -10</div></div>
<div><div><br></div></div>
<div><div>&lt;interactive&gt;:12:1:</div></div>
<div><div>&nbsp; &nbsp; Non type-variable argument in the constraint: Num (t -&gt; [t])</div></div>
<div><div>&nbsp; &nbsp; (Use FlexibleContexts to permit this)</div></div>
<div><div>&nbsp; &nbsp; When checking that &lsquo;it&rsquo; has the inferred type</div></div>
<div><div>&nbsp; &nbsp; &nbsp; it :: forall t. (Integral t, Num (t -&gt; [t])) =&gt; t -&gt; [t]</div></div>
</blockquote>
<div><br></div>
<div>This seems strange to me as 10 and -10 has exactly the same type "Num a =&gt; a". I've done with chapter 1~10 of&nbsp;<a href="http://learnyouahaskell.com/">learnyouahaskell.com</a>&nbsp;but still has no idea on why this error. Anybody can help to explain this?</div>
<div>Thanks a lot.</div>
<div><br></div>
<div>Regards</div>
<div>Zhiyi Xie</div>
</div></div>
Olivier Duhart | 8 Feb 14:54 2016
Picon

Returning Maybe Float -> Float -> Float

hello,

I am starting with Haskell and trying some little exercices on my own. I successfully implemented a reverse polish notation evaluator and I want to improve it a little using Maybe

All i want is to implement  a function that can returned available functions according to its string name
i.e return (+) when gived "+"
I also want to return Nothing if the operation is not available (not implemented yet). 
So my function should Maybe return a (float -> float -> float)

My current implementation is 

operation :: String -> Maybe Float -> Float -> Float
operation op 
   | op == "+" = Just (+)
   | op == "-" = Just (-)
   | op == "*" = Just (*)
   | op == "/" = Just (/)
   | otherwise = Nothing

but it failed to compile with the following error : 

rpn.hs:64:18:
    Couldn't match expected type `Maybe Float -> Float -> Float'
                with actual type `Maybe a0'
    In the expression: Nothing
    In an equation for `operation':
        operation op
          | op == "+" = Just (+)
          | op == "-" = Just (-)
          | op == "*" = Just (*)
          | op == "/" = Just (/)
          | otherwise = Nothing


I don't understand the error :( Do I have to explicitly type the Nothing return ? 

Could you guide me to a solution or explain me what I am doing wrong, please ?

Thanks in advance

Olivier
<div><div dir="ltr">
<span>hello,</span><div><br></div>
<div>I am starting with Haskell and trying some little exercices on my own. I successfully implemented a reverse polish notation evaluator and I want to improve it a little using&nbsp;Maybe</div>
<div><br></div>
<div>All i want is to implement &nbsp;a function that can returned available functions according to its string name</div>
<div>i.e return (+) when gived "+"</div>
<div>I also want to return Nothing if the operation is not available (not implemented yet).&nbsp;</div>
<div>So my function should Maybe return a (float -&gt; float -&gt; float)</div>
<div><br></div>
<div>My current implementation is&nbsp;</div>
<div><br></div>
<blockquote>
<div>operation :: String -&gt; Maybe Float -&gt; Float -&gt; Float</div>
<div>operation op&nbsp;</div>
<div>&nbsp; &nbsp;| op == "+" = Just (+)</div>
<div>&nbsp; &nbsp;| op == "-" = Just (-)</div>
<div>&nbsp; &nbsp;| op == "*" = Just (*)</div>
<div>&nbsp; &nbsp;| op == "/" = Just (/)</div>
<div>&nbsp; &nbsp;| otherwise = Nothing</div>
</blockquote>
<div><br></div>
<div>but it failed to compile with the following error :&nbsp;</div>
<div><br></div>
<div>
<div>rpn.hs:64:18:</div>
<div>&nbsp; &nbsp; Couldn't match expected type `Maybe Float -&gt; Float -&gt; Float'</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with actual type `Maybe a0'</div>
<div>&nbsp; &nbsp; In the expression: Nothing</div>
<div>&nbsp; &nbsp; In an equation for `operation':</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; operation op</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | op == "+" = Just (+)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | op == "-" = Just (-)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | op == "*" = Just (*)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | op == "/" = Just (/)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | otherwise = Nothing</div>
</div>
<div><br></div>
<div><br></div>
<div>I don't understand the error :( Do I have to explicitly type the Nothing return ?&nbsp;</div>
<div><br></div>
<div>Could you guide me to a solution or explain me what I am doing wrong, please ?</div>
<div><br></div>
<div>Thanks in advance</div>
<div><br></div>
<div>Olivier</div>
</div></div>
John Ky | 6 Feb 23:27 2016
Picon

How to convert ST.Trans Array to ByteString efficiently?

Anyone know of an efficient way to convert a ST.Trans Array to a ByteString?

Or is this it?


Cheers,

-John

<div><div dir="ltr">Anyone know of an efficient way to convert a ST.Trans Array to a ByteString?<div><br></div>
<div>Or is this it?</div>
<div><br></div>
<div>
<a href="https://github.com/haskell-works/conduit-succinct-json/commit/1427f10ce67dee86bdda8b7356d92d692c61100d">https://github.com/haskell-works/conduit-succinct-json/commit/1427f10ce67dee86bdda8b7356d92d692c61100d</a><br>
</div>
<div><br></div>
<div>Cheers,</div>
<div><br></div>
<div>-John</div>
<div><br></div>
</div></div>
Mike Houghton | 5 Feb 22:37 2016
Picon

tuple space

Hi,

I’m thinking about how to write  a Tuple Space (TS)  in Haskell.

A tuple can  have many fields of different types, in pseudo code something like

T = (1, ”A string”,  3.4)    i.e. an int, string and double.

How can this (and the many variations) be done in Haskell?

(In Java it would be a list of Object)

Thanks 

Mike

_______________________________________________
Beginners mailing list
Beginners <at> haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Chul-Woong Yang | 2 Feb 08:15 2016

foldr with short circuit and accumulator

Hi, all.

Can it be possible to do fold with short circuit and accumulator both?
For example, can we find an element which is same value to adjacent one?

findAdjacent [1,2..n, n, n+1, n+2.......] => n
                         \__very long__/

Though there can be many ways to do it, Can we do it with fold[r|l]?

I'll be happy to receive any comments.

Chul-Woong
Chul-Woong Yang | 2 Feb 02:32 2016

foldr on infinite list to decide prime number

Hi, all.

While I know that foldr can be used on infinite list to generate infinite list,
I'm having difficulty in understaind following code:

isPrime n = n > 1 && -- from haskell wiki foldr (\p r -> p*p > n || ((n `rem` p) /= 0 && r)) True primes primes = 2 : filter isPrime [3,5..]

primes is a infinite list of prime numbers, and isPrime does foldr to get a boolean value.
What causes foldr to terminate folding? 

Any helps will be deeply appreciated.

Thank you.

Chul-Woong
<div><div dir="ltr">Hi, all.<div><br></div>
<div>While I know that foldr can be used on infinite list to generate infinite list,</div>
<div>I'm having difficulty in understaind following code:</div>
<div>
<div><br></div>
<div>
<span>isPrime n = n &gt; </span><span class="">1</span><span> &amp;&amp;  -- from haskell wiki</span><span>
        foldr (\p r -&gt; p*p &gt; n || ((n `rem` p) /= </span><span class="">0</span><span> &amp;&amp; r)) </span><span class="">True</span><span> primes
primes = </span><span class="">2</span><span> : filter isPrime [</span><span class="">3</span><span>,</span><span class="">5.</span><span>.]</span><br><div><br></div>
</div>
</div>
<div>primes is a infinite list of prime numbers, and isPrime does foldr to get a boolean value.</div>
<div>What causes foldr to terminate folding?&nbsp;</div>
<div><br></div>
<div>Any helps will be deeply appreciated.</div>
<div><br></div>
<div>Thank you.</div>
<div><br></div>
<div>Chul-Woong</div>
</div></div>
Olumide | 1 Feb 18:28 2016
Picon

Why does sequence (map print [1, 2, 3, 4, 5]) produce [(), (), (), (), ()] at the end?

Hello list,

The question says it all.

BTW, I'm studying LYH and I'm on the chapter on IO. The book offers and 
explanation but its not very clear -- to me at least.

Thanks,

- Olumide


Gmane