Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PLT-A3 Maybe Monad

PLT-A3 Maybe Monad

Avatar for kanaya

kanaya

July 26, 2025
Tweet

More Decks by kanaya

Other Decks in Education

Transcript

  1. pineapple.cc f :: Float -> Float f x = 1/x

    f :: Float -> Maybe Float f x = if x /= 0 then Just (1/x) else Nothing
  2. pineapple.cc f :: Float -> Maybe Float f x =

    if x /= 0 then Just (1/x) else Nothing f :: Float -> [Float] f x = if x /= 0 then [1/x] else []
  3. pineapple.cc f′  x ≡ { ⟨1/x⟩ if x ≠

    ∅ and x ≠ 0 ∅ otherwise
  4. pineapple.cc fx ≡ { ⟨1/x⟩ if x ≠ 0 ∅

    otherwise gx ≡ 2.0 × x g′  x ≡ ⟨gx⟩
  5. pineapple.cc fx ≡ { ⟨1/x⟩ if x ≠ 0 ∅

    otherwise gx ≡ 2.0 × x g′  x ≡ ⟨gx⟩ x′  ≡ (g′  ∘ f)x
  6. pineapple.cc fx ≡ { ⟨1/x⟩ if x ≠ 0 ∅

    otherwise gx ≡ 2.0 × x g′  x ≡ ⟨gx⟩ x′  ≡ ⟨x⟩ ↣ f ↣ g′ 
  7. pineapple.cc f :: Float -> Maybe Float f x =

    if x /= 0 then Just (1/x) else Nothing g :: Float -> Float g x = 2.0*x g’ :: Float -> Maybe Float g’ x = Just (g x) x’ = Just 2 >>= f >>= g’
  8. pineapple.cc f :: Float -> Maybe Float f x =

    if x /= 0 then Just (1/x) else Nothing g :: Float -> Float g x = 2.0*x x’ = (liftM g . f) x ผղ
  9. pineapple.cc fx ≡ { ⟨1/x⟩ if x ≠ ∅ and

    x ≠ 0 ∅ otherwise gx ≡ 2.0 × x x′  ≡ (g* ∙ f)x ผղ