Slide 1

Slide 1 text

Lisp in Huskell

Slide 2

Slide 2 text

Huskelldoge

Slide 3

Slide 3 text

By an 100% Haskell dummy

Slide 4

Slide 4 text

What is Lisp, really?

Slide 5

Slide 5 text

(def b 100) ;; defining vars! (def a (fn [c] (+ b c))) ;; defining functions! (a 5 7) ;; executing functions

Slide 6

Slide 6 text

Easy!

Slide 7

Slide 7 text

How do we express it?

Slide 8

Slide 8 text

data LispExpression = LispSymbol String |! ReservedKeyword ReservedKeyword |! LispList [LispExpression] |! LispVector [LispExpression] |! LispNumber Integer |! --LispNumber LispNum |! LispString String |! LispBool Bool |! LispFunction LispFunk |! -- LispFunction [LispExpression] LispExpression |! LispNil! deriving (Generic)

Slide 9

Slide 9 text

Parsing

Slide 10

Slide 10 text

Come on bro, really?

Slide 11

Slide 11 text

Reserved keywords (if, def, fn) parseReserved :: Parser LispExpression! parseReserved = do! res <- try (string "def") <|>! try (string "if") <|>! try (string "fn")! return $ toSexp res

Slide 12

Slide 12 text

Numbers parseNumber :: Parser LispExpression! parseNumber = do! _ <- try (many (string "#d"))! sign <- many (oneOf "-")! num <- many1 (digit)! if (length sign) > 1! then pzero! else return $ (LispNumber . read) $ sign ++ num

Slide 13

Slide 13 text

Booleans parseTrue :: Parser LispExpression! parseTrue = do! res <- try (string "true")! return $ LispBool True! ! parseFalse :: Parser LispExpression! parseFalse = do! res <- try (string "false")! return $ LispBool False

Slide 14

Slide 14 text

Booleans parseReserved :: Parser LispExpression! parseReserved = do! res <- try (string "def") <|>! try (string "if") <|>! try (string "fn")! return $ toSexp res! ! parseTrue :: Parser LispExpression! parseTrue = do! res <- try (string "true")! return $ LispBool True

Slide 15

Slide 15 text

Symbols parseSymbol :: Parser LispExpression! parseSymbol = do! first <- letter <|> symbols! rest <- many (letter <|> digit <|> symbols)! let symbol = first:rest! return $ LispSymbol symbol

Slide 16

Slide 16 text

Lists parens parseList <|>! ! ! ! parseList :: Parser LispExpression! parseList = liftM LispList $ sepBy parseLispExpression whiteSpace

Slide 17

Slide 17 text

Lists parseString :: Parser LispExpression! parseString = do _ <- char '"'! x <- many (noneOf "\"")! _ <- char '"'! return $ LispString x

Slide 18

Slide 18 text

So, let’s see how it werkz

Slide 19

Slide 19 text

(+ 1 1) LispList [LispSymbol "plus", ! LispNumber 1, ! LispNumber 1]

Slide 20

Slide 20 text

Evaluation process

Slide 21

Slide 21 text

Easy-peasy

Slide 22

Slide 22 text

Literals evaluate to their values

Slide 23

Slide 23 text

Numbers - to numbers

Slide 24

Slide 24 text

Strings - to strings

Slide 25

Slide 25 text

Lists - recursively evaluate their values left to right

Slide 26

Slide 26 text

Symbols - to…

Slide 27

Slide 27 text

(def a 1)! (def b 2)! (+ a b)

Slide 28

Slide 28 text

(def a 1)! (def b 2)! (+ a b) What is `a`?

Slide 29

Slide 29 text

Let’s check the Environment

Slide 30

Slide 30 text

Ah ok, a is 1

Slide 31

Slide 31 text

(def a 1)! (def b 2)! (+ a b) What is `b`?

Slide 32

Slide 32 text

Ah ok, b is 1

Slide 33

Slide 33 text

Substitution β-reduction

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

(+ a b)! ! ! ! ! ! (+ 1 1) Becomes

Slide 36

Slide 36 text

(def a 1)! (def b 2)! (def c 3)! (def d 4)! ! (+ a b (* c d))

Slide 37

Slide 37 text

(def a 1)! (def b 2)! (def c 3)! (def d 4)! ! (+ a b (* 4 3))

Slide 38

Slide 38 text

(def a 1)! (def b 2)! (def c 3)! (def d 4)! ! (+ a b 12)

Slide 39

Slide 39 text

(def a 1)! (def b 2)! (def c 3)! (def d 4)! ! (+ 1 2 12)

Slide 40

Slide 40 text

When you see the symbol, look for it in Environment

Slide 41

Slide 41 text

Environment

Slide 42

Slide 42 text

Monad is just a box, right?

Slide 43

Slide 43 text

type Environment k v = H.BasicHashTable k v! type LispEnvironment = (Environment LispExpression LispExpression)!

Slide 44

Slide 44 text

findVar :: LispEnvironment -> LispExpression -> ! ! ! ! ! ! IO (Maybe LispExpression)! ! findVar env symbol = H.lookup env symbol!

Slide 45

Slide 45 text

Closures and polluting Environment

Slide 46

Slide 46 text

I’m not polluting, I just don’t need it anymore

Slide 47

Slide 47 text

(def sum ! (fn [a b] ! (fn [c] (+ c a b))))! ! (+ a b) ;; Shouldn't work Creates Closure

Slide 48

Slide 48 text

Closure is a temporary Environment

Slide 49

Slide 49 text

Passed only to recursive calls

Slide 50

Slide 50 text

(def sum ! (fn [a b] ! (fn [c] (+ c a b))))! ! (+ a b) ;; Shouldn't work Limited Visibility

Slide 51

Slide 51 text

That leads us to two-step lookup (shines in Haskell)

Slide 52

Slide 52 text

Look up var value in Closure

Slide 53

Slide 53 text

If there’s no Closure, look up Environment

Slide 54

Slide 54 text

If there’s Closure, look up in Closure first (shadowing)

Slide 55

Slide 55 text

If all fails, error

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

lookup_ <- (liftM2 mplus) ! (findVarMaybe closure func_) ! (findVar envRef func_)

Slide 58

Slide 58 text

(def sum ! (fn [a b] ! (fn [c] (+ c a b))))! ! (+ a b) ;; Shouldn't work Limited Visibility

Slide 59

Slide 59 text

That makes me dance!

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Built-in Functions

Slide 62

Slide 62 text

Problem: `+` symbol is a “native” function

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

2 function types

Slide 65

Slide 65 text

data LispFunk = UserFunction [LispExpression] LispExpression |! LibraryFunction String ([LispExpression] -> LispExpression)! deriving (Generic)

Slide 66

Slide 66 text

“User” - is just a lisp expression (list)

Slide 67

Slide 67 text

“Library” - is a Haskell (“native”) function

Slide 68

Slide 68 text

numericOp :: (Integer -> Integer -> Integer) -> ! [LispExpression] -> LispExpression! ! numericOp op args = LispNumber $ foldl1 op $ ! ! ! ! ! ! ! ! ! map unpackNum args! ! builtInOp :: String -> [LispExpression] -> ! LispExpression! builtInOp "+" args = numericOp (+) args

Slide 69

Slide 69 text

If statement

Slide 70

Slide 70 text

Consists of 3 blocks: Test expression Truthy expression Faulty expression

Slide 71

Slide 71 text

Test Expression: True is True (optimisation) Nil is False False is False Anything else is True

Slide 72

Slide 72 text

eval env closure (LispList[(ReservedKeyword IfKeyword), ! ! ! ! ! ! ! ! ! testExpression,! truthyExpression, falsyExpression]) = do! test <- (eval env closure testExpression)! res <- if (isTrue test)! then (eval env closure truthyExpression)! else (eval env closure falsyExpression)! return res

Slide 73

Slide 73 text

Map/reduce

Slide 74

Slide 74 text

Primitives

Slide 75

Slide 75 text

(def empty?! (fn [a] (= a (quote ()))))

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

(def map! (fn [f coll]! (if (empty? coll)! (quote ())! (cons (f (first coll))! (map f (next coll)))))) Recur here

Slide 78

Slide 78 text

(def reduce! (fn [f coll acc]! (if (empty? coll)! acc! (reduce f (next coll) (f acc (first coll)))))) Recur here

Slide 79

Slide 79 text

Bottomline

Slide 80

Slide 80 text

Definition is adding an evaluated expression to hash map

Slide 81

Slide 81 text

Function (fn) captures an enclosed environment and evaluates to internal function primitive

Slide 82

Slide 82 text

If Evaluates only test expression and one of given blocks, skipping the other one

Slide 83

Slide 83 text

Recursion Is given basically for free, since it’s just a var lookup and a function call

Slide 84

Slide 84 text

Evaluation Evaluate left from right, recursively

Slide 85

Slide 85 text

Closures Capture the current environment, dismiss when reaching end of lexical scope

Slide 86

Slide 86 text

Thanks everyone!

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

@ifesdjeen