1 / 29

Programming Language Concepts (CIS 635)

Programming Language Concepts (CIS 635). Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635. Type Checking. When is op(arg1,…,argn) allowed? Type checking assures that operations are applied to the right number of arguments of the right types

paley
Télécharger la présentation

Programming Language Concepts (CIS 635)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635

  2. Type Checking • When is op(arg1,…,argn) allowed? • Type checking assures that operations are applied to the right number of arguments of the right types • Right type may mean same type as was specified, or may mean that there is a predefined implicit coercion that will be applied • Used to resolve overloaded operations

  3. Type Checking • Type checking may be done statically at compile timeor dynamically at run time • Untyped languages (eg LISP, Prolog) do only dynamic type checking • Typed languages can do most type checking statically

  4. Dynamic Type Checking • Performed at run-time before each operation is applied • Types of variables and operations left unspecified until run-time • Same variable may be used at different types

  5. Dynamic Type Checking • Data object must contain type information • Errors aren’t detected until violating application is executed (maybe years after the code was written)

  6. Static Type Checking • Performed after parsing, before code generation • Type of every variable and signature of every operator must be known at compile time

  7. Static Type Checking • Can eliminate need to store type information in data object if no dynamic type checking is needed • Catches many programming errors at earliest point

  8. Strongly Typed Language • When no application of an operator to arguments can lead to a run-time type error, language is strongly typed • Depends on definition of “type”

  9. Strongly Typed Language • C is “strongly typed” but type coercions may cause unexpected (undesirable) effects; no array bounds check (in fact, no runtime checks at all) • SML “strongly typed” but still must do dynamic array bounds checks, arithmetic overflow checks

  10. How to Handle Type Mismatches • Type checking to refuse them • Apply implicit function to change type of data • Coerce int into real • Coerce char into int

  11. Conversion Between Types: • Explicit: all conversions between different types must be specified • Implicit: some conversions between different types implied by language definition • Implicit conversions called coercions

  12. Coercion Examples Example in Pascal: var A: real; B: integer; A := B • Implicit coercion - an automatic conversion from one type to another

  13. Coercions Versus Conversions • When A has type int and B has type real, many languages allow coercion implicit in A := B • In the other direction, often no coercion allowed; must use explicit conversion: • B := round(A); Go to integer nearest A • B := trunc(A); Delete fractional part of A

  14. Subtypes • A is a subtype of B if every value of A is a value of B (or more accurately it appears to be) • Frequently candidate for coercion • In C many things are subtypes of integer – many things can be implicitly coerced into a integers

  15. Type Equality (aka Type Compatibility) • When are two types “the same”? • Name equivalence: two types equal only if they have the same name • Simple but restrictive • Usually loosened to allow two types to be equal when one is defined with the name of the other (declaration equivalence)

  16. Type Equality • Structure equivalence: Two types are equivalent if the underlying data structures for each type are the same • Problem: how far to go – are two records with the same number of fields of same type, but different labels equivalent?

  17. Scope of Variable • Range of program that can reference that variable (ie access the corresponding data object by the variable’s name) • Variable is local to program or block if it is declared there • Variable is nonlocal to program unit if it is visible there but not declared there

  18. Static Scoping • Scope computed at compile time, based on program text • To determine the variable of a name used must find statement declaring variable

  19. Static Scoping • Subprograms and blocks generate hierarchy of scopes • Subprogram or block that declares current subprogram or contains current block is its static parent

  20. Static Scoping • General procedure to find declaration: • First see if variable is local; if yes, done • If nonlocal to current subprogram or block recursively search static parent until declaration is found • If no declaration is found this way, undeclared variable error detected

  21. program main; var x : integer; procedure sub1; var x : integer; begin { sub1 } … x … end; { sub1 } begin { main } … x … end; { main } Example

  22. Dynamic Scope • Now generally thought to have been a mistake • Main example of use: original versions of LISP • Common LISP uses static scope • Perl allows variables to be decalred to have dynamic scope

  23. Dynamic Scope • Determined by the calling sequence of program units, not static layout • Name bound to corresponding variable most recently declared among still active subprograms and blocks

  24. program main; var x : integer; procedure sub1; begin { sub1 } … x … end; { sub1 } procedure sub2; var x : integer; begin { sub2 } … call sub1 … end; { sub2 } … call sub2… end; { main } Example

  25. program main; var x : integer; procedure sub1; begin { sub1 } … x … end; { sub1 } procedure sub2; var x : integer; begin { sub2 } … call sub1 … end; { sub2 } … call sub2… end; { main } Example: Static Scope

  26. program main; var x : integer; procedure sub1; begin { sub1 } … x … end; { sub1 } procedure sub2; var x : integer; begin { sub2 } … call sub1 … end; { sub2 } … call sub2… end; { main } Example: Dynamic Scope

  27. Referencing Environment • The referencing environment of a program point is the set of all variables (names bound to data objects) visible to that program point • In a static scoped language referencing environment is all local variables (declared so far) together with all declared variables of all static ancestors minus those that have been hidden

  28. program main; var x, y : integer; procedure sub1; var z : integer begin { sub1 } … point1 … end; { sub1 } procedure sub2; var w, x : integer; begin { sub2 } … point2 … end; { sub2 } …point3 … end; { main } Example (Static Scope)

  29. Example • Referencing Envirnoments: • Point1: main.x main.y sub1.z • Point2: main.y sub2.x sub2.w • Point3: main.x main.y

More Related