Search                        Top                                  Index
TEACH SIMPLEMATHS                                        Aaron Sloman Oct 2011
                                                    Based on older teach files

               Familiar Simple Mathematical Operations in Pop11
                  An Introduction for experienced programmers



CONTENTS - (Use <ENTER> g to access required sections)

 -- Introduction
 -- Pop11 as a stack-based desk calculator
 -- Division can sometimes produce surprises
 -- There are also complex (imaginary) numbers with their own format
 -- Looping over numbers
 -- Adding tracing information to a numerical procedure
 -- More things to do

-- Introduction -------------------------------------------------------

Pop11/Pop-11/pop11 is an AI language whose origins go back to work by
Robin Popplestone around 1969. There is information about its origins in

    POP-11 Comes of Age: The Advancement of an AI Programming Language,
    Editor James A.D.W. Anderson, Ellis Horwood, Chichester, 1989,

That book is now out of print. The online Pop11 primer (version 4
October 2011) gives a more detailed overview for readers with
programming experience. If are are reading this in the Poplog editor Ved
or XVed, you can access the primer with the command

    ENTER teach primer RETURN

It is also available on the internet
    HTML
        http://www.cs.bham.ac.uk/research/projects/poplog/primer/

    Printable PDF
        http://www.cs.bham.ac.uk/research/projects/poplog/primer.pdf
        (About 287 printed pages -- may vary with edits)

    Printable PDF 2 pages per sheet
        http://www.cs.bham.ac.uk/research/projects/poplog/primer2.pdf
        (About 144 printed pages -- may vary with edits)

Like Common Lisp and several other AI languages, Pop11 provides
considerable support for numerical entities and operations on them. The
entities include small integers, big integers (size limit depends on
available memory), short and long floating point numbers (referred to as
"reals" in Pop11), indefinite precision ratios, and complex (imaginary)
numbers. There is also a Pop11 interface to the BLAS and LAPACK linear
algebra packages produced by David Young at Sussex University, and
included with Poplog as part of the Popvision library. This supplies
many of the features of packages like MATLAB, easily combined with other
AI tools.

This teach file introduces a tiny subset of those mathematical
capabilities for new users looking for some familiar constructs. The
ideas will be presented mainly in the form of examples that can be
compiled and run in the editor, then modified, then re-run, etc. More
detailed information is available in TEACH files, HELP files and REF
files, some of which are referred to at the end, and in the Primer.

If you have not yet looked at you may find this short introduction
to the editor useful:
    ENTER teach minived RETURN

(or put the editor cursor after "teach" and type ESC h).

-- Pop11 as a stack-based desk calculator -----------------------------

Add some numbers, using '=>' to print out the result:

(use ESC d on this line):

    10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25 =>

Your output.p file should show this:

    ** 280

Try a numerical expression using different operators:

    10+11*12 =>
    ** 142

spaces between numerals and operator symbols  make no difference:

    10 + 11 * 12 =>
    ** 142

What should this do. Try it?
    (10+11)*12 =>

You can include decimals (reals, floats) instead of only integers:

    10+11*12.0 =>
    ** 142.0

    3.6 + 4.2 =>
    ** 7.8

If a non-integer is involved, its type takes over.

Or this
    (10 + 11)*12.0 =>

-- Division can sometimes produce surprises ---------------------------

    12/4 =>
    ** 3

This one is not exact, so it produces a 'ratio' 12 5ths:

    12/5 =>
    ** 12_/5

So does this

    24/10 =>
    ** 12_/5

Ratios are automatically reduced by dividing numerator and denominator
by highest common factor. In that case 2.

    3 + 4/5 =>
    ** 19_/5

Try some others.

If a decimal is involved, it dominates:

    3.0 + 4/5 =>
    ** 3.8

or the other way round:

    4/5 + 3.0 =>
    ** 3.8

-- There are also complex (imaginary) numbers with their own format ---

The sqrt (square root) function may produce some surprises:

    sqrt(100) =>
    ** 10.0

    sqrt(-100) =>
    ** 0.0_+:10.0

That's a complex number with real part 0.0 and 'imaginary part' 10.

The square root of -1 is generally represented is "i" in maths, but
pop11 represents as (in effect) 0.0 + i*1.0

    sqrt(-1) =>
    ** 0.0_+:1.0

-- Looping over numbers -----------------------------------------------

Our original expression

    10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25 =>

can be expressed as a loop, using a variable e.g. num, to take the
successive values and then repeatedly add them to another
variable, total, which starts from 0:

    vars num, total;
    0 -> total;
    for num from 10 by 1 to 25 do total + num -> total endfor;
    total =>

Mark those four lines using
    Mark first: F1 (or ESC m) and
    Mark Last:  F2 (or esc M)

Then compile the range (CTRL-d).

We can create a pop11 procedure (sometimes called a function) that will
do that sort of thing for any start number and any increment, added
repeatedly until some target is reached or passed:

    define add_range(start, increment, target) -> total;

        0 -> total;

        for start from start by increment to target do
            total + start -> total
        endfor;

    enddefine;

Mark that range (F1, and F2) then compile it (CTRL-d).

Now test it, first with our original sum:

    add_range(10, 1, 25) =>
    ** 280

same result as before.

Will this give a bigger or a smaller total?

    add_range(10, 2, 25) =>

Try other values. Try defining a procedure

    multiply_range(start, increment, target)

that repeatedly increments a multiplier and produces a result.

You can test it on things like

multiply_range(5, 1 8):

    5*6*7*8 =>
    ** 1680

Does it give the right answer for this:

    10*11*12*13*14*15*16*17*18*19*20*21*22*23*24*25 =>

i.e.

    ** 42744736671436800000

-- Adding tracing information to a numerical procedure ----------------


If you wish to know what add_range is doing each time round the loop you
can easily make it print its values, by constructing a list of
information to be printed out after the addition is done.

Use the fact that a list like this

    [start ^start, total ^total] =>

Will print out in the format

    [start <value of start> , total <value of total>]

So try compiling and testing this:

    define add_range(start, increment, target) -> total;

        0 -> total;

        for start from start by increment to target do

            total + start -> total;

            [start ^start, total ^total] =>

        endfor;

    enddefine;

What should these print?

    add_range(2, 2, 10) =>

    add_range(-2, 2, 10) =>

This may surprise you:
    add_range(-10, 0.5, 10) =>

Try some others. If you wish you can add another print instruction
before the addition is done in the loop.

The tracing instructions can be commented out by putting three
semi-colons before the instructions. E.g.

    ;;; [start ^start, total ^total] =>

In pop11 anything after ';;;' to the end of the line is ignored.

Like several other programming languages Pop11 also allows /* ... */
as comments, stretching over several lines, e.g.

/*
    ;;; This can be enabled for tracing purposes
    [start ^start, total ^total] =>

*/

You can mark (F1, F2) and compile (ESC -d) that but nothing will happen.

-- More things to do --------------------------------------------------

Now try
    TEACH * ARITH
        Put the cursor on the asterisk, and type ESC h,
        or do ENTER teach arith RETURN

For revision on using the editor try
    TEACH * ESSENTIALKEYS
    TEACH * MINIVED

The HELP * MATH
        file gives you the following contents:

     MATHEMATICAL OPERATIONS AND PROCEDURES IN POP-11

      -- Types of numbers available in POP-11
      -- Recogniser procedures
      -- Arithmetical operators
      -- Comparison procedures (predicates)
      -- Operations on Ratios
      -- Operations on Complex numbers
      -- Bitwise operations
      -- Floating point utilities
      -- Trigonometric and other utilities
      -- Representation and efficiency
      -- Fast integer procedures
      -- Global variables and constants
      -- LIB FLOAT_PARAMETERS
      -- LIB INT_PARAMETERS

There's lots more.

If you are either using a linux machine locally, or using XMing from a
windows machine to connect to this, you can try out some of Pop-11's
graphical facilities, as demonstrated here, using numerical
coordinates to control what is drawn:

    TEACH * GSTART
        Some standard graphical stuff, e.g. drawing lines.
    TEACH * FACES
        How to make faces out of coloured blobs.

See also
    http://www.cs.bham.ac.uk/research/projects/poplog/examples

--- $usepop/pop/teach/simplemaths
--- Copyright University of Birmingham 2011. All rights reserved.