Created
November 12, 2024 15:23
-
-
Save igstan/2876b57d7abb9e16fdff319ff0c5f135 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
(* Inspired by: https://www.reddit.com/r/ProgrammingLanguages/comments/1golfwz/emit_a_time_travelling_programming_language/ *) | |
structure Timewarp = | |
struct | |
structure Cont = SMLofNJ.Cont; | |
type t = unit Cont.cont option ref | |
fun point () = | |
let | |
val pt : t = ref NONE | |
in | |
Cont.callcc(fn k => pt := SOME k); | |
pt | |
end | |
fun travelOnce pt = | |
case !pt of | |
| NONE => () | |
| SOME k => pt := NONE before Cont.throw k () | |
end | |
fun main () = | |
let | |
open Timewarp | |
val x = ref 10 | |
val p = point () | |
in | |
print ("x=" ^ Int.toString (!x) ^ "\n"); | |
x := 20; | |
travelOnce p | |
end | |
(* | |
* Needs SML/NJ with Successor ML features. Run with: | |
* | |
* $ sml -Cparser.succ-ml=true < timewarp.sml | |
*) | |
do main (); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment