Added state monad

This commit is contained in:
Dmitry Boulytchev 2020-12-24 02:54:54 +03:00
parent 53b2efc3b5
commit 19252991a5
3 changed files with 47 additions and 2 deletions

View file

@ -18,7 +18,7 @@ Ostap.o: List.o Collection.o Ref.o Fun.o Matcher.o
Buffer.o: List.o
Expr.o: Ostap.o
STM.o: List.o Fun.o
%.o: %.lama
LAMA=../runtime $(LAMAC) -I . -c $<

45
stdlib/STM.lama Normal file
View file

@ -0,0 +1,45 @@
-- State Monad.
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
--
-- This unit provides a state
import List;
import Fun;
public infixl => before $ (x, f) {
fun (state) {
case x (state) of
[state, x] -> [state, f (x)]
esac
}
}
public infix =>> at => (x, f) {
fun (state) {
case x (state) of
[state, x] -> f (x) (state)
esac
}
}
public fun returnST (x) {
fun (state) {[state, x]}
}
public fun chainST (xs) {
fun (state) {
case
foldl (fun (f, x) {
fun (state) {
case f (state) of
[state, xs] -> case x (state) of
[state, x] -> [state, x : xs]
esac
esac
}
}, returnST $ {}, xs) (state) of
[state, xs] -> [state, reverse (xs)]
esac
}
}