My program has a bug I want to see within GHCi:
$ ./my-program < ./my-data
Prelude.foldl1: empty list
I tried changing stdin or getLine in GHCi but it doesn't seem to affect the getLine my program uses, even if I load afterwards:
$ ghci
Prelude> import System.IO
Prelude System.IO> getLine <- fmap hGetLine $ openFile "my-data" ReadMode
:l "my-program.hs"
:main
Do I need to rewrite all my IOs to take an explicit handle to be able to test them in GHCi?
You can try wrapping your program in something like this (tested, working code):
import qualified System.IO
import qualified GHC.IO.Handle
filename = "/tmp/myfilename"
main = do
h <- System.IO.openFile filename System.IO.ReadMode
old_stdin <- GHC.IO.Handle.hDuplicate System.IO.stdin
GHC.IO.Handle.hDuplicateTo h System.IO.stdin
System.IO.hClose h
realMain
GHC.IO.Handle.hDuplicateTo old_stdin System.IO.stdin
realMain = ...
It should also be possible to define a user-defined GHCi command that does this for any GHCi command, but I haven't tried yet. See here for someone's .ghci that redirects command's stdout with a user-defined :redir command.
I think you want:
ghci> :set args YOUR_ARG
ghci> main
Or
ghci> :main YOUR_ARG
See here: How to set a program's command line arguments for GHCi?
Related
I am aware of this thread and the agreed-upon ghci :browse command, but I am looking for something similar to run from a script.hs file:
Say I have a module that I can import into my script.hs. How do I then view the list of functions I have just gained access to?
What I've settled on for now
Adapting this thread that suggests the now-deprecated ghc-mod command-line program, I am
calling the terminal command ghc -e ':browse <module, e.g. Data.List>'
from my script.hs using Shelly.
My full script:
#!/usr/bin/env runghc
{-# LANGUAGE OverloadedStrings #-}
import Safe (headDef)
import Shelly
import System.Environment (getArgs)
import qualified Data.Text as T
mdl :: IO String
mdl = getArgs >>= return . headDef "Data.List"
runShelly :: String -> IO ()
runShelly mdl = shelly $ silently $ do
out <- run "ghc" ["-e", T.pack (":browse " ++ mdl)]
let lns = T.lines out
liftIO $ mapM_ (putStrLn .T.unpack) $ lns
main :: IO ()
main = mdl >>= runShelly
This way I can pass the module name on the command line as <script> <module> and get back the functions, one per line. It defaults to Data.List if I pass no arguments.
So that's a solution, but surely there must be handier introspection facilities than this?
I'm on Ubuntu. When I run ghci on Terminal and do this:
Prelude Control.Monad System.IO> forever $ getChar >>= print
The result is something like this:
a'a'
b'b'
C'C'
%'%'
\'\\'
1'1'
''\''
"'"'
^X'\CAN'
^?'\DEL'
^CInterrupted.
That is, the characters I type in my keyboard are being flushed into output. How can I prevent this and have only print as the writer?
To prevent input being flushed into the output (or "echoed"), use hSetEcho stdin False.
Prelude> import System.IO
Prelude System.IO> import Control.Monad
Prelude System.IO Control.Monad> hSetEcho stdin False
Prelude System.IO Control.Monad> forever $ getChar >>= print
'a'
'\n'
'b'
'c'
This can be used to do things like read in a password.
Here I have text file, I want to load it in Haskell, and read it in a List.
import qualified System.Environment
main :: IO ()
main = do
[path] <- System.Environment.getArgs
g <- readFile path
putStr g
But I don't know how to give the address. (Example: the file is /Users/Documents/Programming/test.txt)
After I load this Haskell file:
*Main> main -- How can I write the address?
Under :help, you can see documentation for the :main command:
:main [<arguments> ...] run the main function with the given arguments
Example:
ghci> :main /Users/Documents/Programming/test.txt
...
contents of test.txt
...
How can I change this program to immediately process every line of text in case of interactive input? Preferably flush buffer every newline character.
main = do
input <- T.getContents
mapM_ T.putStrLn $ T.lines input
Update: Something is still missing. Take a look (???? is after newline, stdout is printed out after reaching EOF on stdin) :
> cat Test.hs
import System.IO
import Data.Text as T
import Data.Text.IO as T
main = do
hSetBuffering stdout LineBuffering
input <- T.getContents
mapM_ T.putStrLn $ T.lines input
> runhaskell Test.hs
a
????
a
????
> runhaskell --version
runghc 7.6.3
>
You want to use hSetBuffering from System.IO:
import System.IO
main = do
hSetBuffering stdout LineBuffering
input <- T.getContents
mapM_ T.putStrLn $ T.lines input
It seems like you want to use lazy input to interleave reading lines and handling them.
getContents from Data.Text.IO is not lazy, and will read everything before returning anything at all.
Import the version from Data.Text.Lazy.IO instead.
import Data.Char
main = do
c <- getChar
if not $ isUpper c
then do putChar $ toUpper c
main
else putChar '\n'
Loading and executing in GHCi:
λ> :l foo.hs
Ok, modules loaded: Main.
λ> main
ñÑsSjJ44aAtTR
λ>
This consumes one character at time.
But in a terminal:
[~ %]> runhaskell foo.hs
utar,hkñm-Rjaer
UTAR,HKÑM-
[~ %]>
it consumes one line at time.
Why does it behave differently?
When you run a program in terminal it uses LineBuffering by default, but in ghci it is set to NoBuffering. You can read about it here. You will have to remove buffering from stdin and stdout to get the similar behavior.
import Data.Char
import System.IO
main = do
hSetBuffering stdin NoBuffering
hSetBuffering stdout NoBuffering
foo
foo = do
c <- getChar
if not $ isUpper c
then do putChar $ toUpper c
foo
else putChar '\n'