Top Banner
David Evans http://www.cs.virginia.edu/ ~evans CS200: Computer Science University of Virginia Computer Science Lecture 2: Formal Systems and Languages MU!
42

David Evans cs.virginia/~evans

Jan 08, 2016

Download

Documents

Lecture 2: Formal Systems and Languages. MU!. David Evans http://www.cs.virginia.edu/~evans. CS200: Computer Science University of Virginia Computer Science. Menu. Pictures – passing around now, write your name and draw an arrow to your face Unresolved Questions from Lecture 1 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: David Evans cs.virginia/~evans

David Evanshttp://www.cs.virginia.edu/~evans

CS200: Computer ScienceUniversity of VirginiaComputer Science

Lecture 2: Formal SystemsandLanguages

MU!

Page 2: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 2

Menu• Pictures – passing around now, write your

name and draw an arrow to your face

• Unresolved Questions from Lecture 1

• Formal Systems– MIU-system

• Languages– English– Scheme

Page 3: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 3

Conundrums

• What is the real name of the College?– Web site: “College of Arts and Sciences”– Then what does “CLAS” stand for?

• Col-Lege of Arts and Sciences?

– Why does it have Government, Math and Foreign Languages Departments?

• My theory:– The “Liberal” is Silent

Page 4: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 4

If it takes 60 seconds to compute a photomosaic for Problem Set 1 today on a typical PC, estimate how long it will take CS200 students in 2005 to compute the same photomosaic? How long will it take in 2008? > (/ (* (- 2005 2002) 12) 18))2> (/ 60 (* 2 2))15> (/ (* (- 2008 2002) 12) 18)4> (/ 60 (* 2 2 2 2))15/4> (exact->inexact (/ 60 (* 2 2 2 2)))3.75

Difference in years * 12 = number of monthsNumber of months / 18 = number of doublings according to Moore’s Law

Page 5: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 5

If it takes 60 seconds to compute a photomosaic for Problem Set 1 today on a typical PC, estimate how long it will take CS200 students in 2005 to compute the same photomosaic? How long will it take in 2008? > (/ (* (- 2005 2002) 12) 18))2> (/ 60 (* 2 2))15> (/ (* (- 2008 2002) 12) 18)4> (/ 60 (* 2 2 2 2))15/4> (exact->inexact (/ 60 (* 2 2 2 2)))3.75

60 seconds today, 2 doublings by 200515 seconds in 2005

60 seconds today, 4 doublings by 20083.75 seconds in 2008

Page 6: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 6

Are there any non-recursive natural languages? What would happen to a society that spoke one?

Not for humans at least. They would run out of original things to say.

Chimps and Dolphins are able to learn non-recursive “languages” (some linguists argue they are not really “languages”), but only humans can learn recursive languages.

Page 7: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 7

Running out of Ideas

“Its all been said before.”

Eventually true for a non-recursive language.

Never true for a recursive language.

There is always something original left to say!

Page 8: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 8

Survey Results

Everyone who is in the course should have received an email with the PS1 groups in it. (Sent out at 6:30pm yesterday.)

Page 9: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 9

C(L)AS Class• Majors

– Undecided: 5½– Cognitive Science: 4– Media Studies: 2– Econ (1½), Math (1½), East Asian Studies,

English, Pre-Commerce

• Years– First: 6, Second: 8, Third: 1, Fourth: 2

Page 10: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 10

Wrote a Program

• No: 6

• Yes: 11– Some experience with C++, BASIC, Pascal,

Java

• It won’t make much difference in this class after the first problem set

Page 11: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 11

Other Questions• Found Class: COD (7), People (6), other (4)• Exams: take home (12), in class (3), don’t care (2)

– Exam 1 will be take home, no promises on Exam 2

• Prefer partners (6), some of each (3), no preference (2), alone (6)– Most accomplishments in CS were done by groups– Most of the assignments you will work with partners, some

you will have choices– If you prefer to work alone, you need to convince me you

can work well with a team first, and then maybe I will let you.

Page 12: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 12

Formal Systems

Summary of your answers to the other questions and my answers to the survey questions are on the course web site.

Page 13: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 13

Formal Systems

• Set of symbols– Starting Points– Primitives

• Set of rules for manipulating symbols– Hofstadter: Rules of Production, Rules of

Inference– Also: Rules of Combination

Page 14: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 14

The MIU System• Symbols: M, I, U• Rules of Production:

– Rule I: If you have a string ending in I, you can add a U at the end.

– Rule II: Suppose you have Mx. Then you may add Mxx to your collection.

– Rule III: If III occurs in one of the strings in your collection you may make a new string with U in place of III.

– Rule IV: If UU occurs inside one of your strings, you can drop it.

Page 15: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 15

MIU System ExampleStart with MUI, produce MIU

Rules of Production:Rule I: If you have a string ending in I, you can add a U at the end.Rule II: Suppose you have Mx. Then you may add Mxx to your collection.Rule III: If III occurs in one of the strings in your collection you may make a new string with U in place of III.Rule IV: If UU occurs inside one of your strings, you can drop it.

Page 16: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 16

Languages

Page 17: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 17

What is a language?

Webster:

A systematic means of communicating ideas or feelings by the use of conventionalized signs, sounds, gestures, or marks having understood meanings.

Page 18: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 18

Linguist’s Definition

A description of pairs (S, M), where S stands for sound, or any kind of surface forms, and M stands for meaning.

A theory of language must specify the properties of S and M, and how they are related.

Page 19: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 19

Languages and Formal Systems

What is the difference between a formal system and a language?

With a language, the surface forms have meaning.

Page 20: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 20

What are languages made of?• Primitives (almost all languages have these)

– The simplest surface forms with meaning• Means of Combination (all languages have these)

– Like Rules of Production for Formal Systems– Ways to make new surface forms from ones you

already have• Means of Abstraction (all powerful languages have

these)– Ways to use simple surface forms to represent

complicated ones

Page 21: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 21

Does English have these?• Primitives

– Words (?)• e.g., “antifloccipoccinihilipilification” – not a primitive

– Morphemes – smallest units of meaning• e.g., anti- (“opposite”)

• Means of combination– e.g., Sentence ::= Subject Verb Object– Precise rules, but not the ones you learned in

grammar school

Page 22: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 22

Does English have these?• Means of abstraction

– Pronouns: she, he, it, they, which, etc.– Confusing since they don’t always mean the

same thing, it depends on where they is used.

The “these” in the slide title is an abstraction for the three elements of language introduced 2 slides ago.The “they” in the confusing sentence is an abstraction for pronouns.

Page 23: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 23

Detour:

History of Computer Programming

How should we describe languages?

Page 24: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 24

ENIACElectronic Numerical Integrator and Computer

• First American computer (but not the World’s first) [1946]– Presper Eckert and John Mauchly

• Build to calculate bombing tables for Army

• Programmed by flipping switches and connecting cables

Page 25: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 25

Directions for Getting 61. Choose any regular accumulator (ie. Accumulator #9). 2. Direct the Initiating Pulse to terminal 5i. 3. The initiating pulse is produced by the initiating unit's Io terminal each

time the Eniac is started. This terminal is usually, by default, plugged into Program Line 1-1 (described later). Simply connect a program cable from Program Line 1-1 to terminal 5i on this Accumulator.

4. Set the Repeat Switch for Program Control 5 to 6. 5. Set the Operation Switch for Program Control 5 to  . 6. Set the Clear-Correct switch to C. 7. Turn on and clear the Eniac. 8. Normally, when the Eniac is first started, a clearing process is begun. If

the Eniac had been previously started, or if there are random neons illuminated in the accumulators, the ``Initial Clear'' button of the Initiating device can be pressed.

9. Press the ``Initiating Pulse Switch'' that is located on the Initiating device. 10.Stand back.

http://www.seas.upenn.edu/~museum/qman/node6.html

Page 26: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 26

Admiral Grace Hopper (1906-1992)

• Entered Naval Reserves in 1943

• Programmed Mark I (first “large” computer)

• Wrote first compiler (1952) – program for programming computers

“Nobody believed that I had a running compiler and nobody would touch it. They told me computers could only do arithmetic.”

Page 27: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 27

USS Hopper

Guest on David Letterman

“Dare and Do”

Page 28: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 28

Code written by humans

Compiler

Code machine can run

Compiler translatesfrom code in a high-level language to machine code

DrScheme uses an interpreter. An interpreter is like a compiler, except it runs quickly and quietly on small bits of code at a time.

Page 29: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 29

John Backus

• Chemistry major at UVA (entered 1943)

• Flunked out after second semester

• Joined IBM as programmer in 1950

• Developed Fortran, first commercially successful programming language and compiler

I flunked out every year. I never studied. I hated

studying. I was just goofing around. It had the

delightful consequence that every year I went to summer school in New

Hampshire where I spent the summer sailing and

having a nice time.

Page 30: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 30

IBM 704 Fortran manual, 1956

“I think there is a world market for maybe five computers.”Thomas Watson, Chairman of IBM, 1943

Page 31: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 31

Backus Naur Form• Fortran language was described using English

– Imprecise– Verbose, lots to read– Ad hoc

DO 10 I=1.10Assigns 1.10 to the variable DO10I

DO 10 I=1,10Loops for I = 1 to 10

Often blamed for loss of Mariner-I, but probably not true.

• Wanted a more precise way of describing a language

Page 32: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 32

Backus Naur Form

non-terminal ::= replacement

We can replace non-terminal with replacement

Some replacements are terminals: something that does not appear on the left side of a rule.

A ::= B means anywhere you have an A, you can replace it with a B.

Page 33: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 33

BNF Example

Sentence ::= NP VerbNP ::= NounNoun ::= Dave

Noun ::= SchemeVerb ::= rocks

Verb ::= sucks

How many different things can we express with this language?

What are the terminals?

Dave, Scheme, rocks, sucks

4, but only 2 are useful.

Page 34: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 34

BNF Example

Sentence ::= NP VerbNP ::= NounNP ::= Noun and NPNoun ::= Dave

Noun ::= SchemeVerb ::= rocks

Verb ::= sucks

How many different things can we express with this language?

Infinitely many!Recursion is powerful.

Page 35: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 35

Scheme

Page 36: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 36

Scheme

Like all powerful languages, Scheme has:– Primitives

– Means of Combination

– Means of Abstraction

Page 37: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 37

Scheme Primitives Examples

• Numerals 0 655 3.52 -58

• Functions + - round

• Constants #f #t (false) (true)

The primitives probably mean what you think they do (but expressing precisely what they mean is hard).

Page 38: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 38

Means of Combination• Application

Expression ::= (Expression)Expression ::= (Expression Expression)Expression ::= (Expression Expression Expression)

...Expression ::= (Expression Expression*)

(+ 655 58) (* (+ 0 (+ 2 2)) 6)

“Evaluate all the expressions, then apply the first expression (a procedure) to all the other values.”

Page 39: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 39

Means of Combination

• Special Forms(if Expression1 Expression2 Expression3 )

If the value of Expression1 is #f, the value of the if is the value of Expression3. Otherwise, it is the value of Expression2 .

• A few others we’ll see later…

Page 40: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 40

Essential Scheme

Expression ::= (Expression1 Expression*)

Expression ::= (if Expression1

Expression2

Expression3 )Expression ::= PrimitivePrimitive ::= numeral Primitive ::= +Primitive ::= … (other primitives)

Page 41: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 41

Means of AbstractionExpression ::= (define name Expression)

> (define two 2)> (define plus +)> (plus two two)4> (define five 4)> (= (plus two two) five)#t

Note that defines don’t evaluate to anything.

Page 42: David Evans cs.virginia/~evans

18 January 2002 CS 200 Spring 2002 42The first bug: Admiral Hopper’s Notebook, Sept 1945Moth found in UNIVAC relay

ChargeStart PS1 this weekend– A lot to read and understand– Not a lot of code to write– Take advantage of staffed Lab hours Sunday

7-11 in Olsson 001 (knock loudly if it is locked)