CSCI 334: Principles of Programming Languages. Lecture 2: Lisp Wrapup & Fundamentals. Higher-Order Functions. Activity

Similar documents
CS 415 Midterm Exam Spring 2002

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Pure Functional Programming

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Formal Languages and Compilers Lecture I: Introduction to Compilers

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

FP Foundations, Scheme

MIDTERM EXAMINATION - CS130 - Spring 2005

CS 242. Fundamentals. Reading: See last slide

Functional Programming. Pure Functional Languages

Lambda Calculus see notes on Lambda Calculus

CS426 Compiler Construction Fall 2006

SOFTWARE ARCHITECTURE 6. LISP

LECTURE 3. Compiler Phases

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

John McCarthy IBM 704

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Compiler Design (40-414)

Announcements. CSCI 334: Principles of Programming Languages. Thanks! Lecture 23: Exam Review. Announcements. Announcements


A Tour of Language Implementation

A simple syntax-directed

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Compiling Techniques

Introduction to Compiler Design

Functional Programming. Pure Functional Languages

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

CST-402(T): Language Processors

Lambda Calculus. Gunnar Gotshalks LC-1

CS 360 Programming Languages Interpreters

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

COP 3402 Systems Software Syntax Analysis (Parser)

Compiler Design. Dr. Chengwei Lei CEECS California State University, Bakersfield

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Lecture #24: Programming Languages and Programs

Compilers and Interpreters

CS 415 Midterm Exam Spring SOLUTION

COMPILER DESIGN. For COMPUTER SCIENCE

by the evening of Tuesday, Feb 6

Lexical Scanning COMP360

CS 415 Midterm Exam Fall 2003

Organization of Programming Languages CS3200/5200N. Lecture 11

Concepts of Programming Languages

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

22c:111 Programming Language Concepts. Fall Syntax III

Introduction to Compiler Construction

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Introduction to Compiler Construction

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Programming Languages

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Compilers. Prerequisites

Principles of Programming Languages. Lecture Outline

COMPILER DESIGN LECTURE NOTES

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Midterm Examination (Sample Solutions), Cmput 325

Theory and Compiling COMP360

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

SOFTWARE ARCHITECTURE SUMMARY. Tatsuya Hagino

Compiler Construction

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

CSCI312 Principles of Programming Languages!

Introduction to Compiler Construction

Administrivia. Compilers. CS143 10:30-11:50TT Gates B01. Text. Staff. Instructor. TAs. The Purple Dragon Book. Alex Aiken. Aho, Lam, Sethi & Ullman

Introduction to Scheme

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

Compiling Regular Expressions COMP360

CS 314 Principles of Programming Languages

Compiler Code Generation COMP360

Functional Programming

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

A Simple Syntax-Directed Translator

A Small Interpreted Language

Lambda Calculus LC-1

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

CS131: Programming Languages and Compilers. Spring 2017

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Principles of Programming Languages COMP251: Syntax and Grammars

CSCI 3136 Principles of Programming Languages

UMBC CMSC 331 Final Exam

Functional Programming. Contents

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

Common LISP Tutorial 1 (Basic)

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CS 314 Principles of Programming Languages

Chapter 3. Describing Syntax and Semantics

Lisp. Versions of LISP

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Principles of Programming Languages

Transcription:

Garbage Collection CSCI 334: Principles of ming Languages Lecture 2: Lisp Wrapup & Fundamentals ~] java -verbose:gc Garbage [GC 17024K->3633K(83008K), 0.0067267 secs] [GC 20657K->6988K(83008K), 0.0073014 secs] [GC 24012K->10505K(83008K), 0.0059666 secs]... [GC 121496K->108035K(126912K), 0.0077921 secs] [Full GC 125059K->110934K(126912K), 0.1330559 secs] [Full GC 126911K->114224K(126912K), 0.1077395 secs] [GC 114543K(126912K), 0.0021219 secs]... Instructor: Dan Barowy 2 Higher-Order Functions i.e., functions that take functions (mapcar # function list) Activity Write a function (using mapcar) that replaces the number 3 in a list with the number 6 (mapcar # function list)

Higher-Order Functions Write a function (using mapcar) that replaces the number 3 in a list with the number 6 (defun my-replace (x) (cond ((equal x 3) 6) (T x)) ) (mapcar # my-replace (1 2 3 4 5 6)) (1 2 6 4 5 6) s As Data ; substitute "to" for "from" in "term" (defun substitute (to from term) (cond ((atom term) (cond ((eq term from) to) (t term))) (t (cons (substitute to from (car term)) (substitute to from (cdr term))))) (substitute 3 'w '(+ (- 5 w) (* w w))) is (+ (- 5 3) (* 3 3)) 6 s As Data (defun substitute-and-eval (to from term) (eval (substitute to from term))) (substitute-and-eval '* '+ '(+ 10 2 3)) evaluates to 60 Genetic ming START GP Example (derivative '(* 3 x x)) -> '(* 6 x) (substitute-and-eval 6 'x (derivative '(* 3 x x))) -> 36 7 END 8

Genetic ming Genetic ming... START (cond (test...) (t (m...)))) (cond ((f (+...)) (g (m...)))) (f (+...) (...))) Mutate (f (test...) (h...))) (g (h...) (...)))) (m (h (f (+...)) (cond...)))) END 9... 10 Rule Based Systems (rule symptom-predicate diagnosis treatment confidence) Summary (rule (and (> temp 99) (headache) (cough)) (flu) (take tylenol) 0.75)) (rule (and (williams-student) (sleeping-in-class)) (African Trypanosomiasis) (prescribe pentamidine) 1.0)) Successful language symbolic computation, experimental programming Specific language ideas expression-oriented: functions and recursion lists as basic data structures for rule X: (if (and (symptoms X) (> (confidence X) 0.5)) (print (diagnosis X) "-->" (treatment X)) programs as data, with universal function eval idea of garbage collection https://exploringdata.github.io/vis/programming-languages-influence-network/ 11 12

Parts of a language 1. Syntax - the text of a program - samples: http://www.rosettacode.org/wiki/reverse_a_string 2. Semantics - the meaning or effect of a program - book discusses denotational system for describing semantics - here s another (operational semantics) Level of Abstraction Concrete (assembly,c,c++,...) movf 0x1233, fp2 movf $8(sp), fp1 movf fp1, $12(sp) More abstract (Python,Java,Lisp,...) Lisp: lists, mapcar, higher-order functions Very Abstract (LIM,Dylan,FP,Agda) Agda: _ _ : {A B C : Set} (B C) (A B) A C (f g) x = f (g x) 14 How do programs run? 1. Interpretation 2. Compilation 15

input Interpretation Inside an interpreter foo.lisp Source Compiler eval output Input Target Virtual Machine Output 18 Virtual machine VM simplifies (abstracts) hardware: define types of data define operations on data Why? language implementation is easier porting language easier only need to redefine data / ops for each new hardware platform language can be evolved rapidly Downsides Usually (very) slow (often 100-200x slower than compilation) is just source code so reverse engineering is trivial

Some interpreted languages Compilation Most Lisps foo.c Python Ruby MATLAB compile R (sort of) Java and JavaScript output (machine lang.) movf 0x1233, fp2 movf $8(sp), fp1 movf fp1, $12(sp) Compilation Some compiled languages foo.c assembly comp input movf 0x1233, fp2 movf $8(sp), fp1 movf fp1, $12(sp) x86 output C C++ Go FORTRAN Java (huh?) C# (ditto)

Advantages Usually (very) fast (often 1.5-2X slower than hand-optimized assembly code) Compiled program is in machine (binary) format; hard to reverse engineer (commercial software is often compiled) Downsides Compilation can take a long time Cannot modify program without code. Hard to evolve language; compilers are complex. Typical Compiler Typical Compiler Source Lexical Analyzer Source Lexical Analyzer Syntax Analyzer tokens Syntax Analyzer Semantic Analyzer Intermediate Code Generator Analysis Synthesis parse tree annotated parse tree Semantic Analyzer Intermediate Code Generator Code Optimizer Code Optimizer Code Generator Code Generator Target 27 Target 28

::= <num> + * <num> ::= 0 1 2... Non-Terminals:, <num> Terminals: 0, 1, 2, 3,..., +, * John Backus Turing Award, 1977 Designed FORTRAN Invented BNF Notation influenced by Chomsky's work on context-free grammars Productions for each non-terminal 29 30 ::= <num> + * <num> ::= 0 1 2... Typical Compiler! <num>! 5! +! <num> +! 1 +! 1 + *! 1 + <num> *! 1 + 2 *! 1 + 2 * <num>! 1 + 2 * 3 <num> 1 + <num> 2 * <num> 3 31 Source tokens Lexical Analyzer parse tree annotated parse tree Syntax Analyzer simple code Semantic Analyzer optimized code Intermediate Code Generator Code Optimizer Code Generator target code Target 32

Compiler "Back End" Stages Intermediate Code: temp1 = convert_int_to_double(60) temp2 = mult(rate, temp1) temp3 = add(initial, temp2) position = temp3 Optimized Code: temp1 = mult(rate, 60.0) position = add(initial, temp1) Generated Machine Code: movf rate, fp2 movf initial, fp1 movf fp1, position Some hybrid (JIT) languages Java JavaScript 33 JIT-Compilation foo.java eval x86 input output new program movf 0x1233, fp2 movf $8(sp), fp1 movf fp1, $12(sp)