Download Concepts of Programming Languages A Brief Intro to Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Intuitionistic type theory wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Scala (programming language) wikipedia , lookup

Lambda calculus wikipedia , lookup

Curry–Howard correspondence wikipedia , lookup

Combinatory logic wikipedia , lookup

Lambda lifting wikipedia , lookup

Standard ML wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Lambda calculus definition wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Concepts of Programming Languages
A Brief Intro to Programming in Haskell
Lecturer: Gabriele Keller
Tutor: Liam O’Connor
University of New South Wales
School of Computer Sciences & Engineering
Sydney, Australia
!
!
COMP 3161/9161 Week 1
Functional languages have a solid
mathematical foundation
‣ Introduced by Alonzo Church in the 1930s
‣ Original aim: to solve Hilbert's "Entscheidungsproblem"
‣ A. Church and A. Turing independently gave a
answer to Hilbert's problem (Church-Turning Theorem)
negative
‣ Today, there is a rich theory of typed and untyped lambda calculi
‣ Large parts of the theory of programming languages is based on this
http://en.wikipedia.org/wiki/File:Alonzo_Church.jpg
• Functional languages are based on the lambda calculus
One step ahead
• Pivotal inventions in programming languages:
- Automatic garbage collection
‣ often praised feature of Java, C#
- Strong typing
‣ fundamental component of safety in Java and C#
- Meta programming
‣ highly expressive programming method, e.g., C++’
- Lambda functions and closures
- Pattern matching
‣ language support for decomposition of user defined data types • All of these features initially invented and developed in the context of functional
programming
PL features
• Automatic garbage collections
- implemented by McCarthy around 1960 for his Lisp interpreter
- for a long time, automatic garbage collection was only studied in the
context of declarative languages
• Meta programming and reflection
- Lisp with its quoting and EVAL was the first meta programming
environment
PL features
• Formal type systems and strong typing
- most research on type systems in the context of functional languages
- parametric polymorphism (or generics) invented in ML
- outside of FL, strong typing was until recently regarded as too restrictive
- there is a strong formal link between type systems and mathematical logic
• Lambda expressions and closures (in C#, C/C++, Objective-C, Java, Swift
etc)
- Core feature of functional programming
Haskell
• What is Haskell?
‣ is a polymorphic, statically typed, lazy, purely functional language
• polymorphic
‣ functions and data constructors can be defined abstracting over argument
type
• statically typed ‣ the type of every subexpression is inferred and checked at compile time
• purely functional
‣ side effects are strictly controlled
• non-strict/lazy language:
‣ can handle infinite structures, only computes what is actually needed
Haskell
• Most widely used implementation
- Glasgow Haskell Compiler (GHC)
- Open source (BSD3)
- available for most popular OSes (GNU/Linux, MacOS, Windows, Solaris,
etc)
- is both a highly optimising compiler as well as an interactive interpreter
- implements many extensions to Haskell ‘98
- comes with a extensive set of libraries (with many more at
http://hackage.haskell.org)
- support for multicore parallelism
Haskell
• Things to look at:
‣ Haskell Resources on the course web page
‣ Haskell exercises on the course web page
‣ Books on Haskell:
- Real World Haskell, O’Reilly Media
- Learn You a Haskell for Great Good!, No starch press
- both available in print, and a free version online
Let’s write our first Haskell Program
• Let’s write a lexer for a simple arithmetic expression language:
‣ integers
‣ operators +, *, -
‣ parenthesis
• Lexer: converts a string into a sequence of tokens
‣ doesn’t check if the string is a legal expression
- “3 + + ) 5”
- “2 asa” - “2 ^ 6”
First step: read string from stdIO
• We start by reading a string from stdIO, and printing it
(because we can’t do anything more interesting yet)
module Main where!
!
main = do!
{ putStrLn "Please enter expression:”!
; lexStr <- getLine!
; putStrLn lexStr!
}!
every program contains
exactly one module named
Main, can import
other modules
First step: read string from stdIO
• We start by reading a string from stdIO, and printing it
(because we can’t do anything more interesting yet)
module Main where!
!
main = do !
{ putStrLn "Please enter expression:"!
; lexStr <- getLine!
; putStrLn lexStr!
}!
the Main module contains
exactly one
function called main
First step: read string from stdIO
• We start by reading a string from stdIO, and printing it
(because we can’t do anything more interesting yet)
module Main where!
!
main = do !
{ putStrLn "Please enter expression:"!
; lexStr <- getLine!
; putStrLn lexStr!
}!
putStrLn is a function which takes a string as argument.
No parenthesis necessary around arguments in Haskell!
First step: read string from stdIO
• We start by reading a string from stdIO, and printing it
(because we can’t do anything more interesting yet)
module Main where!
!
main = do !
{ putStrLn "Please enter expression:"!
; lexStr <- getLine!
; putStrLn lexStr!
}!
do is followed by a sequence of IO operations
curly braces and semicolons optional,
but indentation matters!
First step: read string from stdIO
• We start by reading a string from stdIO, and printing it
(because we can’t do anything more interesting yet)
module Main where!
!
main = do !
putStrLn "Please enter expression:"!
lexStr <- getLine!
ok - all IO actions are indented
putStrLn lexStr!
to the same level, assumed to
!
belong to the same block
First step: read string from stdIO
• We start by reading a string from stdIO, and printing it
(because we can’t do anything more interesting yet)
module Main where!
!
main = do !
putStrLn "Please enter expression:"!
lexStr <- getLine!
‘lexStr...’ indentation
putStrLn lexStr!
signals that it is part of the
!
previous line
First step: read string from stdIO
• We start by reading a string from stdIO, and printing it
(because we can’t do anything more interesting yet)
module Main where!
!
main :: IO () -- has to have this type!!
main = do !
putStrLn "Please enter expression:" :: IO ()!
lexStr <- getLine
:: IO String!
putStrLn lexStr
:: IO ()!
• Types: • strongly typed : every subexpression has a type
• the compiler infers the type, the user can annotate expression,
but doesn’t have to provide the types
• however, it’s good style to add type annotations to all function
definitions
• Basic types: more or less the usual stuff
‣ Int Integer Float Double Char ....