Miranda Programming Language - PowerPoint PPT Presentation

About This Presentation
Title:

Miranda Programming Language

Description:

The aim of the Miranda system is to provide a modern functional language, ... Expressions and Assignments. Assignment statements are simply defined using the = sign. ... – PowerPoint PPT presentation

Number of Views:359
Avg rating:3.0/5.0
Slides: 30
Provided by: vin7
Learn more at: https://www.nku.edu
Category:

less

Transcript and Presenter's Notes

Title: Miranda Programming Language


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. 
  • 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 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. 
  • 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. 
  • "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. 
  • 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 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
  • 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
  • Laws

15
Lists
  • A List is defined as a set of homogeneously typed
    values. 
  • 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
  • - This operation prefixes an element  to  the 
    front  of  a list.
  • Example 01,2,3 has the value 
    0,1,2,3
  • - This operation returns the length of a list
  • Example days is 7
  • ! This operation does subscripting
  • 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. 
  • For example
  • The greatest common divisor function can be
    written as         gcd a b gcd (a-b) b, if
    agtb                 gcd a (b-a), if altb
                    a, if ab

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. 
  • 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
    deltalt0                        
    -b/(2a),               if delta0
                            -b/(2a)
    radix/(2a),                           
    -b/(2a) - radix/(2a), if deltagt0
                              where
                              delta bb - 4ac
                              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.  
  • This  provides another method of doing case
    analysis which is often more elegant than the use
    of guards. 
  • For example
  • fac 0 1         fac (n1) (n1)fac n

21
Pattern Matching More Examples
  • Example 1
  • fib 0 0         fib 1 1         fib
    (n2) fib (n1) fib n
  • Example 2 On Lists
  • sum 0         sum (ax) a sum x
  • Example 3 On Tuples
  • 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"). 
  • Example
  • nn n lt- 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. 
  • 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
    nn n lt- 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 -gt Integers and Floating Points
  • Bool -gt True and False
  • Char -gt 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 -gt bool
                  push -gt stack -gt stack
                  pop stack -gt stack
                  top stack -gt
  •         stack         empty
            push a x (ax)         pop (ax) x
            top (ax) 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.
Write a Comment
User Comments (0)
About PowerShow.com