11

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

cxw
  • 16,685
  • 2
  • 45
  • 81
user6189164
  • 667
  • 3
  • 7

1 Answers1

18

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
G. Cito
  • 6,210
  • 3
  • 29
  • 42
Jonathan Worthington
  • 29,104
  • 2
  • 97
  • 136
  • 1
    the single quote `'` in the first piece of code doesn't belong there. I tried to edit it out, but stackoverflow doesn't allow edits shorter than 6 characters … – timotimo Mar 31 '18 at 22:24
  • fixed it by being more verbose ;-) – G. Cito Mar 31 '18 at 22:51