16
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

Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 2: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 3: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

• 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

Page 4: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 5: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 6: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 7: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 8: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 9: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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”

Page 10: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 11: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 12: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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!

Page 13: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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!

Page 14: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 15: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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

Page 16: Concepts of Programming Languages A Brief Intro to ...cs3161/14s2/lectures/01/Haskell.pdfPL features • Formal type systems and strong typing-most research on type systems in the

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