Created
May 25, 2019 18:00
-
-
Save Nymphium/50ee382175e79eaf7ac6b7f191366019 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
effect Imperative : (('a -> 'b) -> 'b) -> 'a;; | |
let runImperative : (unit -> 'ans) -> 'ans = | |
fun f -> match f () with | |
| x -> x | |
| effect (Imperative fx) k -> | |
(* fx : ('a -> '_weak_b) -> '_weak_b *) | |
(* continue k : 'a -> 'ans *) | |
(* Mutlcire OCaml cannot unify '_weak_b and 'ans *) | |
(Obj.magic fx) @@ continue k | |
let imperative e = perform @@ Imperative e | |
let (<@>) f ek = f @@ imperative ek | |
let rec map_k f xs k = | |
match xs with | |
| [] -> k [] | |
| x :: xs' -> | |
f x @@ fun x' -> | |
map_k f xs' @@ fun xs'' -> | |
k (x' :: xs'') | |
let () = runImperative @@ fun () -> | |
let ls = map_k (fun x k -> k (x + 3)) [2; 3; 5] in | |
List.iter (Printf.printf "%d\n") <@> ls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem above is from the misunderstanding that
fx
would have weak polymorphic type variable.Actually
fx
have some concrete type variable so sometimesObj.magic
crashes runtime.One solution is that the effect is to be monomorphically typed.