>>4
Just a way to express computation and keep track of it, in the same way CPS is useful for controlling the flow of computation.
This state monad implementation just relies on clever use of closures. The tuple bit at the top is just an implementation of cons. That's a clever use of closures imho. Similarly, the state monad implemented above stores data in closures.
Er,
print("Parse output:");
try { print(parse(parens, "(()())")); }
catch (e) {
if (typeof e == 'function') print("Parse failed: " + snd(e));
else throw e;
}
Parse output:
null
Name:
Anonymous2008-04-13 17:13
Works fairly nicely.
A tell thee what, though, it's a right bugger to code for without static typing. Most of the expressions are closures so you can't get proper type errors when you accidentally put a value there instead of an action, or whatever. Monads in Javascript = possible, but could be annoying to write code for.
I.e. the parser is recursive, but Javascript has the second `parens' as undefined, which I wasn't aware of, but there you go. Doesn't support recursive declarations. So I had to declare it as a function and explicatively pass the state. Kind of annoying.
Anyone know a way to recursively declare variables? (Other than having a self-evaluating thunk)
I have discovered an amazing site. Turn the volume for your computer ON, and go to http://blocked.on.nimp.org with Internet Explorer. After going there with Internet Explorer, go there with Mozilla Firefox.
function bindM(m ,k)
{
return function (s) {
tmp = m(s);
a = tmp[0]; s_ = tmp[1];
return k(a)(s_);
}
}
function thenM(m,k) { return bindM(m,function(_){return k;}); }
function returnM(v) { return function (s) { return [v,s]; } }
function evalS(m,s) { return m(s)[0]; }
function getS(s) { return [s,s]; }
function putS(s) { return function(_) { return [null,s]; } }
function doM(m) {
if (arguments.length == 1) return m;
else if (arguments.length % 2 && arguments.length >= 2) {
var rest = Array.prototype.slice.call(arguments).slice(2);
return arguments[1](m, doM.apply(this, rest));
}
else throw ("doM: Error: Arguments mismatch.");
}
function mapM(k,l) {
if (l.length > 0) return doM(
k(l[0])
, bindM
, function (item) { return doM(
mapM(k,l.slice(1))
, bindM
, function (rest) {
if (rest)
return returnM(item.concat(rest));
else
return returnM(item);
}
); }
);
else
return returnM(null);
}
//////////////////////////////
// Parse stuff
function parse(parser, input) { return evalS(parser, input); }
function throwInt(msg,input) {
var e = new Error();
e.message = [msg,input];
e.name = 'PARSE_ERROR'; throw e;
}
//////////////////////////////
/// Combinators
function choice_(m,k) {
return function (s) {
try {
return m(s);
}
catch (e) {
switch (e.name) {
case 'PARSE_ERROR':
if (e.message[1].length != s.length) throw e;
else return k(s);
break;
default:
throw e;
}
}
}
}
function try_(m) {
return function (s) {
try {
return m(s);
}
catch (e) {
switch (e.name) {
case 'PARSE_ERROR':
e.message[1] = s; // Pretend we haven't consumed input
throw e;
break;
default:
throw e;
}
}
}
}
var letter_ = doM(
getS
, bindM
, function (input) {
if (input.length == 0)
return throwInt("Unexpected end of input, expected letter",input);
if (input.match(/^[a-z]/i))
return doM( putS(input.slice(1)) ,thenM, returnM(input[0]) );
else
return throwInt("Expected letter ([a-zA-Z])",input);
}
);
function char_(c) {
return doM(
getS
, bindM
, function (input) {
if (input.length == 0)
return throwInt("Unexpected end of input, expected '" + c + "'",input);
if (input[0] == c)
return doM( putS(input.slice(1)) ,thenM, returnM(c) );
else
return throwInt("Expected character '" + c + "'",input);
}
);
}
function string_(str) {
return mapM(char_,str);
}
//////////////////////////////
// Run a parser
//////////////////////////////
/*
run :: Show a => Parser a -> String -> IO ()
run p input
= case (parse p "" input) of
Left err -> do{ putStr "parse error at "
; print err
}
Right x -> print x
*/
function run(parser,string) {
print("Parse output:");
try {
print(parse(parser,string));
}
catch (e) {
switch (e.name) {
case 'PARSE_ERROR':
print("Parse failed: " + e.message[0] + ", input: " + e.message[1]);
break;
default:
throw e;
}
}
}
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy