Is Polish notation good for math?

Having come to mentally “compute” everything in Lisp/Clojure-style, is prefix (Polish) notation good for math? I’m playing around with doing algebra and calculus even on paper, using prefix notation. Has anyone here tried this?

I realize there are wikipedia pages on Polish Notation and the Polish Mathematics movement from which it comes, and I also realize that prefix notation makes computational sense (hence S-Expressions); does it have any advantages or drawbacks for humans, though? Besides the fact that every math text you are likely to encounter uses infix instead?

1 Like

Off the top of my head:

If every function has a fixed arity (unlike Clojure’s basic arithmetic functions, e.g. *), it’s possible to write Polish notation without parentheses. However, this is inherently difficult for most humans to read, I believe, because we are not good at managing complex stacks in short term memory.

The unix-pipe style operators in Clojure are popular because it’s easy to simply read through a sequence in order. I don’t prefer them very often, but everyone’s different.

For complex mathematical expressions, though, I do prefer being able to read from left to right, being reminded that this next expression is to be added because there is a + sign right in front of it. i.e I prefer infix syntax, and I don’t think that is only a matter of habit–but that’s just my intuition. For me this is a drawback of lisps, but nothing’s perfect. (I don’t like using an infix macro system; it makes code confusing in a Clojure context.)

(This is one reason that I started using OCaml as well as Clojure. OCaml math syntax has other drawbacks, though, and it’s very different because it’s statically typed.)

1 Like

Thanks for the reply, @mars0i. I’ve been curious whether certain pen-and-paper operations operate differently in PN, and how gracefully I can work out problems. Note that I’m not trying to use Clojure to compute – just as the notation for hand-working problems. Here’s the homework that inspired the question:

math

In my opinion it’s just a matter of familiarity but that one is ingrained at a very small age, so I find it is much harder to adapt to polish notation for arithmetic use as opposed to programming.

1 Like

I don’t have a big background in math, so I prefer prefix. I can definitely read it better than infix (plus all the other precedence rules and notation).

1 Like

It’s pretty clear that some problems are easier to work out with a choice of notation that’s tailored to that problem. For example, some matrix problems are much easier if the structure of the matrix is rearranged without changing its mathematical essence. So I wouldn’t be shocked if you found that some problems are easier with prefix notation, Webdev_Tory.

1 Like

That’s my intuition as well, @mars0i. I’m hoping to find some specifics; what kind of shortcuts does it enable?

Personal opinion: Clojure’s advantages are that (1) you don’t need operator overloading and (2) you don’t need to remember the infix operator associativity table. No extra fuzz, just use functions with good names.

(1) Operator overloading. In math, you might want to define plus on your own datatype. In Java, you basically can’t touch the plus. In Python, you might have a better chance, but you’d still have to “extend standard plus semantics”. In Clojure, you can just provide your own math namespace, and do (my.math/+ a b). Optionally, you could just import your own plus and avoid the default provided one.

(2) Some languages allow you to create your own operators. Haskell is one of them. But in Haskell, you have to keep the infix table in mind. Each infix operator has its precedence (higher precedence is “collected together first”) and fixity (whether a + b + c = (a + b) + c (infixl) or a + b + c = a + (b + c) (infixr)).

This can become really confusing. You get a bunch of operators where you need to know fixity and precedence. Then you create a super-smart, super-long epressions with a bunch of arrows.

Following is an example of how you can use it. First, we define two “plus-like” operators $+$ (left-associative) and $++$ (right-associative). Pay attention to how they combine in the REPL session.

Note: ClojureVerse syntax hightlighting didn’t seem to like Haskell, so I created a gist (same as source below).

-- source file
module Lib where

someFunc :: IO ()
someFunc = putStrLn "someFunc"

data Expr a =
  Number a
  | Plus (Expr a) (Expr a)
  deriving (Show, Read)

-- shorthand for number, type constructor for Expr
n = Number

($++++$) :: Num a => a -> a -> a
a $++++$ b = a*a + b*b

($+$) :: Expr a -> Expr a -> Expr a
a $+$ b = Plus a b
infixl 6 $+$

($++$) :: Expr a -> Expr a -> Expr a
a $++$ b = Plus a b
infixr 6 $++$
-- Run this in a `ghci` or `stack ghci` -- this isn't really a file.

*Main> :load Lib
*Lib> n 10 $+$ n 20 $+$ n 30
Plus (Plus (Number 10) (Number 20)) (Number 30)
*Lib> n 10 $++$ n 20 $++$ n 30
Plus (Number 10) (Plus (Number 20) (Number 30))
1 Like

That is an excellent and complete reply.