palladin / Eff

A library for programming with Algebraic Effects in F#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Eff

Eff is a library for programming with Algebraic Effects in F# inspired by the Eff programming language and the implementation of Algebraic Effects in OCaml-Haskell(1, 2) and especially from the paper Eff Directly in OCaml.

// state effect example
let test () = 
    eff {
        let! x = get ()
        do! put (x + 1)
        let! y = get ()
        do! put (y + y)
        return! get ()
    } 
    
// state effect handler
let stateHandler (s : 'S) (eff : Eff<#State<'S>, 'T>) : ('T * 'S) =
    let rec loop (s : 'S) (effect : Effect) = 
        match effect with
        | :? Get<'S> as get -> loop s (get.K s) 
        | :? Put<'S> as put -> loop put.Value (put.K ())
        | :? Done<'T> as done' -> (done'.Value, s)
        | _ -> failwith "Unhandled effect"
    loop s (run done' eff) 

// Apply state effect and execute
stateHandler 1 (test ()) // (4, 4)

About

A library for programming with Algebraic Effects in F#


Languages

Language:F# 100.0%