https://github.com/rizo/ppx_monad
Minimalistic monad syntax for OCaml.
https://github.com/rizo/ppx_monad
Last synced: about 2 months ago
JSON representation
Minimalistic monad syntax for OCaml.
- Host: GitHub
- URL: https://github.com/rizo/ppx_monad
- Owner: rizo
- License: mit
- Created: 2016-08-10T00:11:03.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-07T18:04:22.000Z (over 8 years ago)
- Last Synced: 2025-04-06T06:12:15.743Z (2 months ago)
- Language: OCaml
- Size: 4.88 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ppx_monad
Minimalistic monad syntax for OCaml. This extension features a syntax for monadic computations inspired by Haskell. While processing the source code, each occurence of `name <- monad; body` is converted into `monad >>= fun name -> body`. No additional validation or substitutions are performed. Pattern matching is not supported on bind site, but can be used on already bound variables.
**Example**:
```ocaml
module Option = struct
type 'a t = 'a optionlet return x = Some x
let (>>=) self f =
match self with
| Some x -> f x
| None -> None
endlet put_line str =
try
Some (output_string (str ^ "\n"))
with e ->
Nonelet get_line () =
try
Some (input_line stdin)
with End_of_file ->
Noneopen Option
let concat_lines () =
begin%monad
put_line "input two lines";
a <- get_line ();
b <- get_line ();
return (a ^ b)
endlet () =
match concat_lines () with
| Some line -> print_endline line
| None -> print_endline "error"
```**Warning**: this extension conflicts with the syntax for mutable update of object instances. Since objects are not considered an idiomatic OCaml and are not used frequently (and even less with mutable fields), this extension can be safely used in regular projects.