I have installed:
fay-0.19.0.1
fay-base-0.19
And I'm trying to compile
-- test.hs
module Main where
import FFI
main = ffi "alert('hello world');"
with
fay test.hs
And getting
fay: unable to resolve qualified names ffi
Note that I am able to compile things with fay-jquery, fay-text, etc. Full source files that compile and run (with fay-base) and do what I expect them to do when executing them. But as soon as I try to use ffi in any of then, I get this error.
fay-jquery, fay-dom, and other packages that use ffi seem to behave well when I import them.
I'm not sure what else to mention? I'm on ghc 7.6.3, running Ubuntu.
Help would be appreciated :)
Import Prelude and add a type signature to anything using FFI.
module Console (main) where
import Prelude
import FFI
main :: Fay ()
main = putStrLn (showInt (fib 10))
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
showInt :: Int -> String
showInt = ffi "%1+''"
Compiles.
Commenting out import Prelude returns error: src/console.hs:7:8: Not in scope: putStrLn' among others.
Commenting out showInt :: Int -> String returns error: fay: your FFI declaration needs a type signature: showInt = ffi "%1+''"
Fix those issues and give it another whirl.
I am told by our glorious leaders of the Fay Soviet that import Prelude is no longer necessary if you have the right stamps on your labor passport.
Related
Mind the following file:
import Common
main = print (length [1,2,3])
Common is a library that reorganizes functions from Prelude in order to export my favored versions (i.e., fold-based functions instead of list-based functions). This is giving name conflicts:
test.hs:3:15:
Ambiguous occurrence ‘length’
It could refer to either ‘Prelude.length’,
imported from ‘Prelude’ at test.hs:1:1
(and originally defined in ‘GHC.List’)
or ‘Common.length’, imported from ‘Common’ at test.hs:1:1-11
Since the idea is avoiding bureaucracy when creating new files, just using Import Prelude hiding ... wouldn't help here. Is there any way to tell GHC to favor Common.hs's definitions over Prelude's?
There's no way to give priority, but one can easily override individual names. For example, to override length:
module Common where
import Prelude hiding (length)
import qualified Data.List
length :: Num n => [a] -> n
length = Data.List.genericLength
You can check that things are going well in ghci:
% ghci -XNoImplicitPrelude test.hs
GHCi, version 7.10.1: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Common ( test.hs, interpreted )
Ok, modules loaded: Common.
*Common> :t length
length :: Num n => [a] -> n
*Common> :t (+)
(+) :: Num a => a -> a -> a
I am use org-mode blogging, I use org-babel to evaluate the code as following :
#+BEGIN_SRC haskell
import Data.Function (fix)
f :: Int -> Int
f = (+ 1)
main :: IO ()
main = do
putStrLn $ show $ f 1
#+END_SRC
#+RESULTS:
: <interactive>:9:25: Not in scope: ‘f’
I found the org-babel for haskell use infer-haskell mode to start session and eval the code. I also say the session was created, and if I don't define the function but directly putStrLn "hello" , it works.
hope anyone can fix the bug :)
In this article, Yoshinari Nomura describes a way to evaluate Haskell blocks using runhaskell via a Ruby script. I do not understand japanese, so I cannot translate the details, but the method has allowed me to run haskell blocks without having to write specifically for the interpreter.
#+BEGIN_SRC haskell
import Data.Function (fix)
f :: Int -> Int
let f = (+ 1)
main :: IO ()
main = do
putStrLn $ show $ f 1
#+END_SRC
#+RESULTS:
: 2
Org's babel mode is running the Haskell code with ghci. In ghci you are required to use let to declare functions.
I've just began using haskell.. I tried to run a sample script:
import Data.List
module main where
mylength = foldr (const (+1)) 0
main = print (mylength "haskell")
I get a simple error "modules loaded, none." I have zero idea why this is happening.
I'm using GHCi under win7 32bit (if that matters). Am I missing something here?
the example doesn't even include the "module main where" but even without that it fails
to run.
The module line always goes before imports. And the module name should start with a capital letter.
module Main where
import Data.List
mylength = foldr (const (+1)) 0
main = print (mylength "haskell")
If this doesn't work for you, please tell us
the full error message you get; and
which version of which compiler you're using
You don't need module line there.
When you do put it in, though, it must go before any import statements.
module main where -- Optional, in the case of main.
import Data.List
myLength :: [a] -> Int
myLength = foldr (const (+1)) 0
main = print (mylength "haskell")
Below is a Haskell/C FFI code that is throwing schedule error at runtime (GHC 7.0.3, Mac OS 10.7, x86_64). I searched for explanation of the error but didn't find anything relevant.
C Code (mt.c):
#include <pthread.h>
#include <stdio.h>
typedef void(*FunctionPtr)(int);
/* This is our thread function. It is like main(), but for a thread*/
void *threadFunc(void *arg)
{
FunctionPtr fn;
fn = (FunctionPtr) arg;
fn(1); //call haskell function with a CInt argument to see if it works
}
void create_threads(FunctionPtr* fp, int numThreads )
{
pthread_t pth[numThreads]; // array of pthreads
int t;
for (t=0; t < numThreads;){
pthread_create(&pth[t],NULL,threadFunc,*(fp + t));
t++;
}
printf("main waiting for all threads to terminate...\n");
for (t=0; t < numThreads;t++){
pthread_join(pth[t],NULL);
}
}
Haskell code (t.hs) - it calls create_threads in mt.c above with Storable Vector of FunPtr to Haskell function f (after applying first three arguments to f):
{-# LANGUAGE BangPatterns #-}
import Control.Concurrent (forkIO, threadDelay, MVar, newEmptyMVar, putMVar, takeMVar)
import qualified Data.Vector.Storable.Mutable as MSV
import qualified Data.Vector.Storable as SV
import Control.Monad.Primitive (PrimState)
import Control.Monad (mapM, forM_)
import Foreign.Ptr (Ptr, FunPtr)
import Foreign.C.Types (CInt)
type Length = CInt
-- | f is a function that is called back by create_threads in mt.c
f :: MVar Int -> MSV.MVector (PrimState IO) CInt -> Length -> CInt -> IO ()
f m v l x = do
!i <- takeMVar m
case (i< fromIntegral l) of
True -> MSV.unsafeWrite v i x >> print x >> putMVar m (i+1)
False -> return () -- overflow
-- a "wrapper" import gives us a converter for converting a Haskell function to a foreign function pointer
foreign import ccall "wrapper"
wrap :: (CInt -> IO()) -> IO (FunPtr (CInt -> IO()))
foreign import ccall safe "create_threads"
createThreads :: Ptr (FunPtr (CInt -> IO())) -> CInt -> IO()
main = do
let threads = [1..4]
m <- mapM (\x -> newEmptyMVar) $ threads
-- intialize mvars with 0
forM_ m $ \x -> putMVar x 0
let l = 10
-- intialize vectors of length 10 that will be filled by function f
v <- mapM (\x -> MSV.new l) threads
-- create a list of function pointers to partial function - the partial function is obtained by applying first three arguments to function f
lf <- mapM (\(x,y) -> wrap (f x y (fromIntegral l))) $ zip m v
-- convert above function list to a storable vector of function pointers
let fv = SV.fromList lf
-- call createThreads with storable vector of function pointers, and number of threads - createThreads will spawn threads which will use function pointers for callback
SV.unsafeWith fv $ \x -> createThreads x (fromIntegral $ length threads)
Please ignore unsafe parts in the code - my objective here is to test callback using Haskell FFI with multi-threaded C code. When I compile it, and run it, I get the error below:
$ ghc -O2 t.hs mt.c -lpthread
[1 of 1] Compiling Main ( t.hs, t.o )
Linking t ...
$ ./t
main waiting for all threads to terminate...
t: schedule: re-entered unsafely.
Perhaps a 'foreign import unsafe' should be 'safe'?
$ uname -a
Darwin desktop.local 11.2.0 Darwin Kernel Version 11.2.0: Tue Aug 9 20:54:00 PDT 2011; root:xnu-1699.24.8~1/RELEASE_X86_64 x86_64
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.0.3
The schedule error happens only if I have the C threads call back haskell function f. I guess it is more likely there is a bug in my code, than there is a bug in one of the libraries or GHC. So, I will like to check here first for pointers on cause of the error.
In this case, the schedule error happened because the haskell code was compiled without -threaded option.
The haskell code is calling C function create_threads which spawns multiple threads for threadFunc C function. threadFunc calls back into Haskell function f. So, even though Haskell code is compiled without -threaded option, it still results in multiple C threads executing f.
It was a good catch by GHC Runtime Scheduler to detect this oversight of executing multiple threads without threaded runtime, and mark it as error. That is much better than a cryptic run-time crash. I realized the oversight when I checked rts/schedule.c code in GHC code base, and saw the comment below. It tipped me off about threaded runtime not being enabled:
// Check whether we have re-entered the RTS from Haskell without
// going via suspendThread()/resumeThread (i.e. a 'safe' foreign
// call).
Hi i have the following code
import Data.Maybe
import Test.QuickCheck
import System.Random
rndExpr :: Gen Expr -> IO Expr
rndExpr gen = do
rnd <- newStdGen
return (generate 5 rnd gen)
But i get "not in scope "generate", why is this so?
Regards
Darren
Edit i am importing Test.QuickCheck but it still complaints about the "generate" is not in scope.
Edit 2
How would you write this function so that it would work with quickcheck version 2? I simple tried to put "unGen" where generate was with no succsess, i also installed quickcheck v 2 (cabal install QuickCheck-2.1.0.3)
I need a function with following properties stdGen->Gen Expr->Expr'
and unGen seem to give me that functionality, but as I said, my compiler cant find that function. Are there any other functions that I could use for this problem?
It seems like you are using generators from Test.QuickCheck, and generate is a function from version 1 of quickCheck. In version 2 of quickCheck things are a bit different so there is no such function. However, you atleast need to import Test.QuickCheck, and similar functionality can be gotten from unGen like this:
rundExpr gen = fmap (flip (unGen gen) 5) newStdGen
Please note that unGen is in Test.QuickCheck.Gen so you have to import that too.
generate isn't a function in System.Random. Perhaps you are looking for next?
EDIT:
Let me be clear: I don't know why you are using QuickCheck/Arbitrary for a task that Random/MonadRandom would seem to be more fitting. I'll assume you considered your options and move on.
Must you select your generator? Can't you use sample' :: Gen a -> IO a?
getVal :: IO a
getVal = sample' arbitrary
This should work for QC2.
OTOH, if you really want to use your own StdGen (or want to avoid IO) then try:
import System.Random
import Test.QuickCheck
import Test.QuickCheck.Gen
func :: StdGen -> Int
func g = unGen arbitrary g 0
This will use the StdGen named g and a count (0 here,) to generate your value. Because unGen doesn't step the generator, and the counter stepping doesn't give good randomness properties (it seems, you can try and see for yourself) you might end up wanting to wrap this with something that generates StdGens (yuck).
If you don't know what version package you are using then run:
$ ghc-pkg list | grep QuickCheck
(QuickCheck-2.1.1.1)
QuickCheck-1.2.0.1
In my setup (seen above) I have both 1 and 2, but 2 is hidden (the () means hidden) so when I use GHCi and import Test.QuickCheck it's version 1 that I get.