Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Question about example in 'using do notation with
      Writer' section of LYH (Theodore Lief Gannon)
   2.  Change in type that I don't understand
      (Jan Erik =?utf-8?q?Mostr=C3=B6m?=)
   3. Re:  Change in type that I don't understand (Daniel Trstenjak)


----------------------------------------------------------------------

Message: 1
Date: Fri, 27 Jan 2017 00:34:29 -0800
From: Theodore Lief Gannon <tan...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Question about example in 'using do
        notation with Writer' section of LYH
Message-ID:
        <CAJoPsuDAHt-W6jZ7DAXR3a1qCk-Tx=Kexb4=cw_ufaebwfq...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Fully expanding your program might help. One of the great things about
Haskell is equational reasoning: if two things have been declared equal,
you can substitute one for the other.

First, let's desugar that do notation to the equivalent bind chain:

multWithLog =
  logNumber 3 >>= \a ->
    logNumber 5 >>= \b ->
      return (a*b)


Evaluate the logNumber and return calls to normal form from their
definitions, also considering the monoid definitions of (++) and mempty for
lists:

multWithLog =
  Writer (3, ["Got number: 3"]) >>= \a ->
    Writer (5, ["Got number: 5"]) >>= \b ->
      Writer (a*b, [])


Now, refer to the definition of (>>=) for Writer (as shown in LYAH):

(Writer (x, v)) >>= f = let (Writer (y, v')) = f x in Writer (y, v
`mappend` v')


Rewritten as a lamba (and replacing `mappend` with (++) for brevity), this
becomes:

\(Writer (x, v)) f -> let (Writer (y, v')) = f x in Writer (y, v++v')


It's no longer an infix function, so we'll have to shift things around a
little. Here's what it expands to:

multWithLog =
  (\(Writer (x, v)) f ->                -- \
      let (Writer (y, v')) = f x        -- | bind
      in Writer (y, v++v'))             -- /
    (Writer (3, ["Got number: 3"]))     -- Writer (x, v)
    (\a ->                              -- f
        (\(Writer (x2, v2)) f2 ->       -- \
        let (Writer (y2, v2')) = f2 x2  -- | bind
        in Writer (y2, v2++v2'))        -- /
      (Writer (5, ["Got number: 5"]))   -- Writer (x2, v2)
      (\b -> Writer (a*b, []))          -- f2
    )                                   -- (end f)


Now it's just a matter of simplification. Let's start by eliminating the
first argument of each bind, i.e. (Writer (x,v)), by substituting with
concrete values.

multWithLog =
  (\f ->                                        -- \ partially
      let (Writer (y, v')) = f 3                -- | applied
      in Writer (y, ["Got number: 3"]++v'))     -- / bind
    (\a ->                                      -- f
        (\f2 ->                                 -- \ partially
        let (Writer (y2, v2')) = f2 5           -- | applied
        in Writer (y2, ["Got number: 5"]++v2')) -- / bind
      (\b -> Writer (a*b, []))                  -- f2
    )                                           -- (end f)


Substitute f2, and eliminate both \f2 and \b in the same way:

multWithLog =
  (\f ->
      let (Writer (y, v')) = f 3
      in Writer (y, ["Got number: 3"]++v'))
    (\a ->
      let (Writer (y2, v2')) = Writer (a*5, [])  -- applied f2
      in Writer (y2, ["Got number: 5"]++v2')
    )


With a full match on the inner let block, that can also be eliminated:

multWithLog =
  (\f ->
      let (Writer (y, v')) = f 3
      in Writer (y, ["Got number: 3"]++v'))
    (\a -> Writer (a*5, ["Got number: 5"]++[]))


I'll forego the last few substitutions. If it's still not clear how you get
to the final output, you should run through them yourself. You'll
eventually reach a static definition of the result -- which shouldn't
really be surprising, since there were no arguments to this "function" and
its type doesn't contain -> anywhere. :)

On Thu, Jan 26, 2017 at 1:34 PM, Olumide <50...@web.de> wrote:

> On 26/01/17 16:02, David McBride wrote:
>
>> runWriter multWithLogTuple
>>>
>> ((3,5,10),["Got number: 3","Got number: 5"])
>>
>
> On second thoughts I don't think I understand how the logs are
> concatenated. I was expecting (15,["Got number: 15") in the original
> example.
>
>
> - Olumide
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170127/e89ffc45/attachment-0001.html>

------------------------------

Message: 2
Date: Fri, 27 Jan 2017 12:23:56 +0100
From: "Jan Erik =?utf-8?q?Mostr=C3=B6m?=" <li...@mostrom.pp.se>
To: beginners@haskell.org
Subject: [Haskell-beginners] Change in type that I don't understand
Message-ID: <38ef2a30-6ea4-4844-a802-11bf7bc26...@mostrom.pp.se>
Content-Type: text/plain; format=flowed

Hi,

Here is something that I don't understand, why does the named value have 
a different type than just the value itself?

Prelude> let f x = x * x
Prelude> :t f
f :: Num a => a -> a
Prelude> :t [f]
[f] :: Num a => [a -> a]
Prelude> let g = [f]
Prelude> :t g
g :: [Integer -> Integer]

= jem


------------------------------

Message: 3
Date: Fri, 27 Jan 2017 13:08:35 +0100
From: Daniel Trstenjak <daniel.trsten...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Change in type that I don't
        understand
Message-ID: <20170127120835.GA3233@octa>
Content-Type: text/plain; charset=us-ascii


Hi Jan,

> Here is something that I don't understand, why does the named value have a
> different type than just the value itself?

Looks like a bug in your ghci version.

With version 7.10.2 I'm getting:

> :t g
g :: Num a => [a -> a]


Grettings,
Daniel


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 103, Issue 22
******************************************

Reply via email to