Named documentation chunk with Haddock - haskell

I'm trying to document my sources and I have a problem with named chunk.
I would like to document a large amount of patterns (more than 50), I would like to keep each explanation near the code and gather all the explanations in the header
I tried the following :
module Haddock
(
-- A test for Haddock named chunk
--
-- $doc1
--
-- $doc2
--
-- $doc3
)
where
-- $doc1
-- First serie of explanations
myFunction 0 = 1
myFunction 1 = 1
-- $doc2
-- Second serie of explanations
myFunction 2 = 3
-- $doc3
-- Some examples
--
-- > myFunction a
--
myFunction a = a * a
but only the first chunk is shown :
Is there something wrong with my Haddock markup or is it a bug ?

Related

How to compile QuasiQuoter during runtime?

I have a 'QuasiQuoter' which is useful in source code in Haskell, but also as a standalone application. So, I need to be able to run QuasiQuoter
During the compile time in Haskell - [myGrammar|someCommand|]
In runtime (runtime compilation) in shell - mygrammar 'someCommand'
The first part is easy but the second part might be a little clumsy if solved as calling the compiler with some generated code from the runtime.
I would like to solve a second part of the problem using some nice method in Haskell which doesn't accept only the source code, but accepts QuasyQuoter datatype instead so the code is less clumsy. But I can't find any compilation method like that.
Do you know any? Thanks.
Example of usage
Haskell
The function takes tuple [(a,b,c,d,e)] and returns a list of the strings with the products.
function = [lsql| {1..5}, r=[ a.* |> (*) ], "Product of a.1 * a.2 * ... * a.5 is &a.r"|]
Bash
The command reads from stdin csv with at least 5 numerical columns and returns a list of their products (one per line).
lsql-csv '-, r=[ a.* |> (*) ], "Product of a.1 * a.2 * ... * a.5 is &a.r"'
I think the question is how to parse and process a string in a uniform way between a quasiquoter and some other chunk of code. If this interpretation is right, then you just... do that. For example:
-- implementation of these is left to the reader, but can use standard Haskell
-- programming techniques and libraries, like parsec and ADTs and stuff
command :: Parser Command
interpret :: Command -> IO ()
jit :: Command -> Exp -- or Q Exp
Then, in your lsql-csv.hs, you would write something like
main = do
[s] <- getArgs
case parse command s of
Left err -> die (show err)
Right com -> interpret com
and in your LSql/CSV/QQ.hs, you would write something like
lsql = QuasiQuoter { quoteExp = \s -> case parse command s of
Left err -> qReport True (show err) >> fail ""
Right com -> return (jit com) -- or just jit com if that's already a Q Exp
}

Haskell circular definition interrupt

Hello everyone i came over a curious thing trying to learn circular definition in Haskell. We have these two cases:
module Main
where
mgood = 1:head (tail (tail mgood)):mgood
list = take 10 mgood
main = do
print list
For the code above i get this output [1,1,1,1,1,1,1,1,1,1] which i understand why but the proble is in this code:
module Main
where
mbad = 1:head (tail (tail (tail mbad))):mbad
list = take 10 mbad
main = do
print list
Where i get this output [1, {Interrupted!}. Can someone explain me why this happens and what's the difference between the 2 cases?
Both of your definitions fit the pattern result = foo : bar : result. So what we end up with, in both cases, is a circular list that looks like this:
foo : bar : foo : bar : foo : bar : ...
In both cases foo is 1, so it doesn't depend on anything else. But bar does.
In mgood you define bar to be equal to the third element of the list, so bar is equal to foo, which is 1. So both foo and bar are 1 and that's what you get: an infinite list of 1s.
In mbad you define bar to be equal to the fourth element. So bar is equal to itself. This infinitely recursive definition of bar leads to an infinite loop when trying to evaluate bar.

Lua timing thread

How do I write a thread, or a co-routine (correct my nomenclature) to execute one function in Lua while the mainline code looks at its results and makes decisions ?
What I want is a function, something like
-----------------------------------------------------------
-- Countdown_Nine_Seconds() --
-- --
-- On Entry: Nothing --
-- --
-- Returns: Nothing --
-- --
-- Action: Decrements these two counters... --
-- --
-- Full_Wait --
-- Tenth_Wait --
-----------------------------------------------------------
function Countdown_Ten_Seconds()
Full_Wait = 9 -- This will be a global
local i
local j
for i = 9, 0, -1
do -- This is the I loop start
Tenth_Wait = 10 -- ten tenths in a second
for j = 10, 0, -1
do -- This is the J loop start
box.wait(100) -- our implementation has this; wait 0.1 seconds
Tenth_Wait = Tenth_Wait - 1 -- Tell the rest of the world
end -- end of inner J-Loop
Full_Wait = Full_Wait - 1 -- One less second
end -- end of ouer I-Loop
end -- end of this complete function
I would then like to have a main line code do something like
While(Full_Wait > 0)
do
:
:
:
(my stuff here)
:
:
:
:
end
What's the syntax ? Where do I read about this ?
What else am I missing ?
lua co-routines are collaboratively threaded. They need to explicitly yield control to one another. They don't run at the same time.
If you need preemptive threading you need one of the various lua threading libraries.

Lua, Advancing to the next letter of the alphabet

I am very surprised that I did not find this explicitly answered here in StackOverflow, or on the Lua.org Website
If
My Lua variable contains a single letter, and
I want the next letter in the alphabet
Then, how do I manipulate that variable to change from, say, "J" to "K" ?
I looked at The String Library in the Lua.Org website, and didn't see the word "alphabet" on any of the pages there
e.g.
--------------------------------------------------
-- --
-- Func Name: Alphabetic_Advance_By_1 --
-- --
-- On Entry: The_Letter contains one --
-- character. It must be a --
-- letter in the alphabet. --
-- --
-- On Exit: The caller receives the --
-- next letter --
-- --
-- e.g., --
-- --
-- A will return B --
-- B will return C --
-- C will return D --
-- --
-- X will return Y --
-- Y will return Z --
-- Z will return A --
-- --
-- --
-- --
--------------------------------------------------
function Alphabetic_Advance_By_1(The_Letter)
local Temp_Letter
Temp_Letter = string.upper(The_Letter)
-- okay, what goes here ???
return(The_Answer)
end
I am very surprised that I did not find this explicitly answered here in StackOverflow, or on the Lua.org Website
It has to do with the character encodings used in your particular build of Lua, so it's beyond the scope of the Lua language proper.
You can use string.byte to get the byte(s) of a string. You can use string.char to turn bytes into a string.
Lua doesn't guarantee that the character codes for 'A' through 'Z' are contiguous, because C doesn't. You can't even be sure that each is encoded with a single byte. If your implementation is using ASCII then each character is represented by a single byte value and you can add 1 get the next letter, but you shouldn't rely on this. For example, if Temp_Letter < 'Z':
The_Answer = string.char(Temp_Letter:byte() + 1)
Here's a way to do this without relying on character encoding:
local alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
local index = alphabet:find(Temp_Letter)
if index then
index = (index % #alphabet) + 1 -- move to next letter, wrapping at end
TheAnswer = alphabet:sub(index, index)
end

How to test my haskell functions

I just started with Haskell and tried to do write some tests first. Basically, I want to define some function and than call this function to check the behavior.
add :: Integer -> Integer -> Integer
add a b = a+b
-- Test my function
add 2 3
If I load that little script in Hugs98, I get the following error:
Syntax error in declaration (unexpected `}', possibly due to bad layout)
If I remove the last line, load the script and then type in "add 2 3" in the hugs interpreter, it works just fine.
So the question is: How can I put calls of my functions in the same script as the function definition? I just want to load the script and be able to check if it does what I expect it to...I don't want to type them in manually all the time.
Others have said how to solve your immediate problem, but for testing you should be using QuickCheck or some other automated testing library.
import Test.QuickCheck
prop_5 = add 2 3 == 5
prop_leftIdentity n = add 0 n == n
Then run quickCheck prop_5 and quickCheck prop_leftIdentity in your Hugs session. QuickCheck can do a lot more than this, but that will get you started.
(Here's a QuickCheck tutorial but it's out of date. Anyone know of one that covers QuickCheck 2?)
the most beginner friendly way is probably the doctest module.
Download it with "cabal install doctest", then put your code into a file "Add.hs" and run "doctest Add.hs" from the command line.
Your code should look like this, the formatting is important:
module Add where
-- | add adds two numbers
--
-- >>> add 2 3
-- 5
-- >>> add 5 0
-- 5
-- >>> add 0 0
-- 0
add :: Integer -> Integer -> Integer
add a b = a+b
HTH Chris
Make a top level definition:
add :: Integer -> Integer -> Integer
add a b = a + b
test1 = add 2 3
Then call test1 in your Hugs session.
How can I put calls of my functions in the same script as the function definition? I just want to load the script and be able to check if it does what I expect it to...I don't want to type them in manually all the time.
In short, you can't. Wrap it in a function and call it instead. Your file serves as a valid Haskell module, and having "flying" expression is not a valid way to write it.
You seem to come from a scripting language background, but don't try treating Haskell as one of them.
If you have ghc installed, then the runhaskell command will interpret and run the main function in your file.
add x y = x + y
main = print $ add 2 3
Then on the command line
> runhaskell Add.hs
5
Not sure, but hugs probably has a similar feature to the runhaskell command. Or, if you load the file into the hugs interpreter, you can simply run it by calling main.
I was trying to do the same thing and I just made a function that ran through all my test cases (using guards) and returned 1 if they all passed and threw an error if any failed.
test :: Num b => a->b
test x
| sumALL [1] /= 1 = error "test failed"
| sumALL [0,1,2] /= 3 = error "test failed"
...
| otherwise = 1

Resources