How can I unit test Alex code? - unit-testing

I'm writing a lexer in Alex with the monad wrapper. It's not behaving as I expect, and I would like to write some unit tests for it. I can write unit tests for lexing a single token by doing:
runAlex "foo" alexMonadScan `shouldBe` Right TokenFoo
but I don't know how to test that the string "foo bar" gets lexed to [TokenFoo, TokenBar].
Given that Token is my token type, I'd need a function like runAlex that has the type String -> Alex [Token] -> Either String [Token], but I don't know how to transform alexMonadScan so that it has the type Alex [Token] rather than Alex Token.
I tried
runAlex "foo bar" (liftM (:[]) alexMonadScan) `shouldBe` [TokenFoo, TokenBar]
which seems to have the right type, but it returns Right [TokenEOF], apparently dropping the tokens it saw along the way.
How can I achieve this?

There is a function alexScanTokens :: String -> [token] which you can use.
It's defined in the file templates/wrappers.hs
Here's a monadic version I found here:
alexScanTokens :: String -> Either String [Keyword]
alexScanTokens inp = runAlex inp gather
where
gather = do
t <- alexMonadScan
case trace (show t) t of
EOF -> return [EOF]
_ -> (t:) `liftM` gather

Related

Haskell - Saving string input into a list

I'm a Haskell beginner and I am doing a small file for a project that should take the input of interaction data for groups of two people and save it to a list to be output at the end. I have done my best to implement this but it seems like the program hits the "stop" case no matter what is input. Any help or advice would be appreciated.
import Data.List
import Text.Read
main :: IO ()
main = do
putStrLn "This program is a means to record interactions between individuals during the COVID-19 pandemic."
putStrLn "Please enter your interactions in this format: 'x interacted with y'"
inputs <- getUserInputs
putStr "input: "
putStrLn ("list sequence " ++ show (inputs))
parseInput :: String -> Maybe String
parseInput input = if input == "stop" then Nothing else (readMaybe input):: Maybe String
getUserInputs :: IO [String]
getUserInputs = do
input <- getLine
case parseInput input of
Nothing -> return []
Just aString -> do
moreinputs <- getUserInputs
return (aString : moreinputs)
Show and Read are intended to produce and consume the representation of a value as a Haskell expression. That’s why when you call show on a String, it produces a quoted string:
> show "beans"
"\"beans\""
Therefore Read expects the string to be quoted as well, so readMaybe is always returning Nothing in your code because you’re not supplying quotes:
> readMaybe "beans" :: Maybe String
Nothing
> readMaybe "\"beans\"" :: Maybe String
Just "beans"
Therefore the fix is simple: remove the call to readMaybe and just return the string directly:
parseInput1 :: String -> Maybe String
parseInput1 input = if input == "stop"
then Nothing
else Just input
Which, as a matter of style preference, you could also write with guards, pattern matching, or the Maybe monad instead of if:
parseInput2 input
| input == "stop" = Nothing
| otherwise = Just input
parseInput3 "stop" = Nothing
parseInput3 input = Just input
import Control.Monad (guard)
parseInput4 input = do
-- ‘guard’ returns ‘Nothing’,
-- short-circuiting the ‘do’ block,
-- if its condition is ‘False’.
guard (input /= "stop")
pure input
Read and Show are fine for simple programs, particularly when you’re learning Haskell, but in larger applications it’s helpful to use them mostly for debug input and output and reading input you’ve already validated. Parsing and pretty-printing libraries are preferable for more involved parsing and producing human-readable output, respectively; megaparsec and prettyprinter are good default choices in that area.

OCaml special characters formatting?

I have the following ocaml code:
let rec c_write =
"printf(\" %d \");\n"
On calling this function in the interpreter, I expect to get the output
printf("%d"); followed by a new line, but instead I get
printf(\" %d \");\n
How can I get my expected output when I'm calling the function without using any other I/O functions?
The expression let rec c_write = "printf(\" %d \");\n" is not a function. It is a value of type string which is bound to a variable named c_write. So you're not using any I/O functions in your code.
When entered in the interactive toplevel, this value is printed by the interpreter evaluation loop for user convenience. The same as when a Python interpreter will print for you the value that you've just entered.
The representation, chosen by the OCaml toplevel interpreter, in general, has nothing to do with the representation which is used to store a value in a file or to print it. Moreover, in OCaml, there is no canonical representations.
If you want to write a function that prints a C printf statement then this is how it will look like in OCaml
let print_printf () =
print_endline {|printf("%d");|}
In the example above, I've used {||} to denote a sting literal instead of more common "", since in this literal there is no need to escape special characters and they are interpreted literally (i.e., the don't have any special meaning).
You can achieve the same result using the regular "" quotes for denoting it
let print_printf () =
print_endline "printf(\"%d\");"
Here is an example of the toplevel interaction using these definitions:
# let print_printf () =
print_endline {|printf("%d");|};;
val print_printf : unit -> unit = <fun>
# print_printf ();;
printf("%d");
- : unit = ()
# let print_printf () =
print_endline "printf(\"%d\");";;
val print_printf : unit -> unit = <fun>
# print_printf ();;
printf("%d");
- : unit = ()
If you will put this code in a file, compile, and execute and redirect into a C file it will be a well-formed C file (modulo the absence of the function body).
Since you are somehow using the toplevel printer for printing, and that you somehow needs a very specific format, you need to install a custom printer.
The following would work:
# #install_printer Format.pp_print_string;;
# " This \" is not escaped " ;;
- : string = This " is not escaped
However, it seems very likely that this is not really the problem that you are trying to solve.

OCaml |> operator

Could someone explain what the |> operator does? This code was taken from the reference here:
let m = PairsMap.(empty |> add (0,1) "hello" |> add (1,0) "world")
I can see what it does, but I wouldn't know how to apply the |> operator otherwise.
For that matter, I have no idea what the Module.() syntax is doing either. An explanation on that would be nice too.
Module.(e) is equivalent to let open Module in e. It is a shorthand syntax to introduce things in scope.
The operator |> is defined in module Pervasives as let (|>) x f = f x. (In fact, it is defined as an external primitive, easier to compile. This is unimportant here.) It is the reverse application function, that makes it easier to chain successive calls. Without it, you would need to write
let m = PairsMap.(add (1,0) "world" (add (0,1) "hello" empty))
that requires more parentheses.
The |> operator looks like the | in bash.
The basic idea is that
e |> f = f e
It is a way to write your applications in the order of execution.
As an exemple you could use it (I don't particularly think you should though) to avoid lets:
12 |> fun x -> e
instead of
let x = 12 in e
For the Module.() thing, it is to use a specific function of a given module.
You probably have seen List.map before.
You could of course use open List and then only refer to the function with map. But if you also open Array afterwards, map is now referring to Array.map so you need to use List.map.
The |> operator represents reverse function application. It sounds complicated but it just means you can put the function (and maybe a few extra parameters) after the value you want to apply it to. This lets you build up something that looks like a Unix pipeline:
# let ( |> ) x f = f x;;
val ( |> ) : 'a -> ('a -> 'b) -> 'b = <fun>
# 0.0 |> sin |> exp;;
- : float = 1.
The notation Module.(expr) is used to open the module temporarily for the one expression. In other words, you can use names from the module directly in the expression, without having to prefix the module name.

OCaml - Creating a function which prompts for floats and returns a list of floats

I'm teaching myself OCaml and I sometimes need to create a function where I'm not really sure what the proper solution should be. Here's one that I'm a little confused about.
I need a function that will prompt the user for individual float values and return everything entered in a float list. I can create this function but I'm not sure if its the proper/best way to do it in Ocaml.
Here's my attempt.
let rec get_floats() =
match
(
try Some(read_float())
with
| float_of_string -> None
)
with
| None -> []
| Some s -> s :: get_floats();;
This code works buts I'm at a loss deciding if its a 'proper OCaml' solution. Note, to exit the function and return the float list just enter a non-integer value.
(I hope that) this is a simple peephole rewrite involving no thought whatsoever of the function in your question:
let rec get_floats() =
try
let f = read_float() in (* as suggested by Martin Jambon *)
f :: (get_floats())
with
| float_of_string -> []
The idea I tried to apply here is that you do not need to convert the success/failure of read_float into an option that you immediately match: just do what you have to do with the value read, and let the with handle the failure case.
Now that I think of it, I should point out that in both your question and my rewrite, float_of_string is a fresh variable. If you meant to match a specific exception, you failed at it: all exception constructors, like datatype constructors, are Capitalized. You might as well have written with _ -> instead of with float_of_string ->, and a recent version of OCaml with all warnings active should tell you that your function (or mine) binds a variable float_of_string without ever using it.
Thanks everyone for the help. This works.
let rec get_floats() =
try
let x = read_float() in
x :: get_floats()
with
| _ -> [];;
List.iter (fun x -> print_endline(string_of_float x)) (get_floats());;

Adding a Show instance to RWH's RandomState example

I have just typed in the RandomState example from real world haskell. It looks like this:
import System.Random
import Control.Monad.State
type RandomState a = State StdGen a
getRandom :: Random a => RandomState a
getRandom =
get >>= \gen ->
let (val, gen') = random gen in
put gen' >>
return val
getTwoRandoms :: Random a => RandomState (a, a)
getTwoRandoms = liftM2 (,) getRandom getRandom
It works, but the result doesn't get displayed. I get the error message:
No instance for (Show (RandomState (Int, Int)))
arising from a use of `print' at <interactive>:1:0-38
Possible fix:
add an instance declaration for (Show (RandomState (Int, Int)))
In a stmt of a 'do' expression: print it
I am having some trouble adding an instance for Show RandomState. Can anyone show me how this is done?
Thanks.
For the sake of being explicit, as jberryman and the comments on the question imply: Something of type RandomState (a, a) is a function, not a value. To do anything with it, you want to run it with an initial state.
I'm guessing you want something like this:
> fmap (runState getTwoRandoms) getStdGen
((809219598,1361755735),767966517 1872071452)
This is essentially what the runTwoRandoms function a bit further in RWH is doing.
Since RandomState is a synonym for State and there isn't an instance of show defined for State, you won't be able to show it.
You would also not be able to derive show because State is just a wrapper for a function and Haskell has no way to define a show for functions that would be useful:
Prelude> show (+)
<interactive>:1:0:
No instance for (Show (a -> a -> a))
arising from a use of `show' at <interactive>:1:0-7
Possible fix: add an instance declaration for (Show (a -> a -> a))
In the expression: show (+)
In the definition of `it': it = show (+)
EDIT: Forgot to add the other piece: GHCi is giving you that error because it uses show behind the scenes on the expressions you enter... REPL and all that.