Last active
December 22, 2019 15:45
-
-
Save freddi-kit/248ce9f1cb33510a7067f94b440c51b6 to your computer and use it in GitHub Desktop.
ペアノ自然数上の足し算と掛け算の推論規則をSwiftのenumで無理やりしたやつ
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
// ペアノ自然数 | |
indirect enum N { | |
// N + 1 | |
case S(N) | |
// 0 | |
case Z | |
// デバッグ用、ペアノ自然数 -> アラビア数字 | |
func print() -> Int { | |
var selfValue: N = self | |
var i: Int = 0 | |
while true { | |
if case .S(let value) = selfValue { | |
selfValue = value | |
i += 1 | |
} else { | |
return i | |
} | |
} | |
} | |
} | |
// 足し算の推論規則 | |
func +(lhs: N, rhs: N) -> N { | |
switch lhs { | |
case .Z: | |
// 0 + N1 = N1 | |
return rhs | |
case .S(let value): | |
// N1 + N2 = N3 -> S(N1) + N2 = S(N3) | |
return .S(value + rhs) | |
} | |
} | |
(.S(.S(.Z)) + .S(.S(.Z))).print() // 4 | |
(.S(.S(.Z)) + .S(.Z)).print() // 3 | |
(.Z + .Z).print() // 0 | |
// 掛け算の推論規則 | |
func *(lhs: N, rhs: N) -> N { | |
switch lhs { | |
case .Z: | |
// 0 * N = 0 | |
return .Z | |
case .S(let value): | |
// N1 * N2 = N3 かつ N2 + N3 = N4 ならば S(N1) * N2 = N4 | |
return rhs + (value * rhs) | |
} | |
} | |
(.S(.S(.Z)) * .S(.S(.Z))).print() // 4 | |
(.S(.S(.Z)) * .S(.S(.S(.Z)))).print() // 6 | |
(.S(.S(.S(.Z))) * .S(.S(.S(.Z)))).print() // 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment