Created
June 29, 2017 16:55
-
-
Save washort/87d88a82c4f5d8cdf92bbb7f49de4a3e to your computer and use it in GitHub Desktop.
monad example in Monte using control blocks
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
exports (main) | |
def id(x) as DeepFrozen: | |
return x | |
object identity as DeepFrozen: | |
"The identity monad." | |
to unit(x): | |
return x | |
to "bind"(action, f): | |
return f(action) | |
to control(operator :Str, argArity :Int, paramArity :Int, block): | |
return switch ([operator, paramArity]): | |
match ==["do", 1]: | |
# The inner value and our action upon it. | |
def [[value], lambda] := block() | |
object controller: | |
to control(operator, argArity, paramArity, block): | |
def [_, nextLambda] := block() | |
def identityBlock(): | |
return [[value], fn x, ej { | |
nextLambda(lambda(x, ej), ej) | |
}] | |
return identity.control(operator, argArity, | |
paramArity, identityBlock) | |
to controlRun(): | |
return lambda(value, null) | |
match msg: | |
throw(`Unrecognized control operator $msg`) | |
def main(_) as DeepFrozen: | |
traceln(identity (2) do x { x + 3 } do y { y + 3 }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment