Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: How to construct complex string (martin)
2. Re: How to construct complex string (Stephen Tetley)
3. Re: maybe in IO (Mike Ledger)
4. alternative sourcegraph (Kees Bleijenberg)
5. Re: alternative sourcegraph (Brent Yorgey)
----------------------------------------------------------------------
Message: 1
Date: Thu, 08 Aug 2013 22:38:25 +0200
From: martin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to construct complex string
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Am 08/06/2013 07:01 PM, schrieb Stephen Tetley:
> Hi Martin
>
> If you are building syntax as a sequence of commands (or statements) a
> common idiom is to collect them with a Writer monad.
>
> Andy Gill's Dotgen on Hackage is a nice realization of this idiom. See
> the how example included in the package has acceptably nice syntax
> without obliging the implementation to use complex facilities such as
> Template Haskell.
Thanks Stephen,
exactly the nudge I was looking for, except I could not find any
examples in the package. I had a brief look at the source code and it
did not instantly reveal itself.
Actually I already got stuck here:
data Dot a = Dot { unDot :: Int -> ([GraphElement],Int,a) }
Thanks Brent
for you detailed explanation.
------------------------------
Message: 2
Date: Thu, 8 Aug 2013 23:18:28 +0100
From: Stephen Tetley <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to construct complex string
Message-ID:
<CAB2TPRDV3uhFEmPB=hdhmj59c35ucb21-+t64444wl-85ag...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Hi Martin
The example is actually in the test directory - DotTest.hs.
> data Dot a = Dot { unDot :: Int -> ([GraphElement],Int,a) }
This is the monad in which you build syntax - when the code is run it is
pretty printed into a String.
This particular monad is a combination of the Writer monad - it accumulates
a list of GraphicElement and a state monad - Int is passed around and
incremented so it can generate variable names:
As separate pieces the monads are:
> data State a = State { unState :: Int -> (Int, a) }
i.e. a function from state of type `Int` to a tuple of state and a
polymorphic answer (Int,a).
> data Writer a = Writer { unWriter :: ([GraphElement],a) }
i.e. a tuple of GraphElement's accumulated in a list and a polymorphic
answer.
Once you have a "declaration" of a monad you also have to provide an
instance of the monad class so you can use the do-notation,
in the Dotgen code the instance is:
instance Monad Dot where
return a = Dot $ \ uq -> ([],uq,a)
m >>= k = Dot $ \ uq -> case unDot m uq of
(g1,uq',r) -> case unDot (k r) uq' of
(g2,uq2,r2) -> (g1 ++ g2,uq2,r2)
This is a bit complicated as it is a combination of the monadic operation
of both the State and Writer monad. With luck for just generating commands
(and not generating fresh variables) you should be able to manage with only
a Writer monad. There is one already in the monads package (mtl) in the
Platform.
The main part of the Dotgen library are the functions `edge`, `node`,
(.->.) etc. which build Dot commands. In `edge` you can see a single
command being built `[GraphEdge from to attrs]` in the first cell of the
answer tuple:
-- | 'edge' generates an edge between two 'NodeId's, with attributes.
edge :: NodeId -> NodeId -> [(String,String)] -> Dot ()
edge from to attrs = Dot (\ uq -> ( [ GraphEdge from to attrs ],uq,()))
Because no fresh variables are generated, the state `uq` (read as "unique")
is returned unaltered in the answer tuple along with the void answer `()`.
The other key part of the library is the so-called "run function" which
evaluates monadic expressions - here the run function is called showDot:
-- 'showDot' renders a dot graph as a 'String'.
showDot :: Dot a -> String
showDot (Dot dm) = case dm 0 of
(elems,_,_) -> "digraph G {\n" ++ unlines (map showGraphElement
elems) ++ "\n}\n"
showDot unwraps `dm` from the Dot data type. As dm is a function ` Int ->
([GraphElement],Int,a) ` it needs to be applied to the initial state - here
0. This produces the answer 3-tuple, where we are only interested in the
accumulated list of dot commands (the first cell). This list of commands is
pretty printed into legal Dot syntax.
Hope this helps somewhat.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130808/56ac2be3/attachment-0001.html>
------------------------------
Message: 3
Date: Fri, 9 Aug 2013 16:38:37 +1000
From: Mike Ledger <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] maybe in IO
Message-ID:
<CALQ6ZWbAHAx06CY=xb+ezku_dvut2tjtsq-7zdqqjfvtxkg...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
With Data.Foldable, it's quite nice:
check x assoc = lookup x assoc `for_` \f -> putStrLn ("found " ++ f)
On Fri, Aug 2, 2013 at 1:45 AM, Michael Orlitzky <[email protected]>wrote:
> On 08/01/2013 11:00 AM, vold wrote:
> > I've defined a function similar to
> >
> > check x assoc = let found = lookup x assoc in
> > when (isJust found) $ putStrLn $ "found " ++
> fromJust found
> >
> > which I've used several times from within the IO monad. Is there a more
> > compact way of doing this?
> >
>
> maybe (return ()) (putStrLn . ("found " ++)) found
>
> is the best I could do.
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130809/c954e3c7/attachment-0001.html>
------------------------------
Message: 4
Date: Fri, 9 Aug 2013 12:00:42 +0200
From: "Kees Bleijenberg" <[email protected]>
To: "'The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell'" <[email protected]>
Subject: [Haskell-beginners] alternative sourcegraph
Message-ID: <000001ce94e7$4f2516b0$ed6f4410$@[email protected]>
Content-Type: text/plain; charset="us-ascii"
I want to remove 'dead' code and unnecessary exports out of modules. I tried
sourceGraph with sourceGraph tbGlasDll.hs and I got:
Could not parse source file ./Toetsing.hs
Could not parse source file ./TestGlas.hs
Could not parse source file ./TBGlasDll.hs
Could not parse source file ./Glas.hs
Could not parse source file ./CalcGlas.hs
Could not parse source file ./calc.hs
Could not parse source file ./BelastingFactoren.hs
sourceGraph: Map.findMin: empty map has no minimal element
The files above compile without errors with ghc.
Maybe it has to do with Template Haskell code? Is there an alternative to
sourceGraph?
Kees
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130809/d0d7c6c9/attachment-0001.html>
------------------------------
Message: 5
Date: Fri, 9 Aug 2013 07:29:29 -0400
From: Brent Yorgey <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] alternative sourcegraph
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Fri, Aug 09, 2013 at 12:00:42PM +0200, Kees Bleijenberg wrote:
> I want to remove 'dead' code and unnecessary exports out of modules. I tried
> sourceGraph with sourceGraph tbGlasDll.hs and I got:
>
> Could not parse source file ./Toetsing.hs
>
> Could not parse source file ./TestGlas.hs
>
> Could not parse source file ./TBGlasDll.hs
>
> Could not parse source file ./Glas.hs
>
> Could not parse source file ./CalcGlas.hs
>
> Could not parse source file ./calc.hs
>
> Could not parse source file ./BelastingFactoren.hs
>
> sourceGraph: Map.findMin: empty map has no minimal element
>
> The files above compile without errors with ghc.
>
> Maybe it has to do with Template Haskell code? Is there an alternative to
> sourceGraph?
I don't know of any alternatives to sourceGraph. Why don't you file a
bug report with the maintainer?
-Brent
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 62, Issue 7
****************************************