-
-
Save grondilu/a9812e3a8abb904ea5202a19bd46f5d0 to your computer and use it in GitHub Desktop.
Nat and Int in raku
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
module Typ { | |
our class Int {...} | |
our class Nat { | |
has Nat $.pred; | |
method succ { (state %){self.WHICH} //= self.new: pred => self } | |
} | |
multi sub infix:<==>(Nat $a, Nat $b) is export { $a === $b } | |
constant zero is export = Nat.new; | |
constant one is export = zero.succ; | |
constant two is export = one.succ; | |
constant three is export = two.succ; | |
class Int { | |
has Nat ($.left, $.right); | |
multi method new(Nat $n) { self.bless: left => $n, right => zero } | |
} | |
multi sub infix:<+>(Nat $a, zero --> Nat) is export { $a } | |
multi sub infix:<+>(zero, Nat $a --> Nat) is export { $a } | |
multi sub infix:<+>(Nat $a, Nat $b --> Nat) is export { samewith $a.pred, $b.succ } | |
multi sub infix:<*>(Nat $ , zero --> Nat) is export { zero } | |
multi sub infix:<*>(Nat $a, one --> Nat) is export { $a } | |
multi sub infix:<*>(Nat $a, Nat $b --> Nat) is export { | |
$a + samewith $a, $b.pred | |
} | |
multi sub infix:<->(Int $a, Int $b) is export { | |
Int.new: left => $a.left + $b.right, right => $a.right + $b.left; | |
} | |
multi sub infix:<==>(Int $a, Int $b) is export { | |
$a.left + $b.right == $a.right + $b.left | |
} | |
} | |
import Typ; | |
say one.succ + zero == two; | |
say Typ::Int.new: one; | |
say (one ... three).map({"."}).join; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment