mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-05 22:38:44 +00:00
Added state monad
This commit is contained in:
parent
53b2efc3b5
commit
19252991a5
3 changed files with 47 additions and 2 deletions
|
|
@ -1 +1 @@
|
||||||
let version = "Version 1.00, 66dc5c306, Tue Nov 24 23:11:17 2020 +0300"
|
let version = "Version 1.00, 53b2efc3b, Fri Dec 11 01:22:25 2020 +0300"
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ Ostap.o: List.o Collection.o Ref.o Fun.o Matcher.o
|
||||||
|
|
||||||
Buffer.o: List.o
|
Buffer.o: List.o
|
||||||
|
|
||||||
Expr.o: Ostap.o
|
STM.o: List.o Fun.o
|
||||||
|
|
||||||
%.o: %.lama
|
%.o: %.lama
|
||||||
LAMA=../runtime $(LAMAC) -I . -c $<
|
LAMA=../runtime $(LAMAC) -I . -c $<
|
||||||
|
|
|
||||||
45
stdlib/STM.lama
Normal file
45
stdlib/STM.lama
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue