Send Beginners mailing list submissions to
        [email protected]

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
        [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.  How can I lift my function taking a function to  a Monad?
      (Simon Peter Nicholls)
   2. Re:  problem with SDL-gfx (Mart?n Villagra)
   3. Re:  How can I lift my function taking a function to      a Monad?
      (Simon Peter Nicholls)
   4. Re:  How can I lift my function taking a function to a Monad?
      (Kim-Ee Yeoh)
   5. Re:  How can I lift my function taking a function to a Monad?
      (Simon Peter Nicholls)


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

Message: 1
Date: Thu, 10 Dec 2015 13:43:45 +0100
From: Simon Peter Nicholls <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] How can I lift my function taking a
        function to     a Monad?
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"; Format="flowed"

Hi All,

Can anyone help me lift a function that takes a function, so it can be 
used in a Monad?

The function I am given looks like "((b -> b) -> a -> a)", and viewing 
the definition of liftM2, it's almost what I need:

liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 f m1 m2 = do
  x1 <- m1
  x2 <- m2
  return (f x1 x2)

but I get the error:

   Expected type: (m b -> m b) -> m a -> m a
      Actual type: m (b -> b) -> m a -> m a

which makes sense, as liftM2 just wraps my function arg, whereas the 
function itself needs to operate on monadic values. So I figure I need:

neededLift :: (Monad m) => ((b -> b) -> a -> a) -> (m b -> m b) -> m a 
-> m a

Despite a rough appreciation of how liftM2 works ("unpack" desired 
monadic versions of the args, apply the function, then return to put 
result back in the Monad), my mind keeps hazing over regarding the 
changes I need to make in this case. I've tried implementing neededLift 
by composing other functions, and also by manual Monad wrangling, but 
the intuitive leap remains elusive.

I posted the specific problem over at SO 
(http://stackoverflow.com/questions/34152747/how-can-i-lift-an-fclabels-lens-to-a-monad),
 
but I think the specifics may be clouding what is probably a 
straightforward problem.

Cheers,
Si


-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151210/e7218af4/attachment-0001.html>

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

Message: 2
Date: Thu, 10 Dec 2015 11:10:55 -0300
From: Mart?n Villagra <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>,
        mvillagra0 <[email protected]>
Subject: Re: [Haskell-beginners] problem with SDL-gfx
Message-ID:
        <CAE7WeaJhHUvX_4eYC-+ArtPmQOqhxyy7iQ7mLn5L-09LZ=b...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

It works perfect now :D :D
Now I can use it in my project, I didn't even need the -lSDL_gfx.
Many thanks for this fast fix!

On Thu, Dec 10, 2015 at 1:34 AM, Francesco Ariis <[email protected]> wrote:

> On Tue, Dec 08, 2015 at 09:21:39PM -0300, Mart?n Villagra wrote:
> > Same error :/
> > The include folder (/usr/include/SDL/) contained the expected files.
> > (SDL_framerate.h and others)
> > I even recompiled and reinstalled the library but still says the same.
>
> Gotcha, I forgot to to put an extra-libraries: in the .cabal file
> (let me thank Ivan Perez, who had a similar problem on sdl-image and
> provided the right diagnosis).
>
> I have uploaded the new version to hackage, install it and then type:
>
>     runhaskell -lSDL_gfx your-test-file.hs
>
> It should work. Ciao
> -F
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151210/3edf273c/attachment-0001.html>

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

Message: 3
Date: Thu, 10 Dec 2015 16:16:40 +0100
From: Simon Peter Nicholls <[email protected]>
To: Daniel Bergey <[email protected]>
Cc: [email protected]
Subject: Re: [Haskell-beginners] How can I lift my function taking a
        function to     a Monad?
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"; Format="flowed"

Hiya,

That would certainly explain why I found it difficult.

Here's the original definition:

liftMLabel :: Monad m => a :-> b -> m a :-> m b
liftMLabel l = label (liftM $ get l) (liftM2 $ set l)

Are here's a sample iteration of me hacking around:

liftMLens :: Monad m => a :-> b -> m a :-> m b
liftMLens l = lens getter modifier
  where
    getter = liftM $ get l
    modifier mf mm = mf >>= \ f -> modify l f `liftM` mm

I only ever seem to replicate liftM2 characteristics when trying to 
brute force an implementation. And the implementations get more and 
more brutish as time goes on!

It makes me wonder if the implications of fclabels `m a :-> m b` are 
radically different now, bearing in mind the original code worked. The 
fclabels :-> type operator has convinced the compiler that `m b -> mb` 
is required for the actual value modifying function.

Does `m a :-> m b` make sense for the latest releases of fclabels? Is 
it just the lifting that's a problem?

Si

On Thu, 10 Dec, 2015 at 3:02 PM, Daniel Bergey <[email protected]> 
wrote:
> On 2015-12-10 at 07:43, Simon Peter Nicholls <[email protected]> 
> wrote:
>>  So I figure I need:
>> 
>>  neededLift :: (Monad m) => ((b -> b) -> a -> a) -> (m b -> m b) -> 
>> m a
>>  -> m a
> 
> I don't believe `neededLift` is possible.  The type says that given 
> any
> function `m b -> m b`, you can turn that function into one of type `b 
> ->
> b` (as input to the lens).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151210/000d93c3/attachment-0001.html>

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

Message: 4
Date: Fri, 11 Dec 2015 00:43:02 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How can I lift my function taking a
        function to a Monad?
Message-ID:
        <capy+zdsuewmq1nvfkeeg6chqx3gjdig3rb63ehmsxii+xnx...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Thu, Dec 10, 2015 at 10:16 PM, Simon Peter Nicholls <[email protected]
> wrote:


> Does `m a :-> m b` make sense for the latest releases of fclabels? Is it
> just the lifting that's a problem?


Yes, you can still write a function of type Monad m => (a :-> b) -> (m a
:-> m b). But it's gotten a bit trickier as you can tell.

The wrong way is to try to write:

neededLift :: (Monad m) => ((b -> b) -> a -> a) -> (m b -> m b) -> m a -> m
a

which, as Daniel pointed out, is impossible.

So we start with the isomorphisms:

isoL :: (f -> a) -> (a -> f -> f) -> ((a -> a) -> (f -> f))
isoL g s m f = s (m (g f)) f
isoR :: (f -> a) -> ((a -> a) -> (f -> f)) -> (a -> f -> f)
isoR _ m a = m (const a)

It turns out we really only need isoL, but isoR is there for completeness.

Then we write:

liftALabel :: Applicative f => a :-> b -> f a :-> f b
liftALabel l0 = lens g1 m1  where
   g1 = fmap (get l0)
   s1 = liftA2 (set l0)
   m1 = isoL g1 s1

Naturally, monad has been effect-reduced to applicative to admit more legal
programs.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151211/581a7796/attachment-0001.html>

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

Message: 5
Date: Fri, 11 Dec 2015 10:20:24 +0100
From: Simon Peter Nicholls <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How can I lift my function taking a
        function to a Monad?
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"; Format="flowed"

On Thu, 10 Dec, 2015 at 6:43 PM, Kim-Ee Yeoh <[email protected]> wrote:
> The wrong way is to try to write:
> 
> neededLift :: (Monad m) => ((b -> b) -> a -> a) -> (m b -> m b) -> m 
> a -> m a
> 
> which, as Daniel pointed out, is impossible.
> 
> So we start with the isomorphisms:
> 
> isoL :: (f -> a) -> (a -> f -> f) -> ((a -> a) -> (f -> f))
> isoL g s m f = s (m (g f)) f
> isoR :: (f -> a) -> ((a -> a) -> (f -> f)) -> (a -> f -> f)
> isoR _ m a = m (const a)
> 
> It turns out we really only need isoL, but isoR is there for 
> completeness.
> 
> Then we write:
> 
> liftALabel :: Applicative f => a :-> b -> f a :-> f b
> liftALabel l0 = lens g1 m1  where
>    g1 = fmap (get l0)
>    s1 = liftA2 (set l0)
>    m1 = isoL g1 s1
> 
> Naturally, monad has been effect-reduced to applicative to admit more 
> legal programs.
> 
> -- Kim-Ee

Thanks guys, I appreciate your help. You've given me a lot of food for 
thought here, Kim-Ee, and I'll spin off a new project to explore this 
in isolation.

Si
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151211/b2e6db94/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 90, Issue 17
*****************************************

Reply via email to