1

I'm writing my own Symbolic Logic module where I have classes of sentences, subclasses of atoms and conjunctions, and so on. I would like to define operations between sentence in a somewhat natural way, and possibly eventually define operations for universal quantification and so on. So far I've defined __invert__ and __and__.

I'm slightly unhappy with using | for "or" since in Logic this is usually represented with a v-shaped wedge, but I can live with it. Similarly I'd like an infix operator for "if ... then ..." and "... if and only if ...". I'd ideally like the "if ... then ..." to be something like -> but I'm again willing to live with > so I defined __gt__.

But I'm at a bit of an impasse with "... if and only if ..." There seems to be genuinely no good option for that one. I might use == but then again I might want to reserve that for sentences being equivalent, so ...

Any suggestions?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Addem
  • 3,635
  • 3
  • 35
  • 58
  • 2
    If you use Python 3 you can use unicode identifiers http://stackoverflow.com/questions/2649544/unicode-identifiers-in-python but that has its own issues... You'd probably want some preprocessor that transforms from some ascii set of characters to the desired unicode symbol. You can't do this for infix operators, though. In general, if you want something like this you probably want a language like Scala. – Dan Oberlam Feb 07 '16 at 05:49
  • I think maybe to gain the flexibility I want in this regard I should probably just eventually design a way of translating text entries into my Logic objects. – Addem Feb 07 '16 at 05:52
  • 1
    I think ideally you will want `to_LaTeX` methods, returning strings of TeX markup. – BrianO Feb 07 '16 at 06:12
  • @BrianO That's an interesting way to get a nice rendering, but if I'm understanding it correctly it wouldn't make input prettier. – Addem Feb 07 '16 at 06:13
  • @Addem Correct, I meant it for output (only). Accepting LaTeX input requires a LaTeX parser – ouch. Reading your question again, it now seems you want more natural operators *for use in writing Python*. I'd say, don't overcomplicate it. Note that $p\to q$ is true iff $truthval(p) \le truthval(q)$; thus `p <= q` works, even though it suggests $\leftarrow$. Perhaps use Unicode ≤ as function name. In any case, use Python 3 :) Similarly, $p\leftrightarrow q$ is true iff $truthval(p) = truthval(q)$, and `==` already works properly for biconditionals. ¶ (Bah: no MathJax on stackoverflow.) – BrianO Feb 07 '16 at 06:21
  • 1
    Really it sounds like you want a DSL - an internal DSL (like you're describing) can be tricky, and for the kind of fine tuned control you mentioned I stand by my Scala recommendation. If you want to do an external DSL (i.e. you define some new language/grammar, and write a parser and interpreter/compiler for it) then Python would be fine, and won't require as much manhandling of the language. – Dan Oberlam Feb 07 '16 at 19:52
  • Also remember that Python 3.5 gets the `@` sign as an operator, so you have just a little more flexibility there ;) – Dan Oberlam Feb 07 '16 at 19:53

0 Answers0