Created
September 3, 2018 15:30
-
-
Save Nymphium/60d4e2b5888f3e04b9b98c562854f143 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the implementation of 『Eff directly in OCaml』.
https://arxiv.org/abs/1812.11664