Importing documentCreateElement function from Webkit - haskell

I am trying to import documentGetElementById function for use in Reflex FRP. I tried the import below but can't find the function (which according to hackage should be there):
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( Test.hs, interpreted )
Ok, modules loaded: Main.
*Main> import Graphics.UI.Gtk.WebKit.DOM.Document
*Main Graphics.UI.Gtk.WebKit.DOM.Document> :t documentGetElementById
<interactive>:1:1: error:
Variable not in scope: documentGetElementById
I will appreciate help with resolving this. I am running latest Reflex platform build with ghc 8.0.1 on mac where webkit works now. I see the same problem on Linux as well. So, it doesn't seem to be platform-specific.
I also did github code search on ghcjs-dom but documentGetElementById doesn't show up. So, perhaps it has been moved somewhere else?

documentGetElementById has been renamed to getElementById after ghcjs-dom refactoring. So, an import like below would work:
*Main> import Graphics.UI.Gtk.WebKit.DOM.Document
*Main Graphics.UI.Gtk.WebKit.DOM.Document> :t getElementById
getElementById
:: (DocumentClass self, System.Glib.UTFString.GlibString string,
MonadIO m) =>
self -> string -> m (Maybe Graphics.UI.Gtk.WebKit.Types.Element)

Related

ghci vs prelude Haskell

I have recently started learning Haskell and had some questions about ghci> prompt vs prelude> prompt?
When typing ghci i get this:
ghci
GHCi, version 9.2.4: https://www.haskell.org/ghc/ :? for help
ghci>
when checking documentation I see things like this:
$ ghci
GHCi, version 6.12.3: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude>
also in addition to this I do not get into a module once I've loaded it
ghci> :load main
[1 of 1] Compiling Main ( main.hs, interpreted )
Ok, one module loaded.
ghci>
should look something like this?
ghci> :load main
[1 of 1] Compiling Main ( main.hs, interpreted )
Ok, one module loaded.
*Main>
I have tried looking online, tried compiling the program
GHCi is less verbose since version 9.0. This is because after a while the list of modules became very long, making the shell less effective.
Therefore since version 9.0, it keeps showing the ghci> prompt by default. You can set the prompt with %s> [ghc-doc] to show the loaded modules instead:
ghci> :set prompt "%s> "
Prelude> import Data.List
Prelude Data.List>
This was changed in GHC 9.0.1. Earlier versions showed the modules that were loaded, but now it just shows ghci>.
See the release notes for GHC 9.0.1:
2.1.2.3. GHCi
GHCi prompt no longer lists loaded modules. The previous behavior can be restored with :set prompt "%s> " and :set prompt-cont "%s| ".

Haskell deducing constraints works in cabal package, but not when importing from package

I wrote a cabal package with a number of working examples. However, when I copy one of these examples out of the Examples directory and try to run it, I get the error below:
$ cabal sandbox init
$ cabal add-source deckbuild/
$ cabal install deckbuild/
$ cabal repl
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
λ> :l GreedyInference.hs
[1 of 1] Compiling Main ( GreedyInference.hs, interpreted )
GreedyInference.hs:93:27:
Could not deduce (mtl-2.1.3.1:Control.Monad.State.Class.MonadState
Game (StateT Game m))
arising from a use of ‘runGame’
from the context (MonadState Game m, MonadIO m)
bound by the type signature for
runGreedy :: (MonadState Game m, MonadIO m) =>
(Double, Double) -> m Game
at GreedyInference.hs:92:14-94
In the first argument of ‘execStateT’, namely ‘runGame’
In the expression: execStateT runGame
In the expression: execStateT runGame $ greedyGame ps
GreedyInference.hs:107:29:
No instance for (MonadState Game IO)
arising from a use of ‘runGreedy’
In the second argument of ‘($)’, namely
‘runGreedy (param0, param1)’
In the expression: unsafePerformIO $ runGreedy (param0, param1)
In an equation for ‘g’:
g = unsafePerformIO $ runGreedy (param0, param1)
Failed, modules loaded: none.
Since the type signatures work in the cabal package, I have the feeling I'm missing an import or I need to tweak the type signatures slightly when using the code from outside the package. Any ideas? I'm just looking for a push in the right direction and I should be able to figure out the specifics.
The working (when loaded with cabal repl from inside the package) module can be found here: https://github.com/cronburg/deckbuild/blob/master/Examples/GreedyInference.hs
EDIT: I think it has something to do with Lazy vs Class in Control.Monad.State. The deduce wants Class for some reason, but hackage says Lazy is the default. Which one should I be using and why?
I ended up just making a file at Game/Monad.hs containing:
module Game.Monad (execStateT, MonadState, MonadIO) where
import Control.Monad.State
Then replaced my import Control.State.Monad with import Game.Monad in GreedyInference.hs. The file then compiled without the error.
So I think #Rufflewind in the comments was on the right track - the mtl types imported in my package weren't matching the mtl types imported by the standalone GreedyInference.hs. I'm still not sure why though since the versions matched and all the pertinent imports are import Control.Monad.State.

Attoparsec `many` method not found

I tried running the tests for Bryan O'Sullivan's Attoparsec-based HTTP parser (http://www.serpentine.com/blog/2010/03/03/whats-in-a-parser-attoparsec-rewired-2/), and I got this error:
> runhaskell TestRFC2616.hs
TestRFC2616.hs:13:30:
Not in scope: `many'
Perhaps you meant one of these:
`any' (imported from Prelude),
`B.any' (imported from Data.ByteString.Char8),
many' (imported from Data.Attoparsec)
Surprised, I ran ghci and got this:
> ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :m +Data.Attoparsec
Prelude Data.Attoparsec> :t many
<interactive>:1:1:
Not in scope: `many'
Perhaps you meant one of these:
`any' (imported from Prelude),
many' (imported from Data.Attoparsec),
`many1' (imported from Data.Attoparsec)
Prelude Data.Attoparsec>
Can anyone tell me what's going on?
This example is 4 years old. In version 0.8.0 there was a implementation of many in the Data.Attoparsec.Combinator module, you can check the source here.
The current version of the library doesn't implements a many function, but it implements a many' function (source here). That's why you ghci give you many' as a suggestion.
The many that was implemented in Data.Attoparsec.Combinator is the same that is implemented in Control.Applicative (see here the implementation of many in the Alternative type class). You probably need to import the Control.Applicative. If that works I would suggest that you make a pull request to solve that issue (the library repository is here)

A ghci session without Prelude

This question arose on #haskell irc chat:
How can I start ghci without importing prelude?
The possible answer seemed obvious:
ghci -XNoImplicitPrelude, or load a file with import Prelude ()
The latter seems to work, while the former strangely does not. However, import Prelude () imports the declared instances from Prelude, right? Is there a better way of creating a ghci session without loading Prelude at all?
% ghci
GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :m -Prelude
> :i map
Top level: Not in scope: `map'
> :i Eq
Top level: Not in scope: data constructor `Eq'
However, I'm not sure about the instances and how ghci deals with them.
Is there a particular instance that you're concerned about?
The accepted answer doesn't seem to work anymore. This does work in ghci 8.0.2.
Prelude> :set -XNoImplicitPrelude
Prelude> :m -Prelude
>

How can I import a Haskell module in GHCi?

I am trying to teach myself Haskell from the book Learn You A Haskell for Great Good. I got up to the last section of chapter 7 (Modules), where it tells how to create your own module. I did a copy and paste of the Geometry module given in the book at the beginning of the section. The name of the file is Geometry.hs, as the book suggested, and the file is in the bin directory for ghci, which is where I previously was able to successfully do a load using :l for another .hs file.
When I type the following command in GHCi
import Geometry
I get the following error:
Could not find module 'Geometry' It is not a module in the current
program or in any known package
I must be doing something that is obviously wrong, but I can't figure out what it is.
When you use import ModuleName in GHCi, it works (mostly) in the same way import Data.List works: GHC checks your local package database for the module, loads it, and brings its (exported) contents into scope.
However, Geometry isn't a module of a package installed with ghc-pkg. Therefore, GHC doesn't know that a module Geometry exists at all. Neither does it interactive variant GHCi.
But if you :load a program, things change. GHC will take its used modules into account:
-- Foo.hs
module Foo where
foo :: IO ()
foo = putStrLn "Hello from foo!"
-- Main.hs
module Main where
import Foo (foo)
main :: IO ()
main = foo
$ cd /path/to/your/files
$ ghci
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> import Foo
<no location info>:
Could not find module ‘Foo’
It is not a module in the current program, or in any known package.
Prelude> :l Main.hs
[1 of 2] Compiling Foo ( Foo.hs, interpreted )
[2 of 2] Compiling Main ( Main.hs, interpreted )
Ok, modules loaded: Main, Foo.
*Main> :l Main.hs
*Main> foo
Hello from foo!
*Main> import Foo
*Main Foo> -- module now loaded
As you can see, importing Foo first failed. However, after we've actually loaded the program that uses Foo, we were able to use import Foo in GHCi.
So if you want to use import in GHCi, make sure that GHC can find your module, either by including it in a wrapper or installing it. If you just want to load the module itself, use :load.
TLDR: the Learn you a Haskell book fails to mention that you have to :load the Geometry.hs file first. Then :m to go back to Prelude and then import Geometry works.
It is now also possible to add the lib flag when installing packages, i.e. to run cabal install --lib packagename and then to import the corresponding package directly in GHCi. In the present case, for example cabal install --lib hgeometry would facilitate importing modules from this geometry package.

Resources