Opal (programming language)

OPAL (Optimized Applicative Language) is a functional programming language that was developed in 1986 at the Technical University of Berlin under the direction of Prof. Dr. Peter Pepper. The language used there mainly as a test environment. At first, it was first necessary to implement the language efficiently. Later, the entire field of functional concepts was included. These include in particular:

  • Principles of software engineering
  • Integration of formal specification
  • Parallel Programming

In contrast to other functional languages ​​like Haskell OPAL is not standardized. This allows developers to much with various features that they deem interesting to experiment.

The basic structure of an OPAL program

OPAL programs (structures, see Algebraic structure ) consist of a signature part ( file extension. Sign ) and an Implementation ( file extension. Impl ). The signature part of the varieties as well as the definition and value ranges of all functions are described. To this end, the keyword FUN is needed:

FUN f: nat ** nat -> nat declared, for example, a function f whose domain is a tuple of two values ​​of type nat ( natural numbers ) and whose range also represents the sort nat. The keyword must be FUN in the implementation part, such functions can then be used but only in the respective structure.

In the source code the functions with the keyword DEF be implemented ( see examples). Also in the implementation of a user-defined data type is described with the word DATA, whose signature in the. Signed file can be announced globally by TYPE. "

Examples

Fibonacci

An example of an implementation of the function using a Fibonacci lambda abstraction:

DEF fibo == \ \ n        IF n = 0 THEN 0        IF n = 1 THEN 1        IF n> = 2 THEN fibo (n-1 ) fibo ( n-2) FI A more efficient implementation of the above sequence using Dijkstra's IF and overloading.

FUN fib: nat -> nat   DEF fib ( x ) ==     IF x = 0 THEN 0     IF x = 1 THEN 1     IF x > 1 THEN fib ( 2, 1, 1, x )     FI     FUN fib: nat ** nat ** nat ** nat -> nat   - Idx: index of instantaneous   - P1: fib ( idx )   - P2: fib ( idx -1)   - Max: the index of the to be calculated follower   - Example: fib ( 6) -> fib ( 2, 1, 1, 6) -> fib ( 3, 2, 1, 6) -> fib ( 4, 3, 2, 6) ->   - Fib ( 5, 5, 3, 6) -> fib ( 6, 8, 5, 6) => 8   DEF fib ( idx, p1, p2, max) ==     IF idx < max THEN fib ( idx 1, p1 p2, p1, max)     IF idx = max THEN p1     FI Quicksort

An example of an implementation of the Quicksortalgorithmus:

FUN sort: seq [ nat ] -> seq [ nat ]   DEF sort ( <>) == < >    - The empty sequence (written as <>) is already sorted     DEF sort ( a :: R ) ==     LET       Small == (_ bool, and the sequence "R"         - Opal allows the prefix, infix, and postfix notation, as well as the allocation of identifiers of special characters.         - The O.A. Expression is identical to " | (_ a) | R         - Large then the sequence includes all of the numbers of R which are greater than a     IN       sort ( Small) medium sort ( Large)       - The result is the concatenation of the sorted sequence in turn, Small, Medium and Large the sorted sequence. selection sort

An example of an implementation of the Selectionsortalgorithmus:

FUN ssort: seq [ nat ] -> seq [ nat ]   DEF ssort (<>) == < >   DEF ssort ( list) ==     LET       minimum == min ( list)       rest list == cut (minimum, list )     IN       minimum :: ssort (rest list)   FUN cut: nat ** seq [ nat ] -> seq [ nat ]   DEF cut (x, a :: A) ==     IF a = x THEN A     ELSE a :: cut (x, A) FI   FUN min: seq [ nat ] -> nat   DEF min ( a :: A) == minHelp (a, A)   FUN minHelp: nat ** seq [ nat ] -> nat   DEF minHelp (a, <>) == a   DEF minHelp (A, A ) ==     If a < ft (A ) THEN minHelp (a, RT (A))     ELSE minHelp (ft (A) rt (A)) FI Insertion

An example of an implementation of the Insertionsortalgorithmus:

FUN isort: seq [ nat ] -> seq [ nat ]   DEF isort (<>) == < >   DEF isort ( a :: A) == a isort insert (A)   FUN insert: nat ** seq [ nat ] -> seq [ nat ]   DEF x insert <> == x :: <>   DEF x insert ( a :: A) ==     IF x < = a THEN x :: ( a :: A)     ELSE a :: ( insert x A)     FI mergesort

An example of an implementation of the Mergesortalgorithmus:

FUN msort: seq [ nat ] -> seq [ nat ]   DEF msort (<>) == < >   DEF msort ( a :: <> ) == a :: <>   DEF msort ( list) ==     LET       i == # ( list) / 2       (left, right ) == split ( i, list )     IN       msort (left) merge msort (right)   FUN merge: seq [ nat ] ** seq [ nat ] -> seq [ nat ]   DEF <> merge <> == < >   DEF a merge <> == a   DEF <> merge a == a   DEF ( a :: A) merge ( b :: B ) ==     IF a <= b THEN a :: (A merge ( b :: B))     ELSE b :: (B merge ( a :: A))     FI Examples of data types

While a distinction in theory between different forms of data types, OPAL has only a construct to define your own types.

An example of an implementation of a product type

Point3D Point3D DATA == (x: real, y: real, z: real) A sum types ...

DATA object3D == cube ( width: real, height: real, length: real, location: Point3D )                    cylinder ( height: real, radius: real, location: Point3D )                    sphere (radius: real, location: Point3D ) An enumerated types ...

DATA traffic_light == red                         yellow                         green Data type declarations (TYPE) replaced the OPAL compiler internally by the so-called " induced signature". The DATA keyword also adds implementations of the functions then give the programmer the ability to create user-defined values ​​of the variety to access the individual elements of the data structure and to distinguish between variants:

For example, the induced signature for the sum type

- variety    SORT object3D   - constructor    FUN cube: real ** real ** real ** Point3D -> object3D    FUN cylinder: real ** real ** Point3D -> object3D    FUN sphere: real ** Point3D -> object3D   - Selektorfunktionen    FUN length width height radius: object3D -> real    FUN location: object3D -> Point3D   - Diskrimitatorfunktionen    FUN cube: object3D -> bool?    FUN cylinder: object3D -> bool?    FUN sphere: object3D -> bool? literature

  • Peter Pepper: Functional Programming in OPAL, ML, HASKELL and GOFER. Springer - Verlag, 1999, ISBN 3-540-64541-1
621521
de