Download presentation
Presentation is loading. Please wait.
Published byBarbra Washington Modified over 8 years ago
1
Miranda Programming Language By Bindu H. Vinay
2
History of Miranda Miranda was developed in 1985-86 by David Turner It is currently being marketed by Research Software Ltd. of England. It is currently being marketed by Research Software Ltd. of England. Miranda was the successor of the functional languages SASL and KRC. Miranda was the successor of the functional languages SASL and KRC. The main goal for creating Miranda was to produce a commercial version of a standard non- strict purely functional language. The main goal for creating Miranda was to produce a commercial version of a standard non- strict purely functional language. The development environment is very flexible and easy-to-use.
3
Language Features Non-Strict: In Non-Strict functional languages, the arguments to a function are not evaluated until they are actually required within the functions being called. Non-Strict: In Non-Strict functional languages, the arguments to a function are not evaluated until they are actually required within the functions being called. Any parameter can be passed to a function and until it is needed in that function, this parameter is not evaluated. This is also known as lazy evaluation. The main advantage of using this method is that it allows for passing infinite element data structures to a function.
4
Language Features Purely Functional: Pure functional languages perform all computation using function application. Purely Functional: Pure functional languages perform all computation using function application. "Side-effect" features such as destructive assignments and looping are not even provided within the language. All programs have to strictly adhere to the functional approach of programming.
5
Language Features Miranda runs under the UNIX operating system. The aim of the Miranda system is to provide a modern functional language, embedded in an industrial quality programming environment.
6
Miranda Environment The Miranda system is interactive and runs under UNIX as a self contained subsystem. The basic action is to evaluate expressions, supplied by the user at the terminal, in the environment established by the current script.
7
Miranda Environment The Miranda compiler works in conjunction with an editor (normally this is "vi" but it can be set to any editor of the users choice) and scripts are automatically recompiled after edits, and any syntax or type errors signaled immediately. The system permits a high proportion of logical errors to be detected at compile time.
8
Miranda Environment There is quite a large library of standard functions. There is a good interface to UNIX, permitting Miranda programs to take data from, and send data to, arbitrary UNIX files. It is also possible to invoke Miranda programs directly from the UNIX shell, and to combine them, via UNIX pipes, with processes written in other languages.
9
Miranda – Areas of Application Rapid prototyping Teaching functional programming As a specification language Research into functional programming A general purpose programming tool
10
Basic Concepts of Miranda A program or script as it is called by Miranda programmers, is a collection of equations defining various functions and data structures which we are interested in computing. The order in which the equations are given is not significant. The order in which the equations are given is not significant. There is for example no obligation for the definition of an entity to precede its first use. Miranda as a language is terse.
11
Basic Concepts of Miranda There are no mandatory type declarations, although the language is strongly typed. There are no mandatory type declarations, although the language is strongly typed. There are no semicolons at the end of definitions - the parsing algorithm makes intelligent use of layout.
12
Simple Program Example z = sq x / sq y sq n = n * n x = a + b y = a - b a = 10 b = 5 z = sq x / sq y sq n = n * n x = a + b y = a - b a = 10 b = 5 As you can see, there is no order in the way the statements are defined. The function sq is defined as shown, there are no parenthesis etc. n is the formal parameter.
13
Expressions and Assignments Assignment statements are simply defined using the = sign. Example: x = a + b
14
Data Structures The most useful three data structures of Miranda are: Lists Tuples Tuples Laws Laws
15
Lists A List is defined as a set of homogeneously typed values. A List is defined as a set of homogeneously typed values. The list data structure is an extremely powerful feature of Miranda. The list data structure is an extremely powerful feature of Miranda. It allows quick and simple list processing and also allows infinite lists. Example: week_days = ["Mon","Tue","Wed","Thur","Fri"]
16
Operations on Lists ++ Operation – This is used to add items at the end of the list. Example: days = week_days ++ ["Sat","Sun"] -- Operation – This is used to subtract items from the end of the list. Example: noMondays = week_days -- [“Mon”] Example: noMondays = week_days -- [“Mon”] : - This operation prefixes an element to the front of a list. Example: 0:[1,2,3] has the value [0,1,2,3] Example: 0:[1,2,3] has the value [0,1,2,3] # - This operation returns the length of a list Example: #days is 7 Example: #days is 7 ! – This operation does subscripting Example: days!0 is "Mon" Example: days!0 is "Mon"
17
Tuples It is a non-homogenous sequence of values which can be utilized to form enumerated data types or even form complex data structures such as records. In other words, a sequence of elements of mixed type is called a Tuple, and is written using parentheses instead of square brackets. Example: employee = ("Jones",True,False,39)
18
Guarded Equations An equation can have several alternative right hand sides distinguished by "guards" - written on the right following a comma. An equation can have several alternative right hand sides distinguished by "guards" - written on the right following a comma. For example: –The greatest common divisor function can be written as: gcd a b = gcd (a-b) b, if a>b = gcd a (b-a), if a b = gcd a (b-a), if a<b = a, if a=b
19
Where Clause It is also permitted to introduce local definitions on the right hand side of a definition, by means of a "where" clause. It is also permitted to introduce local definitions on the right hand side of a definition, by means of a "where" clause. –For Example: –Definition of a function for solving quadratic equations (it either fails or returns a list of one or two real roots): – quadsolve a b c = error "complex roots", if delta 0 where delta = b*b - 4*a*c radix = sqrt delta
20
Pattern Matching It is permitted to define a function by giving several alternative equations, distinguished by the use of different patterns in the formal parameters. It is permitted to define a function by giving several alternative equations, distinguished by the use of different patterns in the formal parameters. This provides another method of doing case analysis which is often more elegant than the use of guards. This provides another method of doing case analysis which is often more elegant than the use of guards. –For example: fac 0 = 1 fac (n+1) = (n+1)*fac n fac 0 = 1 fac (n+1) = (n+1)*fac n
21
Pattern Matching – More Examples –Example 1: fib 0 = 0 fib 1 = 1 fib (n+2) = fib (n+1) + fib n fib 0 = 0 fib 1 = 1 fib (n+2) = fib (n+1) + fib n –Example 2: On Lists sum [] = 0 sum (a:x) = a + sum x sum [] = 0 sum (a:x) = a + sum x -Example 3: On Tuples first (a,b) = a second (a,b) = b first (a,b) = a second (a,b) = b
22
Higher Order Functions Miranda is a fully higher order language - functions can be both passed as parameters and returned as results. Function application is left associative, so when we write "f x y" it is parsed as “(f x) y”
23
List Comprehensions List comprehensions give a concise syntax for a rather general class of iterations over lists. The syntax is adapted from an analogous notation used in set theory (called "set comprehension"). The syntax is adapted from an analogous notation used in set theory (called "set comprehension"). –Example: [ n*n | n <- [1..100] ]
24
Lazy Evaluation and Infinite Lists Miranda's evaluation mechanism is "lazy", in the sense that no sub-expression is evaluated until its value is known to be required. Miranda's evaluation mechanism is "lazy", in the sense that no sub-expression is evaluated until its value is known to be required. One consequence of this is that is possible to define functions which are capable of returning an answer even if one of their arguments is undefined. The other main consequence of lazy evaluation is that it makes it possible to write down definitions of infinite data structures. –Examples naturals = [0..] odds = [1,3..] squares = [ n*n | n <- [0..] ]
25
Polymorphic Strong Typing Miranda is strongly typed i.e., every expression and every subexpression has a type, which can be deduced at compile time, and any inconsistency in the type structure of a script results in a compile-time error. Type declaration is not required. The compiler is always able to deduce the type of an identifier from its defining equation.
26
Primitive Types There are three primitive types –Num -> Integers and Floating Points –Bool -> True and False –Char -> Ascii character set Examples: [[1,2],[2,3],[4,5]] is of type [[num]] String “hello” is actually a list ['h','e','l','l','o'] Tuple (True,"hello",36) is of type (bool,[char],num)
27
User Defined Types The user may introduce new types. This is done by an equation in "::=“ –Example: tree ::= Nilt | Node num tree tree Nilt – atomic constructor Node – takes 3 params Type Synonyms can be defined using == –Example: string == [char]
28
Abstract Data Types By using “Abstype” and “with” –Stack implementation abstype stack * with empty :: stack * isempty :: stack * -> bool push :: * -> stack * -> stack * pop :: stack * -> stack * top :: stack * -> * stack * == [*] empty = [] push a x = (a:x) pop (a:x) = x top (a:x) = a
29
Conclusion Miranda is a strong-typed non-strict functional language that is useful for rapid prototyping. It is mainly implemented on Unix systems. Provides some useful data types such as Lists and Tuples.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.