haskell programming language. haskell is… memory managed (allocation, collection) “typeful”...

23
Haskell programming language

Upload: sylvia-porter

Post on 12-Jan-2016

247 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell

programming language

Page 2: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell is…

Memory managed (allocation, collection) “Typeful” (static, strong)

– Types are checked at compile-time– Types cannot be coerced (in general)

Pure functional programming– Emphasis on functions– Referential transparency– All variables are constant

Page 3: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell - pros & cons

Concurrency– The #1 on Language Shootout for threading– All non-I/O code is concurrent by default– (mutations are handled as I/O code)

Readability– No parentheses or commas– Higher-order functions reduce lines-of-code– (no syntax as a reminder of context)

Page 4: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell - namespaces

Main main True a Bool Eq

-- modules (packages)

-- value variables (functions)

-- value constructors

-- type variables (generics)

-- type constructors

-- type classes (interfaces)

Page 5: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell - syntax

Derived syntax

f x = expr

(x *)

(* y)

(*)

x `times` y

f $ g $ h x

(f . g . h) x

do a; b; c

-- lambda

-- sections

-- sections

-- sections

-- infix func

-- parens

-- compose

-- I/O bind

Core syntax

f = \x -> expr

\y -> x * y

\x -> x * y

\x y -> x * y

times x y

f (g (h x))

f (g (h x))

a >>= b >>= c

Page 6: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell - type syntax

life = 42 :: Int

life :: Int

life = 42

Page 7: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell - types

“Hello” :: String

length :: [a] -> Int

floor :: Float -> Int

map :: (a -> b) -> [a] -> [b]

42 :: Int

(+) :: Int -> Int -> Int

42 :: Num a => a

(+) :: Num a => a -> a -> a

Page 8: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell - type system

Int, Word, Float, Double, Char

type String = [Char]

data Maybe a = Nothing | Just a

class Eq a where

(==), (/=) :: a -> a -> Bool

instance Eq Bool where

True == True = True

False == False = True

_ == _ = False

-- built-in types

-- type synonyms

-- data types

-- type classes

-- type class instances

Page 9: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell - datatypes

data Bool = False | True

data Tree a = Leaf a

| Branch (Tree a) (Tree a)

data Rect = Rect Int Int Int Int

data Rect = Rect {

x, y :: Int,

width :: Int,

height :: Int}

-- enumerations

-- generics

-- unlabeled record

-- labeled record

Page 10: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell - example programs

main = return ()

main = putStrLn “Hello World”

main = interact id

main = interact (unlines . reverse . lines)

main = do args <- getArgs case args of "-n":a -> putStr (unwords a) a -> putStrLn (unwords a)

-- null program

-- hello world

-- UNIX cat

-- GNU tac

-- UNIX echo

Page 11: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

version control system

Page 12: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Darcs - overview

The Theory of Patches A “branch” is a set of patches “Spontaneous branches” Every checkout is a branch Every checkout is a repository

Page 13: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Darcs - interactive

Interactive “pull”– You chose what patches to download

Interactive “push”– You chose what patches to commit externally

Interactive “record”– You chose what files to commit locally

Page 14: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Parsec

parser library

Page 15: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Parsec - combinators

many :: Parser a -> Parser [a]

many1 :: Parser a -> Parser [a]

optional :: Parser a -> Parser (Maybe a)

sepBy :: Parser a -> Parser s -> Parser [a]

sepBy1 :: Parser a -> Parser s -> Parser [a]

endBy :: Parser a -> Parser s -> Parser [a]

endBy1 :: Parser a -> Parser s -> Parser [a]

char :: Char -> Parser Char

between :: Parser open

-> Parser close

-> Parser a -> Parser a

-- like regex*

-- like regex+

-- like regex?

-- ... , ... , ...

-- ... ; ... ; ... ;

-- c

-- < ... >

Page 16: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Parsec - example parsers

number = many digit

string = between

(char ‘"’)

(char ‘"’)

(many anyChar)

lisp = number

<|> string

<|> identifier

<|> parens $ many $ lexeme lisp

Page 17: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

xmonad

X11 window manager

Page 18: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

XMonad - overview

Tilling window manager (like Ratpoison) Libraries of extensions and status bars Customizable (config file is the app) Full keyboard accessibility

Page 19: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

XMonad - example config

module Main where

import XMonadimport System.Exitimport qualified XMonad.StackSet as Wimport qualified Data.Map as M

myKeys conf@(XConfig {XMonad.modMask = modMask}) = M.fromList $ [ ((modMask .|. shiftMask, xK_Return), spawn $ XMonad.terminal conf), ((modMask .|. shiftMask, xK_q ), io (exitWith ExitSuccess))]

myMouseBindings (XConfig {XMonad.modMask = modMask}) = M.fromList $ [ ((modMask, button1), (\w -> focus w >> mouseMoveWindow w)), ((modMask, button2), (\w -> focus w >> windows W.swapMaster)), ((modMask, button3), (\w -> focus w >> mouseResizeWindow w))]

defaults = defaultConfig { keys = myKeys, mouseBindings = myMouseBindings, terminal = "xterm"}

main = xmonad defaults

Page 20: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Yi

extensible text editor

Page 21: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Yi - overview

Extensible– Fully dynamic application (hs-plugins)– All state is serialized and reloaded

Customizable– yi --as=vim (for vim key bindings)– yi --as=emacs (for emacs key bindings)

Page 22: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Yi - structure

Taken from <http://www.cse.unsw.edu.au/~dons/papers/SC05.html>

Page 23: Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types

Haskell

Thank You