Miranda

A Functional Programming Language

This introduction to Miranda was created by Farhanah Akilwala and Kimberly Blessing for their Programming Languages course at Bryn Mawr College.


MIRANDA is a new style functional language designed by David A. Turner at the University of Kent in the mid-1980's. What distinguishes new style functional languages like MIRANDA (and others such as ML and Haskell) is the method of passing parameters: new style languages use call by need, or lazy evaluation, as opposed to the call by value method of old style (LISP like) functional languages.

Programs, which in MIRANDA are called scripts, consist of functions and data structures, written in the form of recursive equations. An example of a script would be:

z = sq x / sq y
sq n = n * n
x = a + b
y = a - b
a = 10
b = 5
As you can see in the example above, statement separators are not necessary because the parsing algorithm, which relies on block structure, makes intelligent use of the layout. Also, the organization of the code is generally insignificant.

Most commonly seen in MIRANDA are lists, which are written as such:

relatives = ["Mom"; "Dad", "Sis", "Bro"]
Operations that can be performed on lists are: list addition and subtraction (++ and --, respectively), list length (#), list consing (:), and list subscripting (!). In an arithmetic series ".." notation is used to indicate "to".

Elements of a list must all be of the same type. A mixed-type sequence of elements is called a tuple, similar to a record in Pascal, but more concise.

MIRANDA also uses guards, which are boolean expressions that work much in the same manner as if-then-else statements.

distance x y  = distance (x - y), x > y
	= distance (y - x), x < y
	= 0, x = y
Pattern matching allows one to define a function by giving several separate equations which have different formal parameters. As you can see, it is also more concise than using guards:
fac 0 = 1
fac (n + 1) = (n + 1) * fac n
In MIRANDA, functions can be passed as parameters and returned as results. Functions of two or more arguments are higher order functions, meaning each function takes one argument and returns another function.
pythag 3 4	   ||pythag is a function that takes the
(pythag 3) 4	   ||argument 3 and evaluates to a function 
f 4		   || f that finds the hypotenuse of any
                   ||right triangle whose base has length 3
MIRANDA includes ZF expressions which make use of the set theory notation. The general form of a ZF expressions is: [ body | qualifiers ] An example would be:
pythag = {[a, b, c] | a, b, c <- [1 .. ]; a*a + b*b = c*c}
This gives us the ability to define an infinite set and would be read aloud as 'the list of all a, b, c such that a, b, c drawn from the list 1 to infinity; and satisfying the Pythagorean expression.'

In the lazy evaluation mechanism, implemented in new style functional languages, no subexpression is evaluated unless its value is required. This almost makes it possible to define non-strict functions, which are capable of returning an answer even if one of their arguments is undefined. For example:

if True x y = x
if False x y = y
which can be used in a situation such as    if (x = 0) 0 (1/x)
Type declarations in MIRANDA are not necessary because the compiler is able to deduce the type from its defining equation. But the notation (::) comes in handy when defining polymorphic types. Polymorphism allows for the general statement of a function without specifying the type of argument. This is avoids redefining the same function with different argument types, for example:
reverse :: [*] -> [*]
where a list of any type is reversed.

User defined types are created in an equation using "::=". User defined types may be polymorphic; objects of user defined types are analyzed using pattern matching. An example would be:

flag_color ::= Red | White | Blue
MIRANDA, besides allowing concrete types, also allows for abstract data types. The definition of and ADT is broken into two parts, starting with a declaration if the form abstype with where the names following the with are the signatures of the ADT that will bind the ADT to the rest of the program. The set of equations bound to the ADT by the signatures are called implementation equations.

Sources consulted: The Anatomy of Programming Languages. Fischer and Grodzinsky. Prentice Hall, NJ. 1993. "An Overview of Miranda"; David Turner; Reprinted from SIGPLAN NOTICES, December 1986. "Functional programs as executable specifications"; D.A. Turner, Mathematic Logic and Programming Languages. ed. Hoare and Shephardson. Prentice Hall. "Miranda: A non-strict functional language with polymorphic types"; D.A. Turner; Springer Lecture Notes in Computer Science, vol. 201.


Further information about the Miranda system, including the computer systems for which it is available, and current license fees, may be obtained by mail, electronic mail, or telephone, from:

Research Software Limited
23, St Augustines Road,
Canterbury
Kent CT1 1XP
ENGLAND

telephone (24 hours) +44 227 471844
email: mira-request@ukc.ac.uk
uucp: mcvax!ukc!mira-request
internet: mira-request%ukc@nsfnet-relay.ac.uk

(telephone callers from within the UK should dial `0' and not `44')


Added on 05 March 2001:
Miranda System Manual
Miranda: The Craft of Functional Programming
Related search results from Yahoo!
This web site created by:
Kimberly Blessing / kimberly@blackcat.brynmawr.edu / URL = http://blackcat.brynmawr.edu/~kimberly/Miranda.html