1 / 34

Miranda

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

kata
Download Presentation

Miranda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Miranda Programming Languages Joshua Campbell Chris Heyman

  2. Outline History Grammar & Syntax Examples Advantages/Disadvantages Conclusion

  3. History

  4. SASL St. Andrews Static Language David Turner, U. of St. Andrews, 1972 Purely functional 1976 – redesigned to use non-strict (lazy) evaluation

  5. KRC Kent Recursive Calculator David Turner, U. of Kent,1981 Based on SASL Adds pattern-matching, guarded expressions, and list comprehensions

  6. Miranda David Turner, 1983-86 Based on KRC, /w ML and Hope ideas Adds polymorphic strong typing. “A Non-Strict Polymorphic Functional Language”

  7. Miranda • Owned by Research Software Ltd. • First purely functional language to be commercially supported. • Significant influence on future functional languages. • Haskell

  8. 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!”

  9. Grammar & Syntax

  10. 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

  11. 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)

  12. Lists and Tuples • List Syntax • concatenation is ++ • subtraction is - - • construction is : • sizing is # • indexing is ! • Example on next page

  13. 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

  14. 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]

  15. 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

  16. 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..]

  17. 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.

  18. 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.

  19. 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); }

  20. 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

  21. 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

  22. 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; }

  23. 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.

  24. 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

  25. 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.

  26. 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 :: [*] -> [*]

  27. Examples

  28. Example - Fibonacci fibs = map fib [0..] fib 0 = 0 fib 1 = 1 fib (n+2) = fibs!(n+1) + fibs!n

  29. Example - Quicksort qsort [] = [] qsort (a:x) = qsort [b|b<-x;b<=a] ++ [a] ++ qsort[b|b<-x;b>a]

  30. Advantages/Disadvantages

  31. Advantages Explicit (no side-effects) Short, succinct programs Algebraic syntax

  32. 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.

  33. Conclusion

  34. Miranda Programming Languages Joshua Campbell Chris Heyman

More Related