Last active
April 4, 2017 05:26
-
-
Save LightAndLight/c4ed7a9d408ff9c6e16f8399ca6600dd to your computer and use it in GitHub Desktop.
Monads!? In PHP!?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Monad { | |
public function __construct($return, $bind) { | |
$this->return = $return; | |
$this->bind = $bind; | |
} | |
} | |
$liftM2 = function($dict77) { | |
return function($f) use ($dict77) { | |
return function($ma) use ($dict77, $f) { | |
return function($mb) use ($dict77, $f, $ma) { | |
return ($dict77->bind)($ma)(function($a) use ($dict77, $f, $ma, $mb) { | |
return ($dict77->bind)($mb)(function($b) use ($a, $dict77, $f, $ma, $mb) { | |
return ($dict77->return)($f($a)($b)); | |
}); | |
}); | |
}; | |
}; | |
}; | |
}; | |
class NothingCon { | |
public function __construct() { | |
$this->values = array(); | |
} | |
} | |
$Nothing = new NothingCon(); | |
class JustCon { | |
public function __construct($a1) { | |
$this->values = array($a1); | |
} | |
} | |
$Just = function($a1) { | |
return new JustCon($a1); | |
}; | |
$monadMaybe = new Monad($Just, function($ma) use ($Just, $Nothing) { | |
return function($f) use ($Just, $Nothing, $ma) { | |
return (function() use ($Just, $Nothing, $f, $ma) { | |
if ($ma instanceof NothingCon) { | |
return $Nothing; | |
} | |
if ($ma instanceof JustCon) { | |
$a = $ma->values[0]; | |
return $f($a); | |
} | |
})(); | |
}; | |
}); | |
$and = function($a) { | |
return function($b) use ($a) { | |
return (function() use ($a, $b) { | |
if ($a === true) { | |
return $b; | |
} | |
return false; | |
})(); | |
}; | |
}; | |
$asdf = $liftM2($monadMaybe)($and)($Just(true))($Nothing); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment