Miranda (programming language): Difference between revisions

Content deleted Content added
No edit summary
Tags: Mobile edit Mobile app edit iOS app edit
Add external links to the current source code for Miranda and a large example of a Miranda program
 
(44 intermediate revisions by 29 users not shown)
Line 1:
{{Short description|Programming language by David Turner}}
{{moreMore footnotes needed|date=September 2016}}
{{Infobox programming language
| name = Miranda
| logo = [[File:Miranda logo (programming language).jpg]]
| logo caption =
| screenshot =
| screenshot caption =
| file ext =
| paradigm = [[lazyLazy evaluation|lazy]], [[functionalFunctional programming|functional]], [[declarativeDeclarative programming|declarative]]
| scope =
| designer = [[David Turner (computer scientist)|David Turner]]
| released = {{Start date|1985}}
| developer = Research Software Ltd
| designer = [[David Turner (computer scientist)|David Turner]]
| released = {{Start date and age|1985}}
| developer = Research Software Ltd
| latest release version = {{wikidata|property|edit|reference|P348}}
| latest release date = <!-- {{start date and age|YYYY{{wikidata|qualifier|MMP348|DDP577}}}} -->
| latest preview version =
| latest preview date = <!-- {{start date and age|YYYYyyyy|MMmm|DDdd}} -->
| typing = [[strongStrong and weak typing|strong]], [[staticStatic typing|static]]
| implementations = Miranda
| dialects =
| influenced by = [[Kent Recursive Calculator|KRC]], [[ML (programming language)|ML]], [[SASL (programming language)|SASL]], [[Hope (programming language)|Hope]]
| influenced = [[Clean (programming language)|Clean]], [[Haskell]], [[Orwell (programming language)|HaskellOrwell]], [[OrwellMicrosoft Power Fx]]
| (programming language)|Orwell]] =
| programming language platform =
| platform operating system =
| license = {{wikidata|property|edit|reference|P275}}
| operating system =
| website = {{urlURL|http://miranda.org.uk}}
| license =
| wikibooks =
| website = {{url|http://miranda.org.uk}}
| wikibooks =
}}
'''Miranda''' is a [[lazy evaluation|lazy]], [[functional programming|purely functional]] [[programming language]] designed by [[David Turner (computer scientist)|David Turner]] as a successor to his earlier programming languages [[SASL programming language|SASL]] and [[Kent Recursive Calculator|KRC]], using some concepts from [[ML (programming language)|ML]] and [[Hope (programming language)|Hope]]. It was produced by Research Software Ltd. of England (which holds a trademark on the name ''Miranda'')<ref>{{Cite book|last=Turner|first=D. A.|title=Functional Programming Languages and Computer Architecture |chapter=Miranda: A non-strict functional language with polymorphic types |date=September 1985|editor-last=Jouannaud|editor-first=Jean-Pierre|chapter-url=https://www.cs.kent.ac.uk/people/staff/dat/miranda/nancypaper.pdf|series=Lecture Notes in Computer Science|volume=201 |language=en|___location=Berlin, Heidelberg|publisher=Springer|pages=1–16|doi=10.1007/3-540-15975-4_26|isbn=978-3-540-39677-2}}</ref> and was the first purely functional language to be commercially supported.{{citation needed|date=November 2016}}
 
Miranda was first released in 1985, as a fast interpreter in [[C (programming language)|C]] for [[Unix]]-flavour operating systems, with subsequent releases in 1987 and 1989. MirandaIt had a strong influence on the later [[Haskell (programming language)|Haskell]] programming language.<ref>{{citeCite webbook|last1=Hudak|first1=Paul|last2=Hughes|first2=John|last3=Peyton Jones|first3=Simon|last4=Wadler|first4=Philip|title=AProceedings of the third ACM SIGPLAN conference on History of programming languages |chapter=A history of Haskell: beingBeing lazy with class |date=2007-06-09|chapter-url=http://researchdx.microsoftdoi.comorg/en-us10.1145/um/people/simonpj/papers/history-of-haskell1238844.1238856|___location=New York, NY, USA|publisher=ACM|doi=10.1145/1238844.1238856|dateisbn=20079781595937667 |s2cid=52847907 }}</ref> Turner stated that the benefits of Miranda over Haskell are: "Smaller language, simpler type system, simpler arithmetic".<ref name=opensourcing/>
 
In 2020 a version of Miranda was released as open source under a [[BSD 2-Clause|BSD licence]]. The code has been updated to conform to modern C standards ([[C11 (C standard revision)|C11]]/[[C18 (C standard revision)|C18]]) and to generate 64-bit binaries. This has been tested on operating systems including [[Debian]], [[Ubuntu]], [[Windows Subsystem for Linux|WSL]]/Ubuntu, and [[macOS]] ([[macOS Catalina|Catalina]]).<ref name=opensourcing>{{Cite web|last=Turner|first=David|date=2021-03-22|title=Open Sourcing Miranda|url=http://codesync.global/media/open-sourcing-miranda-david-turner-code-mesh-v-2020-codemeshv2020/|access-date=2021-12-30|website=Code Sync|language=en|publication-place=London|publication-date=November 2020}}</ref><ref>{{Cite web|title=Miranda download page|url=https://www.cs.kent.ac.uk/people/staff/dat/miranda/downloads/|access-date=2021-12-30|website=www.cs.kent.ac.uk}}</ref>
 
== Name ==
[[File:Miranda - The Tempest JWW.jpg|thumb|right|''Miranda'' by John William Waterhouse, 1917]]
The name ''Miranda'' is taken from the gerundive form of the latin verb {{lang|la|[[:wikt:miror|miror]]}},<ref>{{cite web |url=https://www.cs.kent.ac.uk/people/staff/dat/miranda/name.html |title=About the name Miranda |access-date=2024-05-18}}</ref> meaning "to be admired".
 
The logo features a rendition by [[John William Waterhouse]] of the character [[Miranda (The Tempest)|Miranda]] from Shakespeare's ''The Tempest''.
 
== Overview ==
Miranda is a [[lazy evaluation|lazy]], [[functional programming|purely functional]] programming language. That is, it lacks [[Side effect (computer science)|side effect]]s and [[imperative programming]] features. A Miranda program (called a ''script'') is a set of [[equation]]s that define various mathematical [[function (mathematics)|function]]s and [[algebraic data type]]s. The word ''[[setSet (mathematics)|set]]'' 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]] algorithm makes intelligent use of layout (indentation, via [[off-side rule|layout]] (indentation), therebracketing isstatements are rarely a need for bracketing statementsneeded and no statement terminators are requiredunneeded. This feature, inspired by [[ISWIM]], is also used in [[occam (programming language)|occam]] and [[Haskell (programming language)|Haskell]] and was later popularized by [[Python (programming language)|Python]].
 
[[Comment (computer programming)|Comment]]ary is introduced into regular scripts by the characters <code>||</code> and continue to the end of the same line. An alternative commenting convention affects an entire source code file, known as a "[[Literate programming|literate script]]", in which every line is considered a comment unless it starts with a <code>&gt;</code> sign.
Line 42 ⟶ 51:
 
[[Tuple]]s are sequences of elements of potentially mixed types, analogous to [[record (computer science)|record]]s in [[Pascal (programming language)|Pascal]]-like languages, and are written delimited with parentheses:
<sourcesyntaxhighlight lang="haskell">
this_employee = ("Folland, Mary", 10560, False, 35)
</syntaxhighlight>
</source>
 
The ''[[listList (computingabstract data type)|list]]'' 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:
 
<sourcesyntaxhighlight lang="haskell">
week_days = ["Mon","Tue","Wed","Thur","Fri"]
</syntaxhighlight>
</source>
List concatenation is <code>++</code>, subtraction is <code>--</code>, construction is <code>:</code>, sizing is <code>#</code> and indexing is <code>!</code>, so:
 
<sourcesyntaxhighlight lang="clean">
days = week_days ++ ["Sat","Sun"]
days = "Nil":days
Line 61 ⟶ 70:
#days
⇒ 7
</syntaxhighlight>
</source>
 
There are several list-building shortcuts: <code>..</code> is used for lists whose elements form an arithmetic series, with the possibility for specifying an increment other than 1:
 
<sourcesyntaxhighlight lang="haskell">
fac n = product [1..n]
odd_sum = sum [1,3..100]
</syntaxhighlight>
</source>
 
More general and powerful list-building facilities are provided by "[[list comprehension]]s" (previously known as "ZF expressions"), which come in two main forms: an expression applied to a series of terms, e.g.:
 
<sourcesyntaxhighlight lang="haskell">
squares = [ n * n | n <- [1..] ]
</syntaxhighlight>
</source>
 
(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.:
 
<sourcesyntaxhighlight lang="haskell">
powers_of_2 = [ n | n <- 1, 2*n .. ]
</syntaxhighlight>
</source>
 
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>[1..]</code>
Line 86 ⟶ 95:
The notation for function application is simply juxtaposition, as in <code>sin x</code>.
 
In Miranda, as in most other purely functional languages, functions are [[first-class function|first-class]] citizens, which is to say that they can be passed as [[parameter (computer science)|parameterarguments]]s to other functions, returned as results, or included as elements of data structures. What is more, a function requiringwith two or more parameters may be "partially parameterised", or [[currying|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:
 
<sourcesyntaxhighlight lang="haskell">
add a b = a + b
increment = add 1
</syntaxhighlight>
</source>
 
is a roundabout way of creating a function "increment" which adds one to its argument. In reality, <code>add 4 7</code> takes the two-parameter function <code>add</code>, applies it to <code>4</code> obtaining a single-parameter function that adds four to its argument, then applies that to <code>7</code>.
 
Any function takingwith two parameters (operands) can be turned into an infix operator (for example, given the definition of the <code>add</code> function above, the term <code>$add</code> is in every way equivalent to the <code>+</code> operator) and every infix operator taking two parameters can be turned into a corresponding function.
Thus:
 
<sourcesyntaxhighlight lang="haskell">
increment = (+) 1
</syntaxhighlight>
</source>
 
is the briefest way to create a function that adds one to its argument. Similarly, in
 
<sourcesyntaxhighlight lang="haskell">
half = (/ 2)
reciprocal = (1 /)
</syntaxhighlight>
</source>
 
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.
Line 113 ⟶ 122:
Although Miranda is a [[strongly typed programming language]], it does not insist on explicit type [[declaration (computer science)|declaration]]s. If a function's type is not explicitly declared, the interpreter [[type inference|infer]]s it from the type of its parameters and how they are used within the function. In addition to the basic types (<code>char</code>, <code>num</code>, <code>bool</code>), it includes an "anything" type where the type of a parameter does not matter, as in the list-reversing function:
 
<sourcesyntaxhighlight lang="haskell">
rev [] = []
rev (a:x) = rev x ++ [a]
</syntaxhighlight>
</source>
 
which can be applied to a list of any data type, for which the explicit function type declaration would be:
 
<sourcesyntaxhighlight lang="haskell">
rev :: [*] -> [*]
</syntaxhighlight>
</source>
 
Finally, it has mechanisms for creating and managing program [[module (programming)|module]]s whose internal functions are invisible to programs calling those modules.
Line 130 ⟶ 139:
The following Miranda script determines the set of all subsets of a set of numbers
 
<sourcesyntaxhighlight lang="haskell">
subsets [] = [[]]
subsets (x:xs) = [[x] ++ y | y <- ys] ++ ys
where ys = subsets xs
</syntaxhighlight>
</source>
 
and this is a literate script for a function <code>primes</code>
which gives the list of all prime numbers
 
<sourcesyntaxhighlight lang="consoletext">
> || The infinite list of all prime numbers.
 
Line 148 ⟶ 157:
> primes = sieve [2..]
> sieve (p:x) = p : sieve [n | n <- x; n mod p ~= 0]
</syntaxhighlight>
</source>
 
Here, we have some more examples
 
<syntaxhighlight lang="texthaskell">
max2 :: num -> num -> num
max2 a b = a, if a>b
Line 162 ⟶ 171:
multiply :: num -> num -> num
multiply 0 b = 0
multiply a b = ab + (multiply (a-1) b)
 
fak :: num -> num
fak 0 = 1
fak 1n = n * fak (n-1)
fak n = n * (fak n-1)
 
 
Line 220 ⟶ 228:
minel (N l w r) = minel l
 
||Traversing: going through values of tree, putting them in list
 
preorder,inorder,postorder :: tree * -> [*]
Line 248 ⟶ 256:
and x y = False
 
|| A AVL-Tree is a tree where the difference between the of the child nodes is not higher than 1
|| i still have to test this
 
Line 255 ⟶ 263:
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) (loeschendelete (minel r) r)
delete x (N l x r) = N (loeschendelete (maxel l) l) (maxel l) r
delete x (N l w r) = N (loeschendelete x l) w (loeschendelete x r)
 
</syntaxhighlight>
Line 272 ⟶ 280:
 
==External links==
* {{Official website}}
* [https://codeberg.org/DATurner/miranda The currently maintained open source version] of Professor Turner's interpreter for Miranda.
* [http://bignum.sf.net The infinite precision math library], a large example of programming in Miranda (and Haskell).
 
{{Programming language}}
{{Authority control}}
 
Line 279 ⟶ 290:
[[Category:Functional languages]]
[[Category:History of computing in the United Kingdom]]
[[Category:Programming languages created in 1985]]