NCCU Programming Languages Concepts 程式語言

NCCU Programming Languages Concepts 程式語言

NCCU Programming Languages: Concepts & Principles 程式語言 Instructor: 資科系 陳恭副教授 Spring 2006 Lecture 1 Contacts • Instructor: Dr. Kung Chen – E-mail: [email protected] –Office: 大仁樓200210 – Office Hours: Tuesday: 11am-12am • Teaching assistants: 資科系研究生 – E-mail: [email protected] [email protected] –Office: 資科系程式語言與軟體方法實驗室 • Class web page: – http://www.cs.nccu.edu/~chenk/Courses/PL What shall we study? Don’t Get Confused by the Course Name 教哪個程式語言? C++? Java? C#? … No! A “principles” course aims to teach the underlying concepts behind all programming languages (Concept-driven). Course Pre-requisite • Required course for CS major (junior) – Implies that it’s not an easy course • Prerequisite – Experience in C programming – Experience in C++ or Java programming • Non-CS major students – Better talk to the instructor or be well-motivated Programming languages Programming languages you have used you’ve heard of • C, Java, C++, C#, • Lisp, … •Basic, … •… Programming Languages’ Tower of Babel • Why are there so many programming languages? "I speak Spanish to God, Italian to women, French to men, and German to my horse." — Emperor Charles V (1500-1558) Why So Many Languages? • Application domains have distinctive (and conflicting) needs • Examples: – Scientific Computing: high performance – Business: report generation – Artificial intelligence: symbolic computation – Systems programming: low-level access – Real-time systems: timing constraints – Special purpose languages –… Why So Many Languages? (Cont’d) • Evolution: Our understanding of programming evolves, so do languages grow! • The advance of programming methods usually lead to new programming languages: – Structured programming • Pascal – Object-Oriented Programming • Simula 67, Smalltalk, C++ – Functional programming • Socio-economic factors – Proprietary interests, commercial advantage • Personal preference Different Programming Language Design Philosophies C Other languages If all you have is a hammer, then everything looks like a nail. Can we say one PL is better than another? What is a Programming Language? A language for programming? 什麼是一個程式語言? 其目的為何? What is Programming? Writing programs? What is a Program? What is Programming? Solution A b c … … Problems Using computers to solve problems! What is a program? • A program is computer coding that: • Takes input • Performs some calculation on the input • Displays output Is a program a function? What is a Function? • y = 2 * x • twice(x) = 2 * x ;; a formula to compute twice; not the only one twice(x) = x + x What is a Function? A function is a mapping (set) from one set to another set. f : A Æ B is a subset of { (a, b) | a in A, b in B} Domain Range • ∀ a ∈ A, ∃ b ∈ B, such that f(a) = b •No one-to-many mapping. A program Computes a function! • Both take inputs and give outputs based on some “rule” • Both make calculations • Both take “arguments” • But one is declarative, the other operational. A program computes a function gcd(i, j): greatest common divisor program gcd (input, output); var i, j : integer; begin read (i, j); while i <> j do if i > j then i := i – j else j := j – i; writeln (i) end. What does the Following Program Computes? r := x ; q := 0 ; while r ≥ y do begin r := r - y ; What if y==0? q := q + 1 end ; It will not terminate! Integer division? x / y = q, remainder = r Foundations: Partial,Total Functions • Value of an expression may be undefined – Undefined operation, e.g., division by zero • 3/0 has no value • implementation may halt with error condition – Nontermination • f(x) = if x=0 then 1 else f(x-2) • this is a partial function: not defined on all arguments • cannot be detected at compile-time; this is halting problem – These two cases are •“Mathematically” equivalent • Operationally different Source: Prof. John Mitchell of Stanford University Partial and Total Functions ¬ ( ∀ a ∈ A, ∃ b ∈ B, such that f(a) = b) f(x) g(x) x – Total function: f(x) has a value for every x – Partial function: g(x) does not have a value for every x Source: Prof. John Mitchell of Stanford University Partial and Total Functions • Total function f:A→B is a subset f ⊆ A×B with – For every x∈A, there is some y∈B with 〈x,y〉∈f (total) – If 〈x,y〉∈f and 〈x,z〉∈f then y=z (single-valued) • Partial function f:A→B is a subset f ⊆ A×B with – If 〈x,y〉∈f and 〈x,z〉∈f then y=z (single-valued) • Programs define partial functions for two reasons – partial operations (like division) – non-termination f(x) = if x=0 then 1 else f(x-2) Source: Prof. John Mitchell of Stanford University Computability • Definition Function f is computable if some program P computes it: For any input x, the computation P(x) halts with output f(x) • Not all functions are computable! • Terminology Partial recursive functions = partial functions (int to int) that are computable Source: Prof. John Mitchell of Stanford University The Halting function • Decide whether program halts on input – Given program P and input x to P, Halt (P,x) = yes if P(x) halts no otherwise Clarifications Assume program P requires one string input x Write P(x) for output of P when run in input x. Here Program P is string input to Halt Fact: There is no program for Halt Source: Prof. John Mitchell of Stanford University Unsolvability of the halting problem • Suppose P solves variant of halting problem – On input Q, assume P(Q) = • Build program D yes if Q(Q) halts no otherwise – D(Q) = run forever if Q(Q) halts halt if Q(Q) runs forever • Does this make sense? What can D(D) do? – If D(D) halts, then D(D) runs forever. – If D(D) runs forever, then D(D) halts. – CONTRADICTION: program P must not exist. Source: Prof. John Mitchell of Stanford University Main points about computability • Some functions are computable, some are not – Halting problem • Programming language implementation – Can report error if program result is undefined due to division by zero, other undefined basic operation – Cannot report error if program will not terminate • We can only have programming languages for implementing computable functions. Source: Prof. John Mitchell of Stanford University Foundations of Programming Languages • Computation and computability • PL’s are 符號系統 for describing computations • In theory, all current programming languages are equivalent – Church-Turing thesis applies – Able to express computable functions All current programming languages are Turing-complete! In One Sense, All PL’s are Equal Alan Turing 1912-1954 • Turing Completeness: – A programming language is Turing complete provided it has integer variables and arithmetic and sequentially executes statements, which include assignment (?), selection (if) and loop (while) statements. – Definition adapted by Louden • Turing Award http://www.turing.org.uk/turing/ The Turing Machine Tape TTACC A G C G 1 Read/Write Head -ACG T 0 HALT HALT HALT HALT HALT 1 -,<=,0 A,=>,1 C,=>,1 G,=>,2 T,=>,1 2 -,<=,0 A,=>,1 C,<=,3 G,=>,2 T,=>,1 3T,=>,4 4A,=>,1 Control Device Replaces GC with TA Slides of Dr. Nick Benton The Turing Machine TTACC A G C G 1 - A CG T 0HALT HALT HALT HALT HALT 1 -,<=,0 A,=>,1 C,=>,1 G,=>,2 T,=>,1 2-,<=,0 A,=>,1 C,<=,3 G,=>,2 T,=>,1 3 T,=>,4 4 A,=>,1 Replaces GC with TA The Turing Machine TTACC A G C G 1 -ACG T 0 HALT HALT HALT HALT HALT 1 -,<=,0 A,=>,1 C,=>,1 G,=>,2 T,=>,1 2 -,<=,0 A,=>,1 C,<=,3 G,=>,2 T,=>,1 3T,=>,4 4A,=>,1 Replaces GC with TA The Turing Machine TTACC A G C G 1 -ACG T 0 HALT HALT HALT HALT HALT 1 -,<=,0 A,=>,1 C,=>,1 G,=>,2 T,=>,1 2 -,<=,0 A,=>,1 C,<=,3 G,=>,2 T,=>,1 3 T,=>,4 4A,=>,1 Replaces GC with TA Church-Turing Thesis • It is not possible to build a machine that is inherently more powerful than a Turing machine. • I.e., your PC and the Supercomputer in Central Weather Bureau are as capable as the Turing Machine in terms of what can be computed. The intuitive notion of algorithm ≈ Turing machine algorithms If all PL’s are equal, How can we say one PL is better than another? Foundations of Programming Languages Then, why are there so many different languages? Able to express is NOT equal to easy to express in practice, different languages provide distinct voices different cultures (application domains) different primitive concepts (operations) different ways of thinking about the world (perspectives) "I speak Spanish to God, Italian to women, No more language wars? French to men, and Come back to this later. German to my horse." — Emperor Charles V (1500-1558) Why Study Programming Languages? • Increased capacity to express programming concepts – A language is a “conceptual universe” (Perlis) – 語言影響我們的思考! Good language aids programmer in writing good programs. • 工欲善其事必先利其器! – Improved background for choosing appropriate languages – Greater ability to learn new languages • Understand significance & cost of implementation • Ability to design new languages But I will never design a programming language! • Many system programs are like languages – command shells – programmable editors – XML schema • Many system programs are like compilers – read & analyze configuration files and command line options • Easier to use and design such things once you know about ‘real’ languages • Martin Fowler, Language-Oriented Programming http://www.martinfowler.com/articles/languageWorkbench.html 你們一定會遇到一些需要PL knowledge的場合 A Little Language: 規則描述語言範例 TABLE 門診醫令清單: 931大四資訊專題 BEGIN 資料格式: $$=12; 醫事服務機構代號: $$ MEMBER{HospitalID.HospID}; 費用年月: today()-date($$)<=day(732); 申報類別: $$ IN {1,2}; 案件分類: $$=門診點數清單.案件分類; 流水號: $$=門診點數清單.流水號; 藥品項目代號: IF $.醫令類別 IN {0,2,7,8} THEN $$ MEMBER{NHIFeepay.Code} ELSE IF ($.醫令類別=1) THEN $$ MEMBER{DrugsCode.Code} ELSE IF ($.醫令類別=3) THEN $$ MEMBER{SpecialStuff.Code} ELSE IF ($.醫令類別=4) THEN $$ MEMBER{NHIFeepay.Code, DrugsCode.Code, $$=SUM 申請金額 FROM 點數清單 SpecialStuff.Code}; WHERE 點數清單.案件分類 IN {02,03,C1,D1,D4,E1}; $$ = COUNT 點數清單 WHERE 點數清單.案件分類=05; END Some Basic Concepts of Programming Languages •Classification of PL’s –Levels of Abstraction –Programming Paradigms •Aspects of PL’s –Syntax vs.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    93 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us