From ca808bc09373f123514bf865a4518bebac3bbbc3 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sun, 5 Mar 2023 14:44:40 +0100 Subject: Monadic list/io interface --- std/Monad.bruijn | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 std/Monad.bruijn (limited to 'std/Monad.bruijn') diff --git a/std/Monad.bruijn b/std/Monad.bruijn new file mode 100644 index 0000000..e015efa --- /dev/null +++ b/std/Monad.bruijn @@ -0,0 +1,27 @@ +# MIT License, Copyright (c) 2023 Marvin Borner +# monadic interface for anything based on lists (e.g. IO, strings) +# afaik originally proposed by John Tromp and inspired by Haskell + +:import std/Pair . +:import std/Combinator . + +read [0] ⧗ a → (M a) + +return [[1 : 0]] ⧗ a → (M a) + +pure return ⧗ a → (M a) + +# monadic bind operator +…>>=… [[[2 0 1]]] ⧗ (M a) → (a → (M b)) → (M a) + +:test ((read >>= return) "woa") ("woa") + +# monadic reverse bind operator +…=<<… \…>>=… ⧗ (a → (M b)) → (M a) → (M b) + +:test ((return =<< read) "woa") ("woa") + +# monadic compose operator +…>>… [[1 >>= [1]]] ⧗ (M a) → (M b) → (M b) + +:test ((read >> (return 'a')) "hah") ("aah") -- cgit v1.2.3