While loop
Yves Jaradin
yves.jaradin at uclouvain.be
Fri Jul 4 14:27:22 CEST 2008
Since my last email about this didn't produce any negative feedback, I
assume that we can move forward. Therefore, here is a formal MEP to
introduce while loops.
Cheers,
Yves
Yves Jaradin wrote :
> Hi,
>
> We at UCL would like to introduce a while loop construct in Mozart of
> the form:
>
> while Expression do Statement end
>
> This has already been discussed a lot on this mailing list (e.g.
> http://lists.gforge.info.ucl.ac.be/pipermail/mozart-hackers/2002/000244.html)
> but no consensus was reached at the time. Here are the (main) arguments:
>
> PRO:
> Easy to use
> Easy to teach/learn
> Much less awkward than "for while:"
>
> CONS:
> The for loop can provide equivalent behaviour
> Yet another keyword
>
> On the equivalent behaviour part, this is true but not unique at all.
> Lots of constructs are redundant in the language. The "if" can be
> completely replaced by "case", "local" by "(", etc. Essentially, this
> is what the language is about ; being multi-paradigm means that we can
> use the most natural way to express a program and not the single way
> in which it is possible to write it.
>
> On the "yet another keyword" part, this is the only keyword addition
> which was asked in more than six years. This should make clear that
> the fear of keyword explosion (breaking lots of programs using them as
> unquoted atoms) is probably not justified. As for the programs that
> use while as an atom, the only one in the Mozart svn is ozmake which
> use it in only in the "for while:" construct, all cases being possible
> to replace with a simple while loop.
>
> Comments are welcome.
>
> Cheers,
> Yves
> _________________________________________________________________________________
>
> mozart-hackers mailing list
> mozart-hackers at mozart-oz.org
> http://www.mozart-oz.org/mailman/listinfo/mozart-hackers
-------------- next part --------------
MEP: XXX
Title: Syntactically light while loop
Author: Yves Jaradin <yves.jaradin at uclouvain.be>, Peter Van Roy <peter.vanroy at uclouvain.be>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 5 Jun 2008
Abstract
========
The purpose of this MEP is to add a new construct to the language. ::
while Expression do Statement end
Motivation
==========
The current syntactic support for while loops is::
for while: Expression do Statement end
This doesn't look right as intuitively this isn't a for loop.
Therefore people resent the language as and odd, alien creation.
This is particularly problematic to teach programming concepts as it focus the student's attention to syntactical peculiarities.
Other users might also have a hard time even realising that this structure is allowed. This might lead to less readable code using explicit recursive procedures.
Specification
=============
The status of the lexical element ``while`` would change from atom to keyword.
A new statement is added to the language::
<statement> += while <expression> do <in-statement> end
The semantic of this statement is defined by the rewrite rule::
while <Expr> do <Stat> end ==> local <P> in
proc{<P>}
if <Expr> then
<Stat>
{<P>}
end
end
{<P>}
end
where P is not a free identifier in <Expr> or <Stat>.
Rationale
=========
A while loop is a very common feature of most programming languges. It's lack in Oz makes the language feel defective to users. This feeling is wrong as the for loop in Oz is very powerful, much more than the for loops in other languages, C included. The power of the for loop is such that every while loop can be written as a for loop.
However, the features of the for loop are not frequently used. Many new users don't even suspect their existence and use only the basic iterators.
A simple while loop is therefore advocated rather than a complex catch-all loop construct, so that most user will know it and use it. The for loop will remain available with all it's possibilities for the power user.
Backwards Compatibility
=======================
Since this proposal introduce a new keyword (``while``), all code using the atom 'while' unquoted is broken, including code using it as a feature in a for loop. This doesn't seem to be a real problem as most user avoid using unquoted keyword-like atoms. The only instance of unquoted while atoms in the Mozart distribution have been either as a feature in a for loop (only found in ozmake and all cases could easily just use the proposed while loop) and in the compiler implementation of the for loop construct and of the previous (and deprecated) loop macro construct.
Reference implementation
========================
A reference implementation of this proposal has been made and is used by the authors. It will be made available as patches or a branch if required.
Copyright
=========
This document has been placed in the public domain.
Votes
=====
::
Raphaël Collet []
Denys Duchier []
Seif Haridi []
Torbjörn Lager []
Konstantin Popov []
Camilo Rueda []
Gert Smolka []
Peter Van Roy []
More information about the mozart-hackers
mailing list