Miranda is a
lazy,
purely functional programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
designed by
David Turner as a successor to his earlier programming languages
SASL and
KRC, using some concepts from
ML and
Hope
Hope is an optimistic state of mind that is based on an expectation of positive outcomes with respect to events and circumstances in one's life or the world at large.
As a verb, its definitions include: "expect with confidence" and "to cherish ...
. It was produced by Research Software Ltd. of England (which holds a trademark on the name ''Miranda'') and was the first purely functional language to be commercially supported.
Miranda was first released in 1985 as a fast interpreter in
C for
Unix
Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and ot ...
-flavour operating systems, with subsequent releases in 1987 and 1989. It had a strong influence on the later
Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lang ...
programming language.
In 2020 a version of Miranda was released as open source under a
BSD licence
BSD licenses are a family of permissive free software licenses, imposing minimal restrictions on the use and distribution of covered software. This is in contrast to copyleft licenses, which have share-alike requirements. The original BSD lice ...
. The codebase has been updated to conform to modern C standards (
C11 C11, C.XI, C-11 or C.11 may refer to:
Transport
* C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War
* Fokker C.XI, a 1935 Dutch reconnaissance seaplane
* LET C-11, a license-build var ...
/
C18) and to generate 64-bit binaries. This has been tested on operating systems including
Debian
Debian (), also known as Debian GNU/Linux, is a Linux distribution composed of free and open-source software, developed by the community-supported Debian Project, which was established by Ian Murdock on August 16, 1993. The first version of D ...
,
Ubuntu
Ubuntu ( ) is a Linux distribution based on Debian and composed mostly of free and open-source software. Ubuntu is officially released in three editions: ''Desktop'', ''Server'', and ''Core'' for Internet of things devices and robots. All the ...
,
WSL/Ubuntu, and
MacOS
macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lapt ...
(
Catalina
Catalina may refer to:
Arts and media
* ''The Catalina'', a 2012 American reality television show
* ''Catalina'' (novel), a 1948 novel by W. Somerset Maugham
* Catalina (''My Name Is Earl''), character from the NBC sitcom ''My Name Is Earl''
...
).
Overview
Miranda is a
lazy,
purely functional programming language. That is, it lacks
side effect
In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequence ...
s and
imperative programming
In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program c ...
features. A Miranda program (called a ''script'') is a set of
equation
In mathematics, an equation is a formula that expresses the equality of two expressions, by connecting them with the equals sign . The word ''equation'' and its cognates in other languages may have subtly different meanings; for example, in ...
s that define various mathematical
function
Function or functionality may refer to:
Computing
* Function key, a type of key on computer keyboards
* Function model, a structured representation of processes in a system
* Function object or functor or functionoid, a concept of object-oriente ...
s and
algebraic data type
In computer programming, especially functional programming and type theory, an algebraic data type (ADT) is a kind of composite type, i.e., a type formed by combining other types.
Two common classes of algebraic types are product types (i.e., t ...
s. The word ''
set
Set, The Set, SET or SETS may refer to:
Science, technology, and mathematics Mathematics
*Set (mathematics), a collection of elements
*Category of sets, the category whose objects and morphisms are sets and total functions, respectively
Electro ...
'' is important here: the order of the equations is, in general, irrelevant, and there is no need to define an entity prior to its use.
Since the
parsing
Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term ''parsing'' comes from Lati ...
algorithm makes intelligent use of
layout
Layout may refer to:
* Page layout, the arrangement of visual elements on a page
** Comprehensive layout (comp), a proposed page layout presented by a designer to their client
* Layout (computing), the process of calculating the position of obje ...
(indentation), there is rarely a need for bracketing statements and no statement terminators are required. This feature, inspired by
ISWIM
ISWIM (acronym for If you See What I Mean) is an abstract computer programming language (or a family of languages) devised by Peter Landin and first described in his article "The Next 700 Programming Languages", published in the Communications o ...
, is also used in
occam and
Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lang ...
and was later popularized by
Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (pro ...
.
Comment
Comment may refer to:
* Comment (linguistics) or rheme, that which is said about the topic (theme) of a sentence
* Bernard Comment (born 1960), Swiss writer and publisher
Computing
* Comment (computer programming), explanatory text or informat ...
ary is introduced into regular scripts by the characters
, ,
and continue to the end of the same line. An alternative commenting convention affects an entire source code file, known as a "
literate script", in which every line is considered a comment unless it starts with a
>
sign.
Miranda's basic
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
s are
char
,
num
and
bool
. A character string is simply a list of
char
, while
num
is silently converted between two underlying forms:
arbitrary-precision
In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, multiple-precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are lim ...
integers (a.k.a. bignums) by default, and regular
floating point
In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be ...
values as required.
Tuple
In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
s are sequences of elements of potentially mixed types, analogous to
records in
Pascal
Pascal, Pascal's or PASCAL may refer to:
People and fictional characters
* Pascal (given name), including a list of people with the name
* Pascal (surname), including a list of people and fictional characters with the name
** Blaise Pascal, Fren ...
-like languages, and are written delimited with parentheses:
this_employee = ("Folland, Mary", 10560, False, 35)
The ''
list
A ''list'' is any set of items in a row. List or lists may also refer to:
People
* List (surname)
Organizations
* List College, an undergraduate division of the Jewish Theological Seminary of America
* SC Germania List, German rugby union ...
'' instead is the most commonly used data structure in Miranda. It is written delimited by square brackets and with comma-separated elements, all of which must be of the same type:
week_days = Mon","Tue","Wed","Thur","Fri"
List concatenation is
++
, subtraction is
--
, construction is
:
, sizing is
#
and indexing is
!
, so:
days = week_days ++ Sat","Sun" days = "Nil":days
days!0
⇒ "Nil"
days = days -- Nil" #days
⇒ 7
There are several list-building shortcuts:
..
is used for lists whose elements form an arithmetic series, with the possibility for specifying an increment other than 1:
fac n = product ..n odd_sum = sum ,3..100
More general and powerful list-building facilities are provided by "
list comprehension
A list comprehension is a Syntax of programming languages, syntactic construct available in some programming languages for creating a list based on existing list (computing), lists. It follows the form of the mathematical ''set-builder notation'' ( ...
s" (previously known as "ZF expressions"), which come in two main forms: an expression applied to a series of terms, e.g.:
squares = n <- ">..
(which is read: list of n squared where n is taken from the list of all positive integers) and a series where each term is a function of the previous one, e.g.:
powers_of_2 = n <- 1, 2*n ..
As these two examples imply, Miranda allows for lists with an infinite number of elements, of which the simplest is the list of all positive integers:
../code>
The notation for function application is simply juxtaposition, as in sin x
.
In Miranda, as in most other purely functional languages, functions are first-class citizens, which is to say that they can be passed as arguments
An argument is a statement or group of statements called premises intended to determine the degree of truth or acceptability of another statement called conclusion. Arguments can be studied from three main perspectives: the logical, the dialectic ...
to other functions, returned as results, or included as elements of data structures. What is more, a function with two or more parameters may be "partially parameterised", or curried, by supplying fewer arguments than the full number of parameters. This gives another function which, given the remaining parameters, will return a result. For example:
add a b = a + b
increment = add 1
is a roundabout way of creating a function "increment" which adds one to its argument. In reality, add 4 7
takes the two-parameter function add
, applies it to 4
obtaining a single-parameter function that adds four to its argument, then applies that to 7
.
Any function with two parameters (operands) can be turned into an infix operator (for example, given the definition of the add
function above, the term $add
is in every way equivalent to the +
operator) and every infix operator taking two parameters can be turned into a corresponding function.
Thus:
increment = (+) 1
is the briefest way to create a function that adds one to its argument. Similarly, in
half = (/ 2)
reciprocal = (1 /)
two single-parameter functions are generated. The interpreter understands in each case which of the divide operator's two parameters is being supplied, giving functions which respectively divide a number by two and return its reciprocal.
Although Miranda is a strongly typed programming language
In computer programming, one of the many ways that programming languages are colloquially classified is whether the language's type system makes it strongly typed or weakly typed (loosely typed). However, there is no precise technical definition o ...
, it does not insist on explicit type declaration
Declaration may refer to:
Arts, entertainment, and media Literature
* ''Declaration'' (book), a self-published electronic pamphlet by Michael Hardt and Antonio Negri
* ''The Declaration'' (novel), a 2008 children's novel by Gemma Malley
Music ...
s. If a function's type is not explicitly declared, the interpreter infer
Inferences are steps in reasoning, moving from premises to logical consequences; etymologically, the word '' infer'' means to "carry forward". Inference is theoretically traditionally divided into deduction and induction, a distinction that in ...
s it from the type of its parameters and how they are used within the function. In addition to the basic types (char
, num
, bool
), it includes an "anything" type where the type of a parameter does not matter, as in the list-reversing function:
rev [] = []
rev (a:x) = rev x ++ [a]
which can be applied to a list of any data type, for which the explicit function type declaration would be:
rev :: ->
Finally, it has mechanisms for creating and managing program module
Module, modular and modularity may refer to the concept of modularity. They may also refer to:
Computing and engineering
* Modular design, the engineering discipline of designing complex devices using separately designed sub-components
* Modul ...
s whose internal functions are invisible to programs calling those modules.
Sample code
The following Miranda script determines the set of all subsets of a set of numbers
subsets [] =
subsets (x:xs) = x] ++ y , y <- ys] ++ ys
where ys = subsets xs
and this is a literate script for a function primes
which gives the list of all prime numbers
> , , The infinite list of all prime numbers.
The list of potential prime numbers starts as all integers from 2 onwards;
as each prime is returned, all the following numbers that can exactly be
divided by it are filtered out of the list of candidates.
> primes = sieve ..> sieve (p:x) = p : sieve n <- x; n mod p ~= 0
Here, we have some more examples
max2 :: num -> num -> num
max2 a b = a, if a>b
= b, otherwise
max3 :: num -> num -> num -> num
max3 a b c = max2 (max2 a b) (max2 a c)
multiply :: num -> num -> num
multiply 0 b = 0
multiply a b = b + (multiply (a-1) b)
fak :: num -> num
fak 0 = 1
fak n = n * (fak n-1)
itemnumber:: >num
itemnumber [] = 0
itemnumber (a:x) = 1 + itemnumber x
weekday::= Mo, Tu, We, Th, Fr, Sa, Su
isWorkDay :: weekday -> bool
isWorkDay Sa = False
isWorkDay Su = False
isWorkDay anyday = True
tree * ::= E, N (tree *) * (tree *)
nodecount :: tree * -> num
nodecount E = 0
nodecount (N l w r) = nodecount l + 1 + nodecount r
emptycount :: tree * -> num
emptycount E = 1
emptycount (N l w r) = emptycount l + emptycount r
treeExample = N ( N (N E 1 E) 3 (N E 4 E)) 5 (N (N E 6 E) 8 (N E 9 E))
weekdayTree = N ( N (N E Mo E) Tu (N E We E)) Th (N (N E Fr E) Sa (N E Su))
insert :: * -> stree * -> stree *
insert x E = N E x E
insert x (N l w E) = N l w x
insert x (N E w r) = N x w r
insert x (N l w r) = insert x l , if x -> tree *
list2searchtree [] = E
list2searchtree [x] = N E x E
list2searchtree (x:xs) = insert x (list2searchtree xs)
maxel :: tree * -> *
maxel E = error "empty"
maxel (N l w E) = w
maxel (N l w r) = maxel r
minel :: tree * -> *
minel E = error "empty"
minel (N E w r) = w
minel (N l w r) = minel l
, , Traversing: going through values of tree, putting them in list
preorder,inorder,postorder :: tree * -> inorder E = []
inorder N l w r = inorder l ++ [w] ++ inorder r
preorder E = []
preorder N l w r = [w] ++ preorder l ++ preorder r
postorder E = []
postorder N l w r = postorder l ++ postorder r ++ [w]
height :: tree * -> num
height E = 0
height (N l w r) = 1 + max2 (height l) (height r)
amount :: num -> num
amount x = x ,if x >= 0
amount x = x*(-1), otherwise
and :: bool -> bool -> bool
and True True = True
and x y = False
, , A AVL-Tree is a tree where the difference between the child nodes is not higher than 1
, , i still have to test this
isAvl :: tree * -> bool
isAvl E = True
isAvl (N l w r) = and (isAvl l) (isAvl r), if amount ((nodecount l) - (nodecount r)) < 2
= False, otherwise
delete :: * -> tree * -> tree *
delete x E = E
delete x (N E x E) = E
delete x (N E x r) = N E (minel r) (delete (minel r) r)
delete x (N l x r) = N (delete (maxel l) l) (maxel l) r
delete x (N l w r) = N (delete x l) w (delete x r)
References
External links
*
{{Authority control
Declarative programming languages
Functional languages
History of computing in the United Kingdom