I'm trying to write some logical statements in Perl6.
I've made logical operators:
multi sub prefix:<¬> ($n) {
return not $n;
}
multi sub infix:<∧> ($n, $b) {
return ($n and $b);
}
multi sub infix:<∨> ($n, $b) {
return ($n or $b);
}
multi sub infix:<⇒> ($n, $b) {
if $n == True and $b == True {
return True;
} elsif $n == True and $b == False {
return False;
} elsif $n == False {
return True;
}
}
multi sub infix:<⇐> ($n, $b) {
return $b ⇒ $n;
}
But would like to be able to introduce new symbols for true and false. At the moment, I have:
say ((False ⇒ ¬(False ∨ True)) ∧ (False ∨ True));
But, I would like:
say ((⟂ ⇒ ¬(⟂ ∨ ⊤)) ∧ (⟂ ∨ ⊤));
I thought maybe I could make define these symbols as constants:
constant ⊤ = True;
constant ⊥ = False;
But, if I do that then I get this error:
Missing initializer on constant declaration at /home/devXYZ/projects/test.pl6:1
The ⊤
character is not valid as an identifier:
say "⊤" ~~ /<.ident>/; # Nil
Even if the constant
syntax allowed declaration of such a name, there wouldn't be a way to use it, since symbol name parsing only looks for identifiers also.
What's needed is to introduce it as a new term. This is much like adding a prefix or infix operator, in that it extends the language to accept things it otherwise would not. That can be done using constant
, like this:
constant \term:<⊤> = True;
say ⊤; # True
To be sure you are using the correct unicode character in the definition and usage lines you can use the convenient .&uniname
method. Different characters can look similar with certain fonts:
> say "⟂".&uniname
PERPENDICULAR
> say "⊥".&uniname
UP TACK
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With