Understanding Type Generality and Synonyms in Programming

cse 341 pl section 2 n.w
1 / 13
Embed
Share

Explore the concept of type generality and synonyms in programming languages, including the importance of defining types accurately for code readability and modularity. Gain insights into how different types relate to each other and how to leverage these concepts in your coding practices.

  • Programming
  • Type Generality
  • Type Synonyms
  • Modularity
  • Code

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSE 341 PL Section 2 Justin Harjanto

  2. Todays Agenda Optional Type Synonyms Type Generality Equality Types More Syntactic Sugar

  3. A quick word on Optional Might be tad confusing and a little strange Type safety!!! Indicates to the clients of your function that your function *can* fail Will demonstrate with an example Useful concept and practice that other languages have adopted Optional

  4. Type Synonyms What does int * int * int represent? In HW1 we called it a date Wouldn t it be nice to reflect this representation in the source code itself? type date = int * int * int

  5. type vs datatype datatype introduces a new type name, distinct from all existing types datatype suit = Club | Diamond | Heart | Spade datatype rank = Jack | Queen | King | Ace | Num of int type is just another name type card = suit * rank

  6. Type Synonyms Why? For now, just for convenience It doesn t let us do anything new Later in the course we will see another use related to modularity.

  7. Type Generality Write a function that appends two string lists

  8. Type Generality We would expect string list * string list -> string list But the type checker found a list * a list -> a list Why is this OK?

  9. More General Types The type a list * a list -> a list is more general than the type string list * string list -> string list and can be used as any less general type, such as int list * int list -> int list But it is not more general than the type int list * string list -> int list

  10. The Type Generality Rule The more general rule A type t1 is more general than the type t2 if you can take t1, replace it s type variables consistently, and get t2

  11. Equality Types Write a list contains function

  12. Equality Types The double quoted variable arises from use of the = operator We can use = on most types like int, bool, string, tuples (that contain only equality types ) Functions and real are not equality types Generality rules work the same, except substitution must be some type which can be compared with = You can ignore warnings about calling polyEqual

  13. Syntactic Sugar If-then-else is implemented as syntactic sugar for a case statement. Function-pattern-case syntax

Related


More Related Content