Transcript of CS152 Programming Paradigm
- 1. CS152 PROGRAM PARADIGMS 1/26/2015 San Jose State University
Computer Science Kaya Ota 4/14/2015
- 2. PROGRAMING PARADIGMS Important concept of Abstraction
Computational paradigms What is the language? Have a grammar rule
Way to communicate Definition of language : the set of words and
sign that people use to communicate each other. 4/14/2015
- 3. PROGRAMMING LANGUAGE What is the programming language?
Programing language is a notation for communicating to a computer
what we want it to do. Paradigms = Or 4/14/2015
- 4. HISTORY OF COMPUTER LANGUAGE 1940 : Hard wired Spaghetti
code comes from here Late 1940 : Machine code has invented by John
Von Neumann CPU Arithmetic / logic unit control unit Memory unit
outputinput John Von Neumann model 4/14/2015
- 5. HISTORY OF COMPUTER LANGUAGE Binary code has a problem
Reading , debugging, tightly coupled with machine i.e. less
portability Assembly language (get closer to human language)
Problem of Assembly: limited instruction set Criteria Readability
Debugblity Portability Abstraction 4/14/2015
- 6. HISTORY OF COMPUTER LANGUAGE What is abstraction in computer
science ? Definition of Abstraction Abstraction is the ability to
separating the interface from the implementation and creating a new
concept 4/14/2015 Abstraction allows programmers to consider a
sequence of actions as a single action
- 7. ASSEMBLY LANGUAGE 4/14/2015
- 8. PROGRAMMING LANGUAGE 4/14/2015
- 9. HIGH LEVEL LANGUAGE High Level Language is notations that
are closer to human language Fortran (Formula Translation) Support
Algebraic notations Floating number Not Support Control statement
Array Functions Data Structures 4/14/2015
- 10. HIGH LEVEL LANGUAGE AlGOL (Algorithmic Language) 1st
Language that support Loop Array 4/14/2015
- 11. ABSTRACTION IN PROGRAM LANGUAGE How we can categorize
Abstractions in programming languages ?? 2 types of Abstractions
Data To simplify behaviors and attributes for humans Control To
simplify properties of the transfer of control Example: Loop,
Functions, Conditional Statement 4/14/2015
- 12. ABSTRACTION IN PROGRAM LANGUAGE Data Abstraction 3 levels
of data abstractions Basic Data Abstraction Basic data = hide
internal representations Variable = use symbolic name to hide
location of memory We HIDE binary representations and IEEE single
or double precision standard representations by using variable.
Structured Data Abstraction Array Unit Data Abstraction Java class
and package, module 4/14/2015 These variables are called atomic or
primitive Normally can not access the component part
- 13. ABSTRACTION IN PROGRAM LANGUAGE Basic Control Abstraction =
combine a few machine instruction into an abstract statement
Shorthand notation (syntactic sugar) example: x+=5 Structured
Control Abstraction = selection, branching, iteration Functions are
close to what it is meant to be mathematically Functions can be
understood independent of Von Neumann model Functions can have
higher-order functions Func1 Func2 Data FUNCTION Another function
Can return function TIP Function is required return something
Procedure is NOT 4/14/2015
- 14. ABSTRACTION : UNIT CONTROL Unit Control Abstraction =
combine a few machine instruction into an abstract statement.
Thread is categorized here Program service class1 class2 class3
request responce 4/14/2015
- 15. COMPUTATIONAL PARADIGMS Imitating the operations of a
computer 1. Imperative paradigms (most of the programming
languages) Sequential execution Use of variable Use of assignment
to change the values of the variables Parallel computation:
computations that can be applied to many different pieces of data
simultaneously Undeterministic computation: computations that does
not depend on order of process 2. Functional paradigms Based on the
abstract notation of a function in lambda calculus 3. Logic
paradigms Based on symbolic logic Both based on math, i.e. we can
use math to see if a prog works correctly 4/14/2015
- 16. COMPUTATIONAL PARADIGMS Language Definition Benefit of math
definition We can reason mathematically Prog-lang can be
independent on implementation Discipline 2 Part Syntax similar to
grammar of a natural language Semantic the meaning of the statement
4/14/2015
- 17. THE POPULARITY OF PROGRAM LANGUAGE The popularity of
programming language is measured by: i. Number of skilled
programmer ii. Number of course in school What is the good
programming language design? i.e. how do we define success or
failure of programming language ? General Criteria for design
language Efficiency Security 4/14/2015
- 18. GOOD PROGRAMMING LANGUAGE We determine the goodness of
programming language by the criteria below Readability
Debuggability Writability Portability Having mechanisms of
abstraction + alpha 4/14/2015
- 19. SUCCESS / FAILURE LANGUAGE Determinants for success or
failure programming language Achieve the goal Attains widespread
use in Applications Serve as a model for the other languages
4/14/2015
- 20. HISTORY OVERVIEW Early day, machines were SLOW and memories
were EXPENSIVE Time complexity and Space complexity were primarily
concerns! COBOL common business oriented language = improve
readability 1970s = focus on simplicity and abstraction
Mathematical definitions for languages were developed Example)
Pascal, Modula2, C, Ada Last 25 years = focus on Reusability is a
big concern Java 4/14/2015
- 21. LIFE CYCLE OF SOFTWARE Life cycle of software Other
includes Analyses, Design, Debug, Coding, Test Software becomes: 1.
More complexity 2. Need to adapt to change 5% Other 95% Maintenance
Readability 4/14/2015
- 22. MEASUREMENT OF PROGRAMMING LANGUAGE 1. Efficiency 1. Target
Code (i.e. executable code) 2. Developing 2. Readability 3.
Writability 4. Portability (key success of java) 5. Deguggability
6. Reusability 7. Abstraction 8. Reliability 4/14/2015
- 23. EFFICIENCY 1. Target Code (executable code ) Java: int x =
10 (type strict) Javascript ver x = 10 At runtime, machine check
the type of x for javascript inefficient Type strict is more
efficient 2. Developing Readability and Writability are important
Expressiveness Conciseness of syntax 4/14/2015
- 24. REGULARITY How well the features of languages are
integrated Fewer restriction 1. Generality Ability or use of a
construct Combining related constructs into a single or more
general Ex) if(x == 5) is valid when x is primitive type If (obj ==
aobj) is not valid 4/14/2015
- 25. REGULARITY 1. Orthogonality Allows languages construct to
be combined in meaningful way. In a language that is truly
orthogonal, language constructs do not behave differently in
different way. Associated with simplicity, the more orthogonal
design, the fewer exceptions Lack of Orthogonality: in C, we can
not return Array (we deal with it as an exception) in Pascal, we
cannot return Object and Array (we deal with them as an exception)
So, they are lack of orthogonality in Other Languages, we can
return any thing you want! No exception YES Orthogonal!
4/14/2015
- 26. REGULARITY 1. Uniformity: Similar things look similar!
Refers to the consistency of appearance and behavior of a language.
Example in C++ Class Definition: class Abc { // implementation };
semicolon needed! Function Definition: int f(int x){//
implementation} semicolon unneeded! Both are definition(similar
things) but looks different! Therefore, this is lack of uniformity
4/14/2015
- 27. REGULARITY Uniformity: Similar things look similar Example
in Pascal function f : boolean ; // function definition begin .. f
: = true // assignment . end; Different things(definition vs.
assignment) but looks similar. So, this is also an example of lack
of Uniformity 4/14/2015 In java boolean f() {..} In java boolean f
= true;
- 28. SECURITY (I.E. RELIABILITY) Reliability Program language is
reliable if it prevents the programmer to misuse the certain
features If allows the errors be discovered and reported quickly
Semantically Safe The language is semantically safe if it prevents
a programmer from compiling and executing any statement that
violate the definition of the language. Errors Caught by Compiler
(checked) Errors Caught by Runtime (unchecked) Errors Caught by
Nothing All Program Errors 4/14/2015
- 29. FUNCTIONAL PROGRAMMING CH3 4/14/2015
- 30. LAMBDA CALCULUS Mathematical formalism for expressing
computation by function expression constant | variable | (exp exp)
| (lambda x. exp) Define d as OR Separato r Inpure lambda = numbers
are not expressed by functions Pure lambda = numbers are expressed
by functions 4/14/2015
- 31. LAMBDA CALCULUS 4/14/2015 Lambda Calculus Representation
Ordinary Mathematical Representation
- 32. LAMBDA CALCULUS Free and Bound variables x is said to be a
bound variables (i.e. local variable in CS side) Otherwise,
variables are said to be free Scope of x is in ( ) 4/14/2015 x is
bound y is free z is bound x is free
- 33. PASS BY VALUE AND PASS BY NAME 4/14/2015 Pass by Value Pass
by Name Remark: Pass by value is NOT ALWAYS EQUAL to pass by
name
- 34. PASS BY VALUE AND PASS BY NAME 4/14/2015
- 35. HIGHER ORDER FUNCTION Higher order function can return a
function Transformation : Alpha-Conversion Changing the name of
bound variables Beta-Conversion (Beta-Induction) Beta-Conversion is
primary way of transforming lambda calculus 4/14/2015
- 36. PROGRAM AS FUNCTIONS Math Function Rule f: x y X black box
y 4/14/2015 Set X {x1, x2...} Set Y {y1, y2} Mapping Domain
Range
- 37. DIFFERENCE BETWEEN IMPERATIVE AND FUNCTIONAL Imperative
programming A variable = memory address that store a value
Functional programming No assignment So, No loop BUT Recursive call
4/14/2015 Memory 0xFFFFFF int x = 1; refer
- 38. REMAINDER Java is ALWAYS Pass by value Pass by value The
local parameters are copied of the original arguments passed in
Changes made in the function to these variables do not affect
originals Pass by Reference The local parameters are reference to
the storage locations of the original arguments Changes to these
variables in the function will affect the originals No Copy made.
So, overhead of copying (time and storage) is saved 4/14/2015
- 39. OOP AND OOD CS152 3/12/2015
- 40. OBJECT ORIENTED PARADIGM Encapsulation contribute to SE
needs: We can change implementation and be still works as long as
we do not change signature
- 41. OBJECT ORIENTED PARADIGM Encapsulation The ability of
bundling data and behaviors together and control how it works.
Abstraction The ability of separating the interface from
implementation What we call class in java
- 42. INHERITANCE Class A Default m3() Protected m2() Private
m4() Public m1() Class B What is the visibility from B In A ??
- 43. Class B B can have access: Public Default Protected Class A
Default m3() Protected m2() Private m4() Public m1() Class D D can
have access: Public Class C C can have access: Public Protected(b/c
inherits A) Diff between protected and default Protected is seen by
class that have inheritance relation Package X Package Y
- 44. HOW OOD CONTRIBUTE TO SE We can reuse an existing and
extends it We can change implementation of the behaviors of the
existing class by overriding its method.
- 45. CS152 PROGRAM PARADIGM 3/17/2015 San Jose State University
Computer Science Kaya Ota 4/14/2015
- 46. INLINE CODE If a method is called frequently, then java
(and also C++) compiler may replace the method call by the actual
code of the method. Inline code: the code for the function body
that is inserted directly at the point where the function would be
called. 4/14/2015
- 47. GOLDEN RULES OF PROGRAMMING When you program, you should
pay attention them! 4/14/2015
- 48. GENERAL GOLDEN RULES No magic constant Document clearly
every thing before starting coding Choose meaningful name DRY (Do
not repeat yourself) Efficiency is not current the top of priority
=> maintainability is more important Not be more then 20+-5
statements Atomic method: a method must do just one responsibility
IN Detail. 4/14/2015
- 49. ATOMIC METHOD Method1() { } Connect to DB Retrieve-the data
Package Data One method do more then one in detail. 4/14/2015
- 50. ATOMIC METHOD Method1() { Call Functions } Connect to DB()
Retrieve-the data() Package Data() Functionality splied in to
methods This is better to debug 4/14/2015
- 51. DEMETER LAW A method M of an Object O may only use the
following members: Instance variable of O Ms formal parameters Any
object created /instantiated within M Method1( type param1, type
param2.) { new O1 ; O1.method#(int x, int y); O1.data }
4/14/2015
- 52. GOLDEN RULE FOR CLASS DESIGN A class does not have more
then one higher-level responsibility! Encapsulate related data and
behavior in one and only one place Give the minimum access Minimize
the dependency between the classes! Loose Coupling. if class A
knows whatever B exposed via its interface, then they re
loosely-coupled, otherwise, they are tightly- coupled. High
Cohesion put everything related in one class Bound of class should
be crystal clear 4/14/2015
- 53. LOGIC PROGRAMMING Logical programming with Prolog
4/14/2015
- 54. HISTORY OF LOGIC Logic is the science of reasoning and
proof, has existed since ancient Greek philosopher. ARISTOTLE is
famous Math is a language of Science. And logic is a language of
math. 4/14/2015
- 55. REVIEW OF PROPOSITIONAL LOGIC Proposition Propositions are
the basic building blocks of logic Definition A proposition is
declarative sentence that is either true or false Propositional
Calculus The area of logic that deals with propositions is called
propositional calculus 4/14/2015
- 56. Logical Operation P Q P and Q P or Q If P then Q P xor Q P
iff Q T T T T T F T T F F T F T F F T F T T T F F F F F T F T
4/14/2015
- 57. LOGICAL OPERATION In implication (conditional statement) p
q, p is called the hypothesis (or premise) and q is called
conclusion. 4/14/2015
- 58. LOGICAL QUIZ Let P be I have access to CS network Let Q be
I am CS major If P then Q = PQ if I have access to Cs network, then
I am CS major If Q then P = PQ if I am CS major, then I have access
to CS network To be bidirectional(if and only if), Both directions
( and ) have to be true. I have access to CS network only if I am
CS major (PQ ) This sentence is direction with PQ b/c Q is in
if-sentence 4/14/2015
- 59. LOGICAL QUIZ If Q then P means Q is sufficient condition,
and P is necessary condition P only if Q means Q is necessary
condition and P is sufficient condition Arrow Always indicates the
direction Sufficient condition Necessary condition 4/14/2015
- 60. CS152 PROGRAM PARADIGM 3/19/2015 San Jose State University
Computer Science Kaya Ota 4/14/2015
- 61. REVIEW OF PROPOSITIONAL LOGIC PQ == not Q not P BUT, PQ !=
QP and not P not Q Tautology: A compound proposition that is always
true, not matter what the truth value of the propositional variable
that occur in it, is tautology. 4/14/2015
- 62. IMPORTANT DEFINITION Tautology: A compound proposition that
is always true, not matter what the truth value of the
propositional variable that occur in it, is tautology.
Contradiction: A compound proposition that is always false ,no
matter what the truth value of the propositional variable that
occur in it, is contradiction. Contingent: A compound proposition
that is neither a tautology nor a contradiction is called a
contingency 4/14/2015
- 63. LOGICAL EQUIVALENCE Identify: P and T = P Domination : P
and F = F Idempotent : P and P = P Double Neg: Not(Not P) = P
Commutative: P and Q = Q and P Negation: P and not P = F
Absorption: P and (P or Q) = P Associative: (P and Q) and R = P and
(Q and R) Distributive: P and (Q or R) = (P and Q) or (P and R)
4/14/2015
- 64. LOGICAL EQUIVALENCES De Morgan Not(P and Q) = Not P or Not
Q P implies Q = not P or Q P implied Q = not Q implied not P P if
and only if Q = (P implies Q ) and (Q implies P) P if and only if Q
= not P if and only if not Q P implies (Q implies R) = (P and Q)
implies R (P or Q) implies R = (Q implies R) and (Q implies R)
4/14/2015
- 65. INTUITIVE UNDERSTAND P implies Q == Not Q implies Not P
4/14/2015 P = not Q Q = not P not Q Not P IDENTICAL If an element
in not in Q, Then the element is not in P too
- 66. DUALITY RULE Dual of a logical equivalence that only
contains not, and or, T, F, can be constructed by the following
exchanges P and not Q = F P or not P = T And or Or and T F F T The
constructed formula is also a valid logical equivalence. Duel of S,
noted S* 4/14/2015
- 67. SATISFIABILITY Definition A compound proposition is
Satisfiable if there is at least an assignment of truth value to
its variables that makes it true. If the proposition is false for
all assignments, then it is called unsatisfiable Each assignment
that makes it satisfiable is called a solution 4/14/2015
- 68. FUNCTIONAL COMPLETE Definition A set of logical operators
is called functionally complete if every compound proposition can
be re-written using the operators of this set. Note: we already
know that every compound proposition can be written by (not, and,
or) set TRY: show that the set of logical operators (not, and) are
functionally complete. 4/14/2015
- 69. CS152 PROGRAM PARADIGM 4/2/2015 San Jose State University
Computer Science Department Kaya Ota 4/14/2015
- 70. REVIEW OF PROPOSITIONAL LOGIC 4/14/2015 If (x > 5) {
if(y > 10) { // r = do something } } P (q r) = Not p or (not q
or r) = (not p or not q) or r = Not(p and q) or r = (p and q) r If
(x > 5 and y > 10) { // r = do something } o u t Example
1
- 71. REVIEW OF PROPOSITIONAL LOGIC 4/14/2015 If (x > 5 || !(y
> 10 || x 5 ) { // r = do something } o u t Example 2
- 72. PREDICATE CALCULUS Quantifiers Using quantifiers is another
way to convert a propositional function to proposition. Universal
Quantifier The notation denotes the universal quantification of
P(x). Here is called the universal quantifier. For all x we mean
for all elements of a specified set of values that is called domain
discourse or just as domain If all p(xi) for i = 1,2,3,,n are true,
then P(x) is true. 4/14/2015
- 73. Existential Quantifier There exists x in the domain, P(x) =
P(x1) P(x2) P(xn) What if the domain is an empty set ? False! There
does not exists such x. 4/14/2015
- 74. RULE OF INFERENCE 4/14/2015 P1 P2 P3 ----------------------
- So, q Premises or hypothesis Conclusion Prove is to shows (P1P2
P3 Pn) q Tautology
- 75. PROOF OF RESOLUTION IN Construction 4/14/2015
- 76. QUERY WITH PROLOG Query: A query is a question that we ask
from prolog KB 4/14/2015 Woman(sally) Man(jack) .. ?- Woman(mia) //
is mia woman? ?-Man(jack) // is jack man? ?- Woman(mia),Man(jack)
// is mia woman and is jack man? QueriesKnowledge Based ASK Tell
All 3 questions are fail b/c info is not in KB.
- 77. QUERY WITH PROLOG 4/14/2015 *Woman(Sally) Man(jack)
**Woman(Mia) Sally .. ?- Woman(x) // is there woman x? Sally ;
//i.e. any other solution? Mia QueriesKnowledge Based ASK for woman
x TELL (Sally is woman) ASK for other solution TELL (Mia is
woman)
- 78. QUERY WITH PROLOG 4/14/2015 Woman(A) *Woman(Mia) Woman(B)
Loves(B, Mia) Loves(pumpkin, honey) *Loves(Marcellus, Mia) . ?-
Loves(Marcellus, x ), Woman(x) // is there Marcellus loves x and
woman x? x = Mia //instantiate Result True! QueriesKnowledge Based
ASK for Loves(Marcellus, X) TELL Loves(Marcellus, mia) ASK for
Woman(Mia) TELL (Mia is woman)
- 79. UNIFICATION Unification is a process of matching two terms.
4/14/2015
- 80. SEARCH TREE Will add later. 4/14/2015