1 / 25

COP4020 Programming Languages

COP4020 Programming Languages . Inductive Data Sets. Inductive sets of data. Recursively Specified data. A brief explanation on LISP. Rules of inference. Deduction tree. Defining sets using grammars. Inductive sets of data. COP4020 Programming Languages .

zanthe
Download Presentation

COP4020 Programming Languages

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. COP4020 Programming Languages Inductive Data Sets Inductive sets of data Recursively Specified data A brief explanation on LISP Rules of inference Deduction tree Defining sets using grammars

  2. Inductive sets of data COP4020 Programming Languages We will present the basic programming tools we will need to write: Interpreters and similar programs that form the heart of a programming language processor. Because the syntax of a programming language is usually a nested or tree-like structure, Recursion will be at the core of our technique

  3. Recursively Specified data COP4020 Programming Languages When writing code for a procedure we must know precisely: What kind of values may occur as arguments to the procedure What kinds of values are legal for the procedure to return We will present formal techniques for specifying set of values Inductive specifications is a powerful method of specifying a set of data values. We will illustrate this method by using it to describe a certain subset of the natural numbers N = {0, 1, 2, 3, 4,…..}. Definition: A natural number n is in S if and only if (iff): 1.- n = 0 or 2.- n – 3 A

  4. Recursively Specified data COP4020 Programming Languages Let us determine what natural numbers are in S. We know that 0 S 3 S, since (3 – 3) = 0 and 0 S 6 S, since (6 – 3) = 3 and 3 S Continuing this way, we can conclude that all multiples of 3 are in S Is 1 in S? We know that 1  0 so first condition is not satisfied. Furthermore, (1 – 3) = -2 which is not a natural number. 1 S 2  Ssame argument we found in former case. 4  S  4  S only if 1  S but 1  S, so 4  S We conclude that S is the set of natural numbers multiple of 3

  5. COP4020 Programming Languages We can use the definition to write a procedure to decide whether a natural number n is in S. in-S?: N  Bool Usage: (in-S? n) = #t if n is in S, #f otherwise. (define in-S? (lambda (n) if (zero? n) #t if (>= (- n 3) 0 ) (in-S? ( - n 3) ) #f ) ) ) ) in-S?: N  Bool: is a comment, called the contract for this procedure. It means that in-S? is intended to be a procedure that takes a natural number and produces a Boolean. This comments are useful for reading and writing programs.

  6. Recursively Specified data COP4020 Programming Languages Here there is an alternative way of writing down the definition of S. Definition: Define the set S to be the smallest set contained in N and satisfying the following properties: 1.- 0 S, and 2.- if nS, then n + 3 S A “smallest set” is one that satisfies properties 1 and 2 and that is a subset of any Other satisfying properties 1 and 2. It is easy to see that there can be only one such set: If S1 and S2 both satisfy properties 1 and 2, and both are smallest, then S1  S2 (since S1 is smallest), and S2  S1 (since S2 is smallest), hence S1 = S2.

  7. Recursively Specified data COP4020 Programming Languages Here is another way of writing the definition(shorthand notation): 0 S n S (n + 3)  S Each entry is call a “rule of inference”, or just a rule. The horizontal line is read as an “if-then”. The part above the line is called the “hypothesis” or the “antecedent”. The part below the line is called the “conclusion” or the “consequent” Note: When there are two or more hypotheses listed, they are connected by and implicit “and” (sometimes by a “;” is used to represent “and”). A rule with no hypotheses is called an “axiom”. We often write an axiom without the horizontal line: 0  S

  8. Recursively Specified data COP4020 Programming Languages The rule are interpreted as saying that a natural number n is in S if and only if (iff) the statement “nS” can be derived from the axioms by using the rules of inference finitely many times. The interpretation automatically makes S the smallest set that is closed under the rules. These definitions all say the same thing: 1.- First version is called top-down definition. 2.- second version is called bottom-up definition. 3.- third version is called rules-of-inference definition.

  9. A brief explanation on LISP COP4020 Programming Languages The names CAR and CDR are a historical curiosity; apparently they abbreviate: “Contents of Address Register”  CAR “Contents of Data Register”  CDR This reflects how the first LISP systems at MIT were implemented in an IBM 709. *M. Gordon, Programming Languages: Theory and implementation. We could say that Head and Tail are synonymous for CAR and CDR. CAR apply to a list returns the first element of its list parameter. CDR returns its parameter list minus its first element. Example: Given the list (A B C D) (CAR ‘(A B C D)) returns A and (CDR ‘(A B C D)) returns ( B C D)

  10. A brief explanation on LISP COP4020 Programming Languages Exercise in class: (CAR(CDR(CDR(CAR ‘((A B ( C ) D) E) ) ) ) ) = ? (CAR(CDR(CDR ‘((A B ( C ) D) ) ) ) = ? (CAR(CDR ‘( B ( C ) D) ) ) = ? (CAR ‘( ( C ) D) ) = ? ( C ) Definition: (List of integers, top-down): A “Scheme” list is a list of integers if and only if either: 1.- It is the empty list, or 2.- It is a pair whose CAR is an integer and whose CDR is a list of integers. We will use Int to denote the set of integers, and List-of-Intto denote the set of a list of integers.

  11. A brief explanation on LISP COP4020 Programming Languages Definition 4 (list of integers bottom-up): The set List-of-Integers is the smallest set of Scheme list satisfying the following two properties: 1.- ( )  List-of-Int, and 2.- IfnInt and L  List-of-Intthen (n . L )  List-of-Int Here we use the infix “.” to denote the result of the CONS operation in Scheme. CONS: The phrase (n . L ) denotes a Scheme pair whose CAR is n and whose CDR is L. Thebuilt-in function CONS returns a list constructed from its two arguments, which will then be the head and tail of the resultant list. Example: (CONS ‘A ‘B) returns (A B) (CONS ‘A ‘(B C)) returns (A B C) B A C

  12. A brief explanation on LISP COP4020 Programming Languages Example: (CONS ‘(A B) ‘(C D)) returns ( (A B) C D ) C D B A

  13. Rules of inference COP4020 Programming Languages Definition 5 (List of integers, rules of inference) ( )  List-of-Int nIntL  List-of-Int (n . L )  List-of-Int Examples: 1.- ( ) is a list of integers, because of property 1 of definition 4 or the first rule of definition 5. 2.- (14 . ( )) is a list of integers, because of the second rule of definition 5 14 Int( ) List-of-Int (14 . ( ))  List-of-Int

  14. Rules of inference COP4020 Programming Languages Example: ( 3 . ( 14 . ( ) ) ) ( 3 . ( 14 . ( ) ) ) is a list of integers since 3 is an integer and ( 14 . ( ) ) is a List-of-Int. We can write this as: 3Int (14 . ( ) )  List-of-Int ( 3 . ( 14 . ( ) ) )  List-of-Int Example: ( -7 . ( 3 . ( 14 . ( ) ) ) ) is a list of integers because -7 Int ( 3 . ( 14 . ( ) ) ) List-of-Int ( -7 . ( 3 . ( 14 . ( ) ) ) ) List-of-Int We can also create a tree-like picture or deduction tree and this is called derivation.

  15. Deduction tree COP4020 Programming Languages We will put together the three last examples to show the deduction tree. 14 Int( ) List-of-Int 3 Int (14 . ( ) )  List-of-Int -7 Int ( 3 . ( 14 . ( ) ) ) List-of-Int ( -7 . ( 3 . ( 14 . ( ) ) ) ) List-of-Int

  16. Defining sets using grammars COP4020 Programming Languages Grammar are typically used to specify sets of strings, but we can use them to define sets of values as well. For example: We can define a the set of “List-of Int” by the following two rule grammar: List-of-Int ::= ( ) List-of-Int ::= (Int . List-of-Int) First rule says that the empty list is in List-of-Int. The second rule says that if n is in Int and Lis in List-of Int, then( n . L) is in List-of-Int.

  17. Defining sets using grammars COP4020 Programming Languages • A grammar is composed of: • Nonterminal symbols • These are the names of the sets being defined and those sets are called syntactic • categories. • We will use capital letters to refer to nonterminals and sets. For example, Expression • is a nonterminal. when we write e Expression it means “e is an expression”. • We can use as well BNF and write expression enclosed in angle brackets to refer to • the syntactic category. For example, <expression>. • Terminal Symbols • These are the characters in he external representation, in this example: • List-of-Int ::= (Int . List-of-Int) • The terminal symbols are “.”, “(“, and “)”

  18. Defining sets using grammars COP4020 Programming Languages • A grammar is composed of: • Productions • - The rules are called productions. • - Each production has a left hand side, which is a nonterminal symbol and a • right hand side (RHS), which consists of terminals and nonterminal symbols. • - The symbol “::=“ can be read as “is defined as” or “is” or “can be”. • The RHS specifies a method for constructing members of the syntactic category • In terms of other syntactic categories and terminal symbols. • The grammar for List-of-Int could be written as: • List-of-Int ::= ( ) • |(Int . List-of-Int) • Another shortcut is the Kleene star, expressed by the notation {…}* which means • “zero or more instances” • List-of-Int ::= ( {Int}* )

  19. Defining sets using grammars COP4020 Programming Languages A variant of the star notation is Kleene plus {…}+, which indicates a sequence of one or more instances. Another variant of the star notation is the separated list notation: We write { Int}*(c) to denote a sequence of any number of instances of the nonterminal Int, separated by the non-empty character “c”. {Int}*(,) includes the strings 8 14, 12 7, 2, 15, 20 {Int}*(;) includes the strings 8 14; 12 7; 2; 15; 20

  20. On derivations COP4020 Programming Languages • If a set is specified by a grammar, a syntactic derivation may be used to show that a given data value is a member of the set. • Derivation starts with a nonterminal corresponding to the set. • At each step, indicated by an arrow ( =>), a nonterminal is replaced by the RHS of the corresponding rule. • or • with a known member of its syntactic class was left defined. • Example: (14 . ( ) ) may be formalized with the syntactic derivation: • List-of-Int List-of-Int • (Int . Lis-of-Int) => (Int . Lis-of-Int) • (14 . List-of-Int) => (Int. ( )) • (14 . ( ) ) => (14 . ( ) ) • The order in which nonterminals are replaced does not matter.

  21. On derivations COP4020 Programming Languages Exercise: Write a derivation from List-of-Int to (-7 . (3 . (14 . ( ) ) ) ) Many symbol manipulation procedures are assigned to operate on lists that contain only symbols and other similar restricted lists. We call these lists s-lists, defined as follows: Definition (s-list, s-exp) (S-list ::= ( { S-exp}* ) S-exp ::= symbol | S-list An s-list is a list of s-exp, and an s-exp is either a list or a symbol. Examples: (a b c) (an ((( s-list)) (with ( ) lots) ) (( of ) nesting ) ) )

  22. l-calculus COP4020 Programming Languages The lambda calculus ( l-calculus) is simple language that is often esed to study the theory of programming language This language consists only of: variable references procedures that take a single argument procedure calls We can defined with the grammar: l-exp ::= identifier | (l .identifier l-exp | (l.expl.exp ) Where identifier is any symbol other than lambda (l). The identifier in the second production is the name of a variable in the body of the lambda expression. This variable is called the bound variable of the expression, because it binds or captures any occurrence of the variable in the body. Any occurrence of the variable in the body refers to this one.

  23. l-calculus COP4020 Programming Languages Example: (lambda (x) ( + x 5 ) This expression describe a procedure that adds 5 to its argument. Example: free variable (occurs-free) (lambda (x) ( + x 5 ) ( - x 7 ) ) The last occurrence of x does not refer to the x that is bound in the lambda expression. These grammars are said to be context-free because a rule defining a given syntactic category may be applied in any context that makes reference to that syntactic category.

  24. To be continued

More Related