| 1 | Prerequisites |
|---|
| 2 | ------------- |
|---|
| 3 | |
|---|
| 4 | You need at least version 2011.4.0.0 of the Haskell platform, an installed |
|---|
| 5 | [Z3](http://research.microsoft.com/en-us/um/redmond/projects/z3/) |
|---|
| 6 | and the packages `sbv` and `value-supply`. If you have a working Haskell platform installed you can |
|---|
| 7 | install value-supply by typing |
|---|
| 8 | |
|---|
| 9 | cabal install value-supply sbv |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | Source files |
|---|
| 13 | ------------ |
|---|
| 14 | |
|---|
| 15 | Examples.hs |
|---|
| 16 | ~ Some examples |
|---|
| 17 | SizedExp.hs |
|---|
| 18 | ~ Embedded language to prove size expressions |
|---|
| 19 | Lambda.hs |
|---|
| 20 | ~ Size expressions |
|---|
| 21 | Constraints.hs |
|---|
| 22 | ~ Constraint solver |
|---|
| 23 | |
|---|
| 24 | Using the examples |
|---|
| 25 | ------------------ |
|---|
| 26 | |
|---|
| 27 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 28 | $ ghci Examples.hs |
|---|
| 29 | GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help |
|---|
| 30 | Loading package ghc-prim ... linking ... done. |
|---|
| 31 | Loading package integer-gmp ... linking ... done. |
|---|
| 32 | Loading package base ... linking ... done. |
|---|
| 33 | Loading package ffi-1.0 ... linking ... done. |
|---|
| 34 | [1 of 4] Compiling Lambda ( Lambda.hs, interpreted ) |
|---|
| 35 | [2 of 4] Compiling Constraints ( Constraints.hs, interpreted ) |
|---|
| 36 | [3 of 4] Compiling SizedExp ( SizedExp.hs, interpreted ) |
|---|
| 37 | [4 of 4] Compiling Main ( Examples.hs, interpreted ) |
|---|
| 38 | Ok, modules loaded: Lambda, SizedExp, Constraints, Main. |
|---|
| 39 | *Main> |
|---|
| 40 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 41 | |
|---|
| 42 | Type "`runTests`" to run all test cases or type "`prove map`" to prove the |
|---|
| 43 | function named map. |
|---|
| 44 | |
|---|
| 45 | Under Windows |
|---|
| 46 | ------------- |
|---|
| 47 | It is not perfect, and you will see a box after every special character, but at |
|---|
| 48 | least it works. |
|---|
| 49 | |
|---|
| 50 | - Open a console by running the program `cmd` |
|---|
| 51 | - Issue the command `chcp 65001` |
|---|
| 52 | - In the console cd to the trunk directory |
|---|
| 53 | - Now, you can run `ghci Examples.hs` |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | Introduction to Haskell syntax |
|---|
| 57 | ------------------------------ |
|---|
| 58 | |
|---|
| 59 | Haskell syntax is very much like Clean, however there are some expressions you |
|---|
| 60 | cannot find in Clean. |
|---|
| 61 | |
|---|
| 62 | ### Backtick ### |
|---|
| 63 | |
|---|
| 64 | Every haskell function can be used as infix operator. For example the |
|---|
| 65 | following two expressions are the same. |
|---|
| 66 | |
|---|
| 67 | ~~~~~~~~~~~ |
|---|
| 68 | func a b |
|---|
| 69 | a `func` b |
|---|
| 70 | ~~~~~~~~~~~ |
|---|
| 71 | |
|---|
| 72 | ### Dollar ### |
|---|
| 73 | |
|---|
| 74 | Dollar sign is just explicit application (but with different precedence), so it |
|---|
| 75 | can be used as a backward pipe operator. The following expressions are the |
|---|
| 76 | same. |
|---|
| 77 | |
|---|
| 78 | ~~~~~~~~~~~~~~ |
|---|
| 79 | func (a b) |
|---|
| 80 | func $ a b |
|---|
| 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 that we do not want to prove that |
|---|
| 133 | branch. |
|---|