pil

3 posts

In the final part of this series, let's create a real program in PIL.

The problem

We will implement arabic to roman number conversion from Rosetta Code.

The algorithm we're going to use is similar to the one used there for BASIC:

• Have a table of all distinct roman numbers ordered by size, including the -1 variants like IV. So roman(0) = "M", roman(1) = "CM", roman(2) = "D" etc.
• Have another table with the same indices for their arabic equivalents. arabic(0) = 1000, arabic(1) = 900, arabic(2) = 500 etc.
• Loop through each index. For each, if the input value is greater than the value of the arabic table at that value, accumulate the roman equivalent at the end of the output string and decrease the input value by the arabic amount. Keep doing this until the remaining input value is smaller than the arabic number.
• So for input 2900 the steps would be
• index 0, output -> "M", input -> 1900
• index 0, output -> "MM" , input -> 900
• index 1, output -> "MMCM", input -> 0 and end

The solution

As PIL is an interpreted language I'll show a lightly reformatted transcript of my session as I build up the program in separate parts (and make mistakes along the way). Let's get started!

# \$run *pil
# Execution begins   20:09:19

The tables

First we need to set up the tables for arabic numbers in part 1. I will use the number command so that PIL prompts me with line numbers followed by an underscore automatically.

*number 1, 0.01
&*1.0  _arabic(0) = 1000
&*1.01 _arabic(1) = 900
&*1.02 _arabic(2) = 500
&*1.03 _arabic(3) = 400
&*1.04 _arabic(4) = 100
&*1.05 _arabic(5) = 90
&*1.06 _arabic(6) = 50
&*1.07 _arabic(7) = 40
&*1.08 _arabic(8) = 10
&*1.09 _arabic(9) = 9
&*1.10 _arabic(10) = 5
&*1.11 _arabic(11) = 4
&*1.12 _arabic(12) = 1
&*1.13 _\$unnumber

The unnumber command exits numbered line prompting mode. It needs to be prefixed with \$ to be executed immediately rather than be entered as part of the program.

Let's run that immediately so we can check it looks correct

*do part 1
*type arabic
arabic(0) =  1000.0
arabic(1) =  900.0
arabic(2) =  500.0
arabic(3) =  400.0
arabic(4) =  100.0
arabic(5) =  90.0
arabic(6) =  50.0
arabic(7) =  40.0
arabic(8) =  10.0
arabic(9) =  9.0
arabic(10) =  5.0
arabic(11) =  4.0
arabic(12) =  1.0

We can then do the same for the roman numbers.

*number 2, 0.01
&*2.0 _roman(0) = "M"
&*2.01 _roman(1) = "CM"
&*2.02 _roman(2) = "D"
&*2.03 _roman(3) = "CD"
&*2.04 _roman(4) = "C"
&*2.05 _roman(5) = "XC"
&*2.06 _roman(6) = "L"
&*2.07 _roman(7) = "XL"
&*2.08 _roman(8) = "X"
&*2.09 _roman(9) = "IX"
&*2.1 _roman(10) = "V"
&*2.11 _roman(11) = "IV"
&*2.12 _roman(12) = "I"
&*2.13 _\$unnumber
*do part 2

The main loop

Let's now make the main loop to convert the number. We'll do it in three parts, first the loop over the indices. I put in some comments fir the function.

*number 5, 0.01
&*5.0 _* Main entry point to arabic -> roman converter
&*5.01 _* Input: a (arabic number to convert)
&*5.02 _* Output: r (roman number equivalent of a)
&*5.03 _for i = 0 to 12: do part 6
&*5.04 _done
&*5.05 _\$unnumber

Next, the loop for each arabic number. We can use a for with a dummy variable and the while controlling how often it is run.

*number 6, 0.01
&*6.0 _for j = 0 while a >= arabic(i): do part 7
&*6.01 _done
&*6.02 _\$unnumber

Finally, in part 7 build up the roman number string and decrease the arabic number.

*number 7, 0.01
&*7.0 _r = r + roman(i)
&*7.01 _a = a - arabic(i)
&*7.02 _done
&*7.03 _\$unnumber

Let's see what these look like now.

*type part 5, part 6, part 7

5.0    * Main entry point to arabic -> roman converter
5.01   * Input: a (arabic number to convert)
5.02   * Output: r (roman number equivalent of a)
5.03   for i = 0 to 12: do part 6
5.04   done

6.0    for j = 0 while a >= arabic(i): do part 7
6.01   done

7.0    r = r + roman(i)
7.01   a = a - arabic(i)
7.02   done

Trying it out

We can set up the input number in a then call part 5 to convert. The output should go into r.

*a = 13
*do part 5
Error at step 7.0: r = ?

Ah, r is not initialised so cannot be appended to. We can patch part 5 and try again.

*5.025 r = ""
*do part 5
*type r
r = "XIII"
*type a
a =  0.0

Great! There is a side effect though, the input value in a is wiped out as PIL does not have local variables.

Thinking about it, we are relying on the tables being initialised when we run part 5. We should really make it stand-alone by calling part 1 and 2 first.

*5.026 do part 1
*5.027 do part 2

Making it interactive

We should have a way to prompt for a number and then display the conversion.

*number 10, 0.01
&*10.0 _demand a
&*10.01 _do part 5
&*10.02 _type r
&*10.03 _\$unnumber

*do part 10
& a = ? _1992
r = "MCMXCII"

Unit tests!

It may be anachronistic, but we should have some unit tests to see if the conversion works. First let's define a unit test handler in part 20 that takes the arabic number in a, the expected result in rExpected and then checks this matches.

*number 20, 0.01
&*20.0 _do part 5
&*20.01 _if r = rExpected, then type "OK", r; else type "ERROR', r, rExpected
Error at step 20.01: SYMBOLIC NAME TOO LONG
&*20.02 _if r = re, then type "OK", r; else type "ERROR", r, re
&*20.03 _done
&*20.04 _\$unnumber

rExpected is too long for a variable number so we use a shorter name instead, re.

Let's test the tester out.

*re = "XLII"
*a = 42
*do part 20
Error at step 20.01: SYMBOLIC NAME TOO LONG

Ah, the bad line is still there, so delete that and try again.

*delete step 20.01
*do part 20
ERROR
r = ""
re = "XLII"

Wait, that's not right, why is the output in r blank?

*type r
r = ""
*type a
a =  0.0

Oh OK, a is clobbered. Let's set it up again.

*a = 42
*do part 5
*type r
r = "XLII"
*do step 20.02
OK
r = "XLII"
*do step 20.02
OK
r = "XLII"
*type part 20

20.0    do part 5
20.02   if r = re, then type "OK", r; else type "ERROR", r, re
20.03   done

*a = 42
*re = "XLII"
*do part 20
OK
r = "XLII"

That fixed it. Try the error case.

*a = 42
*re = "XXX"
*do part 20
ERROR
r = "XLII"
re = "XXX"

With that done, set up the tests.

*number 21, 0.01
&*21.0 _a = 2009
&*21.01 _re = "MMIX"
&*21.02 _do part 20
&*21.03 _a = 1666
&*21.04 _re = "MDCLXVI"
&*21.05 _do part 20
&*21.06 _a = 3888
&*21.07 _re = "MMMDCCCLXXXVIII"
&*21.08 _do part 20
&*21.09 _done
&*21.1 _\$unnumber

And run them.

*do part 21
OK
r = "MMIX"
OK
r = "MDCLXVI"
OK
r = "MMMDCCCLXXXVIII"

All green. However we did not test all cases such as zero, negative numbers, non-integral numbers etc.

To confirm the program is all done and we are not relying on anything in the environment, save it to disk, quit and come back into PIL and try re-running.

*create "roman.pil"
FILE "ROMAN.PIL" IS CREATED
*save as "roman.pil", all parts
SAVE COMPLETED
*stop
# Execution terminated   18:51:16  T=0.279

# \$run *pil
# Execution begins   18:51:37
*do part 10
& a = ?  _42
r = "XLII"
*do part 21
OK
r = "MMIX"
OK
r = "MDCLXVI"
OK
r = "MMMDCCCLXXXVIII"
*stop

The complete listing

*type all parts

1.0    arabic(0) = 1000
1.01   arabic(1) = 900
1.02   arabic(2) = 500
1.03   arabic(3) = 400
1.04   arabic(4) = 100
1.05   arabic(5) = 90
1.06   arabic(6) = 50
1.07   arabic(7) = 40
1.08   arabic(8) = 10
1.09   arabic(9) = 9
1.1    arabic(10) = 5
1.11   arabic(11) = 4
1.12   arabic(12) = 1

2.0    roman(0) = "M"
2.01   roman(1) = "CM"
2.02   roman(2) = "D"
2.03   roman(3) = "CD"
2.04   roman(4) = "C"
2.05   roman(5) = "XC"
2.06   roman(6) = "L"
2.07   roman(7) = "XL"
2.08   roman(8) = "X"
2.09   roman(9) = "IX"
2.1    roman(10) = "V"
2.11   roman(11) = "IV"
2.12   roman(12) = "I"

5.0    * Main entry point to arabic -> roman converter
5.01   * Input: a (arabic number to convert)
5.02   * Output: r (roman number equivalent of a)
5.025  r = ""
5.026  do part 1
5.027  do part 2
5.03   for i = 0 to 12: do part 6
5.04   done

6.0    for j = 0 while a >= arabic(i): do part 7
6.01   done

7.0    r = r + roman(i)
7.01   a = a - arabic(i)
7.02   done

10.0    demand a
10.01   do part 5
10.02   type r

20.0    do part 5
20.02   if r = re, then type "OK", r; else type "ERROR", r, re
20.03   done

21.0    a = 2009
21.01   re = "MMIX"
21.02   do part 20
21.03   a = 1666
21.04   re = "MDCLXVI"
21.05   do part 20
21.06   a = 3888
21.07   re = "MMMDCCCLXXXVIII"
21.08   do part 20
21.09   done

Final thoughts

JOSS is a simple but well designed language - it's easy to pick up, has a carefully chosen set of features and does the job it's supposed to do well. Compared to BASIC it seems much more intuitive as a simple language for non-specialists who want to get numeric calculations done quickly. The lack of functions and local variables, plus the heavily interactive nature of the language makes it harder to write larger programs, but given the first version was running in 1963 it's quite an impressive feat of engineering.

PIL, the version of JOSS implemented on MTS, improves the usability of the original language, eg by not requiring a period at the end of each statement. There is enough integration with the operating system to make it usable. It would be interesting to know what type of use it got at UM.

Several languages were inspired by JOSS, including FOCAL on PDP-8s. It's also one of the influences on MUMPS, which is still in use today.

Further information

Full source code for this program can be found on github.

In the last post we saw some of the history of PIL and how to run it on MTS. We'll now take a closer look at the features of PIL. Examples shown can be entered directly into PIL after starting it with \$run *pil.

Direct mode

PIL starts up in direct mode, where statements entered are immediately executed when you press RETURN. * is used by PIL as the prompt to enter input. You can use the TYPE statement and simple arithmetic expressions to make PIL act as a calculator:

* type 22 * 2
22 * 2 =  44.0
* type 2 ** 16
2 ** 16 =  65536.0

On MTS, PIL is case-insensitive for keywords and lines can optionally end with a period. Errors are immediately reported, usually starting with Eh?.

* TYPE 2+2.
2+2 =  4.0
* TYPE
Eh? IMPROPERLY FORMED STATEMENT

If the last character entered on the line is - then input will continue on the next line before it is executed. The prompt will change to & to show this continuation. * at the start of line can be used to make comments.

* type 1 + 2 + 3 +-
& 5 + 6
1 + 2 + 3 +5 + 6 =  17.0
* * comment here
*

Variables and types

Variables can be introduced by the optional keyword SET followed by a variable name and an assignment.

* set a = 2
* b = 3
* type a, b, a+b
a =  2.0
b =  3.0
a+b =  5.0

PIL understands three types: numerical, which are stored as floating point values, character strings and Boolean values.

* a = 1 / 3
* b = "PIL"
* c = The True
* type a, b, c
a =  0.3333333
b = "PIL"
c = The True

Booleans constants are 'The True' or 'The False' - something I've not seen in any other language. Strings can be up to 255 characters long and can be entered with single or double quotes as delimiters. Floats have 7 digits of precision and can be entered with exponential notation. Volume 12 mentions type 9.999999e64 as the maxomum value but on the version I'm running it seems 62 is the maximum exponent.

* type 9.999999e62
9.999999e62 =  9.999999E+62
* type 9.999999e63
Eh? EXPONENT OUT OF RANGE

Variable names are up to 8 characters long, are case sensitive and are distinguished from keywords, as this silly example shows.

* set set = 1
* set SET = 2
* type set, SET
set =  1.0
SET =  2.0

Arrays are allowed with any number of dimensions, though once set the number of dimensions cannot be changed.

* x(1, -2) = 3
* x(3, 44.0) = 4
* type x
x(1,-2) =  3.0
x(3,44) =  4.0
* x(3,4,5) = 42
Eh? ??-

Expressions

Arithmetic expressions work mostly as expected. The absolute value can be taken by surrounding an expression with |; exponentiation is done with **. Functions for arithmetic operations such as square root, cosine, log etc generally have a short and long form and do not need parentheses unless needed to resolve ambiguities.

* type 1 + |-41|
1 + |-41| =  42.0
* type the square root of 9
the square root of 9 =  3.0
* type sqrt of 16
sqrt of 16 =  4.0
* type sqrt of 25+1
sqrt of 25+1 =  6.0
* type sqrt of (25+1)
sqrt of (25+1) =  5.09902

There are functions for min/max, random numbers and extracting parts of a number, as well as special functions to get time (in 300ths of seconds since midnight), date, cpu/elapsed time and storage used.

* type the min of (1,2,3)
the min of (1,2,3) =  1.0
* type the time, the date, the elapsed time, the cpu time
the time =  1.963908E+07
the date =  17133.0
the elapsed time =  7.819461E+07
the cpu time =  464.0
* type the total size, the size
the total size =  188.0
the size =  165.0

Boolean functions are similar to other languages, with # standing in for logical or.

* type 2 >= 3
2 >= 3 = The False
* type 2 \$lt 3
2 \$lt 3 = The True
* type the true # the false
the true # the false = The True
* type the true & the false
the true & the false = The False

Character expressions include length, case conversion, comparison and extraction.

* type the l of "abc"
the l of "abc" =  3.0
* type the upper of "abC"
the upper of "abC" = "ABC"
* type the first 3 characters of "abcde"
the first 3 characters of "abcde" = "abc"
* type 2 \$fc "abcde" + "X" + 1 \$lc "abcde"
2 \$fc "abcde" + "X" + 1 \$lc "abcde" = "abXe"
* type "aaa" > "AAA"
"aaa" > "AAA" = The False

Finally, the type of an expression can be found and run time evaluation performed.

* type the mode of 42, the mode of the true, the mode of "abc"
the mode of 42 =  1.0
the mode of the true =  2.0
the mode of "abc" =  3.0
* type the value of "2*21"
the value of "2*21" =  42.0

Control flow

There's an IF statement with an optional ELSE clause. The IF and ELSE can be omitted but the punctuation is required.

* if 1 < 2, then type 'yes'; else type 'no'
yes
* if 1 > 2, type 'yes'; type 'no'
no

For loops have a range or an increment and an optional clause to terminate.

* for i = 1 to 5: type i ** 2
i ** 2 =  1.0
i ** 2 =  4.0
i ** 2 =  9.0
i ** 2 =  16.0
i ** 2 =  25.0
* for i = 1 by 2 to 10: type i ** 2
i ** 2 =  1.0
i ** 2 =  9.0
i ** 2 =  25.0
i ** 2 =  49.0
i ** 2 =  81.0
* for i = 1 by 2 while i < 20: type i
i =  1.0
i =  3.0
i =  5.0
i =  7.0
i =  9.0
i =  11.0
i =  13.0
i =  15.0
i =  17.0
i =  19.0

Indirect mode

Lines entered that start with a number are treated as stored instructions, broken down by part and step, that can be run later. Here we define a program in part 1 consisting of four steps.

* 1.0 i = 5
* 1.1 type i
* 1.2 i = i * i
* 1.3 type i

This can then be run with DO, which will execute all steps in a part.

* do part 1
i =  5.0
i =  25.0

Variables set in a program remain after execution is completed and it is possible to run a single step at a time.

* type i
i =  25.0
* do step 1.2
* do step 1.3
i =  625.0

Steps can call other parts with DO: execution will resume after the part is finished. It's also possible to transfer execution with TO which will not return. DONE will return from the current part.

* type part 2, part 3

2.0    i = 3
2.1    do step 3
2.2    i = 5
2.3    do step 3
2.4    i = 7
2.5    to step 3
2.6    type 'not reached'

3.0    type i
3.1    done
3.2    type 'also not reached'

* do part 2
i =  3.0
i =  5.0
i =  7.0

The above also shows it is possible to list out programs with TYPE PART. You can see all entered parts with TYPE ALL PARTS. With TYPE ALL STUFF you will see all variables and parts defined.

DELETE can be used to remove a step or a variable definition.

I/O and system access

We've seen TYPE used to display variables. You can prompt for a value to be entered with DEMAND

* delete part 1
* 1.0 demand i
* 1.1 i = i * 10
* 1.2 type i
* do part 1
i = ?_ 3
i =  30.0

There is also a formatting facility for input and output that is covered in the manual.

Programs can be saved to disk for future list with SAVE. This takes a file name (which must already exist) and what to save. For example:

save as 'x.pil', all stuff

will save all parts and variables to x.pil. File format is plain text so it can be edited outside of PIL if needed. LOAD 'x.pil' will then load the file back into PIL.

The implementation of PIL on MTS includes access to system facilities such as file creation and device access. For example, CREATE 'x.pil' will create a new file.

In the next post we'll see how to construct a larger PIL program.

Further information

MTS volume 12 has a complete reference to PIL as implemented on MTS.

If you can find a copy of the book 'History of Programming Languages', edited by Richard Wexelblat, this has a great section about JOSS talking about its design principles. Screenshot of a teletypewriter running PIL, from 2 4 Using the Michigan Terminal System 6 01.

In this series of posts we'll look at the Pittsburgh Interpretative Language, or PIL, a simple interpreted language that can be used to do calculations and build small programs interactively.

The Pittsburgh Interpretative Language

PIL was developed at the University of Pittsburgh for the System/360 in the late 60s. It was based on one of the first interpreted languages JOSS that originated at the RAND Corporation in 1963. PIL improves on JOSS by providing improved debugging capabilities and error reporting.

The design goals for PIL were, according to MTS Volume 12:

PIL is oriented toward problem-solving, with program development and debugging facilities having highest priority. For the beginning user, PIL was designed to be clear, unambiguous, and hence, easily learned. For the experienced programmer, the language offers increased flexibility with statement structure and expanded capabilities for the solution of non-numeric problems. For the researcher, PIL reduces the amount of time and effort that must be expended in problem solving.

PIL can be used as a simple desktop calculator with variables

* set a = 3
* set b = 4
* type the square root of (a*a + b*b)
the square root of (a*a + b*b) =  5.0

It can also be used to build simple programs interactively that can then be run:

* 1.01 demand a
* 1.02 demand b
* 1.03 type the square root of (a*a + b*b)
* do part 1
a = ?_ 3
b = ?_ 4
the square root of (a*a + b*b) =  5.0

PIL on MTS

PIL on MTS is based on the second version of PIL, PIL/2. It was modified to integrate well with MTS files and system services.

Prerequisites

No special installation instructions to get this language running - just do the standard D6.0 setup as described in this guide and then sign on as a regular user such as ST01.

Running a program using *PIL

Running the command *PIL on MTS will start the PIL interpreter. It is intended to be used in an interactive way where you enter commands and see the output directly. It can be used in batch mode by feeding commands into source but this is not the intended mode of operation. Inside the interpreter, programs and data can be loaded and saved with the load and save as statements.

Hello world

Let's see how to run a simple program to print 'Hello, world!' five times using PIL.

# \$run *pil
# Execution begins   09:56:53
* for i = 1 to 5: type "Hello, world!"
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
* stop
# Execution terminated   09:59:06  T=0.004

After starting PIL, enter the for statement at the * prompt. Output is shown immediately after the command is entered. Type stop to return to MTS.

In the next post we'll look at the language in more detail.

Further information

MTS volume 12 has a tutorial and reference for the PIL language and describes how it is integrated with MTS.

The video pictured at the top of this post, 2 4 Using the Michigan Terminal System 6 01, shows simple operation of PIL on a teletypewriter, starting from 21 minutes into the video.

The Pitt time-sharing system for the IBM system 360: Two year's experience. describes the operation of PIL at the University of Pittsburgh.

JOSS: Introduction To A Helpful Assistant is a very readable paper from the RAND Corporation on the language that inspired PIL.

The source code and implementation notes for PIL can be found in component 566 on tape 6.0T2 in the D6.0 distribution.

Source code for the hello world program can be found on github.