Last active
October 11, 2020 02:35
-
-
Save chiroptical/765394319a2b8768ca99246141553741 to your computer and use it in GitHub Desktop.
Stack Ability in Unison
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
-- These are watchers, they run every time we reload this file in Unison repl | |
> Stack.run Stack.one | |
> Stack.run Stack.two | |
> Stack.run Stack.three | |
Stack.three : '{Stack List Nat} [Nat] | |
Stack.three = 'let | |
Stack.put 1 | |
match Stack.pop with | |
None -> [] | |
Some x -> [x] | |
----- | |
Stack.one : '{Stack List Nat} [Nat] | |
Stack.one = 'let | |
Stack.put 1 | |
Stack.put 2 | |
Stack.get | |
Stack.two : '{Stack List Nat} [Nat] | |
Stack.two = 'let | |
Stack.put 2 | |
Stack.put 4 | |
match (Stack.pop, Stack.pop) with | |
(Some x, Some y) -> Stack.put (x + y) | |
_ -> () | |
Stack.get | |
-- The interpreter of `Stack` for `List a` | |
Stack.run : '{Stack List a} [a] -> [a] | |
Stack.run stack = | |
h acc = cases | |
-- `k _` the hole will always be the return type for the ability method, `get : f a` | |
-- `h _` the hole will be an updated version of the accumulator | |
{Stack.get -> k} -> handle k acc with h acc | |
{Stack.put x -> k} -> handle k () with h (x +: acc) | |
{Stack.pop -> k} -> match acc with | |
x +: xs -> handle k (Some x) with h xs | |
_ -> handle k None with h [] | |
_ -> acc | |
handle !stack with h [] | |
ability Stack f a where | |
get : f a | |
put : a -> () | |
pop : Optional a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment