Skip to content

Instantly share code, notes, and snippets.

@joom
Last active June 28, 2017 08:05
Show Gist options
  • Save joom/b5d6f69f32e1e8d9026c8a27f7d8de96 to your computer and use it in GitHub Desktop.
Save joom/b5d6f69f32e1e8d9026c8a27f7d8de96 to your computer and use it in GitHub Desktop.
Euclid's GCD algorithm, in Gödel's T
let val plus = \(n:nat) \(m:nat) rec m {
z => n
| s(x) with y => s(y)
}
in let val pred = \(n : nat) rec n {
z => z
| s(x) with y => x
}
in let val minus = \(n:nat) \(m:nat) rec m {
z => n
| s(y) with r => pred(r)
}
in let val lte = \(a:nat) rec a {
z => \(b:nat) s(z)
| s(x) with f => \(b:nat) rec b {
z => z
| s(y) with r => f(y)
}
}
in let val modAux =
\(gas:nat) rec gas {
z => \(a:nat) \(b:nat) a
| s(y) with f => \(a:nat) \(b:nat) rec lte(b)(a) {
z => a
| s(j) with k => f (minus(a)(b)) (b)
}
}
in let val mod = \(a:nat) \(b:nat) modAux (plus (a) (b)) (a) (b)
in let val gcdAux = \(gas:nat) rec gas {
z => \(a:nat) \(b:nat) a
| s(y) with r => \(a:nat) \(b:nat) rec b {
z => a
| s(y) with p => r (b) (mod (a) (b))
}
}
in let val gcd = \(a:nat) \(b:nat) gcdAux (plus (a) (b)) (a) (b)
in let val twelve = (s(s(s(s(s(s(s(s(s(s(s(s(z)))))))))))))
in let val eight = (s(s(s(s(s(s(s(s(z)))))))))
in gcd(twelve)(eight)
end end end end end end end end end end
@joom
Copy link
Author

joom commented Jun 27, 2017

@RobertHarper
Copy link

Yes, pretty much what I had in mind, counts down by ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment