360 likes | 601 Vues
Miranda. Programming Languages Joshua Campbell Chris Heyman. Outline. History Grammar & Syntax Examples Advantages/Disadvantages Conclusion. History. SASL. St. Andrews Static Language David Turner, U. of St. Andrews, 1972 Purely functional
E N D
Miranda Programming Languages Joshua Campbell Chris Heyman
Outline History Grammar & Syntax Examples Advantages/Disadvantages Conclusion
SASL St. Andrews Static Language David Turner, U. of St. Andrews, 1972 Purely functional 1976 – redesigned to use non-strict (lazy) evaluation
KRC Kent Recursive Calculator David Turner, U. of Kent,1981 Based on SASL Adds pattern-matching, guarded expressions, and list comprehensions
Miranda David Turner, 1983-86 Based on KRC, /w ML and Hope ideas Adds polymorphic strong typing. “A Non-Strict Polymorphic Functional Language”
Miranda • Owned by Research Software Ltd. • First purely functional language to be commercially supported. • Significant influence on future functional languages. • Haskell
Why “Miranda”? • Latin word meaning “to be wondered at.” • Shakespeare - The Tempest • Daughter of Prospero • Secluded on enchanted isle, free from evils of the world. • “O Brave New World!”
Purely Functional Functions musthave no side-effects No imperative features Programs called “scripts” No ordering applied to statements in a script Lazy evaluation (like Hope) Strongly typed
Lists and Tuples • Tuples • sequences of elements of potentially mixed types, and are written delimited with parentheses • In this case we have, in the same Tuple: • a character string(grammatically a list of characters) • integers • boolean • Ex: this_employee = ("Folland, Mary", 10560, 35, False)
Lists and Tuples • List Syntax • concatenation is ++ • subtraction is - - • construction is : • sizing is # • indexing is ! • Example on next page
Lists and Tuples week_days = ["Mon","Tue","Wed","Thur","Fri"] days = week_days ++ ["Sat","Sun"] days = "Nil":days days!0 → "Nil" days = days -- ["Nil"] #days → 7
List Building Shortcuts • [..] is used for lists whose elements form an arithmetic series, with the possibility for specifying an increment other than 1 • Ex: fac n = product [1..n] odd_sum = sum [1,3..100]
List Building Shortcuts • List Comprehensions (formerly ZF Expressions) • Syntactic construct for creating a list based on existing lists. • Come in two main forms: • An expression applied to a series of terms • Ex: squares = [ n * n | n <- [1..] ] • Which is read: list of n squared where n is taken from the list of all positive integers
List Building Shortcuts • Second Main Form: • A series: where each term is a function of the previous one • powers_of_2 = [ n | 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: [1..]
Functional Language Properties • Functions are first-class citizens, • Which is to say that they can be: • Passed as parameters to other functions • Returned as results • Or included as elements of data structures.
Functional Language Properties A function requiring two or more parameters may be "partially parameterised", or curried, by supplying less than the full number of parameters.
Functional Language Properties • This gives another function which, given the remaining parameters, will return a result. For example: add a b = a + b increment = add 1 • This is a roundabout way of creating a function "increment" which adds one to its argument Int Add(int a, int b) { return (a + b); } Int Increment(int n) { return Add(n, 1); }
Functional Language Properties • Add a b = a + b • 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. I.E.: • Add 4 7 [Add(4, 7)] = 4 + 7 • Add4 [Add4(7)] = 4 + 7
Functional Language Properties • Any function taking two parameters can be turned into an infix operator • For example: given the definition of the add function previously, the term $add is in every way equivalent to the + operator. • 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
Functional Language Properties • Increment = (+) 1 • Increment(int n) = n + 1 • Increment = Add 1 • Increment(int n) { return Add(n,1); } • Increment = Add1 • Increment(int n) { return Add1(n); } • Add1 = + 1 • Add1(int n) { return n +1; }
Functional Language Properties • More Examples: • half = (/ 2) • reciprocal = (1 /) • 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.
Functional Language Properties Although Miranda is a strongly typed programming language, it does not insist on explicit type declarations If a function's type is not explicitly declared, the interpreter infers it from the type of its parameters and how they are used within the function
Functional Language Properties • In addition to the basic types (char, num, bool), Miranda 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] • Given a list, breaks it up into ‘a’ (the first element of the list) and ‘x’ (the rest of the list). • Recursively reverses the rest of the list, appends the old first element onto the end, and returns the reversed list.
Functional Language Properties rev [] = [] rev (a:x) = rev x ++ [a] • This can be applied to a list of any data type, for which the explicit function type declaration would be: • rev :: [*] -> [*]
Example - Fibonacci fibs = map fib [0..] fib 0 = 0 fib 1 = 1 fib (n+2) = fibs!(n+1) + fibs!n
Example - Quicksort qsort [] = [] qsort (a:x) = qsort [b|b<-x;b<=a] ++ [a] ++ qsort[b|b<-x;b>a]
Advantages Explicit (no side-effects) Short, succinct programs Algebraic syntax
Disadvantages • No imperative features at all • May be difficult to translate a task into purely functional form. • Sometimes a problem is more easily understood in imperative form. • Complex syntax, steep learning curve • Official implementation only available for Unix-based systems. • Requires cygwin on Windows.
Miranda Programming Languages Joshua Campbell Chris Heyman