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">
<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">• can be defined as library functions.
• like numbers, they can be stored, defined anonymously, ...
• programs passed to programs; generated from programs
• A type is worth a thousand tests
• so we can utilize <i>types as capabilities</i>; crucial for local reasoning
• 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"><small>6</small>
</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7"><small>7</small>
</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8"><small>8</small>
</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9"><small>9</small>
</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10"><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>
just a singleexpression(but that isuncommon)
</div><span class="text_page_counter">Trang 27</span><div class="page_container" data-page="27"><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"><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"><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"><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 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>
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>
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>
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>
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>
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>
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"><small>39</small>
</div><span class="text_page_counter">Trang 40</span><div class="page_container" data-page="40"><small>40</small>
</div><span class="text_page_counter">Trang 41</span><div class="page_container" data-page="41"><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>
<small>42 * (13 + 1)</small>
</div><span class="text_page_counter">Trang 43</span><div class="page_container" data-page="43"><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"><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"><small>1 + "hello"-->*???</small>
</div><span class="text_page_counter">Trang 47</span><div class="page_container" data-page="47"><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">
<small>48</small>
</div><span class="text_page_counter">Trang 49</span><div class="page_container" data-page="49"><small>51</small>
</div><span class="text_page_counter">Trang 52</span><div class="page_container" data-page="52"><small>52</small>
</div><span class="text_page_counter">Trang 53</span><div class="page_container" data-page="53"><small>53</small>
</div><span class="text_page_counter">Trang 54</span><div class="page_container" data-page="54">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">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">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">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">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">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">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)
<small>well-60</small>
</div><span class="text_page_counter">Trang 61</span><div class="page_container" data-page="61">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">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">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"><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"><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"><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"><small>67</small>
</div><span class="text_page_counter">Trang 68</span><div class="page_container" data-page="68">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"><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"><small>71</small>
</div><span class="text_page_counter">Trang 72</span><div class="page_container" data-page="72"><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"><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"><small>74</small>
</div><span class="text_page_counter">Trang 75</span><div class="page_container" data-page="75"><small>75</small>
</div><span class="text_page_counter">Trang 76</span><div class="page_container" data-page="76"><small>76</small>
</div><span class="text_page_counter">Trang 77</span><div class="page_container" data-page="77"><small>77</small>
</div>