Matthew Moppett | 24 Apr 18:49 2015
Picon

Strange "Not in scope" error message

I have a file with the following relevant lines:

import qualified Control.Concurrent as C

...

isCancelled = C.isEmptyMvar . stopConcurrentProcess   

Trying to compile this or run it in ghci yields a puzzling error message:

Not in scope: `C.isEmptyMvar'
    Perhaps you meant one of these:
      `C.isEmptyMVar' (imported from Control.Concurrent),
      `C.isEmptyChan' (imported from Control.Concurrent)

Any idea what could be going wrong?

<div><div dir="ltr">I have a file with the following relevant lines:<div><br></div>
<div>import qualified Control.Concurrent as C<br>
</div>
<div><br></div>
<div>...</div>
<div><br></div>
<div>isCancelled = C.isEmptyMvar . stopConcurrentProcess &nbsp;&nbsp;<br>
</div>
<div><br></div>
<div>Trying to compile this or run it in ghci yields a puzzling error message:</div>
<div><br></div>
<div>
<div>Not in scope: `C.isEmptyMvar'</div>
<div>&nbsp; &nbsp; Perhaps you meant one of these:</div>
<div>&nbsp; &nbsp; &nbsp; `C.isEmptyMVar' (imported from Control.Concurrent),</div>
<div>&nbsp; &nbsp; &nbsp; `C.isEmptyChan' (imported from Control.Concurrent)</div>
</div>
<div><br></div>
<div>Any idea what could be going wrong?</div>
<div><br></div>
</div></div>
Maurizio Vitale | 24 Apr 14:21 2015
Picon

missing parallelism

G'day,
  I have a test code that I compile with ghc -threaded -eventlog
-rtsopts --make parallel.hs and run with ./parallel 2 +RTS -ls -N4 on
a laptop with 4 physical cores. I would expect activities in two
threads, but threadscope shows only one active thread.
Can somebody explain me the program's behaviour?

Thanks a lot

   Maurizio

{-# LANGUAGE UnicodeSyntax #-}
{-# LANGUAGE TupleSections #-}

import Control.Applicative
import Control.Concurrent.Async (async, waitAny, Async)
import Data.List (delete, sortBy)
import Data.Ord (comparing)
import System.CPUTime
import System.Environment
import GHC.Conc (numCapabilities)

concurrentlyLimited :: Int -> [IO a] -> IO [a]
concurrentlyLimited n tasks = concurrentlyLimited' n (zip [0..] tasks) [] []

concurrentlyLimited' ∷ Int               -- ^ number of concurrent evaluations
                     → [(Int, IO b)]     -- ^ still to run (ordered by
first element)
                     → [Async (Int,b)]   -- ^ currently running
                     → [(Int,b)]         -- ^ partial results (ordered
by first element)
                     → IO [b]
concurrentlyLimited' _ [] [] results = return . map snd $ sortBy
(comparing fst) results
concurrentlyLimited' 0 todo ongoing results = do
    (task, newResult) <- waitAny ongoing
    concurrentlyLimited' 1 todo (delete task ongoing) (newResult:results)

concurrentlyLimited' _ [] ongoing results = concurrentlyLimited' 0 []
ongoing results
concurrentlyLimited' n ((i, task):otherTasks) ongoing results = do
    t <- async $ (i,) <$> task
    concurrentlyLimited' (n-1) otherTasks (t:ongoing) results

euler :: Int → Int
euler n = length (filter (relprime n) [1..n-1])
          where
            relprime x y = gcd x y == 1

sumEuler :: Int → Int
sumEuler = sum . (map euler) . mkList
           where
             mkList n = [1..n-1]

p ∷ IO Int
p = return $ sumEuler 3000

numThreads ∷ [String] → IO Int
numThreads [] = return numCapabilities
numThreads [cores] = return $ read cores

main ∷ IO()
main = do
  threads ← getArgs >>= numThreads
  putStrLn $ "Running up to " ++ show threads ++ " threads in parallel
(on " ++ show numCapabilities ++ " cores)"
  startTime ← getCPUTime
  f ← concurrentlyLimited threads $ replicate 10 p
  endTime ← getCPUTime
  putStrLn $ foldr  ((++) . show ) ""  f
  putStrLn $ "Evaluation took " ++ show (fromIntegral (endTime -
startTime) / 1000000000000∷Double)
_______________________________________________
Beginners mailing list
Beginners <at> haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Peng Yu | 23 Apr 21:39 2015
Picon

Error when running `cabal install cabal-install`

~$ cabal --version
cabal-install version 1.18.0.5
using version 1.18.1.4 of the Cabal library
~$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.8.3

I got the following error. Does anybody know how to resolve it? Thanks.

~$ cabal install cabal-install
Resolving dependencies...
Configuring Cabal-1.22.3.0...
Failed to install Cabal-1.22.3.0
Last 10 lines of the build log ( /Users/py/.cabal/logs/Cabal-1.22.3.0.log ):
cabal: Error: some packages failed to install:
Cabal-1.22.3.0 failed during the configure step. The exception was:
user error (Undefined symbols for architecture x86_64:
"_iconv", referenced from:
_hs_iconv in libHSbase-4.7.0.1.a(iconv.o)
(maybe you meant: _hs_iconv,
_base_GHCziIOziEncodingziIconv_iconvEncodingzuloc1_info ,
_base_GHCziIOziEncodingziIconv_iconvEncoding9_info ,
_base_GHCziIOziEncodingziIconv_iconvEncoding9_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncoding8_info ,
_base_GHCziIOziEncodingziIconv_iconvEncodingzuloc_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncoding10_info ,
_base_GHCziIOziEncodingziIconv_iconvEncoding6_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncoding10_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncoding3_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncoding7_info , _hs_iconv_close ,
_base_GHCziIOziEncodingziIconv_iconvEncodingzuloc1_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncoding2_info ,
_base_GHCziIOziEncodingziIconv_iconvEncoding8_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncoding3_info ,
_base_GHCziIOziEncodingziIconv_iconvEncoding2_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncoding7_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncodingzuloc_info ,
_base_GHCziIOziEncodingziIconv_iconvEncoding4_closure ,
_base_GHCziIOziEncodingziIconv_iconvEncoding6_info , _hs_iconv_open )
"_iconv_close", referenced from:
_hs_iconv_close in libHSbase-4.7.0.1.a(iconv.o)
(maybe you meant: _hs_iconv_close)
"_iconv_open", referenced from:
_hs_iconv_open in libHSbase-4.7.0.1.a(iconv.o)
(maybe you meant: _hs_iconv_open)
"_locale_charset", referenced from:
_localeEncoding in libHSbase-4.7.0.1.a(PrelIOUtils.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
)
cabal-install-1.22.3.0 depends on Cabal-1.22.3.0 which failed to install.
~/ports$ gvim ~/.cabal/logs/


--

-- 
Regards,
Peng
Shishir Srivastava | 22 Apr 17:24 2015
Picon

Value from Monad

Hi, 

Going through the '<-' description in 'learnouahaskell' it says - 

" If we have a Maybe String and we bind it with <- to a variable, that variable will be a String"

so if you write 

do
str <- Just "3"

str should be of type 'String'. My question is how do I return "34" by concatenating it to "4" because the following line of code fails - 

---
do
str <- Just "3"
return str++"4"::Maybe String
---

or for that matter how can I return/print just the value of str from this do block ?

Thanks,
Shishir 
<div><div dir="ltr">Hi,&nbsp;<div><br></div>
<div>Going through the '&lt;-' description in 'learnouahaskell' it says -&nbsp;</div>
<div><br></div>
<div>" If we have a Maybe String and we bind it with &lt;- to a variable, that variable will be a String"<br>
</div>
<div><br></div>
<div>so if you write&nbsp;</div>
<div><br></div>
<div>do</div>
<div>str &lt;- Just "3"</div>
<div><br></div>
<div>str should be of type 'String'. My question is how do I return "34" by&nbsp;concatenating&nbsp;it to "4" because the following line of code fails -&nbsp;</div>
<div><br></div>
<div>---</div>
<div>
<div>do</div>
<div>str &lt;- Just "3"</div>
</div>
<div>return str++"4"::Maybe String</div>
<div>---</div>
<div><br></div>
<div>or for that matter how can I return/print just the value of str from this do block ?</div>
<div><br></div>
<div>Thanks,</div>
<div>
<div><div class="gmail_signature"><div dir="ltr">Shishir&nbsp;<br>
</div></div></div>
</div>
</div></div>
Florian Gillard | 22 Apr 15:39 2015
Picon

integer to noise function in Haskell

I am trying to implement a basic pelin noise function, but I have some problem with the integer to noise function used to generate deterministic noise from integer inputs.

the function I am trying to implement is defined there:
http://libnoise.sourceforge.net/noisegen/index.html#continuousnoise

and my code so far look like this:

noise2d :: (Int32, Int32) -> Double
noise2d (x, y) =
    let m = x + y * 57
        n = (shiftR m 13) ^ m
        j = (n * (n * n * 15731 + 789221) + 1376312589) .&. 0x7fffffff
    in  1.0 - (fromIntegral j / 1073741824.0)

the code compile but I get the same result for any input, due to the fact that n is evaluated to 0.

Is there a better way to do that?
<div><div dir="ltr">
<div>
<div>
<div>
<div>
<div>I am trying to implement a basic pelin noise function, but I have some problem with the integer to noise function used to generate deterministic noise from integer inputs.<br><br>
</div>the function I am trying to implement is defined there:<br><a href="http://libnoise.sourceforge.net/noisegen/index.html#continuousnoise">http://libnoise.sourceforge.net/noisegen/index.html#continuousnoise</a><br>
</div>
<br>
</div>and my code so far look like this: <br><br>noise2d :: (Int32, Int32) -&gt; Double<br>noise2d (x, y) = <br>&nbsp;&nbsp;&nbsp; let m = x + y * 57<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n = (shiftR m 13) ^ m<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j = (n * (n * n * 15731 + 789221) + 1376312589) .&amp;. 0x7fffffff<br>&nbsp;&nbsp;&nbsp; in&nbsp; 1.0 - (fromIntegral j / 1073741824.0)<br><br>
</div>the code compile but I get the same result for any input, due to the fact that n is evaluated to 0.<br><br>
</div>Is there a better way to do that?<br>
</div></div>
Shishir Srivastava | 22 Apr 10:31 2015
Picon

filterM function

Hi, 

Continuing on from my previous question about powerset using filterM - Thanks to Alexey and Chaddaï. 

filterM is implemented as below - 

----
filterM _ [] = return [] filterM p (x:xs) = do flg <- p x ys <- filterM p xs return (if flg then x:ys else ys)---
I still don't quite understand how 'flg' being a boolean [] is used in the last 'if statement' of implementation because when I try to do the same thing outside in GHCi it fails miserably even though I am casting it to [Int] - 

--
return (if [True, False] then "4" else "3")::[Int]
--

Offtopic : Also if someone can explain how do I reply to individual mails/responses to my queries because I only get a digest on my gmail and there is no way to isolate and reply to individual mails either this list's page or from digest.

Thanks,
Shishir 
<div><div dir="ltr">Hi,&nbsp;<div><br></div>
<div>Continuing on from my previous question about powerset using filterM - Thanks to Alexey and&nbsp;<span>Chadda&iuml;.&nbsp;</span>
</div>
<div><span><br></span></div>
<div><span>filterM is implemented as below -&nbsp;</span></div>
<div><span><br></span></div>
<div><span>----</span></div>
<div>filterM _ []     =  return []
filterM p (x:xs) =  do
   flg &lt;- p x
   ys  &lt;- filterM p xs
   return (if flg then x:ys else ys)---</div>
<div>I still don't quite understand how 'flg' being a boolean [] is used in the last 'if statement' of implementation because when I try to do the same thing outside in GHCi it fails miserably even though I am casting it to [Int] -&nbsp;</div>
<div><br></div>
<div>--</div>
<div>return (if [True, False] then "4" else "3")::[Int]<br>
</div>
<div>--</div>
<div><br></div>
<div>Offtopic : Also if someone can explain how do I reply to individual mails/responses to my queries because I only get a digest on my gmail and there is no way to isolate and reply to individual mails either this list's page or from digest.</div>
<div><br></div>
<div>Thanks,</div>
<div>
<div><div class="gmail_signature"><div dir="ltr">Shishir&nbsp;<br>
</div></div></div>
</div>
</div></div>
Shishir Srivastava | 21 Apr 16:56 2015
Picon

Powerset function

Hi, 

In the 'learnyouahaskell' online book the powerset function is described like this - 

----
powerset :: [a] -> [[a]]
powerset xs = filterM (\x -> [True, False]) xs
----

And the filterM function is defined like this 

filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]

--

The filterM function is said to be an extension of 'filter' function which maps the monadic predicate over the given list. 

Now in powerset function the predicate returns a monad [True,False] for every element of the given list.

So for e.g. if the input list was only [1] the output of filterM will understandably be the cartesian product of [True, False] x [1] = [[1], []]. 

Extending this if the given input list contains two elements [1,2] the predicate would be mapped one by one on each of the elements and the result combined which means the output should be 

[[1], [], [2], []] 

But the powerset of [1,2] is definitely not that. 

Please could someone help me in getting my head around with how filterM is working in this case.

Thanks,
Shishir

<div><div dir="ltr">Hi,&nbsp;<div><br></div>
<div>In the 'learnyouahaskell' online book the powerset function is described like this -&nbsp;</div>
<div><br></div>
<div>----</div>
<div>powerset :: [a] -&gt; [[a]]<br>
</div>
<div>powerset xs = filterM (\x -&gt; [True, False]) xs</div>
<div>----</div>
<div><br></div>
<div>And the filterM function is defined like this&nbsp;</div>
<div><br></div>
<div>filterM :: (Monad m) =&gt; (a -&gt; m Bool) -&gt; [a] -&gt; m [a]<br>
</div>
<div><br></div>
<div>--</div>
<div><br></div>
<div>The filterM function is said to be an extension of 'filter' function which maps the monadic predicate over the given list.&nbsp;</div>
<div><br></div>
<div>Now in powerset function the predicate returns a monad [True,False] for every element of the given list.</div>
<div><br></div>
<div>So for e.g. if the input list was only [1] the output of filterM will understandably be the cartesian product of [True, False] x [1] = [[1], []].&nbsp;</div>
<div><br></div>
<div>Extending this if the given input list contains two elements [1,2] the predicate would be mapped one by one on each of the elements and the result combined which means the output should be&nbsp;</div>
<div><br></div>
<div>[[1], [], [2], []]&nbsp;</div>
<div><br></div>
<div>But the powerset of [1,2] is definitely not that.&nbsp;</div>
<div><br></div>
<div>Please could someone help me in getting my head around with how filterM is working in this case.</div>
<div><br></div>
<div>Thanks,</div>
<div>
<div><div class="gmail_signature"><div dir="ltr">Shishir<br><br>
</div></div></div>
</div>
</div></div>
Shishir Srivastava | 21 Apr 13:16 2015
Picon

Return function

Hi, 

Looking at the definition of return function for a Monad, it is described as - 

return :: a -> m a  

However when I do - 

return (Just 3) 

The result is 

Just 3

and not 

Just (Just 3)

If I understand the definition correctly (which doesn't seem to be the case) the return function should wrap the parameter (which in my case is 'Just 3') into the Maybe Monad and therefore should return me 'Just (Just 3)' 

Please can someone help explain.

Thanks,

Shishir Srivastava
 +44 (0) 750 127 5019

<div><div dir="ltr">Hi,&nbsp;<div><br></div>
<div>Looking at the definition of return function for a Monad, it is described as -&nbsp;</div>
<div><br></div>
<div>return :: a -&gt; m a &nbsp;<br>
</div>
<div><br></div>
<div>However when I do -&nbsp;</div>
<div><br></div>
<div>return (Just 3)&nbsp;</div>
<div><br></div>
<div>The result is&nbsp;</div>
<div><br></div>
<div>Just 3</div>
<div><br></div>
<div>and not&nbsp;</div>
<div><br></div>
<div>Just (Just 3)</div>
<div><br></div>
<div>If I understand the definition correctly (which doesn't seem to be the case) the return function should wrap the parameter (which in my case is 'Just 3') into the Maybe Monad and therefore should return me 'Just (Just 3)'&nbsp;</div>
<div><br></div>
<div>Please can someone help explain.</div>
<div><br></div>
<div>Thanks,</div>
<div>
<br clear="all"><div><div><div dir="ltr">Shishir Srivastava<br>&nbsp;<span></span><span></span><a href="tel:%2B44%20%280%29%20750%20127%205019" value="+447501275019" target="_blank">+44 (0) 750 127 5019</a><br><br>
</div></div></div>
</div>
</div></div>
김명신 | 20 Apr 01:33 2015
Picon

What's the mean of >>=

It could be stupid question because i'm very beginner of haskell.
 
I found strange literals when i read haskell code.
Does anybody explan what the mean of >>= below ocode ? And additionally, What is mean of >> itself?
 
putStrLn' [] = putChar '\n'
putStrLn' xs - putStr' xs >>= \ x -> putChar '\n'
 
putStr' [] = return ()
putStr' (x : xs) = putChar x >> putStr' n
 
(because of i18n problem, back slash literal could be shown as '\')
 
Thank you in advance
Myung Shin Kim
 
<div><div dir="ltr">It could be stupid question because i'm very beginner of haskell.<br>&nbsp;<br>I found strange literals when&nbsp;i read haskell code.<br>Does anybody explan what the mean of &gt;&gt;= below ocode ? And additionally, What is mean of &gt;&gt; itself?<br>&nbsp;<br>putStrLn' [] = putChar '\n'<br>putStrLn' xs - putStr' xs &gt;&gt;= \ x -&gt; putChar '\n'<br>&nbsp;<br>putStr' [] = return ()<br>putStr' (x : xs) = putChar x &gt;&gt; putStr' n<br>&nbsp;<br>(because of i18n problem, back slash literal could be shown as '\')<br>&nbsp;<br>Thank you in advance<br>Myung Shin Kim<br>&nbsp;<br>
</div></div>
Mike Houghton | 19 Apr 11:46 2015
Picon

IO and ghci

Hi,

I have this in a .hs file and it works fine. That is I see lines printed to the screen when the .hs file is loaded  and run from ghci.

main = do
file <- readFile "poem.txt"
mapM_ putStrLn (lines file)


Experimenting at the ghci command line with

let f = readFile “poem.txt”     
the above is fine.

But doing 

lines f 

gives 
<interactive>:52:7:
    Couldn't match type ‘IO String’ with ‘[Char]’
    Expected type: String
      Actual type: IO String
    In the first argument of ‘lines’, namely ‘f’
    In the expression: lines f

In my simplistic beginners way I think this is because when I run main = do …
everything is or is within an ‘impure’ area but at ghci command line it is not and I’m mixing pure/impure functions.

So my questions are 
1. Is this simplistic view any where near correct? 
2. How can I progress at the command line in ghci?


Many thanks

Mike




<div>Hi,<div class=""><br class=""></div>
<div class="">I have this in a .hs file and it works fine. That is I see lines printed to the screen when the .hs file is loaded &nbsp;and run from ghci.</div>
<div class=""><br class=""></div>
<div class="">main = do</div>
<div class="">
<span class="Apple-tab-span">	</span>file &lt;- readFile "poem.txt"</div>
<div class="">
<span class="Apple-tab-span">	</span>mapM_ putStrLn (lines file)</div>
<div class=""><br class=""></div>
<div class=""><br class=""></div>
<div class="">Experimenting at the ghci command line with</div>
<div class=""><br class=""></div>
<div class=""><div class="">let f = readFile &ldquo;poem.txt&rdquo; &nbsp; &nbsp;&nbsp;</div></div>
<div class="">the above is fine.</div>
<div class=""><br class=""></div>
<div class="">But doing&nbsp;</div>
<div class=""><br class=""></div>
<div class="">lines f&nbsp;</div>
<div class=""><br class=""></div>
<div class="">gives&nbsp;</div>
<div class="">
<div class="">&lt;interactive&gt;:52:7:</div>
<div class="">&nbsp; &nbsp; Couldn't match type &lsquo;IO String&rsquo; with &lsquo;[Char]&rsquo;</div>
<div class="">&nbsp; &nbsp; Expected type: String</div>
<div class="">&nbsp; &nbsp; &nbsp; Actual type: IO String</div>
<div class="">&nbsp; &nbsp; In the first argument of &lsquo;lines&rsquo;, namely &lsquo;f&rsquo;</div>
<div class="">&nbsp; &nbsp; In the expression: lines f</div>
<div class=""><br class=""></div>
<div class="">In my simplistic beginners way I think this is because when I run main = do &hellip;</div>
<div class="">everything is or is within an &lsquo;impure&rsquo; area but at ghci command line it is not and I&rsquo;m mixing pure/impure functions.</div>
<div class=""><br class=""></div>
<div class="">So my questions are&nbsp;</div>
<div class="">1. Is this simplistic view any where near correct?&nbsp;</div>
<div class="">2. How can I progress at the command line in ghci?</div>
<div class=""><br class=""></div>
<div class=""><br class=""></div>
<div class="">Many thanks</div>
<div class=""><br class=""></div>
<div class="">Mike</div>
<div class=""><br class=""></div>
<div class=""><br class=""></div>
<div class=""><br class=""></div>
<div class=""><br class=""></div>
</div>
</div>
Konstantin Saveljev | 18 Apr 11:04 2015
Picon

StateT MyState IO a vs ReaderT (IORef MyState) IO a

Hello,

I'm working on a project where I use StateT MyState IO a to keep track of the state. MyState is deeply nested and I use lens to help me more easily access and modify the state.

Just about yesterday I found someone mentioning about using ReaderT (IORef MyState) IO a instead of StateT MyState IO a because we are already in IO. 

Can someone explain to me what are the benefits of these different approaches? What about Garbage Collection in both cases (if there is any difference at all)? Can you use lens package to access and update the state which is hidden behind IORef ?

Thanks,
Konstantin Saveljev
<div><div dir="ltr">Hello,<div><br></div>
<div>I'm working on a project where I use StateT MyState IO a to keep track of the state. MyState is deeply nested and I use lens to help me more easily access and modify the state.</div>
<div><br></div>
<div>Just about yesterday I found someone mentioning about using ReaderT (IORef MyState) IO a instead of StateT MyState IO a because we are already in IO.&nbsp;</div>
<div><br></div>
<div>Can someone explain to me what are the benefits of these different approaches? What about Garbage Collection in both cases (if there is any difference at all)? Can you use lens package to access and update the state which is hidden behind IORef ?<br><div><br></div>
<div>Thanks,</div>
<div>Konstantin Saveljev</div>
</div>
</div></div>

Gmane