Skip to content

Instantly share code, notes, and snippets.

@JordanMartinez
Last active July 25, 2019 00:11
Show Gist options
  • Save JordanMartinez/6494136e3acebc80113401fe39dd515b to your computer and use it in GitHub Desktop.
Save JordanMartinez/6494136e3acebc80113401fe39dd515b to your computer and use it in GitHub Desktop.
Rebinding 'bind' via a let binding in a do notation produces an error, but rebinding 'apply' in a let binding in a ado notation does not
module Syntax.RebindableDoAdo where
import Prelude
import Control.Bind as NormalBind
import Data.Functor as NormalMap
import Control.Apply as NormalApply
-- Given this monad (type class instances are at bottom of file)
data Box a = Box a
{-
This fails with error:
Error found:
at src/01-Rebindable-Do.purs:26:5 - 26:27 (line 26, column 5 - line 26, column 27)
The name bind cannot be brought into scope in a do notation block, since do notation uses the same name.
See https://github.com/purescript/documentation/blob/master/errors/CannotUseBindWithDo.md for more information,
or to contribute content related to this error.
-}
-- Commenting out this function will make the file compile.
normalBind_let_only :: Box Int
normalBind_let_only = do
let
bind = NormalBind.bind
discard = NormalBind.discard
three <- Box 3
Box unit
two <- Box 2
pure (three + two)
-- However, this works fine with 'ado'
normalApply_let_only :: Box Int
normalApply_let_only = ado
let
apply = NormalApply.apply
map = NormalMap.map
three <- Box 3
two <- Box 2
in three + two
-- Type class instances
instance functor :: Functor Box where
map :: forall a b. (a -> b) -> Box a -> Box b
map f (Box a) = Box (f a)
instance apply :: Apply Box where
apply :: forall a b. Box (a -> b) -> Box a -> Box b
apply (Box f) (Box a) = Box (f a)
instance bind :: Bind Box where
bind :: forall a b. Box a -> (a -> Box b) -> Box b
bind (Box a) f = f a
instance applicative :: Applicative Box where
pure :: forall a. a -> Box a
pure a = Box a
instance monad :: Monad Box
instance showBox :: (Show a) => Show (Box a) where
show (Box a) = "Box(" <> show a <> ")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment