Slide 1 - School of Computing

advertisement
Computing Fundamentals 1
Lecture 0
Lecturer: Patrick Browne
http://www.comp.dit.ie/pbrowne/Compfund1/compfun1.htm
Room K308
Based on Chapter 0.
A Logical approach to Discrete Math
By David Gries and Fred B. Schneider
Leibniz: Logician, Mathematician
• “It is unworthy of excellent men to lose
hours like slaves in the labour of
calculations which could safely be
relegated to anyone else if machines were
used.” Leibniz 1646-1716.
• Some ideas from Leibniz are used in the
course text.
Leibniz: Logician, Mathematician
• Leibniz identified the main components
that make automated reasoning possible:
• A language for expressing ideas precisely
• Rules of calculation for manipulating
ideas in the language
• The need to mechanize the calculations.
•
http://en.wikipedia.org/wiki/Gottfried_Leibniz
Computing Fundamentals 1
• This module presents theoretical
aspects of computer science which
are necessary to support and
enhance other modules on the
course. In particular the topics
covered on this module will be
required in computer technology,
databases, software engineering,
programming, GIS. Artificial
Intelligence, and algorithms.
Computing Fundamentals 1
• Logic: Propositional calculus, truth tables, logical equivalence,
logical argument, predicate calculus, simple proofs.
• Set Theory: Algebra of sets, power sets, cardinality, Venn diagrams,
programming using sets.
• Relations: Types, representations, equivalence, partial order,
relational database theory.
• Functions: The graph of a function, properties, composition,
functions in programming languages.
• Boolean Algebra: Basic laws, simplification of expressions,
application to switching circuits.
• Number Systems: Binary, octal, decimal, hexadecimal, simple binary
arithmetic.
• Supporting software: The above topics will be supported by
software tools including functional and logic based languages.
Computing Fundamentals 1
• The aim of this module is to
provide the student with the
theoretical foundations for
other modules on the
programme.
Computing Fundamentals 1
• Main Reference
A Logical approach to Discrete Math
By David Gries and Fred B. Schneider
Publisher: Springer; 1 edition (1993)
ISBN: 0387941150
Computing Fundamentals 1
• Useful Reading
• Logic for Mathematics and Computer Science,
Stanley N. Burris © Prentice Hall, 1998. ISBN 013-285974-2.
Mathematics & Logic
• Mathematics can be used to represent, or
model the world. Math provides a precise,
concise representation of quantities and
relationships. As Information Scientists we
are required to reason correctly to reach
conclusions, and hence we need a
knowledge of formal logic.
Formalizing Common Sense
• The following pair of signs are displayed at
the foot of an escalator. What do they
mean? They have exactly the grammatical
structure.
Shoes
Dogs
Must Be
Must Be
Worn
Carried
Formalizing Common Sense
•
•
•
•
If I tell you: “The vase is in the box”
Can you deduce by logic the following?
“Hence the box is not inside the vase”
Are the following well formed logical
statements?
• I am standing and I am sitting.
• I am standing and I am running.
Syntax and Semantics
• What do the following symbols mean?
The number six
VI
3 + 3
The smallest whole number greater
that five.
The integer successor of five.
The positive square root of 36.
Syntax and Semantics
• We need to separate symbols from
meaning.
• What do the following symbols mean?
2^7
2^6
2^5
2^4
2^3
2^2
2^1
2^0
=
=
=
=
=
=
=
=
128
64
32
16
8
4
2
1
10^2 = 100
10^1 = 10
10^0 = 1
Computing Powers in CafeOBJ
mod! POWER {
protecting(NAT)
op _^_ : Nat Nat -> Nat
vars X I : Nat
eq X ^ 0
= s 0 . – s is successor function
eq X ^ I = X * (X ^ ( p I)) . }
• -- Open the module and reduce expression
• open POWER
red (2 ^ 7) + (2 ^ 5) + (2 ^ 4) + (2 ^ 1) + (2 ^ 0) .
(179):NzNat
Syntax and Semantics
Syntax refers to the structure of expressions, or rules for putting
symbols together to form expressions.
Semantics refers to the meaning of expressions or how they are
evaluated.
Syntax and Semantics
Syntax refers to the structure of expressions, or rules for putting
symbols together to form expressions.
Semantics refers to the meaning of expressions or how they are
evaluated.
What is computing?
Computer Science is no more about
computers than astronomy is about
telescopes. Attributed to Dijkstra
Logic
• Logic is the glue that binds together methods of reasoning, in all
domains. Proof methods have their basis in formal logic. An
interpretation assigns meaning to the operators, constants, and
variables of a logic (model theory). From Gries & Schneider[1]
• Logic is the study of reasoning; it is specifically concerned with
whether reasoning is correct. Logic focuses on the relationship
among statements as opposed to the content of any particular
statement. From Johnsonbaugh[2]
• Logic as Theory of Science: People can hold scientific theories. A
scientific theory consists of a multiplicity of acts of knowing, of
verifying and falsifying, validating and calculating. Logic is a theory
which seeks to determine the conditions which must be satisfied by
a collection of acts if it is to count as a science. Logic is a ‘theory of
science’. Barry Smith[3]
Different types of logic
Different types of logic
• We will cover
– Equational logic
– Propositional logic
– Predicate Logic
• We will also cover
– Sets
– Relations
– Functions
Logic and Language
• Logic takes into account syntactically wellformed sentences and studies whether they are
semantically correct.
• There are sentences that are syntactically
correct but not from the semantic point of view.
For instance "The big house is green" is both
syntactically and semantically correct (provided
the house under consideration is green), while
"The big house is small" is contradictory as a
sentence and thus semantically incorrect.
Logic and Language
• Keep in mind the distinction between
formal logic on this course and the intuitive
everyday logic (informal)
• Further we often use informal logic to
reason about formal logic. For example,
we can say the a particular logic is not
suited to a particular task.
Logic and Language
• Consider two English sentences:
– A Mother is a Person.
– Ellen is a Mother.
• What does “is a” mean in each sentence?
• Two possible mathematical meanings:
– A Mother is a Person: Mother ⊆ Person
– Ellen is a Mother: Ellen ∊ Mother
• What is the relation between Ellen and
Person?
Logic and Language
• We will start with a logic called equational logic
• We will use the CafeOBJ language, which is based on
equational logic. We will also use CafeOBJ as a
functional programming language and as a theorem
prover.
• The role of CafeOBJ on this course is to provide a
logically based language that can be used to represent
the mathematical concepts such as logic itself, sets,
functions, relations and even programs (considering
programs as mathematical objects). At the end of the
course you should be able to read and understand small
CafeOBJ proofs and programs.
Mathematical Models
• Mathematical models are more concise, precise,
and rigorous than informal English descriptions.
• A mathematical model allows us compute
solutions.
• A mathematical model allows computers and
humans to reason about the problems in a
mechanical way. This allows us to:
– manipulate expressions;
– prove properties from and about expressions;
– obtain new results from know facts or expressions
Mathematical Models
• There are rules that allow use to perform syntactic
manipulations without regards for semantics.
• Rigour does not mean complexity.
• Rigour is defined as “strict precision or exactness”.
• Rigour usually leads to simplicity of proof and
extensibility.
• With a mathematical based language, like CafeOBJ, one
can specify or represent , reason, prove, and compute
• A value is, in contrast a computation does something.
• A value has a value type, a computation has
computation type.
Equational logic
• Equational logic (EL) can be used as a tool to
reason about systems. EL is based on equality
and Leibniz’s rule for substituting equals for
equals.
• You should be familiar with EL from school. For
example. Mary has twice as many apples as
John, can be written as:
m = 2 * j
• There are many values of m and j that make the
equation true (e.g. m=4 and j=2, or m=6 and
j=3).
Equational logic
• Mary has twice as many apples as John. Mary throws
half her apples away, because they were rotten, and
John eats one of his. Mary still has twice as many apples
as John. How many apples did Mary and John have
initially?
(0.1) m = 2 * j and
[EQ1]
m/2 = 2 * (j – 1) [EQ2]
• There are now fewer values of m and j that make these
two equations true (m=4 and j=2).
• Are these equations consistent? Are there values of m
and j that make one equation true and the other false?
It happens that these two equations have a common
solution.
The Apple0 problem 1
Equations represent facts about
John’s and Mary’s apples
mod APPLES0 {
[ Apples ]
var M : Apples -- Mary's apples
var J : Apples -- Johns apples
ops 0 1 2 : -> Apples
ops * quo : Apples Apples -> Apples
eq [eq1]: M
= J * 2 .
eq [eq2]: M quo 2 = 2 * (J – 1) . }
• The above specification represents the mini-world of Mary and John’s apples. As it
stands it does not compute a solution.
• we have symbol;
–
–
–
–
•
* representing multiplication operation
quo representing division operation (quotient)
0, 1, 2 representing numbers
M,J variables representing number of Apples
But these as yet do not have their normal computable interpretation. You could say
that at the moment the symbols have an intended interpretation. Initially we focus
representation not computation.
The Apple0 problem 1
• We can rewrite the following equations in terms of J
only:
[eq1]: M
= J * 2 .
[eq2]: M quo 2 = 2 * (J – 1) .
• Substituting giving an new equation [eq2a]:
[eq2a] : 2 * J quo 2 = 2 * (J - 1) .
The Apple problem 1
Equations represent some of the
A quotient is the result of a
true facts about John’s apples
division. For example, dividing 6
mod! APPLES1 {
by 3, the quotient is 2. In this
[ Apples ]
case we do not have a numeric
var M : Apples -- Mary's apples
value for J. Order of equations
var J : Apples -- Johns apples
matters.
ops 0 1 2 : -> Apples
op _quo_ : Apples Apples -> Apples {assoc }
op _*_ : Apples Apples -> Apples {assoc }
op _-_ : Apples Apples -> Apples {assoc }
eq [eq1] : 2 * J quo 2 = 2 * (J - 1) .
eq [eq2] : 2 * (J - 1) = 2 * J - 2 .
eq [eq3] : 2 * J quo 2 = J . -- what this equation mean?}
To test if an equation, say [eq1], is true:
open APPLES1
reduce 2 * J quo 2 == 2 * J - 2 . – true
This equality only holds for one
value of J, it is not generally true.
The Computing the Apple problem
mod! APPLES2 {
pr(INT)
op solution : Int -> Int
var M : Int -- Mary's apples
var J : Int -- John’s apples
eq solution(J) =
if (2 * J quo 2 == 2 * (J - 1))
then J
else
solution(J + 1) fi . }
Mary’s apples not used to compute J.
Pre-condition, Post-condition
• A pre-condition of a program (or algorithm)
statement is an assertion about the
program variables in a state in which the
statement must be executed, and a postcondition is an assertion about the state in
which it may terminate. A reasonable
precondition for the Apples2 program is
that the first guess should be equal or
greater than zero (no negative number of
apples)
More Examples
• In English: we wish approximate
• In Math:
–
–
–
–
n
Let b approximate n (therefore b2 approx. n)
Precondition: 0 <= n (root of negative undefined)
Postcondition: b2 <= n < (b+1)2
The postcondition corresponds to computing the
largest integer (b) that is at most n . It is what we
would expect to hold after the computation.
• These conditions do not tell us how to compute.
• With maths we can model, reason, and
compute
n
More Examples
Here is an equation in CafeOBJ that will allow us to
compute an approximation of the square root of a
number.
eq approx(B, N) =
if ((B * B <= N) and
(N < (B + 1) * (B + 1)) )
then B
else
approx(B + 1, N) fi .
• The Postcondition: b2 <= n < (b+1)2 can be written
as two conditions (b2 <= n) and (n <(b+1)2
Systematic Syntactic manipulation
From e=mc2 show e/c2=m
Precondition 0<c2
e = m*c2
=
<Divide both sides by c2>
e/c2 = (m*c2)/c2
=
<Associativity of / and *>
e/c2 = m*(c2/c2)
=
<(c2/c2) = 1 >
e/c2 = m*1
<m*1 = m >
e/c2=m
Einstein showed a mass–energy
equivalence. Matter can be turned into
energy, and energy into matter. Here
we focus on syntactic manipulation
disregarding the scientific meanings of
the symbols in physics.
Transitivity: if a=b and b=c then we can conclude that a=c
Equality is transitive, hence we can conclude e=mc2 is equivalent e/c2=m
Mathematical expressions
• Syntax of mathematical expressions are
constructed from constants, variables, and
operators:
• A constant such as 123 is an expression
• A variable such as y is an expression1.
• An operator such as + used in expression
– 123 + y
• Brackets can be used to aggregate
expressions e.g. 2 * (3 + 4) .
Mathematical expressions
• Syntax refers to the structure of
expressions, or rules for putting symbols
together to form expressions.
• Semantics refers to the meaning of
expressions or how they are evaluated.
• We are interested in syntax, semantic, and
how they are related.
Evaluation
• State is a list of variables with associated
values.
• Evaluation of an expression E in a state is
performed by replacing all variables in E by their
values in the state and then computing the value
of the resulting expression. For example:
–
–
–
–
Expression x – y + 2
State (x,5),(y,6)
Gives 5 – 6 + 2
Evaluates to 1
Programming & Logic
• The basic components of current languages are:
–
–
–
–
Data types e.g. Integers, String, Polygon.
Variables to refer to data types e.g. a = 2;
Operations on data types e.g. area(polygon)
Control structures e.g. sequence, iteration, and
conditions.
– Logic is an important part of programming, but it is
often implicit and external to the language. Some
languages like Prolog or SQL are quite close to logic.
– In many cases a CafeOBJ program has a one-to-one
relation with its mathematical or logical meaning.
Installing CafeOBJ
• For this course you should install CafeOBJ on your U:
drive in college and on your own machine.
• For a lab machine copy the M:\PBrowne\cafeobj-1.4
folder to your U: drive.
• Open a command prompt
• cd U:\cafeobj-1.4
• and type
• cafeobj
• To install on your own machine copy the
M:\PBrowne\cafeobj-1.4 folder to a memory key and
then to your own machine.
Staring CafeOBJ
• After installing CafeOBJ there are typically four
things that you need to do to work on a file:
1. In a command prompt, start CafeOBJ, type CafeOBJ
2. Set CafeOBJ to point to files in your current working
directory (or folder), type:
cd yourDir
3. Load the file you wish to work on:
in workingFile
4)Open the module you wish to working with:
open ModuleName
Download