Skip to content

Instantly share code, notes, and snippets.

@igstan
Created November 12, 2024 15:23
Show Gist options
  • Save igstan/2876b57d7abb9e16fdff319ff0c5f135 to your computer and use it in GitHub Desktop.
Save igstan/2876b57d7abb9e16fdff319ff0c5f135 to your computer and use it in GitHub Desktop.
(* 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