unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Xah Lee <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ?
Date: Thu, 6 Jan 2011 14:59:21 -0800 (PST)	[thread overview]
Message-ID: <370ce4b6-b0ed-4c84-b008-099226a6734b@j32g2000prh.googlegroups.com> (raw)
In-Reply-To: e37b64b7-61ba-4247-a6cd-e32323c7be15@i32g2000pri.googlegroups.com

On Jan 5, 6:58 am, Xah Lee <xah...@gmail.com> wrote:
> On Dec 31 2010, 11:04 pm, girosenth <girose...@india.com> wrote:
>
> > How to improve the readability of (any) LISP or any highlevel
> > functional language to the level of FORTH ?

some basics that might be helpful.

〈What's Function, What's Operator?〉
http://xahlee.org/math/function_and_operators.html

--------------------------------------------------
What's Function, What's Operator?

Xah Lee, 2010-12-14, 2011-01-06

Typically, we understand what “function” and “operator” mean, but
programer may have a hard time explaining them, and mathematician may
never thought about it. Here, we clarify a bit on the meaning of the
word “function” and “operator”, their context, their relation.

--------------------------------
Function is a Mathematical Concept

Function you probably understand. A function takes a input, and output
a value for a given input. The inputs are called “parameters”. A
specific set of input is called “arguments”. The number of parameters
of a function is called the function's “arity”. So, for example, the
function “sin” has arity 1. A constant, such as 35, π, can be
considered as functions of no parameter, so they are functions of
arity 0. A function of 2 parameters, such as f(x,y) := x+y has arity
2.

Function is a mathematical concept. Viewed in another way, it's a map
from one space (aka set) to another.

--------------------------------
Operator is About Notation

A operator, is less of a mathematical concept, but more of a concept
of notation. Or, in computer language contexts, a element of syntax. A
“operator” is a symbol (or symbols) that are written to indicate
operations. For example, we write 1+2, the “+” is a operator, and the
“1” and “2” are its “operands”. Mathematically, operator is a function
that acts on its operands. The operands are the arguments of the
function.

--------------------------------
Binary Operators

Typically, operators takes 2 arguments, the left and right of the
symbol. e.g. 2×3, 3/4, 2^3, union {3,4}∪{2,4,1}, etc.

--------------------------------
Unary Operators

But there are also 1-argument operators. For example the minus sign
-3, and the logical not ¬ sign, the factorial 3!, square root √3.

--------------------------------
Multi-symbol Operators

Operators can also involve other forms with more symbols. For example,
the absolute sign |-3|, floor ⌊3.8⌋ uses a bracket, summation ∑ takes
4 arguments, a expression, a variable, and start and end values. The
anti-derivative (aka indefinite integral) ∫ takes 2 arguments, a
expression and a symbol. In traditional notation, the integration
operator involves 2 symbols, ∫ and ⅆ. For example, we write ∫ sin(x)
ⅆx .

--------------------------------
Implicit Operators

Operator can be a bit complicated. For example -3 can be interpreted
in several ways. We can think of the minus sign as unary operator
acting on the argument 3. So, mathematically, it is a function of 1
arity that returns the addictive inverse of the element 3. Or, we can
interprete it as one single entity, a element of Reals denoted -3.
When we write 3-2, the ways to interprete it gets a bit more complex.
One way to think of it as a notation shorthand for 3 + (-2). The -2
part can be thought of as before. Another way is to think of - as a
binary operator on 3 and 2, but this seemingly simple interpretation
is a bit complex. Because, what is math definition of the minus binary
function? I'm not sure how it can be defined in terms of algebra
without ultimately thinking of it as additon of 2 elements, one being
a addictive inverse. The other way is to think of it as a real line,
moving the first argument to the left by a distance of the second
argument. Of course ultimately these are equivalent, but i can't think
of a simple, direct, interpretation that can serve as a math
foundation. Also, this is directly related to how does one interprete
division, such as 3/2.

The multiplication operator also gets complicated. For example, when
we write 3 x, it usually means 3*x. The space acts as implicit
multiplication sign. But when we write 3 +2, the space there has no
significance. When we write 3x, even there is no space nor any
operator, but we mean 3*x. As a computer language syntax based on
traditional notation, the parsing rule is not trivial.
Operator Stickiness

There's also the concept of “operator stickiness” (aka “operator
precendence”) at work that makes expressions with operators more
concise. When we write3△4▲5, how do you know it's (3△4)▲5 or 3△(4▲5)?
The concept of operator stickiness is needed to resolve that.
Otherwise, you'll need to always write 3+(4*5) instead of the simpler
3+4*5. But this again, also introduced more complexity. When you have
a expression of tens of different operators, it becomes a problem of
remembering the stickiness grammar for each operator. (in practice,
when you are not sure about the procedence, you usually just use
explicit priority indicator by parens. This often happens in
programing with logic operators (e.g. and &&, or ||, not !.)
Forgetting Operator Precedence is a common error in programing. In
written math for human communication, it is prone to
miscommunication.)

--------------------------------
Operator is tied to Notation

Because the concept of “operator” more has to do with syntax and
notation, and when considering traditional math notation of writing in
“2-dimensions”, also the fact that traditional math notation has a lot
ambiguities, it gets a bit complicated and not as imprecise as we
like. (See: The Problems of Traditional Math Notation)

Mathematically, operator and function are the same thing.

Math function in traditional notation has the form e.g. sin(x),
f(x,y), where the things inside the paren are its parameter/arguments,
and function name is placed to the left.

Operators are useful because writing everything out in full function
notation gets very cumbersome and hard to read. For example, we write
3+4*5 instead of plus(3,times(4,5)) or +(3,*(4,5)).

Here's a example of traditional notation using operators:

-b+√(b^2-4 a c)/(2 a)

If you don't allow space as implicit multiplication sign, then you
have to write:

-b+√((b^2)-4*a*c)/(2*a)

If you don't allow the the concept of operator precedence, then you
have to write:

(-b)+(√((b^2)-((4*a)*c))/(2*a))

If you prefer the structural clarity of the traditional function
notation f(x), you have to write:

/(+(-(b),√(+(^(b,2),-(*(4,a,c))))),*(2,a))

If you prefer words than symbols, as traditionally practiced when
writing out functions, you have to write:

divide(add(minus(b),sqrt(add(power(b,
2),minus(times(4,a,c))))),times(2,a))

The notation using operators is much concise, readable, but at the
cost of relatively complex lexical grammar. The full functional
notation is precise, grammatically simple, but difficult to read.

In math context, it's best to think of functions instead of operator,
and sometimes also use a uniform function notation, where all
arguments are explicitly indicated in one uniform way.

Here's what Wikipedia has to say about operators: Operation
(mathematics). Quote:

    An operation ω is a function of the form ω : X1 × … × Xk → Y. The
sets Xj are called the domains of the operation, the set Y is called
the codomain of the operation, and the fixed non-negative integer k
(the number of arguments) is called the type or arity of the
operation.

Note that it doesn't really speaks of “operators”, but speaks of
“operations”, and flatly defines operation as a function.

--------------------------------
Traditional Function Notation sin(x) Isn't Perfect

Note that, even the functional notation such as sin(x), isn't perfect.
Problem of Functions Returning Functions

Normally, with full function notation, you'd expect that execution
order of operations clearly corresponds to the nesting structure. For
example, in our example before:

divide(add(minus(b),sqrt(add(power(b,
2),minus(times(4,a,c))))),times(2,a))

The inner-most parts are evaluated first.

But there's a problem when a function returns a function. For example,
the derivative takes a function and returns a function. We might
write:

derivative(f)

Now, if we want to evaluate the result at 3, then we might write:

derivative(f)(3)

You can see that the notation no longer nests. The operator precedence
issue is back. Now, you need to have a slightly more complex notion of
precedence to work out the notation.

One solution to this is the lisp language's syntax. In lisp syntax,
everything is written inside a paren. The first element is the
function name, the rest is its arguments. So, sin(x) would be written
as (sin,x). (comma is used for separator) Our derivative example would
then be:

((derivative,f),3)

In this way, the syntax remains a pure nested form, and provides the
utmost precision.

Our formula example in fully nested syntax be:

(/,(+,(-,b),(√,(+,(^,b,2),(-,(*,4,a,c))))),(*,2,a))

We could change the comma separator to space. So, it would look like
this:

(/ (+ (- b) (√ (+ (^ b 2) (- (* 4 a c))))) (* 2 a))

(Note: actual lisp language's syntax is not 100% regular. Many of its
syntax does not have the form (a b c ...). See: Fundamental Problems
of Lisp.)

--------------------------------
Syntax Design for Computer Languages
Mixing Operator Syntax with Full Function Notation Syntax

In most computer language, they allow both the operator and full
function syntax. For example, you can write (sin(x))^2+3. (almost all
languages do this; e.g. C, C++, C#, Java, Pascal, Perl, Python, Ruby,
Bash, PowerShell, Haskell, OCaml. The only exception is lisps.)

(Though, almost all computer languages does not have a regular syntax,
in fact, non of any major computer lang has a syntax specification.
The closest one that has a somewhat regular and simple syntax grammar
is Mathematica. See: The Concepts and Confusions of Prefix, Infix,
Postfix and Fully Nested Notations ◇ Math Notations, Computer
Languages, and the “Form” in Formalism.)

Mixed form is normal, and most flexible. Because, not all functions
have a associated operator symbol. And, writing everything in nested
brackets is not readible and hard to write too. The question is, is it
possible to design a syntax, that is fully regular with a very simple
lexical grammar, and easy to read and write?

 Xah ∑ http://xahlee.org/

  parent reply	other threads:[~2011-01-06 22:59 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-01  7:04 How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ? girosenth
2011-01-01 11:39 ` Elena
2011-01-01 14:15 ` Pascal J. Bourguignon
2011-01-01 16:08   ` Nathan
2011-01-01 18:05     ` Jan Burse
2011-01-01 23:56     ` The Quiet Center
2011-01-02  1:10       ` Jan Burse
2011-01-02  3:45       ` LanX
2011-01-02  4:04         ` LanX
2011-01-02  5:36           ` Nathan
2011-01-02  6:46             ` Paul Rubin
2011-01-02 15:01               ` LanX
2011-01-02 15:14                 ` Jerome Baum
2011-01-02 21:21                 ` Paul Rubin
2011-01-02  7:14             ` w_a_x_man
2011-01-02  6:59       ` w_a_x_man
2011-01-03 15:22         ` LanX
2011-01-02 14:07       ` Frank GOENNINGER
2011-01-07  9:41       ` w_a_x_man
2011-01-09 10:41         ` How to improve the readability of (any) LISP or any highlevelfunctional " WJ
2011-01-03 10:29     ` How to improve the readability of (any) LISP or any highlevel functional " Didier Verna
2011-01-03 13:05       ` Tim Harig
2011-01-04  4:49         ` rusi
2011-01-04  5:39           ` D Herring
2011-01-04  8:02             ` Tim Harig
2011-01-04  9:09               ` Nicolas Neuss
2011-01-04 10:00               ` Tim Bradshaw
2011-01-04 12:21                 ` Tim Harig
2011-01-04 12:23                   ` Tim Bradshaw
2011-01-04 13:33                     ` Tim Harig
2011-01-04 12:41                   ` Tamas K Papp
2011-01-04 23:32                 ` Tim X
2011-01-05  1:35                   ` [OT] LISP community advocacy [was Re: How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ?] Tim Harig
2011-01-05  9:52                   ` How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ? Tim Bradshaw
2011-01-04  6:24           ` Pascal J. Bourguignon
2011-01-04 15:43             ` Raffael Cavallaro
2011-01-04 10:18         ` Didier Verna
2011-01-01 22:50   ` girosenth
2011-01-02 21:11     ` Thien-Thi Nguyen
2011-01-03 16:21     ` José A. Romero L.
2011-01-03 18:03     ` jacko
2011-01-02 23:45   ` Chip Eastham
2011-01-01 18:27 ` How to improve the readability of Steve Revilak
2011-01-01 19:22 ` How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ? prad
2011-01-01 23:56 ` Pascal Costanza
2011-01-02  5:39   ` D Herring
2011-01-02 14:17     ` Frank GOENNINGER
2011-01-02 14:53       ` Jerome Baum
2011-01-02 14:58         ` LanX
2011-01-02 17:55     ` Bernd Paysan
2011-01-03 18:07   ` jacko
2011-01-02 12:59 ` Doug Hoffman
2011-01-02 19:14   ` w_a_x_man
2011-01-03  5:20     ` Elizabeth D Rather
2011-01-03 10:48     ` MarkWills
2011-01-03 15:13       ` LanX
2011-01-03 18:20     ` jacko
2011-01-03 18:22     ` jacko
2011-01-03 10:18 ` Didier Verna
2011-01-04  6:47 ` pineapple
2011-01-04 14:14   ` P.M.Lawrence
2011-01-05 14:58 ` Xah Lee
2011-01-05 16:21   ` Andrew Haley
2011-01-05 21:29     ` Pascal J. Bourguignon
2011-01-06  8:57       ` Jonathan Groll
2011-01-07  5:36     ` Xah Lee
2011-01-07 13:28       ` Andrew Haley
2011-01-07 16:19         ` Xah Lee
2011-01-05 17:59   ` Elena
2011-01-05 20:13     ` Xah Lee
2011-01-05 21:42     ` Pascal J. Bourguignon
2011-01-06 13:38       ` Doug Hoffman
2011-01-06 19:20         ` Pascal J. Bourguignon
2011-01-07  0:09           ` w_a_x_man
2011-01-07 12:04           ` Doug Hoffman
2011-01-06 22:59   ` Xah Lee [this message]
2011-01-07 11:47     ` Jan Burse
2011-01-21 10:03       ` rupertlssmith
2011-01-21 16:08         ` Jan Burse
2011-01-21 21:43           ` D. J. Penton
2011-01-18 22:55 ` m_l_g3
2011-01-19 13:45   ` Doug Hoffman
2011-01-21 16:15   ` Jan Burse
  -- strict thread matches above, loose matches on Subject: below --
2011-01-02 16:50 Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=370ce4b6-b0ed-4c84-b008-099226a6734b@j32g2000prh.googlegroups.com \
    --to=xahlee@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).