Programming with Algebraic Structures: Design of the Magma Language

19 downloads 0 Views 623KB Size Report
Greg Butler, John. Cannon, Robin Deed and Jim Richardson in 1987. Jim. Richardson implemented a prototype lparser for the lan- guage in 1987, while Robin.
Programming

with Algebraic

Structures:

Design of the Magma Language Wieb

Bosma

John

Cannon

Graham School

of Mathematics

Matthews

and Statistics,

Sydney,

iVSW

2006,

Abstract

University Australia

classifying

all structures

set of axioms. MAGMA

is a new software

algebra,

number

centred The

theory

on the concept

use of algebraic

system

for

and geometry of algebraic structure

computational

structure

(magma).

concern

are used for the basic data types.

of their

which

tures,

objects

themselves

and their

ysis of entire

The result is a power-

deals with

satisfy

some (interesting)

successes of this approach

of associative algebras, of finite simple groups.

paradigm

provides a natural strong typing mechanism. Further, structures and their morphisms appear in the language Standard mathematical notions as first class objects. ful, clean language

that

Notable

in a math-

and the very recent classification Classification problems t ypically

with

solution

structures,

elements,

Examination

that

categories

of algebraic

usually

requires

rather

then just

computer

reveals that

most of them were designed

tion

order computation.

this

as Macsyma,

This

conceptual

from those underlying

1

computer

significantly

algebra

systems.

Structural is

roughly Thus,

instructive according

Computation to

classify

algebraic

MOD Z/mZ

computations

to the degree of abstraction

a first order computation

tures and substructures. is one in which

egories of algebraic computation

will

ment calculations,

Finally,

a third

the operations structures.

ultimately

Although reduce

it will usually

effort unless support is provided language. The paradigm that has driven bra over the past century

purpose

entire

affects

of doing

of) several

cat-

such [13]

are elements of one structures. Indeed,

to live in a fixed

a flag.

every

integer

calculus

algebraic

cise identification

a structural

object

to a series of ele-

involve

the no-

systems

differential

structure

For example,

is of-

the global

calculation).

In Maple,

in a fixed

structure

by setFor the this may

be a valid approach. However, the power of modern algebra comes from the interaction between (elements

order compu-

involve

systems

around

[6] and Mathematical

in a non-elementary

by setting

algebra

calculation in a residue class ring is achieved ting a parameter on each function invocation.

of cal-

anal-

consideration

flag in Macsyma is used to simulate arithmetic in (often with strange results since the current value

of MOD

involved.

consists entirely

Maple

are assumed

Calculation

ten fudged

culations with elements of some algebraic structure; the structure itself can be essentially ignored. A second order computation involves explicit calculation with structation

Reduce,

most objects ring.

detailed

Further,

assume that all algebraic objects of a small number of elementary

Introduction

1.1 It

other

base differs

of jirst

struc-

is, second order computation.

of the major

ematically rigorous manner. The conceptual and implementation ideas behind MAGMA will be examined in paper.

have

been the classification of all simple Lie algebras over the field of complex numbers, the Wedderburn classification

whose design is

as a design

of Sydney

belongs.

of domains

a great deal of

structures

A major

and categories

the basis for a strongly

by the programming

tas [11]). structures

most research in alge-

has been the idea of precisely

Permission to copy w“thout fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association of Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. iSAAC 94- 7/94 Oxford England UK @ 1994 ACM 0-89791 -636-7/94/0007.. $3.50

52

together

of the axiom

system

advance in Axiom typed

with

the pre-

to which

each

was the int reduction [9], which

system.

provided

(See also San-

However, Axiom does not support as first class objects.

algebraic

Two fundamental prerequisites for serious computation in the more advanced parts of algebra are:

1. A strong

notion

2. First class status homomorphisms.

of type. for algebraic

structures

and their

1.2

Cayley

and

Magma



The

MAGMA

mental Cayley

[4] was the first

bra language with

to provide

certain

used Computer

an environment

structures

momorphisms.

widely (primarily

Alge-

The success of Cayley

stemmed,

Many

of the ideas, initially

developed

ical’ puter

undertook

algebraic

computation

for

the design of a general language based on a ‘theory

computation’

which,

emphasised

computation

in contrast

algebraic

for



Be-

as a new language

as prac-

‘mathemat-

such as sets, sequences

than

adopting

science data structures

The

system

knowledge

cause of limitations in the original design of Cayley, the new language was conceived not as an extension of Cayley, but rather

rather

notation

and

more typical

com-

such as lists, trees, or

notions.

would

allow

complete

integration

of

three key types of knowledge, namely, algorithmic knowledge (the library of algorithms), static

systems,

structures.

using

mathematical

we chose to support

of these mathematical

of structural

to t ra,ditional

with

algebra,

even pointers. Our view is that the most natural way of specifying an algebraic algorithm is in terms

computation with groups, were seen to be generally applicable to algebraic and geometric structures. We therefore

be based on funda-

to standard

structures

mappings,

albeit

in Cayley

data

would

from

tice. For example,

ho-

in large

part, from its exploitation of (1) and (2) above, for a limited class of structures.

concepts

close as possible

for computing

groups) and their

language

(data

bases of useful

knowledge),

and

dynamic knowledge (properties and relationships remembered in the course of computation).

([2], [3]). ●

The use of algebraic structure as an organizing principle for algebraic computation led us to develop the out-

Efficiency

was to be a paramount

the system

was intended

concern

to be capable

since

of tackling

structure

plays a role analogous

hard problems. The basic machinery for computationally important classes of algebraic structures

to, say, the role of the function ming. These ideaa also provided

in functional programa model for the imple-

(mainly

particular

modules

and algebras)

lines of what whereby

might

be called

the algebraic

mentation

of MAGMA,

combinators

just

provides

both

‘algebraic

programming’,

as the theory

of groups,

would



of the

The system kernel would low-level Magma

Summary

of

‘standardized

Objectives

group.

be constructed platform’

The system

would

mathematics

that

given

be designed are algebraic

mathematical

differently

object

by workers

mat hematics,

areas of

in nature.

Since a

viewed

distinct

system for calculus

presented

that

algorithms

of effort

on our part.

im-

amount

Algebra

2

of Structures

to take an explicit

for

2.1

view of

Magmas

As the design of MAGMA is intended to reflect the structural view of algebra, we take as fundamental the notion

and structure-preserving

of an algebraic

mappings

– we shall refer

magma

view.

guage for large programs

is one defined

tem would

be implemented

in the MAGMA The MAGMA

be provided.

The sys-

abstract

partby in C and partly

specified.

language

adopt

a strong

user-defined

magma

structures and their morphisms objects in the language.

notion

would

oped theory

a magma?

The

to denote

a

actual

magma.

A concrete magma

carrier

is one in which

It is clear that

(concrete) interesting

types.

structure.

by Bourbakil

over an explicit

place in a concrete

would

provide

we use the term

a law of composition.

How does one represent

in-

language.

of type and would Algebraic first-class

would

For brevity, to an algebraic

was introduced

set with

and as the interactive

structure.

when referring

A high-level programming language (the MAGMA language), designed both as an implementation lanterface for the system,



within

of many

of the

name magma

b

field, we could include at the cost of a small

the

by the

algebra, and develop a system in accordance with it. Our view asserts the primacy of the structure to this as the structural ●

code written

implementations

portant

allowing

in a successful

is likely to prove satisfactory

We proposed

optimal

on top of a

thereby

quite

branches

there is no reason to expect

view of, say polynomials, algebraists.

for those

is often

from

in the

by people outside

By importing

experts in a particular the system



fields,

for the CAML

implementation

us to ‘plug in’ C code written

1.3

rings,

be hand-coded

C kernel.

of categorical

a semantics

system and a basis for the CAM system [7].

families

set, whereas

the carrier computation

an

set is not must take

We now assume that

our

magmas are finitely generated. Since most infinite algebraic structures having a develare finitely

generated,

this restriction

is not

1~, BOurb&i, Alg&bre ~, P. 1: “Un ensemble muni d’ une loi de composition est appel~ un magma.”

be

53

particularly

onerous.

sented by a finite The following

A concrete

magma

will

(i.e., functions

be repre-

plied to magmas

set of generators.

organizational

principles

underlie

The notion

the de-

Every

object

x that

longs to a unique A magma

may be defined

magma

will

(called

be represented

set of generating

in MAGMA

the parent

be-

simplified

of x).

forming

in terms

of a finite

Magmas egory

version

elements.

are organized

corresponds

into categories

to a family

a common

representation

polynomial

rings).

A collection

where a cat-

of magmas

of categories

of magmas

Consider

of subalgebras,

definable

is defined

in terms

matrices,

vectors

deed are only which

they

sharing

For example,

be obtained

is either

may only

elements.

or

categories

such as exist

of

For example,

a

integer

minor

(in-

of a magma

x with

as an element

a magma

objects

a polynomial ring

and

provides for

to the same variety

the sense of Category

Theory,

it will sometimes

venient to apply the term to a family do not form a category in the strict a magma

belongs

of the elements



The representation

of the carrier



The

operations

which

may

in

be con-

M

together

The

operations

which

set of the magma.

be performed

2.3

on ele-

on the

a category

C contains

all of the functions

an existing

of elements

generated that

with

of

by X.

takes an exist-

a set X of elements

the quotient

of M

by the ideal

an extension

of a magma

by X. that

forms

magma

upon

(the form

the variety

of this

to which

importance

to the

of a mapping

concept

between

structure-preserving

mappings

the following

A natural maa

ture representing a magma A contains the information needed to completely specify A, and possibly a represenset. The data structure

takes

a set X

are used to represent ●

Every object has associated with it a pointer to its parent. Thus, an element refers to its parent magma, while a magma refers to its parent category. The data struc-

of its carrier

con-

is rather

the magmaa

Mappings

Of equal

magma.

tation

generic

belong).

of the magma.

the concept may

The

constructor

together

M

A constructor

ments of the magma. ●

some

work.

and category-specific

that

with

magma

and creates

generated

of one

In the case of usually

magmas.

constructor

of M

each of the

be performed

generic

A submagma

dependent The representation

by application

the following:

by some other



values for the param-

M and creates the submagma

of magmas which sense. The cate-

determines

both

constructor.

A quotient

of categories

exists a

do not form a variety,

A free magma

ing magma

families

always

operations.

which

creating

include

either

or indexed

is parameterized

of the above ideas will

Magma structions

coeffi-

of magmaa

variation

and which share a common representation, form a ‘catWhile MAGMA categories usually correspond egory’. to categories

images and

of indeterminates. there

the free algebra

constructors

of Z [x].

belonging

ring

number

given particular

from

magma of magmaa

are closed under

eters. Now theory tells us that any algebra belonging to the category and having the given parameters can

Categories

gory to which following:

a framework

unique ‘free’ algebra having those parameters. When a category is installed, a function is provided to crest e

whose laws of

in the context

in the indeterminate

A collection

ap-

a class of algebras

For a given choice of parameters,

set of identical

Thus,

and polynomials

definable)

cients, is regarded

2.2

in MAGMA,

of a magma.

are considered

polynomial

maybe

homomorphic

or more of the three variety object

us with

Now these algebras

by its coefficient

(such as the class of all

composition all satisfy a common relations form a variety. Every

that

to C. provides

here.

a variety.

this free algebra, ●

format)

Cartesian products. The class of magmas that constitute a particular category will be parameterized in some way.



belonging

of a variety

the formation ●

in infix

for designing a generic method for defining a magma. Because of space limitations, we can only present a very

sign of MAGMA: ●

written

(e.g.

relationship

of a magma,

magmas,

(morphisms).

holding

between

A general

homomorphism



An endomorphism



An action

between

54

two

mag-

two magmas.

of a magma.

representing and operators

Mappings

types of associations:

inclusion).



of magma

is

especially

A on magma

B.



Homomorphisms

An association between two sets.

are defined

age of a general How do we represent versal

Algebra

mappings?

states

that

A th~eorem from

in

the

specifying

case of a class of

magmas forming a variety V, a homomorphism magma A belonging to V is uniquely determined images of a generating set for A. During the execution of a MAGMA time system automatically

Uni-

program,

domain

of any by the

ural

when

the run-

creates and stores most nat-

a quotient

epimorphism

tempted

magma

is stored.

on objects

is created,

the nat-

operations

are at-

When

belonging

to different

magmas

either

the images for the defining

> print 71*u”5

tau(x-4*y-5 + 29*u + 31

and y over the ring

r from

bedding

P to T which

K ! 23 indicates

The

MAGMA

language

language

is an imperative

with standard

procedures.

In

imperative-style

addition,

it

longing

programming statements

supports

both

and

the

set-

theoretic [12] and functional programming In particular, the language has a functional

paradigms. subset pro-

viding

order func-

tions,

functions partial

as first class objects,

evaluation,

A novel and central

higher

sequence, and tuple.

set,

reader a brief introduction by means of simple Magmas

language

brackets

is

IVe

give

created

by applying

a varietal

to some existing magma. The process the creation of a ‘free’ magma. Consider

following

MAGMA

preceded

by a > prompt

where

MAGMA

input

Q (a magma). a free magma,

is

notation

determinate

over Q.

the rational

>Q:=

field

by X2 + 1. Finally,

statement

creates

or equal to 100:

[1. .1001

I IsPrime(x)

}; funcin the

are prime.

Note

for arithmetic lying

the use of a spe-

progressions between

the mathematical

z is prime }. example creates

to desig-

1 and 100.

notation

The

{z

: 1
F := quo< P I X-2+1 >; Rat ionalField

Carte-

by explicitly

to select the subset of the integers

cial construction

>1

The function

IsPrime

range

bethe

sign.

RationalFieldo; >Q:= > P : = PolynomialRing

of a general

either

set.

be-

holds

a predicate

delimiters:

: x in

The constructor tion

operation gins with

{x

of elements convention

the

to some of these constructors

statements

syntax

for sequences.

>p:=

x at 23. The notation is to be coerced into the

is an element

or by using

the set of primes

examples.

are typically

A tuple

A set is specified

by similar

x

create the

ring T in one indep is the natural em-

A similar

sian product.

set aa a subset of a larger

etc.

aspect of the MAGMA

the integer

for sequences.

defined

- y + 7) ;

we define a homomorphism

evaluates

to a single magma.

the use of different

y -> u>;

We next

finite field element. set consists of a collection

ing its elements

the provision of general constructors. These are used to define magmas and mappings as well as instances of the basic data types:

that

corresponding A MAGMA

Language

Finally,

of Z in K.

() , 2);

+ X-2

Z.

common

The

of the

ring P in two indeterminates

of integers

finite field F1O1 and a polynomial terminate u over K. The mapping

magma.

or by

generators

- 24*x-2*y

We create a polynomial

re-

the im-

magma

magma.

lated by such natural homomorphisms, the evaluation mechanism will automatically apply these homomorphisms iteratively so as to coerce the operands into a

3

by giving

of the domain

> P := PolynomialRing (Integers >K := FiniteField(lOl) ; > T : = PolynomialRing (K) ; > tau := hom

T I x -> K ! 23,

ural homomorphisms that arise. For example, when a submagma is created, the inclusion mapping is stored. Similarly,

element

the field

1sConstructed w the quotient of P by the F = Q(=) ideal generated by Z2 + 1.

55

the Chinese

Remainder

Algorithm

4

on the polynomials

a mod m and b mod p. The function

Chinese,

While

ences UIX = VI mod ml, U2X = vz mod mz, where ml and m2 are coprime. The function Parent, applied to returns

its parent

:= function(a, Chinese C : = f unc;

prime,

the

function

ModGcd cre-

calling

the intrinsic

ModGcd

function

: = function(f,

It :=

P d

~ and g in FP [u] is found

:=

readers

ming,

MAGMA’S

that

finite

Gtd.

;

there

[10]. For example,

field elements

fields.

phi(f),

phi(g));

lt

Degree(d)

Degree(e)

d := Chinese(R!l, m :=

1,

p;

arguments

elements equality

from different

of the parents

machinery

a common

to determine

over-magmaiV

(f

while

N using and the

it is in fact

x>;

found on the first attempt and the using the relationship machinery is

then

avoided.

Observe

also that

rho(e),

checking

(including

p);

are both handled m, rho(e),

as the central

It is also efficient

the two problems

type casting)

and method

from

the parent

maps as required.

benefits objects

magma,

of type a mech-

and which

Here then

of using the magma

since

selection

by the one general mechanism,

starts

uses remembered

p);

the practical

mod d eq O) and

into

the relations,

this may seem complex, decisions.

whether

for the parents

a match is usually overhead of lifting

i,f;

return

of

we do want to

u>;

else

end

for

in gen-

of two elements belonging to subfinite field. Insuch a case, MAGMA

a series of simple

anism which d := Chinese(d, m *:= p;

indicating

just

:=homRlu->

e := Gcd(

Note that

FiniteField(p));

the multi-

entry

are proper

So first wecheck

exists

program-

But this is only part of the story:

the transition maps stored with operation is performed in N.

:=homSlx->

rho

is

is akin to multi-

of x and y. If so, x and y are each lifted

phi

which

oriented

will have a signature

uses its relationship

repeat

until

object

inCLOS

operator

two finite

with

mechanism

permit multiplication fields ofacommon

1;

if

is provided

mechanism

z and g. Even this is not the full story:

P := NextPrime(p); S := PolynomialRing(

such

z and y,

for~?

signature

eral, we do not want to multiply

by

2; :=R!l;

m ;=

.f to objects

matching

familiar

dispatch

the operation.

g)

Parent(f)

of rela-

questions

answer such questions

signature

those

placation

ates FP[u] together with the natural homomorphisms @: Z[z]+FP[u] andp:FP[u] +Z[z]. The GCDof the images of polynomials

answer

operation

useful

use of parent

a generic method for locating a function with a given name, and having a given argument specification. For

method each successive

The

very

the availability

where is the algorithm

by MAGMA’S

end function;

For

with

expressive

are also

us to rapidly

The machineryto

M : = Max(Degree (a) , Degree(b)) ; x := [ Solution( 11, [c(a, 1), C(b, 1)1, [l, [m, p]) : i in [0. .M] 1; return Parent(a) ! [ x gt (p*m) div 2 select x-p*m else x : x in X 1 ;

they

in MAGMA.

as: Is it legal to apply (p,

have an obvious

power,

in combination

tionships

Coercion

and maps

internally

structures

m, b, p) gt Degree (p) Coefficient

Issues and

organizational

mechanisms

magma.

O else

Signatures

4.1

calls the Magma intrinsic Solution (u, v, m), where u, v and m are pairs of integers, to solve the congru-

any object

Implementation

in turn,

we see

and the map

in MAGMA.

(g mod d eq O);

d;

4.2

A

Software

Architecture

end function; An important We apply > R

our program

to polynomials

>f:=

:= PolynomialRing(Integerso); - 4*x + (X-3 - 1)*(X-2

‘g:=

(X-2+X+

> print X-5

-

ModGcd(f, 5*x”4

Afulldescription and

1)*(x-

fand

g:

4)-3*(x-2

2)-3*(x-

-

2*x-2

+ 4*x

+

1);

7);

of thelanguage

may

-

of a

of C programs

tion takes several forms:

8

be found

has been the development

written independently of the Magma project into the system kernel. To this end we devised a software system architecture, later dubbed the ‘the software bus’, which isolated the language part of MAGMA from the part that implements the mathematical algorithms. This isola-

g);

+ 7*x-3

objective

kernel design which allows the integration

in

1. The memory pointer and

[1]

[5].

56

management handle

software

based

provides

memory

both

allocation.

Thus, and

code free

MAGMA’S

written

using

the

C library

interface

can be added

memory

manager

provides

machinery

provides

malloc

to MAGMA

as

in addition

to the authors.

was funded

in part by the Australian

The development

of MAGMA

Research

Council.

equivalent

services.

References 2. The

relationship

generic

services

for registering

tween two objects,

3. The

run-time

system

tion

the mathematical

matical

six system functions.

level facilities

relationship

assumes

the objects

through

of [1] Bosma W. and Cannon

be-

Fhnctions,

chains,

and so on.

ble about with

set

a relationship

for tracing

for accessing a relationship

a

[2] Butler

as little

it manipulates. part

functions

such as print,

8, 1988, LNCS

They

magma tors.

such tasks as the creation

and evaluation All

modules

added

have the same status the system,

of the standard through

and, in particular,

both to code written

this

as code written in the MAGMA

ules adhere

to the interface

of a free

mechanism

rings, will work immediately ber of the new ring type.

Systems,

[4] Cannon

Cayley,

are accessible

putational

Group

language,

don, 1984, 145-183.

and

[5] Cannon

The latter

if defined

over a mem-

tains

very efficient

machinery

categories,

own categories

for groups,

rings,

tific

in the MAGMA

language.

Robin

C. A., An Introduction

V Language Reference New York, 1991.

Algebra,

abstract

machine,

of Functional

Program-

1990.

Y. Siret and E. Tournier, Academic

Press, London,

and Sutor

Computation

R.S. AXIOM

Com1988.

- The ScienNew

[10] Paepcke A., Object-Oriented

Programming:

The

1992.

MIT

Press, 1993.

[11] Santaa. P. S., A type system for computer Alfonso

Miola

(cd),

Design

algebra,

and Implemen-

tation of Symbolic Computation Systems. LNCS 722, Springer-Verlag, Berlin, 1993, 177-191. [12] Schwartz J. T., Dewar R.B.K., Dubinsky E., and Schonberg E. Programming with Sets - An In-

Cannon, Robin Deed and Jim Richardson in 1987. Jim Richardson implemented a prototype lparser for the lanof the run-time

Com-

Press, Lon-

1993, 240 pages.

The categorical

CLOS Perspective,

A preliminary design for MAGMA (then known as Cayley V4) was developed by John Brownie, Greg Butler, John

guage in 1987, while

theory

(cd),

Springer-Verlag,

York,

Acknowledgements

prototype

to the group Atkinson

System,

in:

6

1990, 10-19.

Academic

Edition,

Addison-Wesley,

[9] Jenks R.D.

confields,

at 100 sites. In 1994, mechato enable users to create their

of magmas

Theory,

(cd),

Compu-

the com-

The kernel

modules, graphs and linear codes. MAGMA VI was released for general distribution in December 1993 and by April 1994 was installed nisms will be implemented

M.D.

Foundations

[8] J.H. Davenport,

of user defined

in:

First

[7] G. Cousineau,

haa been implemented.

429,

[6] Char B.W. et al, Maple Manual, Springer-Verlag,

and polynomial

Status

the exception

plete language

1989,

in: A. Miola

of Symbolic

J.J. and Playoust

to Magma,

puter With

New York,

all kernel mod-

ming,

Current

algebra,

J. J., An introduction

in Logical

5

on Sym-

J. J., The design of Cayle~

LNCS

language

protocols.

rings, such as matrix

Proceedings

Rome, July 4-

Springer,

and Implementation

tation

point is critical since when an implementor installs a new ring type by defining its interface, all recursively defined

358,

for

they

to kernel C code. Most importantly,

Design

construc-

especially

(cd),

Symposium

Computation,

[3] Butler G. and Cannon a language for modern

are obligatory for any code module that implements a magma. A set of fundamental mathematical functions implement

J. J., Cayley version 4: The

P. Gianni

456466.

define low

copy and delete.

in:

bolic and Algebraic

is

and a set of mathe-

The system functions

G. and Cannon

of Magma

1993, 690 pages.

of the 1988 International

interac-

of the system

Edition,

user language,

as possiAll

First

J. J., Handbook

Deed implemented system

in 1988.

troduction 1986.

to SETL,

Springer-Verlag,

New York,

a small The

[13] Wolfram

final

S., Mathematical

- A System

for Do-

ing Mathematics by Computer, Addison-Wesley Publishing Company, Second edition, 1991.

design and implementation was carried out with the participation of Mark Bofinger, John Brownie, Steve Collins, Bruce Cox, Andrew Solomon, and Allan Steel,

57