Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Created January 12, 2018 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandfoy/b85de0526e3a747c15376a3d33629f28 to your computer and use it in GitHub Desktop.
Save briandfoy/b85de0526e3a747c15376a3d33629f28 to your computer and use it in GitHub Desktop.
Knuth Arrow custom operators in Perl 6
#!/Applications/Rakudo/bin/perl6
use Test;
multi infix:<↑> ( Int:D \n, Int:D \m --> Int:D )
is equiv(&infix:<**>)
is assoc<right>
{ n ** m }
proto infix:<↑↑> ( Int:D \n, Int:D \m --> Int:D )
is tighter(&infix:<↑>)
is assoc<right>
{ * }
multi infix:<↑↑> ( \n, 0 ) { 1 }
multi infix:<↑↑> ( \n, 1 ) { n }
multi infix:<↑↑> ( \n, \m ) { [↑] n xx m }
proto infix:<↑↑↑> ( Int:D \n, Int:D \m --> Int:D )
is tighter(&infix:<↑↑>)
is assoc<right>
{ * }
multi infix:<↑↑↑> ( \n, 0 ) { 1 }
multi infix:<↑↑↑> ( \n, 1 ) { n }
multi infix:<↑↑↑> ( \n, \m ) { [↑↑] n xx m }
multi infix:<↑⁰> ( Int:D \n, Int:D \m --> Int:D ) is assoc<right> { 1 }
multi infix:<↑²> ( Int:D \n, 0 --> Int:D ) is assoc<right> { 1 }
multi infix:<↑²> ( Int:D \n, Int:D \m --> Int:D ) is assoc<right> {
[↑] n xx m
}
multi infix:<↑³> ( Int:D \n, 0 --> Int:D ) is assoc<right> { 1 }
multi infix:<↑³> ( Int:D \n, Int:D \m --> Int:D ) is assoc<right> {
[↑↑] n xx m
}
# tetration notation
multi prefix:<⁰> ( Int:D \m --> Int:D ) { 1 }
multi prefix:<²> ( Int:D \m --> Int:D ) { m ↑↑ 2 }
multi prefix:<³> ( Int:D \m --> Int:D ) { m ↑↑ 3 }
multi prefix:<⁴> ( Int:D \m --> Int:D ) { m ↑↑ 4 }
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Now a bunch of tests
is 2↑0, 1, '2↑0 == 1';
is 2↑1, 2, '2↑1 == 2';
is 2↑2, 4, '2↑2 == 4';
is 2↑3, 8, '2↑3 == 8';
is 2↑4, 16, '2↑4 == 16';
is 2↑2↑2, 16, '2↑2↑2 == 16';
is 2↑2↑2↑2, 65536, '2↑2↑2↑2 == 65536';
is 2↑⁰0, 1, '2↑⁰0 == 1';
is 2↑2↑2, 16, '2↑2↑2 == 16';
is 2↑2↑2↑2, 65536, '2↑2↑2↑2 == 65536';
is 2↑↑0, 1, '2↑↑0 == 1';
is 2↑↑1, 2, '2↑↑1 == 2';
is 2↑↑2, 4, '2↑↑2 == 4';
is 2↑↑3, 16, '2↑↑3 == 16';
is 2↑↑4, 65536, '2↑↑4 == 65536';
is ²2, 2↑↑2, '²2 == 4';
is 2↑²4, 65536, '2↑²4 == 65536';
is ⁴2, 2↑↑4, '⁴2 == 65536';
is (2↑↑2)↑↑2, 4↑↑2, '(2↑↑2)↑↑2 == 4↑↑2';
is ²(²2), (2↑↑2)↑↑2, '²(²2) == 256';
is 3↑↑2, 27, '3↑↑2 == 27';
is 3↑²2, 27, '3↑²2 == 27';
is ²3, 27, '²3 == 27';
is 3↑↑3, 7625597484987, '3↑↑3 == 7625597484987';
is 3↑²3, 7625597484987, '3↑²3 == 7625597484987';
is ³3, 7625597484987, '³3 == 7625597484987';
is 4↑↑3, 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096,
'4↑↑3 == 1.34078e154';
is 4↑²3, 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096,
'4↑²3 == 1.34078e154';
is 2↑↑↑3, 65536, '2↑↑↑3 == 65536';
is 2↑³3, 65536, '2↑³3 == 65536';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment