Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Concepts of Programming Languages A Brief Intro to Programming in Haskell
Lecturer: Gabriele KellerTutor: Liam O’ConnorUniversity of New South WalesSchool of Computer Sciences & EngineeringSydney, Australia!!COMP 3161/9161 Week 1
Functional languages have a solid mathematical foundation
• Functional languages are based on the lambda calculus
‣ Introduced by Alonzo Church in the 1930s
‣ Original aim: to solve Hilbert's "Entscheidungsproblem"
‣ A. Church and A. Turing independently gave a negative answer to Hilbert's problem (Church-Turning Theorem)
‣ 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/w
iki/File:Alonzo_Church.jpg
• 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
One step ahead
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
module Main where!!main = do! { putStrLn "Please enter expression:”! ; lexStr <- getLine! ; putStrLn lexStr! }!
• We start by reading a string from stdIO, and printing it (because we can’t do anything more interesting yet)
every program contains exactly one module named
Main, can import other modules
First step: read string from stdIO
module Main where!!main = do ! { putStrLn "Please enter expression:"! ; lexStr <- getLine! ; putStrLn lexStr! }!
• We start by reading a string from stdIO, and printing it (because we can’t do anything more interesting yet)
the Main module contains exactly one
function called main
First step: read string from stdIO
module Main where!!main = do ! { putStrLn "Please enter expression:"! ; lexStr <- getLine! ; putStrLn lexStr! }!
• We start by reading a string from stdIO, and printing it (because we can’t do anything more interesting yet)
putStrLn is a function which takes a string as argument. No parenthesis necessary around arguments in Haskell!
First step: read string from stdIO
module Main where!!main = do ! { putStrLn "Please enter expression:"! ; lexStr <- getLine! ; putStrLn lexStr! }!
• We start by reading a string from stdIO, and printing it (because we can’t do anything more interesting yet)
do is followed by a sequence of IO operations curly braces and semicolons optional,
but indentation matters!
First step: read string from stdIO
module Main where!!main = do ! putStrLn "Please enter expression:"! lexStr <- getLine! putStrLn lexStr! !
• We start by reading a string from stdIO, and printing it (because we can’t do anything more interesting yet)
ok - all IO actions are indented to the same level, assumed to belong to the same block
First step: read string from stdIO
module Main where!!main = do ! putStrLn "Please enter expression:"! lexStr <- getLine! putStrLn lexStr! !
• We start by reading a string from stdIO, and printing it (because we can’t do anything more interesting yet)
‘lexStr...’ indentation signals that it is part of the
previous line
First step: read string from stdIO
module Main where!!main :: IO () -- has to have this type!!main = do ! putStrLn "Please enter expression:" :: IO ()! lexStr <- getLine :: IO String! putStrLn lexStr :: IO ()!
• We start by reading a string from stdIO, and printing it (because we can’t do anything more interesting yet)
• 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 ....