Skip to content

Instantly share code, notes, and snippets.

@Nymphium
Created September 3, 2018 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nymphium/60d4e2b5888f3e04b9b98c562854f143 to your computer and use it in GitHub Desktop.
Save Nymphium/60d4e2b5888f3e04b9b98c562854f143 to your computer and use it in GitHub Desktop.
#lang racket
(provide (except-out (all-defined-out) with-free Pure Free))
(require racket/control)
(struct Pure (r) #:transparent)
(struct Free (a k) #:transparent)
; (: compose [All (a b c) (-> (-> a b) (-> b c) (-> c d))])
(define (compose f g) (λ (x) (g (f x))))
(define (with-free f ph fh)
(match f
[(Pure r) (ph r)]
[(Free a k) ((fh a) k)]))
(define (newi)
(make-continuation-prompt-tag))
(define-syntax define-effect
(syntax-rules ()
((_ name) (define name (newi)))))
(define ((invole p) e)
(shift-at p k (Free e k)))
(define ((((handler p) valh) oph) th)
(letrec
[(h (λ (freer)
(with-free
freer
(λ (r) (valh r))
(λ (v) (λ (k) (oph v (compose k h)))))))
(freer (reset-at p (Pure (th))))]
(h freer)))
(define ((handle h) e) (h e))
(define (test)
(define-effect read)
(define (((readerh p) th) v)
(((((handler p)
(lambda (v) (lambda (_) v) ))
(lambda (x k) (lambda (s) ((k (+ x s)) s))))
th) v))
(define (hr p)
((readerh p)
(λ ()
(let* [(x ((invole p) 1))
(y ((invole p) (+ x 1)))]
y))))
((hr read) 10))
@Nymphium
Copy link
Author

Nymphium commented Sep 8, 2019

This is the implementation of 『Eff directly in OCaml』.
https://arxiv.org/abs/1812.11664

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment