30
David Evans http://www.cs.virginia.edu/ evans CS200: Computer Science University of Virginia Computer Science Lecture 24: Metalinguistics

David Evans cs.virginia/evans

  • Upload
    topper

  • View
    34

  • Download
    0

Embed Size (px)

DESCRIPTION

Lecture 24: Metalinguistics. David Evans http://www.cs.virginia.edu/evans. CS200: Computer Science University of Virginia Computer Science. Menu. Theory and Reality Nondeterministic Computing PS7: Quantum Computing Making New Languages. Problem Classification. - PowerPoint PPT Presentation

Citation preview

Page 1: David Evans cs.virginia/evans

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

CS200: Computer ScienceUniversity of VirginiaComputer Science

Lecture 24: Metalinguistics

Page 2: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 2

Menu

• Theory and Reality

• Nondeterministic Computing

• PS7: Quantum Computing

• Making New Languages

Page 3: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 3

Problem ClassificationTo show a problem is decidable/in NP/in P, you need to show it is easy enough to be solved with a procedure in that class:– Decidable: it is easy enough to be solved by

some procedure that always terminates– NP: it is easy enough to be solved by a

nondeterministic polynomial procedure: try an exponential number of guesses, but takes only P-time to check one if correct

– P: it is easy enough to be solved by a polynomial time procedure – O (nk)

hard

er to

sho

w, m

ean

s pro

ble

m is e

asier

Page 4: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 4

To show a problem is undecidable or NP-complete, you need to show it is as hard as a problem you know enough to be solved with a procedure in that class:– Undecidable: if you had a procedure that solves

this problem, you could use it to solve a known undecidable problem (e.g., the halting problem)

– NP-Complete: if you had a procedure that solves this problem, you could use it to solve a known NP-Complete problem (e.g., SAT, Smiley Puzzle)

• Subtlety: the transformation of the problem and answer must be in P

Page 5: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 5

Intellectual Computability Discussion on TV

Page 6: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 6

Ali G Multiplication Problem

• Input: a list of n numbers

• Output: the produce of all the numbers

Is it decidable?Yes – a straightforward algorithmsolves it.

Is it tractable?Yes – it is in P

Can real computers solve it?

Page 7: David Evans cs.virginia/evans
Page 8: David Evans cs.virginia/evans
Page 9: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 9

What about C++?

int main (void){ int alig = 999999999;

printf ("Value: %d\n", alig); alig = alig * 99; printf ("Value: %d\n", alig); alig = alig * 99; printf ("Value: %d\n", alig); alig = alig * 99; printf ("Value: %d\n", alig);}

Value: 999999999Value: 215752093Value: -115379273Value: 1462353861

Results from SunOS 5.8:

Page 10: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 10

Ali G was Right!• Theory assumes ideal computers:

– Unlimited memory– Unlimited power– Unlimited (finite) time– We will formalize this later…

• Real computers have:– Limited memory, time, power outages, flaky

programming languages, etc.– There are many decidable problems we cannot

solve with real computer: the numbers do matter

Page 11: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 11

Quantum Computing

Page 12: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 12

Quantum Mechanics for Dummies

• Light behaves like both a wave and a particle at the same time

• A single photon is in many states at once

• Observing its state forces it into one state

Books Search Results: we were unable to find exact matches for your search for scheme for dummies

Page 13: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 13

Qubit• Quantum Bit

• A superposition of both 0 and 1– Represents both 0 and 1 at the same time!– Some probability it will be 0 when it is

observed– Some probability it will be 1 when it is

observed

• Two qubits can represent 0, 1, 2 and 3 all at the same time

Page 14: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 14

Metalinguistic Abstraction

Page 15: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 15

Solving Problems: Recap• PS1-4:

– Divide a problem into procedures that can be combined to solve it

• PS5: – Divide a problem into procedures and state

that can be combined to solve it

• PS6:– Divide a problem into objects that can be

used to model it

Page 16: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 16

Solving Problems• PS7:

–Divide a problem into creating a good language for solving the problem, and defining a solution using that language

Languages change the way we think. Sometimes the best way to solve a

problem is to invent a new language first.

Page 17: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 17

PS7: Quantum SchemeExtend Mini-Scheme evaluator to support

special forms:(quist ExpressionList)

Abstraction for an infinite number of qubits

Represents all of the values the

expressions evaluate to at once.

(observe Procedure Quist)

Turns a quist into a normal value. Picks one of the quist values for which (Procedure value) is true.

Page 18: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 18

Schrödinger’s Cat

• Put a live cat in a box with cyanide vial that opens depending on quantum state

• Cat is both dead and alive at the same time until you open the box

(qeval ‘(define cat (quist “alive” “dead”)))(qeval ‘(observe (lambda (q) #t) cat)“dead”or “alive”

Its only a thought experiment,not necessary to try with real cat!

Page 19: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 19

“Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. Printed by Ottaviano Dei Petrucci in

1501 (first music with movable type)

Page 20: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 20

J S Bach, “Coffee Cantata”, BWV 211 (1732) www.npj.com/homepage/teritowe/jsbhand.html

“Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. (1501)

Page 21: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 21

Inventing a Language• Design the grammar

– What strings are in the language?– Use BNF to describe all the strings in the

language

• Make up the evaluation rules– Describe what everything the grammar can

produce means

• Build an evaluator– A procedure that evaluates expressions in the

language

Page 22: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 22

Is this an exaggeration?It is no exaggeration to regard this as the most fundamental idea in programming:

The evaluator, which determines the meaning of expressions in the programming

language, is just another program.

To appreciate this point is to change our images of ourselves as programmers. We come to see ourselves as designers of languages, rather than only users of languages designed by others.

(SICP, p. 360)

Page 23: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 23

Programming an Evaluator

If a language is just a program, what language should we program the language in?

Page 24: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 24

The Metacircular Evaluator

Page 25: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 25

Environmental Model of Evaluation1. To evaluate a combination, evaluate all the

subexpressions and apply the value of the first subexpression to the values of the other subexpressions.

2. To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. To construct this environment, make a new frame with an environment pointer that is the environment of the procedure that contains places with the formal parameters bound to the arguments.

Page 26: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 26

EvalEval

ApplyApply

Eval and Apply are defined in terms of each other.

Page 27: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 27

meval(define (meval expr env) (cond ((self-evaluating? expr) expr) ((variable? expr) (environment-lookup-name expr env)) ((lambda? expr) (make-procedure (lambda-parameters expr)

(lambda-body expr) env)) ((application? expr) (mapply (meval (application-operator expr) env) (map (lambda (subexpr) (meval subexpr env)) (application-operands expr)))) (else (error "Unknown expression: " exp))))

Page 28: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 28

mapply(define (mapply procedure operands) (cond ((primitive-procedure? procedure) (apply-primitive procedure operands)) ((compound-procedure? procedure) (meval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) operands (procedure-environment procedure)))) (else (error “Can’t apply: " procedure))))

Page 29: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 29

> (meval 3 the-global-environment)|(meval 3 (((+ primitive-procedure #<primitive:+>) (* primitive-procedure #<primitive:*>))))|33> (meval '(+ 2 2) the-global-environment)|(meval (+ 2 2) the-global-environment)| (meval + the-global-environment)| (primitive-procedure #<primitive:+>)| (meval 2 the-global-environment)| 2| (meval 2 the-global-environment)| 2| (mapply (primitive-procedure #<primitive:+>) (2 2))| 4|44

Page 30: David Evans cs.virginia/evans

24 March 2003 CS 200 Spring 2003 30

Charge• Defining eval and apply is the guts of it

• Wednesday we will see the details

• This is powerful: once we have an metacircular evaluator, we can easily make changes to the language!