Title: Computing Fundamentals 1 Lecture 0
1Computing Fundamentals 1Lecture 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
2Leibniz 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. - The ideas of Leibniz are extensively used in the
course text.
3Computing 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, database,
software engineering, programming, and
algorithms.
4 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.
5Computing Fundamentals 1
- The aim of this module is to provide the student
with the theoretical foundations for other
modules on the programme.
6Computing Fundamentals 1
- Main Reference
- A Logical approach to Discrete Math
- By David Gries and Fred B. Schneider
- Publisher Springer 1 edition (1993)
- ISBN 0387941150
7Computing Fundamentals 1
- Recommended Reading
- Seymour Lipschutz, Essential Computer
Mathematics, Schaum's Outline series, 1987, ISBN
0-07-0379990-4. - Winfred Karl Grassmann and Jean-Paul Tremblay ,
1996, Logic and Discrete Mathematics A Computer
Science Perspective, Prentice Hall 1996, ISBN
0-13-501206-6. - Seymour Lipschutz and Marc Lars Lipson, 1997,
Discrete Mathematics, Schaum's Outline series,
ISBN 0-07-038045-7. Â
8Mathematics 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.
9Formalizing 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.
Dogs Must Be Carried
Shoes Must Be Worn
10Syntax 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.
11Syntax and Semantics
- We need to separate symbols from meaning.
- What do the following symbols mean?
27 128 26 64 25 32 24 16 23 8 22
4 21 2 20 1
102 100 101 10 100 1
12Computing Powers in CafeOBJ
- mod! POWER
- protecting(NAT)
- op __ Nat Nat -gt 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
13Syntax 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.
14Syntax 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.
15Logic
- 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
Schneider1 - 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 Johnsonbaugh2 - 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
Smith3
16Logic and Language
- Logic takes into account syntactically
well-formed 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.
17Logic 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.
18Logic 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 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.
19Mathematical Models
- Mathematical models are more concise, precise,
and rigorous than informal English descriptions. - A mathematical model allows us compute solutions.
- A mathematical model allows us 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
20Mathematical Models
- There are rules that allow is 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 model, reason, prove, and compute - A value is, a computation does. A value has value
type, a computation has computation type.
21Equational logic
- Equational logic (EL) can be used as a tool to
reason about systems. EL is based on equality and
Leibnizs 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. m4 and j2, m6 and j3).
22Equational 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
- m/2 2 (j 1)
- There are now fewer values of m and j that make
these two equations true (m4 and j2).
23The Apple0 problem 1
- mod APPLES0
- Apples
- var M Apples -- Mary's apples
- var J Apples -- Johns apples
- ops 0 1 2 -gt Apples
- ops quo Apples Apples -gt Apples
- eq eq1 M J 2 .
- eq eq2 M quo 2 2 (J 1) .
- The above specification represents the mini-world
of Mary and Johns 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,N 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. Our priority is initially
representation not computation. - See Apples1.mod and Apples2.mod on web page.
24The Apple problem 1
A quotient is the result of a division. Dividing
6 by 3, the quotient is 2
- mod! APPLES1
- Apples
- var M Apples -- Mary's apples
- var J Apples -- Johns apples
- ops 0 1 2 -gt Apples
- op _quo_ Apples Apples -gt Apples assoc
- op __ Apples Apples -gt Apples assoc
- op _-_ Apples Apples -gt 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 - 1) . true
25The Computing the Apple problem
- mod! APPLES2
- pr(INT)
- op solution Int -gt Int
- var M Int -- Mary's apples
- var J Int -- Johns apples
- eq solution(J)
- if (2 J quo 2 2 (J - 1)) then J
- else
- solution(J 1) fi .
- Marys apples not used to compute J.
26Pre-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 post-condition is an assertion
about the state in which it may terminate. A
reasonable precondition for the last program is
that the first guess should be equal or greater
than zero (no negative number of apples)
27More Examples
- In English we wish approximate
- In Math
- Let b approximate (therefore b2 approx. n)
- Precondition 0 lt n
- Postcondition b2 lt n lt (b1)2
- The postcondition that n computes the largest
integer that is at most - These conditions do not tell us how to compute.
- With maths can model, reason, and compute
28More 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 lt N) and
- (N lt (B 1) (B 1)) )
- then B
- else
- approx(B 1, N) fi .
- (see ROOTN1.MOD on web page)
29Systematic Syntactic manipulation
- From emc2 show e/c2m
- Precondition 0ltc2
- e mc2
- ltDivide both sides by c2gt
- e/c2 (mc2)/c2
- ltAssociativity of / and gt
- e/c2 m(c2/c2)
- lt(c2/c2) 1 gt
- e/c2 m1
- ltm1 m gt
- e/c2m
- Transitivity if ab and bc then we can conclude
that ac - Equality is transitive, hence we can conclude
emc2 is equivalent e/c2m
Einstein showed a massenergy equivalence. Matter
can be turned into energy, and energy into
matter. Here we focus on syntactic manipulation
disregarding the meanings of the symbols.
30Mathematical 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) .
31Mathematical 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.
32Equational logic
- 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
33Equational logic
- We should to distinguish between the syntactic
manipulation of formulas and the evaluation and
meaning of those formulas. - See
- http//www.cs.cornell.edu/gries/logic/Equational.h
tml - http//homepages.feis.herts.ac.uk/comqejb/algspec
/node8.html - http//scom.hud.ac.uk/scomtlm/book/node275.html
34Equational logic (Wikipedia)1
- Equational logic (EQ) provides a set of
domain-independent rules. The rules provide a
sound logic for numerical domains and many other
kinds of mathematical structures such as sets.
The reasoning rules for EQ. - Reflexivity. t t. That is, any equation whose
two sides are the same term t is a law2. - Symmetry. From s t infer t s. That is, the
two sides of a law may be interchanged.
Intuitively one attaches no importance to which
side of an equation a term comes from. - Transitivity. A chain s t u of two laws
yields the law s u.3 - Substitution. Given two laws and a variable, each
occurrence of that variable in the first law may
be replaced by one or the other side of the
second law.4
35Programming 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 is mathematical or logical meaning.
36 CafeOBJ
- The CafeOBJ system and documentation can be
downloaded from http//www.ldl.jaist.ac.jp/cafeob
j/ - You should download the latest version.
- The site contains installation instructions for
Linux, Mac OS X, and Windows - There are excellent CafeOBJ notes by Kokichi
Futatsugi at - http//www.jaist.ac.jp/kokichi/class/i613-0712/
- Kazuhiro OGATA has written an excellent set of
notes on functional programming using CafeOBJ - http//www.jaist.ac.jp/ogata/lecture/i217/
- Takahiro Seino has written an excelent
introduction to CafeOBJ as a theorem prover. - http//www.jaist.ac.jp/t-seino/lectures/cafeobj-i
ntro/en/index.html - The CafeOBJ manual is useful but a bit detailed
and technical. User-friendly tutorials will be
provided at the labs.
37Staring 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.bat - 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