Haskell for Imperative Programmers #17 - Monads

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 พ.ย. 2024

ความคิดเห็น • 75

  • @Lucas-ns9hd
    @Lucas-ns9hd 8 หลายเดือนก่อน +20

    After listening to this video without any of the visuals, I am only left with one question: What is a monad?

  • @Chemaclass
    @Chemaclass 4 ปีที่แล้ว +80

    Best Monads explanation ever.

  • @francisking708
    @francisking708 4 ปีที่แล้ว +19

    The first time that I (roughly) understand what a Monad is. And then the do notation from working with IO - light bulb moment!
    Thanks.

  • @LucasTraverso6
    @LucasTraverso6 3 ปีที่แล้ว +27

    Nice video!
    One small detail and I'm nitpicking but the fail function is no longer part of Monad, it now lives in the MonadFail typeclass

  • @moritzschmidt6791
    @moritzschmidt6791 4 ปีที่แล้ว +16

    Und auch dieses Video ist wirklich sehr sehr gut gelungen.
    Unter jedem Video hört man die Leute meckern wie schlecht es erklärt sei und das stimmt auch. Außer hier.
    Vielen Dank für die Haskell-Reihe!!!

  • @andrewt580
    @andrewt580 24 วันที่ผ่านมา

    Monadd was definitely worth it lol, these videos have all been really good so thank you!

  • @micknamens8659
    @micknamens8659 2 ปีที่แล้ว +11

    Beware of over-monading :-) .
    Often the Applicative type (which is the supertype of Monad) can be used leading to more concise code, e.g.
    monadd = liftA2 (+)
    Another advantage is that this expression doesn't impose a certain evaluation sequence.

    • @TruthNerds
      @TruthNerds ปีที่แล้ว +1

      monadd = liftM2 (+)
      with liftM2 from Control.Monad… so I'd not say more concise (but more general)

  • @_Redu
    @_Redu 2 ปีที่แล้ว +10

    ">>" is actually called "then".

    • @Parapascal
      @Parapascal 2 ปีที่แล้ว

      Thank you. I thought it's called join. Confusing

    • @TruthNerds
      @TruthNerds ปีที่แล้ว +1

      @@Parapascal No, join = (>>= id) so join :: Monad m => m (m a) -> m a.
      It removes one layer of monad wrapping for "double wrapped" values. For lists, it's also called flatten with the signature [[a]] -> [a].
      On the other hand, (>>) :: Monad m => m a -> m b -> m b

  • @wlcrutch
    @wlcrutch 2 ปีที่แล้ว +9

    Literally laughed out loud when you said “Right…That was worth it”

  • @felixlipski3956
    @felixlipski3956 4 ปีที่แล้ว +13

    mind

  • @Chemaclass
    @Chemaclass 4 ปีที่แล้ว +42

    "monadd" hahaha

  • @frankt9156
    @frankt9156 4 ปีที่แล้ว +5

    Now please also explain how flatten is equivalent with bind.

    • @Bratjuuc
      @Bratjuuc 3 ปีที่แล้ว +4

      flatten = (>>= id)
      bind f ma = flatten $ fmap f ma

  • @A1rPun
    @A1rPun 3 ปีที่แล้ว +2

    5:45 lol. All the "maybe" jokes were fun too 8:11

  • @frankt9156
    @frankt9156 4 ปีที่แล้ว +6

    Now please explain the State Monad :)

    • @kahnfatman
      @kahnfatman 3 ปีที่แล้ว +1

      How about the State Monarch :D

  • @geogreenmanu
    @geogreenmanu 4 ปีที่แล้ว +2

    Any example for associativity? m >>= (\x -> k x >>= h) = (m >>= k) >>= h , is this what you mean? f1 x = (Just x >>= (\x -> Just x)) >>= (\x -> Just x) should always be equal f2 x = (Just x) >>= (\x -> Just x >>= (\x -> Just x)). What is the point? We still have to evaluate it left to right in both cases in this example. Any benefits of having this?

    • @philipphagenlocher
      @philipphagenlocher  4 ปีที่แล้ว +6

      Think of a large expression of many maybys used with the bind operator. A single Nothing results in the whole computation to be nothing, due to associativity. That's something we could optimise for!

    • @geogreenmanu
      @geogreenmanu 4 ปีที่แล้ว

      @@philipphagenlocher Oooh, smart 🤠. Makes sense.

    • @bashful228
      @bashful228 ปีที่แล้ว

      @@geogreenmanu monads and maybe were literally star-destined to meet at the club.

  • @lucasclerisse5205
    @lucasclerisse5205 3 ปีที่แล้ว +5

    You're a legend

  • @Bbdu75yg
    @Bbdu75yg ปีที่แล้ว

    What is the Monad(m::*->*) ??
    Specifically what is the (m::*->*) ?
    Do it means that monad is some kind of function ?
    Does it means m is a constructor ?

    • @vm683
      @vm683 ปีที่แล้ว +3

      The notation * -> * is called kinds it's basically can be considered types of types it means monad expect a type which itself expect a concrete type.

  • @1Dr490n
    @1Dr490n ปีที่แล้ว

    What was the at 9:05? I don’t think you explained what the instance is and I didn’t understand anything else there

    • @0LoneTech
      @0LoneTech 9 หลายเดือนก่อน

      It's an instance of a type class, the subject of video number 13 in this series.

  • @WCiossek
    @WCiossek ปีที่แล้ว

    I wanted to use the λ x . x instead of >>= operator. x and λ - function should have the type Maybe. How can I do this? I think, serialization of Parameters can be done of currying. I am making my own functional programming language and I wanted to build an operating system with it, where it is possible to built a memory management. I wanted to do it with monads with functions in operator notation and not infix notation. Most of the functions should be implemented by the λ-calculus. The λ-calculus is very restrictive and direct access to storage is not allowed but it should be done. I think monads should be a solution for this problem. But I am not sure.

  • @paulzupan3732
    @paulzupan3732 ปีที่แล้ว +1

    I think my third eye opened when I realized that maybeadd can be written as
    maybeadd mx my = (+) mx my

  • @alphaprimal1669
    @alphaprimal1669 3 ปีที่แล้ว +4

    mon add for some reason i mixed french and english and understood it as my add LOL

  • @vaishnavisb1878
    @vaishnavisb1878 3 ปีที่แล้ว

    Can u help to test a function that returns MaybeT type

  • @Bbdu75yg
    @Bbdu75yg 2 ปีที่แล้ว

    Brilliant ❤!

  • @Angadsingh95
    @Angadsingh95 3 ปีที่แล้ว +8

    Monadd (with 2 D's) got me good 🤣

  • @ratfuk9340
    @ratfuk9340 ปีที่แล้ว

    Thanks so much

  • @shadow-ht5gk
    @shadow-ht5gk 3 หลายเดือนก่อน

    Took a few watches to get this one down lol

  • @Daniel-ws9qu
    @Daniel-ws9qu 4 ปีที่แล้ว +2

    Is what = in the monad?

    • @philipphagenlocher
      @philipphagenlocher  4 ปีที่แล้ว +5

      Correct! (This is true since "x >= (\x -> ...")

    • @NiDeCo
      @NiDeCo 4 ปีที่แล้ว +5

      Yes.
      do
      x = \x -> getLine >>= \y -> return $ x ++ y
      So it's literally using >>=.

    • @Daniel-ws9qu
      @Daniel-ws9qu 4 ปีที่แล้ว +3

      Ok, thank you! And using a let binding inside a do notation, would be evaluated equally, as if we would define it using "where" when using >>= . That's then why let bindings are evaluated lazyly, nice.

    • @NiDeCo
      @NiDeCo 4 ปีที่แล้ว +3

      @@Daniel-ws9qu Pretty much, the let binding is just inserted in between like this:
      do
      x >= \x -> let s = x ++ "? Cool!" in purStrLn s

  • @Mhan_solo
    @Mhan_solo 3 ปีที่แล้ว +5

    Philipp is da real mvp

  • @ccgarciab
    @ccgarciab 4 ปีที่แล้ว +13

    Subscribing just for the joke

  • @Chalisque
    @Chalisque ปีที่แล้ว

    Stephen Edwards' video th-cam.com/video/_Gk_lwhJMzk/w-d-xo.html does an excellent job of explaining monads
    to the less technical user.
    Then we can take the more commonly know async functions
    and promises in Javascript: how we go from
    * I know this is not an exact analogue
    f().then(x => f(x)).then(x => h(x)).then(x => k(x)).catch()
    to
    {
    let a,b,c,e
    try {
    // cf. Haskell's do
    a = await f()
    b = await g(a)
    c = await h(b)
    await k(c)
    } catch( e ) {
    console.log("Exception",e)
    }
    }
    and then explain how Haskell's do
    is a case of running one function and sticking
    its output into another, until we finish or an
    error occurs. (the 'and then... and then...' idea from Stephen's video)
    Only when the concept is understood should we bring
    in concise notation and abstract definitions.

  • @bdjeosjfjdskskkdjdnfbdj
    @bdjeosjfjdskskkdjdnfbdj 4 ปีที่แล้ว +4

    perhaps it would have been better if you started with a general intuition!

  • @maheerali531
    @maheerali531 3 ปีที่แล้ว +31

    If anyone understands this I salute him.

    • @bashful228
      @bashful228 ปีที่แล้ว +5

      Start at video number one. Or start with Learn You A little Haskell For The Greater Good. It’s free and online courses work for anybody at any level and prior experience.

    • @torarinvik4920
      @torarinvik4920 ปีที่แล้ว +2

      I find it easiest to use IO Monad as example.
      This is how I understand it, it might not be completely
      accurate, but it's enough to give you some hint of how it works.
      getLine :: IO String
      putStrLn :: String -> IO()
      lol ::IO
      lol = getLine >>= putStrLn
      alternatively
      lol = do
      trololol >= "extracts" the value out of the monad and pushes
      it into a function.
      If you want to chain these you will have to have
      functions that goes from "regular value" to Monad of "regular value"
      So signature becomes
      foo :: Whatever -> IO Whatever
      foo2 :: Whatever -> IO Whatever
      foo3 :: Whatever -> IO Whatever
      bar :: IO Whatever
      bar = foo >>= foo2 >>= foo3
      The >>= in this case turns IO Whatever into a Whatever and
      shoves it into a function.
      Whatever can be a string or any other type.
      You can also do it like this
      bar = do
      x

    • @maheerali531
      @maheerali531 ปีที่แล้ว

      @@torarinvik4920 Thanks a lot bro. You cleared my concept of >>=

    • @maheerali531
      @maheerali531 ปีที่แล้ว

      @@torarinvik4920 BTW. Are you haskell developer. Did you earn anything through haskell ever?

    • @torarinvik4920
      @torarinvik4920 ปีที่แล้ว

      @@maheerali531 My pleasure :)

  • @bashmogd4468
    @bashmogd4468 4 ปีที่แล้ว +8

    just another video that i watch that i hope to understand monad with it but i didnt ,sorry it is probably me problem

    • @philipphagenlocher
      @philipphagenlocher  4 ปีที่แล้ว +12

      Monads are not an easy topic! A big problem that occurs with monads is that you can interpret them in many different ways. In this video I interpreted them as "containers for values with predefined operations" yet others interpret them as "encapsulated computations" and others might have another interpretation. Depending on the use-case the interpretation also changes so it's a bit of a strange thing. ;)

    • @bashmogd4468
      @bashmogd4468 4 ปีที่แล้ว

      @@philipphagenlocher thanx very much

    • @francisking708
      @francisking708 4 ปีที่แล้ว +4

      It's taken me at least three goes. You'll get there.

    • @bashful228
      @bashful228 ปีที่แล้ว

      Just keep trying. This is my tenth time reading or listening to a monad explanation and it was the bast for me so far. I finally get it. But need to do lots of practice in code to lock it in!!

    • @mrGuido98
      @mrGuido98 6 หลายเดือนก่อน

      That's just a monoid in the category of endofunctors

  • @EvanZamir
    @EvanZamir 3 หลายเดือนก่อน

    Take my value. Please. 😉

  • @kacperozieblowski3809
    @kacperozieblowski3809 4 ปีที่แล้ว +1

    5:57 I genuinely laughed but for a different reason

  • @mostordinaryexistence
    @mostordinaryexistence 2 ปีที่แล้ว

    MONADD

  • @EpicNicks
    @EpicNicks 4 หลายเดือนก่อน

    5:45 moan add

  • @mehdikamani8004
    @mehdikamani8004 3 ปีที่แล้ว

    5:46 rofl

  • @vikingthedude
    @vikingthedude ปีที่แล้ว

    monads don't exist, silly