From 19252991a5d0acd2f7d1cd2b8f2294827c2f1edc Mon Sep 17 00:00:00 2001 From: Dmitry Boulytchev Date: Thu, 24 Dec 2020 02:54:54 +0300 Subject: [PATCH] Added state monad --- src/version.ml | 2 +- stdlib/Makefile | 2 +- stdlib/STM.lama | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 stdlib/STM.lama diff --git a/src/version.ml b/src/version.ml index be85ec54e..ea69867c6 100644 --- a/src/version.ml +++ b/src/version.ml @@ -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" diff --git a/stdlib/Makefile b/stdlib/Makefile index e6ce887f0..3224cff78 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -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 $< diff --git a/stdlib/STM.lama b/stdlib/STM.lama new file mode 100644 index 000000000..230e7d50f --- /dev/null +++ b/stdlib/STM.lama @@ -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 + } +} + \ No newline at end of file