tag:blogger.com,1999:blog-876358347971598886.post7817256343130860240..comments2023-03-25T16:20:46.021+03:00Comments on nothingmuch's perl blog: Abstracting Ambiguitynothingmuchhttp://www.blogger.com/profile/03855760206940108541noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-876358347971598886.post-66890588924144942572009-09-06T03:15:01.871+03:002009-09-06T03:15:01.871+03:00You might find this old post of mine about doing s...You might find this old post of mine about doing some continuation based declarative coding in Perl of mild interest<br /><br />http://www.perlmonks.org/?node_id=193649<br /><br />it's a nice model for those times I miss Prolog :-)Unknownhttps://www.blogger.com/profile/07129653155892872494noreply@blogger.comtag:blogger.com,1999:blog-876358347971598886.post-55558580482917495932009-08-27T03:12:40.811+03:002009-08-27T03:12:40.811+03:00I can't wait to play with this!
I found a sim...I can't wait to play with this!<br /><br />I found a similar backtracking example in <a href="http://www.iro.umontreal.ca/%7Eboucherd/mslug/meetings/20041020/90-min-scc/90-min-scc.pdf" rel="nofollow">scheme</a> a while back and ported it to Ruby (1.8):<br /><br /><a href="http://pastie.org/596143" rel="nofollow">http://pastie.org/596143</a><br /><br />I wrote a perl version as well using <a href="http://search.cpan.org/perldoc?Coro::State" rel="nofollow">Coro::State</a>, but it's untested because I never got round to configuring support for the continuation "game" (as Marc Lehmann calls it) in Coro (it uses the callcc implementation from the POD):<br /><br /><a href="http://pastie.org/596157" rel="nofollow">http://pastie.org/596157</a>Unknownhttps://www.blogger.com/profile/08409735001739000046noreply@blogger.comtag:blogger.com,1999:blog-876358347971598886.post-46886354016953800362009-08-25T12:52:31.440+03:002009-08-25T12:52:31.440+03:00I was thinking of a related acme module, basically...I was thinking of a related acme module, basically an object that overloads all operators to do predicate unification and when it's resolved into an actual value (e.g. '""' or '+0' overloading) that invokes amb as a thunk, and returns some value that satisfies the constraints.<br /><br />Comparators in void context become assertions:<br /><br />$x == $y; # this is true<br /><br />and we get instant prolog.<br /><br />The problem is that I have no idea how to capture continuations with actual C stack frames (i don't think that's even possible in a portable way), so that kills overloading.nothingmuchhttps://www.blogger.com/profile/03975438115490089158noreply@blogger.comtag:blogger.com,1999:blog-876358347971598886.post-22752654163271965472009-08-25T07:48:42.708+03:002009-08-25T07:48:42.708+03:00You could use this for solving problems like this:...You could use this for solving problems like this:<br /><br /> send + more = money<br /><br />for my $l (split "", "sendmorny") {<br /> $l{$l} = amb(0..9);<br /> $$l = $l{$l};<br />}<br /># must be unique<br />my %vals = map { $_ => 1 } values %l;<br />amb_assert(keys(%vals) == keys(%l));<br /><br /># run off the expression<br />my $cry;<br />amb_assert(($cry=$d + $e)%10 == $y);<br />$cry = ($cry > 9 ? 1 : 0);<br />amb_assert(($cry=$n + $r + $cry)%10 == $e);<br />$cry = ($cry > 9 ? 1 : 0);<br />amb_assert(($cry=$e + $o + $cry)%10 == $n);<br />$cry = ($cry > 9 ? 1 : 0);<br />amb_assert(($cry=$s + $m + $cry)%10 == $o);<br />$cry = ($cry > 9 ? 1 : 0);<br />amb_assert($cry == $m);<br /><br />$string = "$s$e$n$d + $m$o$r$e == $m$o$n$e$y";<br />amb_assert(eval $string);<br />print "$string\n";<br /><br />see also <a href="http://mail.pm.org/pipermail/wellington-pm/2005-March/000293.html" rel="nofollow">my experiments with Junctions from 2005</a>SamVhttps://www.blogger.com/profile/15104557254324587099noreply@blogger.comtag:blogger.com,1999:blog-876358347971598886.post-21454651808851943832009-08-24T14:09:34.425+03:002009-08-24T14:09:34.425+03:00You can do that in Haskell because every expressio...You can do that in Haskell because every expression in a do block is an implicit lambda and you can control the way they are sequenced based on the typeclass of the data involved.<br /><br />Since mbind is effectively the increment-the-instruction-pointer operation for any data that is relevant or interesting, they are a more generic abstraction than continuations.<br /><br />Unsurprisingly, continuations in Haskell are implemented in terms of a monad (which also makes the implementation very clean and simple, since mbind is already effectively CPS and there's no mutable data).<br /><br />In Perl 6 this could theoretically still be done (e.g. by rewriting the AST) but in Perl 5 I am not sure there is a way to do this without suffering brain damage.<br /><br />B::Generate is not stable enough and there are no APIs for compiling new lexical pad structures and assigning indices, it would be a massive effort (but if done the results would be awesome).<br /><br />I guess by walking the optree and annotating some sort of ANF representation (op_next is pretty much ANF if you squint right) you could generate a minimal syntactic unit for new symbol introduction and convert all expressions into implicit subroutines, and then reparse that, but I think that'll end up being pretty brittle.<br /><br />Anyway, if anyone has read this far, real monads are the next step ;-)nothingmuchhttps://www.blogger.com/profile/03975438115490089158noreply@blogger.comtag:blogger.com,1999:blog-876358347971598886.post-14435406108961984322009-08-24T13:59:05.695+03:002009-08-24T13:59:05.695+03:00Or you can overload control flow and data at the s...Or you can overload control flow and data at the same time...<br /><br />do { <br />x <- [1,2,3];<br />y <- [1,2,3];<br />guard (x > 2);<br />guard (x + y == 5);<br />guard (y <= 2);<br />return (show x ++ " + " ++ show y ++ " = 5")<br />}<br /><br />A major contributor to my stifled inspiration for Perl 6 was realizing how much Junctions wanted to be Monad []. But it was no fault of the Junctions: it was their society keeping them from realizing their true potential.Luke Palmerhttps://www.blogger.com/profile/09807388788677769669noreply@blogger.comtag:blogger.com,1999:blog-876358347971598886.post-77600920570772000842009-08-24T11:35:08.223+03:002009-08-24T11:35:08.223+03:00In case you didn't see this: http://search.cpa...In case you didn't see this: http://search.cpan.org/dist/Amb/kappahttps://www.blogger.com/profile/10283564172364338483noreply@blogger.com