Tải bản đầy đủ (.pdf) (77 trang)

AN INTRODUCTION TO OCAML

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (4.51 MB, 77 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

Introduction to Ocaml Speaker: Andrew Appel

COS 326

Princeton University

<small>slides copyright 2020 David Walker and Andrew Appelpermission granted to reuse these slides for non-commercial educational purposes</small>

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

Why OCaml?

<sup>2</sup>

Small, orthogonal core based on the <i>lambda calculus</i>.

• can be defined as library functions.

Supports <i>first-class, lexically scoped, higher-order</i>procedures

–first-class: functions are data values like any other data value

• like numbers, they can be stored, defined anonymously, ...

–lexically scoped: meaning of variables determined statically.

–higher-order: functions as arguments and results

• programs passed to programs; generated from programs

These features also found in Scheme, Haskell, Scala, F#, Clojure, ....

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

Why OCaml?

<sup>3</sup>

Statically typed: debugging and testing aid

• A type is worth a thousand tests

<i>typed </i>– type errors are discovered while the code is running.

Strongly typed: compiler enforces type abstraction.

• so we can utilize <i>types as capabilities</i>; crucial for local reasoning

Type inference: compiler fills in types for you

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

Installing, Running OCaml

<sup>4</sup>

•OCaml comes with compilers:

–"ocamlc" – fast bytecode compiler

•And an interactive, top-level shell:

–"ocaml" at the prompt.

–<i>but use the compiler most of the time</i>

•And many other tools

•See the course web pages for installation pointers

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

Editing OCaml Programs

<sup>5</sup>

•Many options: pick your own poison

• what your professors use

• good but not great support for OCaml.• we like it because we’re used to it

• (extensions written in elisp – a functional language!)

• integrated development environment written in OCaml.• haven’t used it, so can’t comment.

• we’ve put up a link to an OCaml plugin

• we haven't tried it but others recommend it

• A lot of students seem to gravitate to this

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

XKCD on Editors

<small>6</small>

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

<b>AN INTRODUCTORY EXAMPLE(OR TWO)</b>

<small>7</small>

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

OCaml Compiler and Interpreter

<small>8</small>

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

OCaml Compiler and Interpreter

<small>9</small>

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

OCaml demo

<sup>10</sup>

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

OCaml demo

<sup>11</sup>

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

OCaml demo

<sup>12</sup>

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

OCaml demo

<sup>13</sup>

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

OCaml demo

<sup>14</sup>

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

OCaml demo

<sup>15</sup>

</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">

OCaml demo

<sup>16</sup>

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

OCaml demo

<sup>17</sup>

</div><span class="text_page_counter">Trang 18</span><div class="page_container" data-page="18">

OCaml demo

<sup>18</sup>

</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">

OCaml demo

<sup>19</sup>

</div><span class="text_page_counter">Trang 20</span><div class="page_container" data-page="20">

OCaml demo

<sup>20</sup>

</div><span class="text_page_counter">Trang 21</span><div class="page_container" data-page="21">

OCaml demo

<sup>21</sup>

</div><span class="text_page_counter">Trang 22</span><div class="page_container" data-page="22">

OCaml demo

<sup>22</sup>

</div><span class="text_page_counter">Trang 23</span><div class="page_container" data-page="23">

OCaml demo

<sup>23</sup>

</div><span class="text_page_counter">Trang 24</span><div class="page_container" data-page="24">

OCaml demo

<sup>24</sup>

</div><span class="text_page_counter">Trang 25</span><div class="page_container" data-page="25">

A First OCaml Program

<small>print_string "Hello COS 326!!\n";;</small>

<small>25</small>

</div><span class="text_page_counter">Trang 26</span><div class="page_container" data-page="26">

<small>print_string "Hello COS 326!!\n"</small>

A First OCaml Program

just a singleexpression(but that isuncommon)

</div><span class="text_page_counter">Trang 27</span><div class="page_container" data-page="27">

A First OCaml Program

<small>print_string "Hello COS 326!!\n"</small>

<small>$ ocamlbuild hello.d.byte$ ./hello.d.byte</small>

<small>Hello COS 326!!$</small>

</div><span class="text_page_counter">Trang 28</span><div class="page_container" data-page="28">

A First OCaml Program

</div><span class="text_page_counter">Trang 29</span><div class="page_container" data-page="29">

A First OCaml Program

<small>$ ocaml</small>

<small>Objective Caml Version 3.12.0# 3 + 1;;</small>

<small>- : int = 4#</small>

<small>interpreting and playing with hello.ml:print_string "Hello COS 326!!\n"</small>

<small>29</small>

</div><span class="text_page_counter">Trang 30</span><div class="page_container" data-page="30">

A First OCaml Program

<small>- : unit = ()#</small>

<small>interpreting and playing with hello.ml:print_string "Hello COS 326!!\n"</small>

<small>30</small>

</div><span class="text_page_counter">Trang 31</span><div class="page_container" data-page="31">

A First OCaml Program

<small>- : unit = ()# #quit;;</small>

</div><span class="text_page_counter">Trang 32</span><div class="page_container" data-page="32">

<small>(* sum the numbers from 0 to n </small>

<small>precondition: n must be a natural number*)</small>

<small>let rec sumTo (n:int) : int =match n with</small>

<small>0 -> 0</small>

<small>| n -> n + sumTo (n-1)let _ =</small>

<small>print_int (sumTo 8);print_newline()</small>

A Second OCaml Program

a comment(* ... *)

<small>32</small>

</div><span class="text_page_counter">Trang 33</span><div class="page_container" data-page="33">

<small>(* sum the numbers from 0 to n </small>

<small>precondition: n must be a natural number*)</small>

<small>let rec sumTo (n:int) : int =match n with</small>

<small>0 -> 0</small>

<small>| n -> n + sumTo (n-1)let _ =</small>

<small>print_int (sumTo 8);print_newline()</small>

A Second OCaml Program

the name of the function being defined

the keyword "let" begins a definition; keyword "rec" indicates recursion<small>sumTo8.ml:</small>

<small>33</small>

</div><span class="text_page_counter">Trang 34</span><div class="page_container" data-page="34">

<small>(* sum the numbers from 0 to n </small>

<small>precondition: n must be a natural number*)</small>

<small>let rec sumTo (n:int) : int =match n with</small>

<small>0 -> 0</small>

<small>| n -> n + sumTo (n-1)let _ =</small>

<small>print_int (sumTo 8);print_newline()</small>

A Second OCaml Program

result type intargument named nwith type int<small>sumTo8.ml:</small>

<small>34</small>

</div><span class="text_page_counter">Trang 35</span><div class="page_container" data-page="35">

<small>(* sum the numbers from 0 to n </small>

<small>precondition: n must be a natural number*)</small>

<small>let rec sumTo (n:int) : int =match n with</small>

<small>0 -> 0</small>

<small>| n’ -> n’ + sumTo (n-1)let _ =</small>

<small>print_int (sumTo 8);print_newline()</small>

A Second OCaml Program

deconstruct the value nusing pattern matching

data to be

betweenkey words"match" and"with"

<small>35</small>

</div><span class="text_page_counter">Trang 36</span><div class="page_container" data-page="36">

<small>(* sum the numbers from 0 to n </small>

<small>precondition: n must be a natural number*)</small>

<small>let rec sumTo (n:int) : int =match n with</small>

<small>0 -> 0</small>

<small>| n -> n + sumTo (n-1)let _ =</small>

<small>print_int (sumTo 8);print_newline()</small>

A Second OCaml Program

deconstructed data matches one of 2 cases:

(i) the data matches the pattern 0, or (ii) the data matches the variable pattern nvertical bar "|" separates the alternative patterns

<small>36</small>

</div><span class="text_page_counter">Trang 37</span><div class="page_container" data-page="37">

<small>(* sum the numbers from 0 to n </small>

<small>precondition: n must be a natural number*)</small>

<small>let rec sumTo (n:int) : int =match n with</small>

<small>0 -> 0</small>

<small>| n -> n + sumTo (n-1)let _ =</small>

<small>print_int (sumTo 8);print_newline()</small>

A Second OCaml Program

Each branch of the match statement constructs a result

constructthe result 0

construct a resultusing arecursivecall to sumTo<small>sumTo8.ml:</small>

<small>37</small>

</div><span class="text_page_counter">Trang 38</span><div class="page_container" data-page="38">

<small>(* sum the numbers from 0 to n </small>

<small>precondition: n must be a natural number*)</small>

<small>let rec sumTo (n:int) : int =match n with</small>

<small>0 -> 0</small>

<small>| n -> n + sumTo (n-1)let _ =</small>

<small>print_int (sumTo 8);print_newline()</small>

A Second OCaml Program

print theresult of calling

sumTo on 8

print anew line<small>sumTo8.ml:</small>

<small>38</small>

</div><span class="text_page_counter">Trang 39</span><div class="page_container" data-page="39">

<b>OCAML BASICS:</b>

<b>EXPRESSIONS, VALUES, SIMPLE TYPES</b>

<small>39</small>

</div><span class="text_page_counter">Trang 40</span><div class="page_container" data-page="40">

Terminology: Expressions, Values, TypesExpressionsare computations

<small>40</small>

</div><span class="text_page_counter">Trang 41</span><div class="page_container" data-page="41">

Some simple types, values, expressions

<sup>41</sup>

<small>float3.14, -1., 2e12(3.14 +. 12.0) *. 10e6 char’a’, ’b’, ’&’int_of_char ’a’</small>

<small>string"moo", "cow""moo" ^ "cow"</small>

<small>booltrue, falseif true then 3 else 4</small>

For more primitive types and functions over them, see the OCaml Reference Manual here:

class="text_page_counter">Trang 42</span><div class="page_container" data-page="42">

Evaluation

<sup>42</sup>

<small>42 * (13 + 1)</small>

</div><span class="text_page_counter">Trang 43</span><div class="page_container" data-page="43">

Evaluation

<sup>43</sup>

<small>42 * (13 + 1)</small> <b><small>-->*</small></b> <small>588</small>

Read like this: "the expression 42 * (13 + 1) evaluates to the value 588"The "*" is there to say that it does so in 0 or more small steps

</div><span class="text_page_counter">Trang 45</span><div class="page_container" data-page="45">

Evaluation

<sup>45</sup>

<small>42 * (13 + 1)-->* 588</small>

<small>(3.14 +. 12.0) *. 10e6 -->* 151400000.int_of_char ’a’-->*97</small>

<small>"moo" ^ "cow"-->* "moocow"if true then 3 else 4-->* 3</small>

</div><span class="text_page_counter">Trang 46</span><div class="page_container" data-page="46">

Evaluation

<sup>46</sup>

<small>1 + "hello"-->*???</small>

</div><span class="text_page_counter">Trang 47</span><div class="page_container" data-page="47">

Evaluation

<sup>47</sup>

<small>1 + "hello"-->*???</small>

"+" processes integers"hello" is not an integerevaluation is undefined!

Don’t worry! This expression doesn’t type check.

Aside: See this talk on Javascript:

class="text_page_counter">Trang 48</span><div class="page_container" data-page="48">

<b>OCAML BASICS:</b>

<b>CORE EXPRESSION SYNTAX</b>

<small>48</small>

</div><span class="text_page_counter">Trang 49</span><div class="page_container" data-page="49">

Core Expression Syntax

<sup>49</sup>

The simplest OCaml expressions eare:

•values<i>numbers, strings, bools, ...</i>

•id e

<sub>1</sub>

e

<sub>2</sub>

… e

<sub>n</sub>

<i>function call (foo 3 42)</i>

•<b>let id = e</b>

<sub>1</sub>

<b>in e</b>

<sub>2</sub>

<i>local variable decl.</i>

•<b>if e</b>

<sub>1</sub>

<b>then e</b>

<sub>2</sub>

<b>else e</b>

<sub>3</sub>

<i>a conditional</i>

•(e : t)<i>an expression with its type</i>

</div><span class="text_page_counter">Trang 50</span><div class="page_container" data-page="50">

The first one passes three arguments to f (x, y, and z)

The second passes two arguments to f (x, and the result of applying the function y to z.)

</div><span class="text_page_counter">Trang 51</span><div class="page_container" data-page="51">

<b>OCAML BASICS:TYPE CHECKING</b>

<small>51</small>

</div><span class="text_page_counter">Trang 52</span><div class="page_container" data-page="52">

Type Checking

Every value has a type and so does every expression

This is a concept that is familiar from Java but it becomes more important when programming in a functional language

We write (e : t) to say that <i>expression e has type t</i>. eg:

<small>52</small>

</div><span class="text_page_counter">Trang 53</span><div class="page_container" data-page="53">

Type Checking Rules

There are a set of simple rules that govern type checking

O’Caml will refuse to compile them for you (the nerve!)

But types are a great thing:

<small>53</small>

</div><span class="text_page_counter">Trang 54</span><div class="page_container" data-page="54">

Type Checking Rules

Example rules:

0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

<small>54</small>

</div><span class="text_page_counter">Trang 55</span><div class="page_container" data-page="55">

Type Checking Rules

Example rules:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

<small>55</small>

</div><span class="text_page_counter">Trang 56</span><div class="page_container" data-page="56">

Type Checking Rules

Example rules:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

if e1 : string and e2 : string

then e1 ^ e2 : string <sup>if </sup><sub>then </sub><sup>e : int</sup><sub>string_of_int e : string</sub>0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

<small>56</small>

</div><span class="text_page_counter">Trang 57</span><div class="page_container" data-page="57">

Type Checking Rules

Example rules:

Using the rules:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

if e1 : string and e2 : string

then e1 ^ e2 : string <sup>if </sup><sub>then </sub><sup>e : int</sup><sub>string_of_int e : string</sub>

2 : int and 3 : int. (By rule 1)

0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

<small>57</small>

</div><span class="text_page_counter">Trang 58</span><div class="page_container" data-page="58">

Type Checking Rules

Example rules:

Using the rules:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

if e1 : string and e2 : string

then e1 ^ e2 : string <sup>if </sup><sub>then </sub><sup>e : int</sup><sub>string_of_int e : string</sub>

2 : int and 3 : int. (By rule 1)Therefore, (2 + 3) : int (By rule 3)

0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

<small>58</small>

</div><span class="text_page_counter">Trang 59</span><div class="page_container" data-page="59">

Type Checking Rules

Example rules:

Using the rules:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

if e1 : string and e2 : string

then e1 ^ e2 : string <sup>if </sup><sub>then </sub><sup>e : int</sup><sub>string_of_int e : string</sub>

2 : int and 3 : int. (By rule 1)Therefore, (2 + 3) : int (By rule 3)

5 : int (By rule 1)

0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

<small>59</small>

</div><span class="text_page_counter">Trang 60</span><div class="page_container" data-page="60">

Type Checking Rules

Example rules:

Using the rules:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

if e1 : string and e2 : string

then e1 ^ e2 : string <sup>if </sup><sub>then </sub><sup>e : int</sup><sub>string_of_int e : string</sub>

2 : int and 3 : int. (By rule 1)Therefore, (2 + 3) : int (By rule 3)

5 : int (By rule 1)

Therefore, (2 + 3) * 5 : int (By rule 4 and our previous work)

0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

<i><b>FYI: This is a formal proof</b></i>

that the expression is typed!

<small>well-60</small>

</div><span class="text_page_counter">Trang 61</span><div class="page_container" data-page="61">

Type Checking Rules

Example rules:

Another perspective:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

if e1 : string and e2 : string

then e1 ^ e2 : string <sup>if </sup><sub>then </sub><sup>e : int</sup><sub>string_of_int e : string</sub>

???? * ???? : int0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

rule (4) for typing expressionssays I can put any expression with type int in place of the ????

<small>61</small>

</div><span class="text_page_counter">Trang 62</span><div class="page_container" data-page="62">

Type Checking Rules

Example rules:

Another perspective:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

if e1 : string and e2 : string

then e1 ^ e2 : string <sup>if </sup><sub>then </sub><sup>e : int</sup><sub>string_of_int e : string</sub>

7 * ???? : int0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

rule (4) for typing expressionssays I can put any expression with type int in place of the ????

<small>62</small>

</div><span class="text_page_counter">Trang 63</span><div class="page_container" data-page="63">

Type Checking Rules

Example rules:

Another perspective:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

if e1 : string and e2 : string

then e1 ^ e2 : string <sup>if </sup><sub>then </sub><sup>e : int</sup><sub>string_of_int e : string</sub>

7 * (add_one 17) : int0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

rule (4) for typing expressionssays I can put any expression with type int in place of the ????

<small>63</small>

</div><span class="text_page_counter">Trang 64</span><div class="page_container" data-page="64">

Type Checking Rules

You can always start up the OCaml interpreter to find out a type of a simple expression:

<small>$ ocaml</small>

<small>Objective Caml Version 3.12.0#</small>

<small>64</small>

</div><span class="text_page_counter">Trang 65</span><div class="page_container" data-page="65">

Type Checking Rules

You can always start up the OCaml interpreter to find out a type of a simple expression:

<small>(";;" can also end a top-level phrase in a file, but I’m going to avoid using it there because then some of you will confuse it with a ";" ….)</small>

</div><span class="text_page_counter">Trang 66</span><div class="page_container" data-page="66">

Type Checking Rules

You can always start up the OCaml interpreter to find out a type of a simple expression:

<small>$ ocaml</small>

<small>Objective Caml Version 3.12.0# 3 + 1;;</small>

<small>- : int = 4# </small>

pressreturnand you find outthe typeand thevalue

<small>66</small>

</div><span class="text_page_counter">Trang 67</span><div class="page_container" data-page="67">

Type Checking Rules

You can always start up the OCaml interpreter to find out a type of a simple expression:

<small>67</small>

</div><span class="text_page_counter">Trang 68</span><div class="page_container" data-page="68">

Type Checking Rules

You can always start up the OCaml interpreter to find out a type of a simple expression:

</div><span class="text_page_counter">Trang 69</span><div class="page_container" data-page="69">

Type Checking Rules

Example rules:

Violatingthe rules:

if e1 : int and e2 : int

then e1 + e2 : int <sup>if </sup>then <sup>e1 : int</sup>e1 * e2 : int<sup>and </sup><sup>e2 : int</sup>

if e1 : string and e2 : string

then e1 ^ e2 : string <sup>if </sup><sub>then </sub><sup>e : int</sup><sub>string_of_int e : string</sub>

"hello" : string (By rule 2)

1 : int (By rule 1)

1 + "hello" : ?? (NO TYPE! Rule 3 does not apply!)

0 : int (and similarly for any other integer constant n)

"abc" : string (and similarly for any other string constant "…")(2)

<small>69</small>

</div><span class="text_page_counter">Trang 70</span><div class="page_container" data-page="70">

•Violating the rules:

•The type error message tells you the type that was expected and the type that it inferred for your subexpression

•By the way, this was one of the nonsensical expressions that did not evaluate to a value

•<i><b>It is a good thing that this expression does not type check!</b></i>

<i>"Well typed programs do not go wrong"Robin Milner, 1978</i>

Type Checking Rules

Violating the rules:

The type error message tells you the type that was expectedand the type that it inferredfor your subexpression

By the way, this was one of the nonsensical expressions that did not evaluate to a value

It is a <i><b>good thing</b></i>that this expression does not type check!

<small># "hello" + 1;;</small>

<small>Error: This expression has type string but an expression was expected of type int</small>

<small>70</small>

</div><span class="text_page_counter">Trang 71</span><div class="page_container" data-page="71">

Type Checking Rules

Violating the rules:

<small>71</small>

</div><span class="text_page_counter">Trang 72</span><div class="page_container" data-page="72">

Type Checking Rules

What about this expression:

Why doesn't the ML type checker do us the favor of telling us the expression will raise an exception?

<small># 3 / 0 ;;</small>

<small>Exception: Division_by_zero.</small>

<small>72</small>

</div><span class="text_page_counter">Trang 73</span><div class="page_container" data-page="73">

Type Checking Rules

What about this expression:

Why doesn't the ML type checker do us the favor of telling us the expression will raise an exception?

the divisor evaluates to 0.

solving the halting problem:

There are type systems that will rule out divide-by-zero errors, but they require programmers supply proofs to the type checker

<small># 3 / 0 ;;</small>

<small>Exception: Division_by_zero.</small>

<small># 3 / (run_turing_machine(); 0);;</small>

<small>73</small>

</div><span class="text_page_counter">Trang 74</span><div class="page_container" data-page="74">

Isn’t that cheating?

<i>"Well typed programs do not go wrong"Robin Milner, 1978</i>

(3 / 0) is well typed. Does it "go wrong?" Answer: No."Go wrong" is a technical term meaning, "have no defined semantics." Raising an exception is perfectly well defined

semantics, which we can reason about, which we can handle in ML with an exception handler.

So, it’s not cheating.

<i>(Discussion: why do we make this distinction, anyway?)</i>

<small>74</small>

</div><span class="text_page_counter">Trang 75</span><div class="page_container" data-page="75">

Type Soundness

<i>"Well typed programs do not go wrong"</i>

Programming languages with this property have

<i>sound</i>type systems. They are called <i>safe</i>languages.Safe languages are generally <i>immune</i>to buffer overrun

vulnerabilities, uninitialized pointer vulnerabilities, etc., etc.(but not immune to all bugs!)

Safe languages: ML, Java, Python, …Unsafe languages: C, C++, Pascal

<small>75</small>

</div><span class="text_page_counter">Trang 76</span><div class="page_container" data-page="76">

<b>OVERALL SUMMARY:</b>

<b>A SHORT INTRODUCTION TOFUNCTIONAL PROGRAMMING</b>

<small>76</small>

</div><span class="text_page_counter">Trang 77</span><div class="page_container" data-page="77">

–express control flow and iteration by defining <i>functions</i>

–the <i>type</i>of an expression <i>correctly predicts</i>the kind of <i>value</i>

the expression will generate when it is executed–types help us <i>understand</i>and <i>write</i>our programs–the type system is <i>sound</i>; the language is <i>safe</i>

<small>77</small>

</div>

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×