• Stars
    star
    101
  • Rank 326,268 (Top 7 %)
  • Language
    Scala
  • Created almost 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Monadic Reflection

This project provides support for monadic reflection (Filinski 1994, 1999) to integrate monadic code with direct style code.

Tired of writing code using for-comprehensions?

The monadic-reflection library provides a convenient alternative!

Before

for {
  _ <- monadicActionA()
  r <- monadicActionB()
  result <- if (predicate(r)) {
    monadicActionC()
  } else {
    monadicActionD()
  }
} yield result

After

effectfulActionA()
if (predicate(effectfulActionB())) {
  effectfulActionC()
} else {
  effectfulActionD()
}

Looks familiar? Yes, it is just the direct-style code you would write in an imperative programming language.

Concepts

The underlying idea is very simple: Instead of using your monadic type constructor M[A] everywhere, your effectful programs now have the type CanReflect[M] ?=> A where CanReflect is a type defined by the monadic-reflection library.

As you can see from the type, given the capability CanReflect[M] you immediately get a value of type A that you can just use in direct-style. No need for flatMap and friends.

The best thing is, that you can go back and forth between the two representations:

trait Monadic[M[_]] {
  // embed a monadic value into direct style
  def reflect[R](mr: M[R])(using r: CanReflect[M]): R = r.reflect(mr)

  // reveal the monadic structure of a direct-style program
  def reify[R](prog: CanReflect[M] ?=> R): M[R]
}

How can I use this with my monad?

All you need to do is implement the Monadic trait which has two abstract methods:

def pure[A](a: A): M[A]
def sequence[X, R](init: M[X])(f: X => Either[M[X], M[R]]): M[R]

The first should look very familiar to you -- and if you already have a monad is very easy to implement. The second is just a slight variation of flatMap. In order to be stack safe you need to make sure to either implement sequence as a tail recursive function, or perform trampolining on your own.

Well, there is a fineprint: You also need to run your programs using a special JDK fork called "Project Loom". See below for more details.

Example Integrations

We provide a few case studies showing how to program with established monadic libraries in direct style:

Dependencies

To implement monadic reflection we require some implementation of (delimited) continuations. At the moment, our library only runs on JDK >= 21

Run Sbt

Finally, since we are accessing jvm internal types (Continuation and ContinuationScope), we need to allow our program to access them. This is done by forking the process in the sbt configuration.

If this does not work for you (for whatever reason), you can try to run sbt with:

sbt -J--add-exports=java.base/jdk.internal.vm=ALL-UNNAMED

Some experimental performance optimizations of project loom can be enabled by

-XX:-DetectLocksInCompiledFrames -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseNewCode

More Repositories

1

progfun-wiki

HTML
1,776
star
2

gears

A strawman for a low-level async library in Scala 3.
Scala
175
star
3

dotty-macro-examples

Various tasks solved via metaprogramming in Dotty
Scala
121
star
4

scala3-macro-tutorial

JavaScript
39
star
5

dotty-feature-requests

Historical feature requests. Please create new feature requests at https://github.com/lampepfl/dotty/discussions/new?category=feature-requests
31
star
6

xml-interpolator

XML String Interpolator for Dotty
Scala
28
star
7

dotty-knowledge

A knowledge base of Dotty internals and all things related
20
star
8

bench

benchmark compilation of Dotty
Scala
17
star
9

scala3doc

Scala
16
star
10

lsp-viewer

Web UI for analyzing Language Server Protocol logs
Vue
9
star
11

dotty-ecosystem

Procedures to manage projects ported to Dotty
Scala
8
star
12

sbt-dotty

A plugin to build your application using dotty with sbt support Edit Add topics
7
star
13

dotty-community-build

Dotty Community Build
7
star
14

cs206-2021-exam

3
star
15

dotty-ci

Docker image for testing dotty and other projects in the LAMP EPFL org
Dockerfile
3
star
16

homebrew-brew

homebrew formula
Ruby
3
star
17

dotty-semanticdb

Scala
3
star
18

fos-coq

fos coq project
Jupyter Notebook
3
star
19

coursera-notifications

2
star
20

bench-data

bench data for https://dotty-bench.epfl.ch
1
star
21

scala3-reference-docs

This repository contains generated language reference documentation
HTML
1
star
22

dotty-website

Dotty Website
1
star
23

dotty-remote-tracer

A receptor for Dotty LSP remote tracing
Scala
1
star
24

dotty-release-scripts

Scripts that are used to release new versions of Dotty
Python
1
star
25

dotty-bot

Scala
1
star