Skip to content

Commit c3e000d

Browse files
committed
Working implementation of Haskell do notation in PHP
1 parent 8dcc77e commit c3e000d

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

example/FreeDooDSLTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function test_it()
1515
{
1616
$result = doo(
1717
let('a', Identity::of(1)),
18-
let('b', Identity::of(2)),
18+
let('b', Identity::of(3)),
1919
let('c', in(['a', 'b'], function (int $a, int $b): Identity {
2020
return Identity::of($a + $b);
2121
})),
@@ -24,6 +24,6 @@ public function test_it()
2424
})
2525
);
2626

27-
$this->assertEquals(Identity::of(9), $result);
27+
$this->assertEquals(Identity::of(16), $result);
2828
}
2929
}

src/Monad/Control/Doo/Algebra/DooF.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/**
1010
* DooF next = Let name m next
11-
* | In [name] fn
11+
* | In [name] fn (m -> next)
1212
*/
1313
interface DooF extends Functor, PatternMatcher
1414
{

src/Monad/Control/Doo/Algebra/In.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
namespace Widmogrod\Monad\Control\Doo\Algebra;
56

67
use Widmogrod\FantasyLand\Functor;
8+
use function Widmogrod\Functional\compose;
79

810
class In implements DooF
911
{
1012
private $names;
1113
private $fn;
14+
private $next;
1215

13-
public function __construct(array $names, callable $fn)
16+
public function __construct(array $names, callable $fn, callable $next)
1417
{
1518
$this->names = $names;
1619
$this->fn = $fn;
20+
$this->next = $next;
1721
}
1822

1923
/**
@@ -23,7 +27,8 @@ public function map(callable $function): Functor
2327
{
2428
return new self(
2529
$this->names,
26-
$this->fn
30+
$this->fn,
31+
compose($function, $this->next)
2732
);
2833
}
2934

@@ -32,6 +37,6 @@ public function map(callable $function): Functor
3237
*/
3338
public function patternMatched(callable $fn)
3439
{
35-
return $fn($this->names, $this->fn);
40+
return $fn($this->names, $this->fn, $this->next);
3641
}
3742
}

src/Monad/Control/Doo/actions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
namespace Widmogrod\Monad\Control\Doo;
56

67
use Widmogrod\FantasyLand\Monad;
@@ -19,5 +20,5 @@ function let(string $name, Monad $m): MonadFree
1920

2021
function in(array $names, callable $fn): MonadFree
2122
{
22-
return liftF(new Algebra\In($names, $fn));
23+
return liftF(new Algebra\In($names, $fn, Pure::of));
2324
}

src/Monad/Control/Doo/interpretation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ function interpretation(DooF $f)
4242
});
4343
});
4444
},
45-
In::class => function (array $names, callable $fn): Reader {
46-
return Reader::of(function (Registry $registry) use ($names, $fn) {
45+
In::class => function (array $names, callable $fn, callable $next): Reader {
46+
return Reader::of(function (Registry $registry) use ($names, $fn, $next) {
4747
$args = array_map(function ($name) use ($registry) {
4848
return $registry->get($name);
4949
}, $names);
5050

51-
return Pure::of($fn(...$args));
51+
return $next($fn(...$args));
5252
});
5353
},
5454
], $f);

0 commit comments

Comments
 (0)