The Northern Spy
June 2015
A First Look at Modula-2 R10
by
Rick Sutcliffe
|
|
Last month
the Spy opined on the software crisis--safety, security, and reliability having been abandoned for quick-and-dirty-get-it-out-the-door and let the customers do our debugging, software has become prey for the black hats. There are too many vulnerabilities in both commercial packages and the Internet, and one day this house of cards is going to come crashing down upon us all. We have sound software engineering techniques and universities such as the ivory basement where the Spy toils do teach them--but they are not used because too few developers are university trained, too many managers haven't a clue what those principles are, and have other priorities that do not necessarily eventuate in quality code, and too many people use tools that are not optimized for producing that quality code--even mitigate against doing so.
This month, the Spy will begin presenting a new dialect of Modula-2 that he and Telecom engineer Benjamin Kowarsch have been working on since 2008. Since recent inductees into the developer fraternity will not be familiar with Modula-2, we begin with the basics--not "hello world", but the simple solution to a class of problems often encountered in the sciences, and holding down a minor character position in one of the Spy's new novels.
Population systems generally grow or decay in the short term proportionally to the number of individuals. That is:
And, as every first year calculus student knows, such differential equations have solutions of the form:
where t is in time units, A0 is the initial population, and k is an experimental constant that is positive for growth and negative for decay. Of course, this may be simplistic, as other factors, such as maximum population loads and predation may complicate the model, but there are may problems, including continually compounding interest, radioactive and other decay, and short-term growth that do follow the simple model.
In the case of decay, it is often convenient to put things in terms of half-life, that is the time that it would take for half the population or quantity to decay. Here we have:
so that or whence where T is the half life.Thus, if either k or the half life is available (and tables of chemical properties publish both) the remaining amount of an initial quantity after a specified time can be easily computed. The following Modula-2 R10 program illustrates this solution, and some simple points about the syntax--both the familiar and the new.
(* This program demonstrates a simple Modula-2 R10 solution.
It computes exponential growth or decay amounts. *)
MODULE GrowthAndDecay;
IMPORT IOSupport;
FROM StdIO IMPORT stdIn, stdOut, WriteLn, SkipLine;
IMPORT ASCII, RealMath;
TYPE
ProblemType = (growth, decay);
VAR
initialAmount, finalAmount, k, time : REAL;
thisProblem : ProblemType;
ans : CHAR;
useHalfLife, doAgain : BOOLEAN;
BEGIN
useHalfLife := FALSE;
WRITE ("This program computes amounts of an exponentially growing or decaying population remaining after a specified time.\n");
WRITE ("Note that when providing numerical answers, real format must be used--a decimal is required.\n");
WRITE ("\nType 'G' for a growth problem or 'D' for a decay problem ==> ");
READ (ans);
SkipLine;
IF ASCII.toUpper (ans) = "G" THEN
thisProblem := growth
ELSE
thisProblem := decay
END;
IF thisProblem = decay THEN
WRITE ("\nTo use half life, type an 'H'; to use decay constant, type 'K'==> ");
READ (ans);
SkipLine;
useHalfLife := ASCII.toUpper (ans) = "H";
END;
WRITE ("\nVery well, what is the ");
IF useHalfLife THEN
WRITE ("half life? ==> ");
ELSE
WRITE ("constant k (include sign if negative) ==> ");
END;
READ (k);
SkipLine;
IF useHalfLife THEN
k := (-1.0)*RealMath.ln(2.0)/k
END;
WRITE ("\nSo provide the original amount ==> ");
READ (initialAmount);
SkipLine;
REPEAT
WRITE ("\nProvide the elapsed time ==> ");
READ (time);
SkipLine;
finalAmount := initialAmount*RealMath.exp(k*time);
WRITE ("\nGiven a starting amount of ");
WRITEF ( "F.3", initialAmount);
WRITE (", the amount after ");
WRITEF ( "F.3", time);
WRITE (" time units will be ");
WRITEF ( "F.3", finalAmount);
WriteLn;
WRITE ("\nDo another changing only elapsed time? (Y/N) ==> ");
READ (doAgain);
SkipLine;
UNTIL NOT doAgain;
WRITE ("\nExiting.\n");
END GrowthAndDecay.
Familiar points of Interest:
- Unlike many other languages, Modula-2 is strictly typed. This continues in R10, though we are more strict about typing. Note for instance that a real must be divided by 2.0, not by 2 (which is a whole type literal).
- Modula-2 is case sensitive, and this continues in R10.
- Modula-2 is modular. Most compilation units are either program modules or library modules. The latter have two parts--a definition, or interface, and an implementation. Program code can be compiled in the presence of the compiled definition modules, and the implementation of same done later, after the interface has been refined.
- Library items are imported into other module either qualified (IMPORT modulename) or unqualified (FROM modulename IMPORT item). Examples here include the modules ASCII and RealMath, among others.
- The very versatile IF--THEN..[ELSIF]*..ELSE..END construction, along with the original LOOP..EXIT..END, REPEAT..UNTIL, and WHILE..DO..END have been retained.
- Comments are delineated with (* comment brackets *), may run over several lines, and may be nested.
- Reserved words and standard identifiers are here presented in bold face, but this is merely for publication readability.
Changes and updates apparent here:
- In Wirth's Modula-2 calls to NEW and DISPOSE were automatically translated into calls to ALLOCATE and DEALLOCATE, which had to be imported (generally from Storage.) We call these Wirthian macros, and have greatly expanded the utility of this idea.
- Thus, READ, READNEW, WRITE, and WRITEF, along with several other standard procedure names, are Wirthian Macros. For instance, READ (item) is automatically translated into IO.Read (stdIn, item). In particular, READ (ans) becomes CharIO.Read (stdIn, ans).
- stdIn must be made visible by being imported.
- Any other File variable can be used instead of the defaults stdIn or stdOut.
- The module IO support aggregates all the IO procedures from the .IO modules, and re-exports them so they can be imported qualified in bulk with a single satement.
- A literal string may contain the escape sequence "\n", which outputs the OS-specific character(s) to generate a new line. The two other escape sequences are \t for tab and \\.
- WRITEF takes format strings and also defaults to the stdIn file. In the use shown here, "F.3" means generate a fixed point real numeral with three digits after the decimal.
- All identifications and simple manipulations of characters (including change of case) are located in ASCII.
Changes not apparent here:
- The FOR loop is now FOR variable [++|--] IN type DO..END, and has other variants. The iteration type may be an index (indirect iteration) or the type of the items being processed (direct iteration).
- All loops can have an EXIT statement.
- Library modules are extensible. RealIO is an extension of the REAL type, for instance.
- Programmers may create their own aggregator modules or extension modules.
- The automatic compile-time binding of Wirthian macros by simple substitution effectively allows for a low-cost, simple form of overloading.
- This binding can be performed for many operators, such as + - * / and structural elements such as FOR and []. However, arbitrary combinations of operators cannot be defined as new operators in the manner Apple has allowed in Swift, a practice we regard as opening a language up to abuse.
- We have kept the list of reserved words and standard identifiers (the latter also reserved) deliberately small, in the same order as the original, not as the much larger ISO dialect. To do this, we dropped some features, such as local modules and the WITH statement and merged all the type conversion operators and syntax into a single notation.
A Run from this program:
In the Spy's latest novel, time is somewhat malleable, for two alternate earths have developed a time difference of 30 days. But time is running faster on the one that lags, so the difference is decaying with a half life of 78 days. To check on the difference after 263 days, the Spy ran his program, with the input and output as shown. The units are irrelevant, as long as they are consitent.
This program computes amounts of an exponentially growing or decaying population remaining after a specified time.
Note that when providing numerical answers, real format must be used--a decimal is required.
Type 'G' for a growth problem or 'D' for a decay problem ==> d
To use half life, type an 'H'; to use decay constant, type 'K'==> h
Very well, what is the half life? ==> 78.0
So provide the original amount ==> 30.0
Provide the elapsed time ==> 263.0
Given a starting amount of 30.000, the amount after 263.000 time units will be 2.898
Do another changing only elapsed time? (Y/N) ==> n
Exiting.
In the following months
The Spy will continue discussing some of the interesting points of Modula-2 R10, including extensibility, generics, blueprints, binding, and some of the standard library modules that are to be included with every R10 distribution. To forestall one group of questions, no, a compiler is not yet available. Programs here, and for the upcoming Springer-Verlag book, are compiled using boostrapped libraries atop older compilers, then have their syntax morphed into R10 style using macros.
--The Northern Spy
Opinions expressed here are entirely the author's own, and no endorsement is implied by any community or organization to which he may be attached. Rick Sutcliffe, (a. k. a. The Northern Spy) is professor of Computing Science and Mathematics at Canada's Trinity Western University. He has been involved as a member or consultant with the boards of several community and organizations, and participated in developing industry standards at the national and international level. He is a co-author of the Modula-2 programming language R10 dialect. He is a long time technology author and has written two textbooks and nine alternate history SF novels, one named best ePublished SF novel for 2003. His columns have appeared in numerous magazines and newspapers (paper and online), and he's a regular speaker at churches, schools, academic meetings, and conferences. He and his wife Joyce have lived in the Aldergrove/Bradner area of BC since 1972.
Want to discuss this and other Northern Spy columns? Surf on over to ArjayBB. com. Participate and you could win free web hosting from the WebNameHost. net subsidiary of Arjay Web Services. Rick Sutcliffe's fiction can be purchased in various eBook formats from Fictionwise, and in dead tree form from Amazon's Booksurge.
URLs for Rick Sutcliffe's Arjay Enterprises:
The Northern Spy Home Page: http: //www. TheNorthernSpy. com
opundo : http: //opundo. com
Sheaves Christian Resources : http: //sheaves. org
WebNameHost : http: //www. WebNameHost. net
WebNameSource : http: //www. WebNameSource. net
nameman : http: //nameman. net
General URLs for Rick Sutcliffe's Books:
Author Site: http: //www. arjay. ca
Publisher's Site: http: //www. writers-exchange. com/Richard-Sutcliffe. html
The Fourth Civilization--Ethics, Society, and Technology (4th 2003 ed. ): http: //www. arjay. bc. ca/EthTech/Text/index. html
Sites for Modula-2 resources
Modula-2 FAQ and ISO-based introductory text: http://www.modula-2.com
R10 Repository and source code: https://bitbucket.org/trijezdci/m2r10/src
More links, Wiki: http://www.modula-2.net
p1 ISO Modula-2 for the Mac: http://modula2.awiedemann.de/
--The Northern Spy
|