From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Jan Burse Newsgroups: gmane.emacs.help Subject: Re: How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ? Date: Fri, 07 Jan 2011 12:47:05 +0100 Organization: albasani.net Message-ID: References: <80ceeca0-1d32-47d1-ba96-feb4d9729c3a@v17g2000yqv.googlegroups.com> <370ce4b6-b0ed-4c84-b008-099226a6734b@j32g2000prh.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1294404053 11739 80.91.229.12 (7 Jan 2011 12:40:53 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 7 Jan 2011 12:40:53 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jan 07 13:40:49 2011 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PbBcd-0004tX-WA for geh-help-gnu-emacs@m.gmane.org; Fri, 07 Jan 2011 13:40:48 +0100 Original-Received: from localhost ([127.0.0.1]:52627 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PbBcd-0000D3-4q for geh-help-gnu-emacs@m.gmane.org; Fri, 07 Jan 2011 07:40:47 -0500 Original-Path: usenet.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!news2.euro.net!newsfeed.freenet.ag!news.albasani.net!not-for-mail Original-Newsgroups: comp.lang.functional, comp.lang.lisp, gnu.emacs.help, comp.lang.forth, comp.lang.prolog Original-Lines: 269 Original-X-Trace: news.albasani.net WfOEVJ3/3I7YuFrXCm4d4NCDsORx9PevHeJwZubOBKWtYfjXzgNEILhhzxjZV3kCiT1rarCMFfFZA2Y2BysRbg== Original-NNTP-Posting-Date: Fri, 7 Jan 2011 11:47:35 +0000 (UTC) In-Reply-To: <370ce4b6-b0ed-4c84-b008-099226a6734b@j32g2000prh.googlegroups.com> Cancel-Lock: sha1:IgkgPHx/iFzVbYHpEJmTd8dHcpo= User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.1.16) Gecko/20101123 SeaMonkey/2.0.11 Injection-Info: news.albasani.net; logging-data="0SSgvytBc7JOFqU9qSJwWIUZswDbNnleBHid0gnt4UNnUp+VkxClI3Dc8ZADpbtNbnQtX9zWw4AgC69RDzl5YOVZMoj+kywHsajUgj/rjI72hP6KPPX/+g8NVfg+KJUY"; mail-complaints-to="abuse@albasani.net" Original-Xref: usenet.stanford.edu comp.lang.functional:69143 comp.lang.lisp:297376 gnu.emacs.help:184097 comp.lang.forth:160419 comp.lang.prolog:44058 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:78290 Archived-At: Hi This is quite funny to write a tractate about operator definitions to comp.lang.prolog. Prolog has already for some time operator definitions. But some other programming language do have also. Best Regards See also: http://cs.union.edu/~striegnk/learn-prolog-now/html/node82.html#sec.l9.operators from http://www.learnprolognow.org/ Xah Lee schrieb: > On Jan 5, 6:58 am, Xah Lee wrote: >> On Dec 31 2010, 11:04 pm, girosenth 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/ ☄