Skip to content

Instantly share code, notes, and snippets.

@Zaffer
Created July 24, 2024 21:42
Show Gist options
  • Save Zaffer/82475833706ebf6386e7da3971f67cc0 to your computer and use it in GitHub Desktop.
Save Zaffer/82475833706ebf6386e7da3971f67cc0 to your computer and use it in GitHub Desktop.
HVM1 implementation of SKI combinator
// run with this command: $ hvm1 run -f ski.hvm1 -t 1 "(Main)"
// HVM1 has a bug on the parallelizer (that's what HVM2 fixed) so you need to run with -t 1
// expected output:
// "norm: ((((s ((s s) s)) s) (s s)) k)"
// "norm: (((s ((s s) s)) s) (s s))"
// "norm: ((s ((s s) s)) s)"
// "norm: (s ((s s) s))"
// "norm: ((s s) s)"
// "norm: (s s)"
// "norm: (s s)"
// "((s (k ((s (s s)) k))) ((s (k (k ((s (s s)) k)))) ((s (s s)) k)))"
// Utils
// -----
(U60.seq 0 cont) = (cont 0)
(U60.seq n cont) = (cont n)
(String.seq (String.cons x xs) cont) = (U60.seq x λx(String.seq xs λxs(cont (String.cons x xs))))
(String.seq String.nil cont) = (cont String.nil)
(String.concat (String.nil) str) = str
(String.concat (String.cons c rest1) str2) = (String.cons c (String.concat rest1 str2))
(String.join List.nil) = ""
(String.join (List.cons x xs)) = (String.concat x (String.join xs))
(Print [] value) = value
(Print msg value) = (String.seq (String.join msg) λstr(HVM.log str value))
// SKI Combinators
// ---------------
(Norm (C.App f x)) = (Print ["norm: " (Show (C.App f x))] (Norm.App (Norm f) (Norm x)))
(Norm term) = term
(Norm.App (C.App (C.App C.S x) y) z) = (Norm.App (Norm.App x z) (Norm.App y z))
(Norm.App (C.App C.K x) y) = x
(Norm.App C.I x) = x
(Norm.App f x) = (C.App f x)
(Show C.S) = "s"
(Show C.K) = "k"
(Show C.I) = "i"
(Show (C.App f x)) = (String.join ["(" (Show f) " " (Show x) ")"])
Main =
let term = (Norm (C.App (C.App (C.App (C.App C.S (C.App (C.App C.S C.S) C.S)) C.S) (C.App C.S C.S)) C.K))
(Show term)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment