|
1 | | -- Type Errors are Bad! |
| 1 | +- The stages of Programming |
2 | 2 |
|
| 3 | +1. Write a program |
| 4 | + > hello.c |
| 5 | + > printf("Hello!"); |
| 6 | +
|
| 7 | +2. Compile the program (to get an .exec) |
| 8 | + |
| 9 | + |
| 10 | + > gcc -o hello.exec hello.c |
| 11 | + > ; gives hello.exec |
| 12 | +
|
| 13 | +3. Run the program |
| 14 | + > ./hello.exec |
| 15 | + > Hello! |
3 | 16 | |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +- Programs have errors! |
| 21 | + |
| 22 | +Google chrome is an .exec |
| 23 | +That crashes |
| 24 | + |
| 25 | +https://www.google.com/search?biw=1435&bih=681&tbm=isch&sa=1&ei=ZeoRWoaHGIGTmQGDk4qwDA&q=crash+message&oq=crash+message&gs_l=psy-ab.3..0j0i5i30k1l4j0i24k1l2.83915.83915.0.84201.1.1.0.0.0.0.74.74.1.1.0....0...1.1.64.psy-ab..0.1.74....0.he5kszenZ-g#imgrc=f6hEeIqbiI84xM: |
| 26 | + |
| 27 | +- Types: Sanity checks that prevent errors before .exec is generated! |
| 28 | +1 + "Hello!" |
| 29 | +Error! |
| 30 | +1 + 2 |
| 31 | +OK! |
| 32 | + |
| 33 | +- Example: |
| 34 | + |
| 35 | + > printf(1 + "Hello!"); |
| 36 | + > 1. > 1Hello! |
| 37 | + > 2. > Hello! |
| 38 | + > 3. > ello! |
| 39 | + > 4. 1+Hello! |
| 40 | +
|
| 41 | +Unexpected, correct? |
| 42 | + |
| 43 | + |
| 44 | +- Types |
| 45 | +Types catch these unexpected behaviors early (at compile time)! |
| 46 | + |
| 47 | +- Let's design a type system! |
| 48 | +1. Use Types to Clasify Data (statements) |
| 49 | + |
| 50 | +1 + "Hello!" |
| 51 | + |
| 52 | +1 : Int |
| 53 | +"Hello!" : String |
| 54 | + |
| 55 | +2. Define How Data Are combined (rules) |
| 56 | +If e1 : Int and e2 : Int, then |
| 57 | +e1 + e2 : Int |
| 58 | + |
| 59 | +where e1 e2 are variables |
| 60 | + |
| 61 | + |
| 62 | +e1 :: Int |
| 63 | +e2 :: Int |
| 64 | +---------- |
| 65 | +e1 + e2 : Int |
| 66 | + |
| 67 | +e.g., e1 := 1, e2 := 2 |
| 68 | +e.g., e1 := 1+2, e2 := 2 |
| 69 | +e.g., e1 := 1, e2 := "Hello!" |
| 70 | + |
| 71 | + |
| 72 | +- Type System Design |
| 73 | +Add rules (+, ) |
| 74 | + |
| 75 | +What prevents me from adding bad rules? |
| 76 | + |
| 77 | +we like to name things |
| 78 | +Goal 1: Catch all possible errors! [Sound] |
| 79 | +(Just reject everything!) |
| 80 | + |
| 81 | +Goal 2: Accept all correct programs! [Complete] |
| 82 | +(Just accept everything!) |
| 83 | + |
| 84 | +- Soundness |
| 85 | +If e has a type, then it will not crash! |
| 86 | +If e has a type, then it return a value ! |
| 87 | + |
| 88 | +(1+1)+2 is an expression |
| 89 | +4 is an value |
| 90 | + |
| 91 | +(1+1)+2 `returns` 4 |
| 92 | +(1+1)+2 |
| 93 | + -> 2 + 2 |
| 94 | + -> 4 |
| 95 | +(1+1)+2 ->* 4 |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +If e has a type, then e ->* 4 ! |
| 100 | +If e:t, then e ->* 4 ! |
| 101 | + |
| 102 | + |
| 103 | +At compile type we only have e! |
| 104 | +And we want to check that e ->* v. |
4 | 105 |
|
5 | 106 | - Preservation |
| 107 | +If e1 -> e2 and e1:t, then e2:t. |
| 108 | + |
| 109 | +(1+1)+2 : Int, progress there exists |
| 110 | + -> 2 + 2: Int |
| 111 | + -> 4 |
| 112 | + |
| 113 | + |
6 | 114 |
|
7 | 115 | - Progress |
| 116 | +If e1:t, then either e1 is a value or e1 -> e2. |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +Example |
| 122 | +------- |
| 123 | + |
| 124 | +(1+1)+2 : Int, progress there exists |
| 125 | + -> 2 + 2: Int |
| 126 | + -> 4 |
| 127 | +(1+1)+2 ->* 4 |
| 128 | + |
| 129 | +Generally, |
| 130 | +e1:t -> e2:t -> ... -> en:t |
| 131 | + |
| 132 | +Soundness |
| 133 | +e : t & e ~> v, then v:t |
| 134 | + |
| 135 | + |
| 136 | +- The stages of Programming |
| 137 | + |
| 138 | +1. Write a program e |
| 139 | + > hello.c |
| 140 | + > printf("Hello!"); |
| 141 | +
|
| 142 | +2. Compile the program (to get an .exec) |
| 143 | + |
| 144 | + |
| 145 | + > gcc -o hello.exec hello.c |
| 146 | + > ; gives hello.exec |
8 | 147 |
|
| 148 | +3. Run the program |
| 149 | + > ./hello.exec |
| 150 | + > Hello! |
| 151 | + |
0 commit comments