25
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

COP4020 Programming Languages

  • Upload
    zanthe

  • View
    44

  • Download
    0

Embed Size (px)

DESCRIPTION

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 . - PowerPoint PPT Presentation

Citation preview

Page 1: 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

Page 2: COP4020 Programming Languages

COP4020 Programming Languages Inductive sets of data

We will present the basic programming tools we will need to write:

Interpreters and similar programs that form the heart of a programminglanguage processor.

Because the syntax of a programming language is usually a nested or tree-likestructure,

Recursion will be at the core of our technique

Page 3: COP4020 Programming Languages

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 setof 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

or2.- n – 3 A

Recursively Specified data

Page 4: COP4020 Programming Languages

COP4020 Programming Languages Recursively Specified data

Let us determine what natural numbers are in S.

We know that 0 S 3 S, since (3 – 3) = 0 and 0 S6 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 S2 S same 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

Page 5: COP4020 Programming Languages

COP4020 Programming Languages

We can use the definition to write a procedure to decide whether a naturalnumber n is in S.

in-S?: N BoolUsage: (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 meansthat in-S? is intended to be a procedure that takes a naturalnumber and produces a Boolean.

This comments are useful for reading and writing programs.

Page 6: COP4020 Programming Languages

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 anyOther 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, thenS1 S2 (since S1 is smallest), and S2 S1 (since S2 is smallest), hence S1 = S2.

Recursively Specified data

Page 7: COP4020 Programming Languages

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 byand 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

Recursively Specified data

Page 8: COP4020 Programming Languages

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 ofinference 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.

Recursively Specified data

Page 9: COP4020 Programming Languages

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)

A brief explanation on LISP

Page 10: COP4020 Programming Languages

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 )

A brief explanation on LISP

Definition: (List of integers, top-down): A “Scheme” list is a list of integers ifand 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-Int to denote the setof a list of integers.

Page 11: COP4020 Programming Languages

COP4020 Programming Languages

Definition 4 (list of integers bottom-up): The set List-of-Integers is the smallest setof Scheme list satisfying the following two properties:

1.- ( ) List-of-Int, and2.- If n Int and L List-of-Int then (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. The built-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)

A B C

A brief explanation on LISP

Page 12: COP4020 Programming Languages

COP4020 Programming Languages

Example: (CONS ‘(A B) ‘(C D)) returns ( (A B) C D )

C D

A B

A brief explanation on LISP

Page 13: COP4020 Programming Languages

COP4020 Programming Languages

Definition 5 (List of integers, rules of inference)

( ) List-of-Int

n Int L 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

Rules of inference

Page 14: COP4020 Programming Languages

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.

Rules of inference

Page 15: COP4020 Programming Languages

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

Deduction tree

Page 16: COP4020 Programming Languages

COP4020 Programming Languages

Grammar are typically used to specify sets of strings, but we can use them to definesets 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 L is in List-of Int, then( n . L ) is in List-of-Int.

Defining sets using grammars

Page 17: COP4020 Programming Languages

COP4020 Programming Languages

A grammar is composed of:

- Nonterminal symbols These are the names of the sets being defined and those sets are called syntacticcategories. We will use capital letters to refer to nonterminals and sets. For example, Expressionis 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 SymbolsThese are the characters in he external representation, in this example:

List-of-Int ::= (Int . List-of-Int)

The terminal symbols are “.”, “(“, and “)”

Defining sets using grammars

Page 18: COP4020 Programming Languages

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 categoryIn 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}* )

Defining sets using grammars

Page 19: COP4020 Programming Languages

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

814, 127, 2, 15, 20

{Int}*( ; ) includes the strings

814; 127; 2; 15; 20

Defining sets using grammars

Page 20: COP4020 Programming Languages

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.orwith 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.

On derivations

Page 21: COP4020 Programming Languages

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 containonly 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 ) ) )

On derivations

Page 22: COP4020 Programming Languages

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 referencesprocedures that take a single argumentprocedure calls

We can defined with the grammar:

l-exp ::= identifier | (l .identifier l-exp | (l.exp l.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.

l-calculus

Page 23: COP4020 Programming Languages

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.

l-calculus

Page 24: COP4020 Programming Languages

To be continued

Page 25: COP4020 Programming Languages