[5] | 1 | Prerequisites |
---|
| 2 | ------------- |
---|
| 3 | |
---|
| 4 | You need at least version 2011.4.0.0 of haskell platform and the package |
---|
| 5 | value-supply. If you have haskell platform installed you can install |
---|
| 6 | value-supply by typing |
---|
| 7 | |
---|
| 8 | cabal install value-supply |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | Source files |
---|
| 12 | ------------ |
---|
| 13 | |
---|
| 14 | Examples.hs |
---|
| 15 | ~ Some examples |
---|
| 16 | SizedExp.hs |
---|
| 17 | ~ Embedded language to prove size expressions |
---|
| 18 | Lambda.hs |
---|
| 19 | ~ Size expressions |
---|
| 20 | Constraints.hs |
---|
| 21 | ~ Constraint solver |
---|
| 22 | |
---|
| 23 | Using the examples |
---|
| 24 | ------------------ |
---|
| 25 | |
---|
| 26 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
| 27 | $ ghci Examples.hs |
---|
| 28 | GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help |
---|
| 29 | Loading package ghc-prim ... linking ... done. |
---|
| 30 | Loading package integer-gmp ... linking ... done. |
---|
| 31 | Loading package base ... linking ... done. |
---|
| 32 | Loading package ffi-1.0 ... linking ... done. |
---|
| 33 | [1 of 4] Compiling Lambda ( Lambda.hs, interpreted ) |
---|
| 34 | [2 of 4] Compiling Constraints ( Constraints.hs, interpreted ) |
---|
| 35 | [3 of 4] Compiling SizedExp ( SizedExp.hs, interpreted ) |
---|
| 36 | [4 of 4] Compiling Main ( Examples.hs, interpreted ) |
---|
| 37 | Ok, modules loaded: Lambda, SizedExp, Constraints, Main. |
---|
| 38 | *Main> |
---|
| 39 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
| 40 | |
---|
| 41 | Type "`runTests`" to run all test cases or type "`prove map`" to prove the |
---|
| 42 | function named map. |
---|
| 43 | |
---|
| 44 | Under Windows |
---|
| 45 | ------------- |
---|
| 46 | It is not perfect, and you will see a box after every special character, but at |
---|
| 47 | least it works. |
---|
| 48 | |
---|
| 49 | - Open a console by running the program `cmd` |
---|
| 50 | - Issue the command `chcp 65001` |
---|
| 51 | - In the console cd to the trunk directory |
---|
| 52 | - Now, you can run `ghci Examples.hs` |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | Introduction to Haskell syntax |
---|
| 56 | ------------------------------ |
---|
| 57 | |
---|
| 58 | Haskell syntax is very much like Clean, however there are some expressions you |
---|
| 59 | cannot find in Clean. |
---|
| 60 | |
---|
| 61 | ### Backtick ### |
---|
| 62 | |
---|
| 63 | Every haskell function can be used as infix operator. For example the |
---|
| 64 | following two expressions are the same. |
---|
| 65 | |
---|
| 66 | ~~~~~~~~~~~ |
---|
| 67 | func a b |
---|
| 68 | a `func` b |
---|
| 69 | ~~~~~~~~~~~ |
---|
| 70 | |
---|
| 71 | ### Dollar ### |
---|
| 72 | |
---|
| 73 | Dollar sign is just explicit application (but with different precedence), so it |
---|
| 74 | can be used as a backward pipe operator. The following expressions are the |
---|
| 75 | same. |
---|
| 76 | |
---|
| 77 | ~~~~~~~~~~~~~~ |
---|
| 78 | func (a b) |
---|
| 79 | func $ a b |
---|
| 80 | ~~~~~~~~~~~~~~ |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | The Embedded Language |
---|
| 85 | --------------------- |
---|
| 86 | |
---|
| 87 | As an example the function `map` is examined. |
---|
| 88 | |
---|
| 89 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
| 90 | 1 smap = Abs 5 $ AAbs 18 6 $ List (Var 18) (Abs 8$ App (Var 5) (App (Var 6) (Var 8))) |
---|
| 91 | 2 map :: (SizedExp se) => Size se ( (a->b) -> [a] -> [b] ) |
---|
| 92 | 3 map = bind smap body |
---|
| 93 | 4 where body f l = match l (nil) |
---|
| 94 | 5 ( \x xs -> cons `app` (f `app` x) `app` (map `app` f `app` xs )) |
---|
| 95 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
| 96 | |
---|
| 97 | Here line 3 tells us map is a top-level binding, where the body of the function is |
---|
| 98 | defined in line 4--5 and its size expression is in line 1. Unfortunately the |
---|
| 99 | size expression language has not yet been embedded, so it is a bit difficult to |
---|
| 100 | read. Type `smap` in the GHC console to get a pretty printed form. |
---|
| 101 | |
---|
| 102 | ~~~~~~~~~~~~~~~~~ |
---|
| 103 | *Main> smap |
---|
| 104 | λf.Îs,g.List s (λi.f (g i)) |
---|
| 105 | ~~~~~~~~~~~~~~~~~ |
---|
| 106 | |
---|
| 107 | The type of the function tells us the underlying type, ie. `(a->b) -> [a] -> [b]`. |
---|
| 108 | |
---|
| 109 | The body of map corresponds to the following expression |
---|
| 110 | |
---|
| 111 | ~~~~~~~~~~~~~~~~~~~~~~~ |
---|
| 112 | match l of |
---|
| 113 | nil -> nil |
---|
| 114 | cons x xs -> cons (f x) (map f xs) |
---|
| 115 | ~~~~~~~~~~~~~~~~~~~~~~~ |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | In the embedded language `bind`, `match` and `app` are supercombinators. |
---|
| 119 | |
---|
| 120 | `bind sexp exp` |
---|
| 121 | |
---|
| 122 | : combines a size and an expression and creates a top-level binding |
---|
| 123 | |
---|
| 124 | `match list nilexp consexp` |
---|
| 125 | |
---|
| 126 | : embedding of the match operator of our language to haskell |
---|
| 127 | |
---|
| 128 | `app exp1 exp2` |
---|
| 129 | |
---|
| 130 | : embedding of the function application of our language to haskell |
---|
| 131 | |
---|
| 132 | There is also a function called `true` to denote we do not want to prove that |
---|
| 133 | branch. |
---|
| 134 | |
---|
| 135 | |
---|