lecture-7

Document Sample
lecture-7 Powered By Docstoc
					Quiz                                                  Last time
                                                      • We saw recursion

                                                      • And higher-order functions




                                                1                                                                2




Benefits of higher-order functions                    Funcs taking/returning funcs
Identify common computation “patterns”                Higher-order funcs enable modular code
                                                      • Each part only needs local information
• Iterate a function over a set, list, tree …
                                                                    Data                  Data
                                                                  Structure             Structure
• Accumulate some value over a collection                           Client               Library
                                                                  Uses list               list

Pull out (factor) “common” code:                            Uses meta-functions:     Provides meta-functions:
                                                             map,fold,filter         map,fold,filter
• Computation Patterns                               With locally-dependent funs     to traverse, accumulate over
• Re-use in many different situations                     (lt h), square etc.        lists, trees etc.
                                                    Without requiring Implement.     Meta-functions don’t need client
                                                3        details of data structure   info                        4




Different way of thinking                             Next... semantics!
                        “Free your mind”
                               -Morpheus

• Different way of thinking about
  computation
• Manipulate the manipulators


                                                5                                                                6




                                                                                                                        1
 Holy trinity …                                                   Language Semantics
                          Exec-time
                                                                  • In previous slide, we said:
                          “Dynamic”
                                                                    – ML checks if expression is “well-typed”
Expressions (Syntax)                    Values (Semantics)
                                                                    – ML evaluates expression to compute its value
           Compile-time                                           • But...
                                                                    – What does it mean to be “well-typed”?
             “Static”        Types
                                                                    – What does it mean to evaluate?
1. Programmer enters expression                                   • We need to define these notions
2. ML checks if expression is “well-typed”                        • The definition of these notions is called the
  •    Using a precise set of rules, ML tries to find a unique      language semantics
       type for the expression meaningful type for the expr         – i.e.: the meaning of the language
3. ML evaluates expression to compute value
  •    Of the same “type” found in step 2                    7                                                             8




 Two kinds of semantics                                           Language Semantics
 • Static semantics                                               • Defining the semantics of ML is not just
      – States how expressions typecheck                            an exercise in pedantics.
      – tells us how “ML checks if expression is well-            • ANY widely used language must have a
        typed”
                                                                    semantics definition (why?)
 • Dynamics semantics
      – states how expressions evaluate                           • But... How does one do this?
      – tells how “ML evaluates expression to                     • Two possibilities
        compute its value”

                                                             9                                                             10




 Option 1: informal semantics                                     Option 2: formal semantics
 • We can state using English prose what the                      • Express using precise math notation the
                                                                    semantics of the language
   semantics of the language is
                                                                  • Has been used for small core languages
 • Who here has heard of the ANSI C++                               – these languages are not used in practice, but are
   standard?                                                          theoretical tools to reason about larger languages
                                                                  • Or has been used for full-fletched languages
 • These documents are usually long,
                                                                    that are simple enough that a formal semantics
   tedious, and sometimes hard to interpret                         is feasible (eg: Scheme, ML, OCAML)
 • Which leads to programmers and                                 • C/C++ has too many corner cases to be
   compiler writers being unhappy                                   formalized...

                                                             11                                                            12




                                                                                                                                2
Tradeoffs: informal vs. formal                      Lesson of the day
• Formal harder to understand for the lay-          • Regardless of whether you are using an
  programmer                                          informal semantics or a formal one:
• Formal can provide stronger theoretical
  guarantees
  – in particular, once semantics is formalized,    Make your language simple
    once can prove things about it
  – for example, that the type system does what       – Why?
    it is supposed to do
• What is done in practice?
                                               13                                                     14




Simpler languages leads to...                       But also...
• Fewer cases to define in your semantics           • Don’t have to address interaction
  – Scheme has informal semantics, and it’s only      between language features
    a handful of pages long                         • Easier for humans to read
  – vs C++: 500+ pages                                – some languages (eg: Perl) give you many
• Shorter documentation makes it easier                 different ways of doing the same thing, but
  for humans to understand/cope with the                this makes code harder to read
  language                                            – “wow, what feature of Perl is this”
  – essentially, fewer things to learn...             – remember: readability more important than
                                                        writability
                                               15                                                     16




In this course: formal semantics                    Base Types
• We’ll get to see just a flavor of formal          • We will first look at base types, and
  semantics                                           consider only the “expression calculator”
• Why focus on formal semantics?                    • No variables!
  – Formal semantics is more rigorous than
    informal one
                                                    • Notice that we are going through exactly
  – If you understand the concepts behind formal
                                                      the same step-by-step progression we
    semantics, it’s pretty easy to understand the
    concepts behind informal ones.                    used the first time through this material!

                                               17                                                     18




                                                                                                           3
 Base Type: int static semantics                                   Base Type: int static semantics
                                                                    example expr        general form        typing rule
 • We will define the semantics (both static
                                                                                                          e1:int e2:int
                                                                                          e1 + e2
   and dynamic in fact) using one core                                2+3;
                                                                                                            e1+e2:int
   concept: inference rules                                                                               e1:int e2:int
                                                                      7-4;                e1 - e2
                                                                                                            e1-e2:int
          antecedent                         consequent
                                                                                                          e1:int e2:int
                                                                 (2+3)*(7-4);             e1 * e2
                                                                                                           e1 * e2:int
                       e1:int e2:int
                                                                       2;                    i                 i:int
                         e1+e2:int

    Read as: “antecedent implies consequent”                     Expressions built from sub-expressions
                                                            19   Types computed from types of sub-expressions                  20




 Proof in static semantics                                         Proof in static semantics
    Show that   (2+3)*(7-4);          has type     int                Show that     (2+3)*(7-4)     has type    int

                                                                                          i:int



                                                                             2:int   3:int    7:int 4:int         e1:int e2:int
                                                                               2+3 : int       7-4 : int            e1-e2:int
                                                                                 (2 + 3) * (7-4) : int


                                                                  e1:int e2:int                                e1:int e2:int
                                                                    e1+e2:int                                   e1 * e2:int
                                                            21                                                                 22




 Base Type: int dynamic semantics                                  Proof in dynamic semantics
  example expr          general form        evaluation rule
                                                 e1⇒v1 e2⇒v2          Show that     (2+3)*(7-4)        evaluates to       int
     2+3;                   e1 + e2
                                                  e1+e2⇒v1+v2
                                                                                          i ⇒i
                                                 e1⇒v1 e2⇒v2
     7-4;                   e1 - e2
                                                  e1-e2⇒v1-v2
                                                 e1⇒v1 e2⇒v2
 (2+3)*(7-4);               e1 * e2                                          2 ⇒2 3 ⇒3       7 ⇒7 4 ⇒4
                                                  e1*e2⇒v1*v2                                                    e1⇒v1 e2⇒v2
                                                                                (2+3) ⇒ 5    (7-4) ⇒ 3
                             i                     i ⇒i                            (2+3)*(7-4) ⇒ 15               e1-e2⇒v1-v2
     2;


Expressions built from sub-expressions                           e1⇒v1 e2⇒v2                                   e1⇒v1 e2⇒v2
Values computed from values of sub-expressions              23
                                                                  e1+e2⇒v1+v2                                   e1*e2⇒v1*v2
                                                                                                                          24




                                                                                                                                    4
  Base Type: float                                                            Base Type: string
   example expr          general form       typing rule       eval rule        example expr       general form   typing rule       eval rule
         2.0                   i              i:float           i ⇒i
                                                                                  “ab”           s                 s:string          s ⇒s
                                         e1:float e2:float e1⇒v1 e2⇒v2
    2.0 +. 3.0               e1 + e2
                                           e1 +.e2:float   e1+.e2⇒ v1+.v2
                                                                                                          e1:string e2:string e1⇒v1 e2⇒v2
                                         e1:float e2:float e1⇒v1 e2⇒ v2       “ab” ^ “cd”       e1^e2
     7.0 –. 4.0              e1 - e2                                                                         e1^e2:string      e1^e2⇒v1^v2
                                           e1-.e2:float    e1-.e2⇒ v1-.v2

                                         e1:float e2:float e1⇒v1 e2⇒ v2
(2.0+3.0) /. (7.0-4.0)       e1 / e2
                                           e1/.e2:float    e1/.e2⇒ v1/.v2

Expressions built from sub-expressions                                       Expressions built from sub-expressions
Types computed from types of sub-expressions                                 Types computed from types of sub-expressions
                                                                        25                                                                  26
Values computed from values of sub-expressions                               Values computed from values of sub-expressions




  Base Type: bool                                                             Base Type: bool

                                                                                                             e1:T e2:T           e1⇒v1 e2⇒v2
(“ab”=“cd”)          e1 = e2                                                 (“ab”=“cd”)        e1 = e2
                                                                                                             e1=e2 : bool        e1=e2 ⇒ v1=v2


                                                                             • (In)equality testing built-in
                                                                                – for all expressions, values, types
                                                                                – but compared expressions must have same type
                                                                             • … except for ?
                                                                                – function values … why ?
                                                                        27                                                                  28




  Base Type: bool                                                             Type Errors
example expr      general form         typing rule         eval rule                                         e1:string e1:string
                                                                                            “pq” ^ 9;
      true               b             b:bool               b ⇒b                                                e1^e2:string

                                     e1:T e2:T       e1⇒v1 e2⇒v2                                                 e1:int e1:int
 (“ab”=“cd”)       e1 = e2                                                                 (2 + “a”);
                                    e1=e2 : bool     e1=e2 ⇒ v1=v2                                               e1 + e2 : int


     2 < 3          e1 < e2
                                     e1:T e2:T          e1⇒v1 e2⇒v2          • Expressions built from sub-expressions
                                    e1<e2 : bool        e1<e2 ⇒ v1<v2
                                                                             • Types computed from types of sub-expression
                                      e : bool           e   ⇒ v             • If a sub-expression is not well-typed then whole
   not(2<3)         not e
                                    not e : bool        not e ⇒ ¬ v            expression is not well-typed

  not (2<3)                        e1:bool e2:bool e1⇒v1 e2⇒v2
                   e1 && e2
      &&                            e1&&e2:bool    e1&&e2⇒ v1 ∧ v2 29                                0 * (2 + “a”);                         30
 (“ab”=“cd”)




                                                                                                                                                 5
 Complex types: Tuples                                                   Complex types: Tuples

           (2+2 , 7>8);            int * bool                                     (2+2 , 7>8);             int * bool




                                                                                       e1:T1 e2:T2         e1⇒v1 e2⇒v2
                                                                                      (e1,e2) : T1 * T2   (e1,e2)⇒ (v1,v2)



                                                                  31                                                                           32




 Complex types: Tuples                                                   Complex types: Lists
                                                                         [1+1;2+2;3+3;4+4];                    [2;4;6;8]                 int list
 • Can be of any fixed size
             (9-3,“ab”^“cd”,7>8)    (int * string * bool)                [“a”;“b”; “c”^“d”];               [“a”;“b”; “cd”]              string list



                                                                       [(1,“a”^“b”);(3+4,“c”)];           [(1,“ab”);(7,“c”)]         (int*string) list


  e1:T1 e2:T2    …      en: Tn        e1⇒v1 e2⇒v2 … en ⇒ vn             [[1];[2;3];[4;5;6]];              [[1];[2;3];[4;5;6]];       (int list) list
 (e1,e2,…,en) : T1 * T2* …* Tn      (e1,e2,…,en)⇒ (v1,v2,…,vn)

 • Elements can have different types                                    • Unbounded size
 • Tuples can be nested in other tuples                                 • Can have lists of anything (e.g. lists of lists)
                                                                  33                                                                           34




 Complex types: Lists                                                    Complex types: Lists
     general form         typing rule                 eval rule             general form         typing rule                     eval rule

          []             []:’a list                   [] ⇒ []                    []             []:’a list                       [] ⇒ []

                                                                                              e1:T e2: T e3: T …   e1⇒v1 e2⇒v2 e3⇒v3
[e1;e2;e3;…]                                                           [e1;e2;e3;…]
                                                                                            [e1;e2;e3;…] : T list [e1;e2;…] ⇒ [v1;v2;…]



                                                                              All elements have the same type

                                                                                                [1; “pq”];
                                                                  35                                                                           36




                                                                                                                                                         6
  Complex types: list ..construct                                         Complex types: list ..construct
  Recal Cons “operator”:                                                  Recal Cons “operator”:
                  1::[2;3]         [1;2;3]        int list                                         1::[2;3]                [1;2;3]         int list

example expr general form      typing rule             eval rule        example expr general form                typing rule                       eval rule
                                                                                                              e1:T e2: T list                e1⇒v1 e2 ⇒ v2
 1::[2;3]      e1::e2                                                    1::[2;3]              e1::e2         e1::e2 : T list                e1::e2 ⇒ v1::v2



                                                                                                1::[“b”; “cd”];
                                                                         Can only “cons” element to a list of same type
                                                                   37                                                                                            38




  Complex types: list …construct                                          Complex types: list … deconstruct
  Recall Append “operator”                                                Reading the elements of a list:
              [1;2]@[3;4]        [1;2;3;4]         int list               • Two “operators”: hd (head) and tl (tail)

                                                                                                     hd [1;2;3;4;5] 1                                        [2;3;4;5]
example expr general form      typing rule             eval rule              [1;2;3;4;5]                                             tl [1;2;3;4;5]
                                                                                                                         int                                     int list
[1;2]@[3;4]      e1@e2      e1:T list e2:T list      e1⇒v1 e2 ⇒ v2
                              e1@e2 : T list         e1@e2 ⇒ v1@v2       [“a”;“b”; “cd”]        hd [“a”;“b”;“cd”] “a”             tl [“a”;“b”;“cd”]       [“b”; “cd”]
                                                                                                                      string                                   string list
                    1@[“b”; “cd”];
                                                                        [(1;“a”);(7;“c”)]      hd [(1,“a”);(7,“c”)] 1             tl [(1,“a”);(7,“c”)]        [(7, “c”]
                   [1]@[“b”; “cd”];                                                                            int                                     (int * string) list


                                                                        [[];[1;2;3];[4;5]]      hd [[];[1;2;3];4;5] 1              tl [[];[1;2;3];4;5] [2;3;4;5]
   Can only append lists of the same type                          39
                                                                                                                     int list
                                                                                                                                                                 40
                                                                                                                                                              int list list




  List: Heads and Tails                                                    List: Heads and Tails
                                                                                                        e :T list               e ⇒v1::v2
                                                                           Head                         hd e : T                hd e ⇒ v1


                                                                                                       e :T list                e ⇒ v1::v2
                                                                           Tail                      tl e : T list              tl e ⇒ v2




                                                                            (hd [[];[1;2;3]]) = (hd [[];[“a”]])
                                                                                    int list                e1:T e2:T                string list
                                                                                                            e1=e2 : bool
                                                                   41                                                                                            42




                                                                                                                                                                              7
  If-then-else expressions                                                  If-then-else expressions
                            if (1 < 2) then 5 else 10           5                                    if (1 < 2) then 5 else 10              5
                                                              int                                                                         int

if (1 < 2) then [“ab”;“cd”] else [“x”]              [“ab”;“cd”]           if (1 < 2) then [“ab”;“cd”] else [“x”]               [“ab”;“cd”]
                                                       string list                                                                 string list

                                                                            If-then-else semantics

                                                                                                if e1 then e2 else e3

                                                                                        e1      : bool     e2: T        e3: T
                                                                                               if e1 then e2 else e3 : T

                                                                               e1 ⇒ true      e2 ⇒ v2                   e1 ⇒ false      e3 ⇒ v3
                                                                     43    if e1 then e2 else e3 ⇒ v2                                         44
                                                                                                                     if e1 then e2 else e3 ⇒ v3




  If-then-else expressions                                                  If-then-else expressions
                  e1    : bool     e2: T        e3: T                                     e1     : bool     e2: T        e3: T
                       if e1 then e2 else e3 : T                                                if e1 then e2 else e3 : T


• Then-subexp, Else-subexp must have same type!                           • Then-subexp, Else-subexp must have same type!
     – Equals type of resulting expression                                   – Equals type of resulting expression


             if (1 < 2) then [1;2] else 5
                                                                          if 1>2 then [1;2] else [] []            if 1<2 then [] else [“a”] []

                  if false then [1;2] else 5                                                           int list                                  string list


                                                                          (if 1>2 then [1;2] else [])=(if 1<2 then [] else [“a”])
                                                                     45                                                                              46




     Type of function application                                            Type of function application
 Application: fancy word for “call”

        (e1 e2)

 •      “apply” argument e2 to function value e1


 •      if e1       has type T1 → T2         and
           e2       has type        T1       then
  (e1 e2) has type                  T2
                                                                     47                                                                              48




                                                                                                                                                               8
Example                                                                Example
                                        e1:T1 e2:T2    …      en: Tn                                          e1:T1 e2:T2    …      en: Tn
   e1:T e2: T e3: T …                                                     e1:T e2: T e3: T …
                                       (e1,e2,…,en) : T1 * T2* …* Tn                                         (e1,e2,…,en) : T1 * T2* …* Tn
 [e1;e2;e3;…] : T list                                                  [e1;e2;e3;…] : T list

                                          e1:T1 → T2      e2: T1                                                e1:T1 → T2      e2: T1
e1    : bool     e2: T        e3: T            e1 e2 : T2              e1    : bool     e2: T        e3: T           e1 e2 : T2
     if e1 then e2 else e3 : T                                              if e1 then e2 else e3 : T
                                                    i:int                                                                i:int




     if gt(1,2) then [1;2] else [] : int list                               if gt(1,2) then [1;2] else [] : int list
                                                               49                                                                    50




What about fun expressions?                                            What about fun expressions?

               e : T2                                                                 e : T2
        (fun x -> e ) : T1 → T2                                                (fun x -> e ) : T1 → T2




                                                               51                                                                    52




Environment assumptions                                                Examples


            Old:             e:T



           New:          Γ     e:T

             Under typing assumptions in Γ, e has type T


                                                               53                                                                    54




                                                                                                                                             9
Examples                                               How do we change existing rules?
                                                                        e1:int e2:int
                                                             Old:         e1+e2:int


                                                                    Γ    e1:int Γ     e2:int
                                                            New:         Γ    e1+e2:int




                                                  55                                                     56




How do we change existing rules?                       Need one new rule too
                 e1:int e2:int
      Old:         e1+e2:int


             Γ    e1:int Γ     e2:int
     New:         Γ    e1+e2:int
                                                                        “x:T” ∈ Γ
                                                                        Γ   x : T




                                                  57                                                     58




Going back to functions                                Going back to functions
                              e : T2                                                 e : T2
      Old:             (fun x -> e ) : T1 → T2               Old:             (fun x -> e ) : T1 → T2




                           ??? e : T2                                           Γ, x:T1  e : T2
      new:         Γ    (fun x -> e ) : T1 → T2              new:         Γ    (fun x -> e ) : T1 → T2




                                                  59                                                     60




                                                                                                              10
Example                                                   Example
       Γ, x:T1  e : T2          “x:T” ∈ Γ                      Γ, x:T1  e : T2          “x:T” ∈ Γ
Γ     (fun x -> e ) : T1 → T2   Γ   x : T                 Γ   (fun x -> e ) : T1 → T2   Γ   x : T

 Γ     e1:int Γ     e2:int                                Γ    e1:int Γ     e2:int
                                Γ   i:int                                               Γ   i:int
       Γ    e1+e2:int                                          Γ    e1+e2:int




                                                     61                                             62




Next time
• Parametric types
     – aka: what’s up with those ’a ???


• Type inferences
     – aka: how in the world does Ocaml figure out
       all the types for me ???




                                                     63




                                                                                                         11

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:3/23/2011
language:English
pages:11