Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views45 pages

Functional Logical Programming

The document discusses fundamental concepts of functional logic programming, including programming styles, evaluation of expressions, and the definition of functions. It contrasts imperative programming, which specifies how to solve problems, with declarative programming, which focuses on what needs to be computed. The text also outlines the evolution of programming languages and their characteristics, emphasizing the importance of clarity, simplicity, and cost-effectiveness in programming language design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views45 pages

Functional Logical Programming

The document discusses fundamental concepts of functional logic programming, including programming styles, evaluation of expressions, and the definition of functions. It contrasts imperative programming, which specifies how to solve problems, with declarative programming, which focuses on what needs to be computed. The text also outlines the evolution of programming languages and their characteristics, emphasizing the importance of clarity, simplicity, and cost-effectiveness in programming language design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

FUNCTIONAL LOGIC PROGRAMMING

1. Fundamental Concepts.

C 1.1. Programming styles.

A
P
1.2. Evaluation of expressions.

I 1.3. Definition of functions.

T
U 1. Fundamental Concepts.

L 1.4. Type Discipline.

O 1.5. Data types.

1 1.1 Thematic diagram of chapter 1.

1
FUNCTIONAL LOGICAL PROGRAMMING
Fundamental Concepts.

1. Fundamental concepts.

1.1. Programming styles.

The designs, programming styles, and implementation methods of languages


programming has continuously evolved since it appeared
first high-level languages in the 1950s.

Conventional languages (imperative) are a high-level abstraction of the


structure of the Von Neumann model, for which have been developed and
many of their defects and imperfections stem from this close relationship. In
this model the processors execute instructions in a purely
sequential, another characteristic of this computing model is that it performs a
complete separation between the type of information that memory stores,
dividing them into instructions and data, which has a subsequent impact
about the design of this type of languages. Thus, programming languages
conventional (for example JAVA), regardless of the number of levels
the existing software between them and machine language are essentially a
machine language extension. (Julián, Alpuente; 2007).

A fundamental aspect of programming tasks is, what should


compute? This is the essential question and, in fact, it is what motivates the use of
a computer as a means to solve a specific problem. The
Declarative programming (sometimes called inferential programming) can
understand as a programming style in which the programmer specifies
What? It should be computed instead of How? The computations should be carried out.

(Julián, Alpuente; 2007).

2
FUNCTIONAL LOGIC PROGRAMMING
Fundamental Concepts.

Programming languages can be classified according to their style of


programming that supports, the fundamental programming styles, also
called programming paradigms, are the imperative and the declarative. In this
This unit presents the fundamental concepts of programming styles.
evaluation of expressions, definition of functions, type discipline and types of
data.

How do programming languages emerge?

The first versions of programming languages were developed during


the fifties, for example Ada, C, Pascal; Prolog and Smalltalk date back to the
the 1970s, and C++ and ML from the 1980s. In the 1960s and 1970s,
new languages were usually developed as part of important projects
software development. When the United States Department of Defense
conducted a study to develop the Ada language in the 1970s, found
that more than 500 languages were being used in various projects. The groups
Of the programming languages that have been created since the 1950s to the present, they are:

Machine Language. The language began with combinations of circuits of 0 and


1, it is the only one that processors understand directly. All the
Instructions prepared in any machine language have at least two
parts: The first is the command or operation, which tells the computer which
it is the function that is going to be performed; the second part of the instruction is the

an operand that tells the computer where to find or store the data and
other instructions that will be manipulated.

Assembly Language. It was the grouping of machine language instructions.


through mnemonics (mov, bx, cx) for operations and addresses

3
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

symbolic. One of the first steps to improve the preparation process


The purpose of the programs was to replace the numerical operation codes with symbols.

alphabetical, which are mnemonic codes. (Sanders; 1997).


High-Level Language. They are the most used currently, allowing that the
algorithms are expressed in a level and style of writing that is easily readable and
understandable by other programmers.

Fourth Generation Languages. They are those in which you simply


describe what is desired to be done, through a series of rules, and the program
generate the code automatically.

What are the characteristics of a good programming language?

The characteristics of a good programming language are:

Clarity, in order to easily grasp the structure.

Simplicity, so that when writing it, it is not forgotten.

Flat relationship between the problem and the program.

Simple syntax of the program.

Ease of reading in the written program.

Ease of program debugging.

It supports a suitable programming environment for its use.

Minimum usage cost.

Cost of program execution.

4
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

Cost of the program translation.

Cost of creating a program, testing, and usage.

Cost of maintaining a program.

What relationship exists between programming languages


And the programming styles?

Programming languages, according to processing structures


what they use and how they are programmed, they can be classified into two paradigms
programming styles: imperative languages and declarative languages. This
it means that programming styles determine the use of structures of
processing and the logic of programming languages.

What characterizes the imperative programming style?

The theoretical basis of imperative programming was provided (in England) by Alan
Thuringia in the 1930s. The first computers were built in the years
forty, at that time the use of a computer was very expensive and it was
It's logical that the programming language will have a lot in common with the
architecture of the machine. A computer consists of a control unit and
a memory, that's why a program consisted of instructions to change the
content of the memory; the control unit was responsible for executing them. From
this way the imperative programming style was created. (Fokker; 1996)

Imperative programming languages such as Pascal, Ada, C, C++, Java,


are characterized by the existence of assignments executed consecutively: one

5
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

A program is defined as a sequence of instructions that are executed one after another.
another, changing the values of the variables through assignments. The
Imperative languages are those in which it is specified how it should be done.
solve the problem. They are usually of general applicability. In this style of
the following programming paradigms can be found in programming:

SPAGHETTI: Handling GO TO labels without defined structures.


2. PROGRAMMING TOP_DOWN (PROCEDURAL): Definition of
procedures, functions and mechanisms of parameter passing. 'Decide
which procedures do you want, use the best algorithms that
you can use." Examples of languages: C, Pascal.
3. MODULAR: It allows data concealment. "Decide which modules
do you want to partition the program in such a way that the data remains
hidden in the modules. Examples of languages: Modula, ADA.
4. DATA ABSTRACTION: Allows declaring templates for the visible part
and the details of representation and manipulation of data types
abstract and data encapsulation. "Decide which data types
you want, provide a complete set of operations for each
type. Language example: C++.
5. OBJECT-ORIENTED: This paradigm is supported by the following
properties:
➢ Data abstraction: The ability to examine a problem without
to worry about their internal details. The structures and the items of
data can be used without having to worry about the details
exactly as they were carried out.

➢ Encapsulation: It is the storage of data and methods in


a single entity (store in a black box the information of a
resulting object of abstraction: state and behavior). The

6
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

data is hidden (for users and other objects) and only through
It is possible to access the member functions.
➢ Inheritance. It is thepropertythat allows objects to be built at
starting from the characteristics of other objects. Theclassfather (base)
contains all common features. The derived classes or
Subclasses define particular characteristics of the subclass.
also considering the characteristics of the base class. In the
simple inheritance the derived class inherits only from one class
base; in multiple inheritance the derived class inherits from two or more
base class.
➢ Polymorphism: Property that allows an operation to have different
behavior in different objects. It literally means
quality of having more than one form. In other words, different
objects react to the same message in different ways.
➢ Reuse. Reusing the code through inheritance.
In addition, encapsulation and modularity allow for the use of one and
again the same classes in different applications. Indeed, the
isolation between different classes means that it is possible to add
a new class or a new module (extensibility) without affecting the
rest of the application.

This paradigm is characterized by: 'Decide which classes you want,


provide a complete set of operations for each class;
make explicit the community of classes through the use of inheritance
example: EIFFEL, SMALLTALK. The latter has two important aspects:
It was designed as a total system and not simply as a
notation for developing programs. Object-oriented programming was a
integrated primitive concept.

7
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

This language was developed by Alan Kay in the early years.


seventy. Initially, Smalltalk required the complete environment of
computation of a special computer to be executed. (Pratt,
Terrence W; 1998.

6. CONCURRENT OBJECT-ORIENTED: Handling of concurrency by


means of multiprocessors and distributed systems.

What characterizes the declarative programming style?

Conventional languages have exploited the data-program duality with a


marked bias towards the data: programs are just a type of data
particular represented in the machine's memory; this allows to see the
programs as input data for other programs, for example a
compiler. However, it can be said that declarative languages exploit
the duality in a much broader way. In lambda calculus, a logic
Higher-order equational serves as a formal model for many languages
functionally, a numeric constant appears as a constant function that
takes on that value again. And in many functional languages, in fact, functions and
data have the same appearance: they are all lists. The functions are then
first-class citizens and can be passed as arguments to others
functions and even return as a result of the execution of other functions.
Likewise, in logical languages, data and programs also have the same
representation (everything is structures) and this allows modifications in real time

execution of the program itself that is being executed. That is, the duality not only
it is used not only to execute data as programs but also to modify it
programs as data. (Julián, Alpuente; 2007).

8
FUNCTIONAL LOGICAL PROGRAMMING
Fundamental Concepts.

Declarative languages are those in which it is specified what is the


problem that needs to be solved. They tend to be of specific application, and usually
incorporate general problem-solving methods. Basically,
they can distinguish two types:
• Logical: they are based on predicate logic, and their application
The most immediate is the automatic demonstration of theorems. Example:
PROLOG Language:
It is a programming language for artificial intelligence. It was developed
in 1972 at the University of Marseille. PROLOG stands for "PROgramming"
in LOGic. The main aspect of language is symbolic logic.
Solve problems through a form of symbolic logic known
as predicate calculus, which represents the logical relationships between
elements and attributes. (Sanders;)

• Functional: they are based on the mathematical concept of function, and


its most immediate application is symbolic calculation. Example: Language
LISP:
John McCarthy's Lisp language was the first programming language.
functional and was the only one for many years. Although Lisp is still used,
It is a language that meets the demands. Due to the increasing complexity
from the computer programs, a greater necessity arose
verification of the program by her. Therefore, the typing acquired
great importance. That is why it is not surprising that in the 1980s
a large number of typed functional languages will be created. Some
examples are ML, Scheme (an adaptation of Lisp), Hope, and Miranda.

In the long run, each researcher dedicated themselves to developing their own language. For

stop this uncontrolled growth, a group of notable researchers


he conceived a language that included all the best qualities of the different

9
FUNCTIONAL LOGIC PROGRAMMING
Fundamental Concepts.

languages. This language is called Haskell. The first implementations of


Haskell was created in the early 1990s. This language is
quite ambitious and difficult to implement.
The Gofer language is a language like Haskell, but a little simpler. It is used
for theoretical research and educational goals. Due to its great availability
gains popularity quickly.
The question is whether Gofer will survive. This doesn't matter much, the study of Gofer is
useful because it has a strong relationship with Haskell, which is accepted by many
people in the area of computer science. (Fokker; 1996).
The following figure summarizes some very popular programming languages.
usual, as well as what type they are and the year of their development:

Name Type Year

FORTRAN Imperative 1956

COBOL Imperative 1960

BASIC Imperative 1965

C Imperative 1974

SMALLTALK O. Object 1979

PASCAL Imperative 1971

LISP Functional 1960

PROLOG Logical 1971

JAVA O. Object 1995

Fig. 1.1.1 Comparative table of programming languages.

10
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

1.2. Evaluation of expressions.

How is the value of the expressions determined?

To calculate the value of an expression, the equations of the


program, oriented from left to right. This process will be called reduction.
rewriting, for example the replacement of formal parameters for
current parameters are the result of an adequate substitution process
of variables.

In general, to evaluate an expression, a single rewriting step is not enough.


but it is necessary to reiterate the reduction process until reaching an expression
that can no longer be reduced, which is called the normal form of the expression
initial. The series of steps that leads from an initial expression to its form
Normally it is called reduction sequence. Since each rewriting step
It can be chosen in several ways, usually there are several sequences of
reduction from the same initial expression, for example:

square(2+3)
square 5
5*5
25

Reduced expression (redex)

11
FUNCTIONAL LOGICAL PROGRAMMING
Fundamental Concepts.

square (2+3)
(2+3)*(2+3)
5*(2+3)
5*5
25
Reduced expression (redex)

(Rodríguez; 2004).

What are the most important reduction strategies?

The most important reduction strategies are:

1.Impatient assessment:
It consists of choosing a redex (l') of the expression at each step.
possible internal, that is to say, such that no expression strictly contained
link another redex.

2.Lazy evaluation:
It consists of choosing a redex from the outermost expression at each step.
possible, that is, such that no part of it strictly contains the
be another redex.
(Rodríguez; 2004).

How are expressions executed?

12
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

Any process or instruction in a specific language requires a certain order of


sequence for its execution. Examples: expressions,
loops, subprograms, etc.

To develop the sequence in the arithmetic expressions (e):

1. The order of operator precedence should be followed (according to the ...


programming language that I know is being used.
2. Execute the arithmetic expression from left to right, respecting the
operator hierarchy rules.
3. Associate the operations in parentheses to have a better
control in operations.
4. Solve the arithmetic expression.

Example:

a) Solve the following exercise according to the rules of hierarchy of operations

operators of the C language.

x + y / z * 2 < x raised to the power of y divided by a minus b

1. Associating the expression would result in the following form:

[(x + ((y/z) * 2)) < (((x ↑ y) / a) –b)]

2. The tree diagram would look like this in the


next figure:

13
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

Figure 1.2.1: derivation tree

There are three types of formats for algebraic expressions:

✓ Infix
✓ Prefix: this is further divided into: Cambridge Polish and Polish
Ordinary
✓ Postfix

In infix format, the expression is in normal form, in prefix format the


operators remain on the left side and in the Postfix format the operators

14
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

they are on the right side. Consider the following example for your better
understanding

INFIX

PREFIX

CAMBRIDGE POLISH

ORDINARY POLISH

POSTFIX

1.3. Definition of functions.

What is a function?

The concept of function is a mathematical concept. A function (f) is a


relationshipbetween a given set X (called domain) and another set of
elementsY(calleddomain) so that each elementx from the domain has

15
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

corresponds to a unique element of (x) of the codomain (those that form the range,
also caller range) all the elements of the domain participate
In the relationship. Mathematical functions are equivalent to the common logical process that

is expressed as 'depends on'. Mathematical functions can refer to


Everyday situations, such as: losing weight depends on taking on discipline.
to eat healthily.

What relationship exists between the theory of functions and computation?

The theory of functions as a model of computation dates back to the 1920s and
thirty. The founders include, among others, M. Schönfinkel (in Germany and Russia),
Haskell Curry (in England) and Alonzo Church (in the United States). Since this
focus a function or subprogram consists of a single main program, the
which during execution can call several subprograms, which in turn
they can call other subprograms and so on. It is expected that each
subprogram ends its execution at a certain point and returns control to the program
which temporarily halts it. When the execution of the subroutine is completed,
the execution of the program that called it resumes at the following point of
immediate to the subprogram call.
It was in the early fifties when someone came up with the idea to use this
theory effectively, as the basis of a programming language (LISP). The
Most programming languages have predefined functions.
to carry out various mathematical operations, as well as resources that
They allow users to define other functions for specific purposes.
Naturally, the functions used in programming do not always operate with
mathematical objects; for example, for a word processing application
a formatting function could be designed, its parameters would be a text without

16
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

formatting along with information on how to process it, while your


the result would be a formatted text. (Rodríguez; 2004).

What are the reasons for using functions or subprograms?

There are at least four reasons to use subprograms:

1.Repetition: sometimes a task must be executed several times within


of a program.
2. Universal use: writing a subprogram and making it available to
the whole group can avoid duplicating effort for the same one
procedure.
3. Teamwork: a large part of programming in the 'real world' is
made by programming teams. Instead of making everything
the team worked on the entire program, which is usually divided into subprograms.
4. Modularity: the use of subprograms allows for dividing a large project
in more manageable parts. Modularization allows for focused attention
in the specific task during its development.
(Crawley J. Winston, McArthur William G., Structured Programming Pascal)

What does nested invocation of subprograms mean?

Subprograms can, in turn, invoke other subprograms. For example,


we can have the call pattern suggested by the hierarchy diagram, figure
1.3.1:

17
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

Principal

A B C

D E F G H

Figure 1.3.1. Hierarchy Diagram

This suggests that the main module uses modules A, B, and C to perform
necessary subtasks. In turn, module A invokes modules D and E, and the
module E invokes I. This situation is defined as nested invocation
(invocations within invocations). When module I completes its task, the
control returns to module E; when E ends, control goes back to module A; and
when A ends, control returns to the main module. Therefore, in some
programming languages, like module A uses module E, the declaration
The module E must precede that of module A.

What characterizes recursive subprograms?

The concept of recursion is a formal (mathematical) concept, in which a


expression is defined in terms of itself.

e=f(e)

18
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

A subprogram that calls itself is a recursive subprogram and to this


The process of self-calling is known as recursion.
Recursion is an extremely useful tool in certain areas of science.
computing. In some applications, it represents the simplest method to
understand and solve a problem. Furthermore, it is often easier to test
the correction of a subprogram when it is written recursively.
For example, consider the problem of raising an integer to a power.
Enter. It is desired to calculate a.n Where a and n are integers and n is non-negative.

Suppose that it is decided to write a function with the header


function Power (A, N : integer) : integer;
How is it done?
One possibility is to observe that the following property holds:

An = a. an-1

One method may be the following:

Function Power (A, n: integer) : integer


Begin {Power}
Power := A * Power (A, n–1)
End; {Power}

Although simple, the method captures the spirit of recursion very well.
appropriate

• Restate the problem in terms of a "simpler" form of


same problem.

In this case,n-1 it's simpler thann because the exponent is one less

19
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

What are examples of transcendent subprogramming?

1) Exceptions.
The unforeseen circumstances in the execution of the programs receive the
name of exceptions, for which the systems implement to resolve these
exception handler problems. Thus, exceptions are
exceptional conditions that may occur within the program during its
execution (for example, a division by zero occurs, memory is exhausted
available, etc.) that require special resources for their control.
Exception handling is based on a mechanism whose operation has
three basic stages:

1. An attempt is made to execute a block of code and a decision is made on what to do if it

an exceptional circumstance during its execution.


2. The circumstance occurs: an exception is "thrown" (otherwise the
the program continues its normal course).

3. The execution of the program is redirected to a specific site where the


exception is "captured" and it is decided what to do about it.

But what is this 'throwing' and 'catching' an exception'? In general, the phrase is
It is used with a double meaning: On one hand, it is a jumping mechanism that transfers the
execution from one point (the one that "throws" the exception) to another provided by
in advance for such purpose (the one that "captures" the exception). The latter is given

they call the 'handler' of the exception. In addition to the jump (like ango
at the point of throwing the exception, an object is created, as a

20
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

formatting along with information on how to process it, while your


the result would be a formatted text. (Rodríguez; 2004).

What are the reasons for using functions or subprograms?

There are at least four reasons to use subprograms:

1.Repetition: sometimes a task must be executed several times within


of a program.
2. Universal use: writing a subprogram and making it available to
the whole group can avoid duplicating effort for the same one
procedure.
3. Teamwork: a large part of programming in the 'real world' is
made by programming teams. Instead of making everything
the team worked on the entire program, which is usually divided into subprograms.
4. Modularity: the use of subprograms allows for dividing a large project
in more manageable parts. Modularization allows for focused attention
in the specific task during its development.
(Crawley J. Winston, McArthur William G., Structured Programming Pascal)

What does nested invocation of subprograms mean?

Subprograms can, in turn, invoke other subprograms. For example,


we can have the call pattern suggested by the hierarchy diagram, figure
1.3.1:

17
Functional Logic Programming
1. Fundamental Concepts.

Fig. 1.3.2 Coroutines.

If A calls the subprogram B as a coroutine, B executes for a while and returns the
control A, just like any ordinary subroutine would do. When A passes
again the control to B through a resume (resumes), B is executed again by
a while and the control returns to A, like an ordinary subprogram. For A, by
So, B looks like an ordinary subroutine.

From subprogram B, A looks a lot like an ordinary subprogram. The


The name coroutine comes from this symmetry. Instead of having a relationship
from parent to child or from caller to callee between the two subprograms, both
they seem rather alike, two subprograms that exchange control in a
sense and another as each is executed and where neither of the two controls
clearly to the other. (Pratt, Terrence W., Programming Languages, Page 382).

22
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

c) Tasks.

Consider a subprogram A that is executing in normal form. If A


call the subprogram B, then ordinarily the execution of A is suspended
while B is executed. However, if B is started as a task, then the
execution of A continues while B is executing. Either A or B, or both, can
start additional tasks now, which allows any number of coexistence
parallel execution sequences.
Each task is considered a dependent of the task that initiated it. When
a task is ready to finish, it must wait until all its
dependents have concluded so that I can finish. Thus the division in
multiple execution sequences are reversed as tasks complete,
melting into fewer and fewer sequences until finally only one remains
sequence. (Terrence; 1998)
The majority of the body of a task definition contains statements and
ordinary statements that deal with the processing that the task performs
while working independently of other tasks. The initiation of the
execution of a task can take the form of a subroutine call
ordinary. For example, in many implementations of PL/1, a task B
start by executing the statement:
Call B (parameters) task;
For example: Consider a computer system that controls a set of
user terminals. The main task could be the program that monitors the
condition of all terminals. When a user identifies themselves in a
terminal, this task (monitor), starts a new task (terminal), to control the
instructions with the user of that particular terminal. When the user logs out, the

23
FUNCTIONAL LOGIC PROGRAMMING
Fundamental Concepts.

the terminal task concludes the monitor task, running continuously except in
in the event of a catastrophic system failure. Once a task has started, the
Statements of its body are executed in order, just like for a subprogram.
ordinary. When a task ends, control does not return. Its sequence
The parallel execution individual simply concludes.
A task cannot finish until its dependents have finished and when
Finish, any task that is dependent on it must be notified of this.
so that this task can also be completed. A task that never ends
write in such a way that it contains an infinite iteration that progresses in cycles

continuously, until an error occurs. (Terrence W;1998)


During the concurrent execution of several tasks, each proceeds
asynchronously concerning the others; since each task runs at its own
speed, independently of the others. For two tasks that are
execute asynchronously coordinate their activities, the language must supply
a synchronization medium, so that one task can notify another when
completes the execution of a particular section of its code. The signals that
they send among these allow them to synchronize their activities so that the
second, do not start processing data before the first has finished
read them and input them, and so that the first one does not overwrite the data that the second one

still processing. (Terrence;1998).

d) Interruptions.

The interruption is a common mechanism found in hardware of


computers. If task A wishes to inform task B that an event has occurred
in particular, then task A executes an instruction that causes the execution
the task B is interrupted immediately. Control is transferred to a
subprogram or segment of code whose sole purpose is to handle the
interruption and when it ends it returns execution control to the place in

24
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

where the interruption arose. The traffic light is a data object used to
synchronize tasks. It consists of two parts:

a) An integer counter, whose value is always positive or zero, since it


used to count the number of signals sent but not yet received.

b) A queue of tasks that are waiting for signal sending.

In a binary semaphore, the counter can only have the values zero and one.
a general semaphore, the counter can take any positive integer value.
The message is a transfer of information from one task to another. Provides
a means for each task to synchronize its actions with another task and without
the task remains free to continue executing when not
needs to be synchronized.
The concept is similar to a tube. A send command places a message.
in the tube (or message queue), as a task that is waiting for a message
emit a receive command and accept a message coming from the other
end of the tube. The task that sends this is free to continue executing,
sending more messages and filling the message queue, while the task
the receiver will continue running as long as there are pending messages in
waiting to be processed.

e) Concurrency.

Hardware operations occur in parallel if they occur at the same time.


time. The operations in the source text are concurrent if they can be executed
in parallel. The fundamental concept of concurrent programming is the notion
of process. A process corresponds to a sequential calculation with its own
control monitoring.

25
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

The illusion of concurrent execution on a single processor is achieved by interleaving.


the execution of individual tasks, in such a way that each one executes a
portion of your code, then it is exchanged to be replaced by another task that
executes a portion of its code, and so on. The Principles of
Programming Languages in Parallel or Concurrency are:

1. Definition of variables: variables can be mutable or


definition. Mutable variables are declared in almost all languages
sequential; values can be assigned to the variables and changed
during the execution of the program. Definition variables can be
assign a value only once, as there are no problems with
synchronization.

2. Parallel composition: the execution moves from one statement to the next.

next.

3. Program structure:
They can be transformative when the goal is to transform the data.
input into an appropriate output value.
It can be reactive when the program responds to stimuli.
externals, called events.

4. Communication: they must communicate with each other; as it takes place


typically through shared memory with common data objects
to those who have access to all programs in parallel, or through
messages.
(Terrence;1998)

26
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

1.4. Type discipline.

What is type discipline?

It is the validation of the allowed data types in the defined expressions.


in a programming language, this can take place at runtime
compilation (static type discipline), or at runtime (discipline
dynamics of types.

Static type discipline:

Well-typed programs can be recognized at times of


compilation. A well-typed program can be used without checks.
of type at runtime, being guaranteed that no errors will be made
type errors during computation. A well-typed program can include
polymorphic functions, polymorphism allows a function to be able to
apply to parameters of different types (Rodríguez; 2004).

Based on Aho, Lam, Sethi, and Ullman (2008); to perform the verification of
types, a compiler must assign a type expression to each component of
source program; then, the compiler must determine that these expressions
types conform to a collection of logical rules, known as the
type system for the source language. Type checking has the
potential to catch errors in programs.

27
FUNCTIONAL LOGICAL PROGRAMMING
Fundamental Concepts.

Dynamic typing discipline:

In principle, any verification can be performed dynamically.


(runtime), if the target code carries the type of an element,
together with the value of the element. A strong type system eliminates the
the need for dynamic checking for type errors, as
allows us to determine statically that these errors cannot
it occurs when the target program is executed.

A language implementation is strongly typed if a compiler


ensures that the programs it accepts will run without errors. In addition to its
used for compilation, the ideas of type checking have been utilized
to improve the security of the systems that allow import and execution
of software modules. Programs in Java are compiled into bytecodes
independent of the machine, which include detailed information about the types,
in relation to operations in the bytecodes. The imported code is verified
before allowing it to run, to protect against inadvertent errors
and the malicious behavior.

Type checking can take two forms: synthesis and inference.

Type synthesis constructs the type of an expression based on the types of its
subexpressions. Requires that the names be declared before they are used. The type
of the E1+ E2is defined in terms of the types of E1y E2.

28
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

Type inference determines the type of a language construct from


in the way it is used. For example: Consider that a function which
evaluate if a list is empty; then, from usonull(x), it can be inferred that x
it must be a list; the type of elements of x is not known, all that is known is
that x must be a list of elements of some type that so far
unknown. A common rule for type inference has the following form:

iff(x) is an expression,
then for certain α and β,
f has type α → β and x has type α.

Where α and β are variable types in the


type expressions.

1.5. Data types

What is an object?

An object is a unit that contains data and the functions that operate on it.
those data. The data is called data members and the functions, members.
function (also member functions).

The data and functions are encapsulated in a single entity. The data is
hidden and can only be accessed through member functions. The
the terms data encapsulation and data hiding are
key terms used in programming languages.

29
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

Objects support a series of specific characteristics of themselves:

• They are grouped into types called classes;


• They contain internal data that define their current state;
• They support data hiding;
• They can inherit properties from other objects;
• They can communicate with other objects by sending or passing.
messages
• They have methods that define their behavior.

What is a variable?

A variable is actually just a name to identify one (or several)


memory positions where the program stores the different values of a
same entity. A program must DEFINE all the variables it will use,
before starting to use them, in order to indicate to the compiler what type they will be, and

Therefore, how much memory should be allocated to house each of them.

Depending on where the variables are defined, they can be used in the
totality of the program. The area of a program in which a variable is active
It is usually referred to as scope. The scope of a
the variable extends to the limits of the definition of its block.

The basic types of variables are:

• Local variables;
• Global variables;
• Dynamic variables;

30
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

• Object field variable.

What is a constant?

Those values that, once the program is compiled, cannot be changed,


such as the literal values that are used in the initializations of the
variables (1000, 2, 'a', '\n', etc.) are usually called constants.

As these constants are stored in memory in the way that


The compiler tends to be more efficient, certain effects often appear.
secondary, sometimes disconcerting.

In C++ there are four types of constants:

• Literal constants,
• Defined constants (symbolic),
• Declared constants,
• Enumerated constants.

Literal constants or constants, in general, are also classified into four.


groups each of which can be of any of the following types:

• Integer constants,
• Constant characters,
• Floating point constants (real numbers),
• String constants.

31
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

How are elemental data specified?

An elemental data object contains a single data value. A class of these


data objects in which various operations are defined is known as a
elemental data type.

The basic elements of a data type specification are:

1. The attributes that distinguish data objects of that type,


2. The values that data objects of that type can have, and
3. The operations that define the possible manipulations of objects
data of that type

Although each programming language tends to have a somewhat different set


elementary data types usually include integer, real, character types,
boolean, enumeration, and pointer, although the exact specification may
to differ significantly among the different languages. (Terrence; 1998).

How are elemental data implemented?

The implementation of a fundamental data type consists of a


representation of storage for data objects and values of that type, and
a set of algorithms or procedures that define operations of the type
in terms of manipulations of the storage representation.

32
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

How are the types of elementary data classified?

Enteros.

Probably the most familiar data type is integer. The integers are
suitable for applications that work with numerical data. The types
Integers are stored internally in 2 bytes (or 16 bits) of memory.

Floating Coma.

Floating point data types represent real numbers that


contain a coma (a point decimal tal how 3.14159.

Turbo C++ supports three floating-point formats. The float type requires 4 bytes of
memory, double requires 8 bytes and long double requires 10 bytes.

The presence of floating-point arithmetic, both in large computers


as small ones, it is of great convenience for many users. In addition to
a greater range of values, the flexibility of locating the binary point is a
important aspect that should be taken into account. The user (programmer) does not
you have to worry about manipulating scale factors, aligning binary points,
etc. It can be used at the programming level fractions and mixed numbers with
as easy as integers.

The IEEE Computer Society has developed a standard for binary arithmetic of
floating point. The main reason for its establishment arose from the interest in facilitating that
the programs for numerical application should be interchangeable among different
computer systems and promote the development of high-performance numerical software

33
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

quality. This is important, especially in the environment of the


microprocessors and small machines, where it is unlikely that the
individual manufacturers develop extensive general-use numerical routines.

The standard covers three aspects: the format of each type of data, the operations
arithmetic and handling exceptions when errors occur (spill)
insufficiency, division by 0, etc.).

The standard specifies the action that should be taken when operations
produce results that are outside the range of the normalized numbers
floating point. In general, these exceptions should trigger a trap
(software interruption) whose action is under the user's control. They must
five exceptions are detected: invalid operation, division by 0, excess,
insufficiency and inaccuracy.

Characters.
C++ processes character data (such as text) using the char data type.
union with the array structure can be used to store strings
characters (groups of characters).
Internally, characters are stored as numbers. The letter A, for
for example, it is stored internally as the number 65, the letter B is 66, etc. The
char type represents values in the range -128 .. +127 and is associated with the code
ASCII.
Since the char type stores values in the range of -128 .. +127, C++
it provides the unsigned char type to represent values from 0 to 255.
Since characters are stored internally as numbers, they can be
perform arithmetic operations with char type data.
A wide variety of operations on string data is provided.
characters. Some of the most important are:

34
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

• Concatenation
• Relational operations on strings
• Selection of substrings using position subindices
• Input-output formatting
• Selection of substrings using pattern matching

How are the types of data structures characterized?

A data structure is a data object that contains other data objects.


like its elements or components.
The most important types of structured data are arrays, records,
chains, stacks, lists, pointers, sets, and files.
The types of data structures involve the same specification points.
of type, type implementations, and type declarations and checks that are
they are presented for the basic types of data, but once again, the problems
are more complex. Two aspects in particular are important. First, the
Specification and implementation of structural information becomes modular.
Second, many operations on data structures bring up
storage management issues that are not present for objects.

The main attributes for specifying data structures include:


Number of components.
2. Type of each component.
3. Names that should be used to select components.
4. Maximum number of components.
5. Organization of the components.

35
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

The specification of the domain and scope of operations on types of


data structures can be given largely in the same way as for
elementary types. Certain new classes of operations are of particular
importance:

1. Component selection operations.


2. Operations with complete data structures.
3. Insertion/deletion of components.
4. Creation/destruction of data structures.

The storage representation for a data structure includes


storage for the components of the structure and an operational descriptor
that keeps some of the attributes of the structure.
Vectors and arrays are the most common type of data structure in
programming languages.

How are the types of data structures classified?

Vectors and arrays:


A vector is a data structure made up of a fixed number of components.
from a vector is selected by giving its subscript, an integer that indicates the position
of the component in the series.
A vector is also referred to as a one-dimensional array or linear array.
A two-dimensional array, or matrix, has its components organized in a
rectangular grid of rows and columns. A subscript is needed in both rows.
like a column one to select a component of a matrix. The

36
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

multidimensional arrays of three or more dimensions are defined as


similar.
The attributes of a vector are:
Number of components.
2. Data type of each component.
3. Subscript to be used to select each component.

Records.
A data structure composed of a fixed number of components of
different types are commonly known as a record.
Both records and vectors are forms of linear data structures.
of fixed length, but the records differ in two aspects:
1. The components of the records can be heterogeneous, of
mixed data types, instead of homogeneous ones.
2. The register components are designated with symbolic names.
(identifiers) instead of indexing with subindices.

Lists.
A data structure composed of an ordered series of structures.
data is commonly known as a list.
Lists are similar to vectors in that they consist of a series
ordered collections of objects. However, lists differ from arrays in several
important aspects:

Lists are rarely of fixed length. They are often used for
represent arbitrary data structures, and typically increase and
they reduce during the execution of the program.
2. Lists are not usually homogeneous. The data type of each
A member of a list may differ from its neighbor.

37
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

3. Languages that use lists typically declare this data as


implicitly, without explicit attributes for list members.

In certain languages, variations of the typical structure are presented.


lists:

Stacks and Queues.

A stack is a list where the selection, insertion, and deletion of components


are registered at one end. A queue is a list in which the selection and
removal of components is restricted to one end and insertion is
restricted to the other end.

Trees.
A list where the components can be lists and also data objects
elemental is known as a tree, as long as each list is just a
component of at most another list.

Directed graphs.
A data structure in which components can be linked to each other
Using arbitrary linking patterns is known as a directed graph.

String.
A character string is a data object composed of a series of
characters. A string data type is important in almost all
languages, partly due to the use of character data representations
for entry and exit.

It is possible to identify at least three distinct treatments of data types.


character strings:

38
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

Declared fixed length.


Variable length up to a declared limit.
3. Unlimited length.

Pointers
Usually, instead of including various types of linked data structures of
variable size in a programming language, facilities are provided for
allow the construction of any structure using pointers to link
between themselves the component data objects as desired.

A pointer data type defines a class of data objects whose values


they are the locations of other data objects. A single data object of type
a pointer could be treated in two ways:

Pointers can only refer to objects of a single type.


type.

2. Pointers can reference data objects of any type.


type.

Sets.

A set is a data object that contains an unordered collection of


distinct values. In contrast, a list is a collection of values, some of
which can be repeated. The basic operations on sets are:

1. Belonging.
2. Insertion and deletion of individual values.
3. Union, intersection, and difference of sets.

39
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

In programming languages, the term set is sometimes applied to a


data structure that represents an ordered set. An ordered set
it is actually a list from which duplicate values have been removed; it does not require
special consideration.

Files.
A file is a data structure with two special properties:

1. It is usually represented on a storage device.


secondary, like a disc or a tape, and can therefore be much more
larger than most data structures of other types.
2. Its lifespan can cover a time interval greater than the
of the program that creates it.

Sequential files are the most common type of files, but


many languages also provide shortcut files and files
indexed sequential. Two general uses for the files are contemplated:
for input and output of data to an external operating environment and how
temporary removable storage for data when there is not enough available
high-speed memory. The components of a file are often referred to as
records.

What characterizes abstract data types?

40
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

For primitive types like real and integer, a language provides a way to
declare variables of that type and a set of operations on reals and
integers. Thus, the storage representation of reals and integers is
effectively encapsulates; that is to say, it remains hidden from the programmer.

The programmer can use real and integer data objects without knowing, or without that
it matters, exactly how they are represented in storage. Everything that
the programmer sees is the name of the type and the list of available operations for
manipulate data objects of that type.

With the purpose of extending this concept to the data defined by the
programmer, an abstract data type is defined as:

translatedText
A set of data objects, usually using one or more
type definitions
2. A set of abstract operations on those data objects,
3. The encapsulation of the whole in such a way that the user of the new
type cannot manipulate data objects of the type except through the
defined operations.

Data abstraction is the design of abstract data objects and


operations on those objects is a fundamental part of programming,
as has already been stated

In a programming language that provides little direct support for


data abstraction beyond the ordinary mechanism of subprograms, the
a programmer can project and use their own data types in any way
abstract, but the concept is not present in the language itself. In this case,
the programmer must use "coding conventions" to organize their

41
FUNCTIONAL LOGICAL PROGRAMMING
Fundamental Concepts.

program in such a way that the effect of an abstract data type is achieved. Without
embargo, without support from the language for data type definitions
abstract types, encapsulation of a new type is not possible. Therefore, if it is
they violate coding conventions, whether intentionally or not, the
The language implementation cannot detect the violation. These types of data
abstracts created by the programmer often appear as libraries
special features of subprograms in languages such as C, FORTRAN, Pascal, Ada and
C++ is among the few widely used languages with features
of data abstraction.

42
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

ACTIVITY 1.
QUESTIONNAIRE.

Answer the following questionnaire, JUSTIFY EACH ANSWER.

1. Work as a team to outline a development focused on the


artificial intelligence.
2. What is your programming style in your development? Justify.
3. What are the characteristics of the problem that justify the style of
selected programming?
4. What is the programming paradigm of your development? Justify.
5. What characterizes the programming paradigm that you use in the
development?
6. What are the characteristics of the programming language that you use?
7. What entities do you use in development?
8. How do you evaluate the expressions you use in your development?
9. What types of data are defined in the programming language used in
the development?
10. According to the type of data, what operations are defined in the language of
programming used?
11. How are integer data types implemented in the language of
programming used in development?
12. What type discipline does the programming language use?
development?
13. What are the characteristics of your program's subprogramming?
14. What types of parameter passing does the programming language support?
What do you use in the development?

43
FUNCTIONAL LOGICAL PROGRAMMING
1. Fundamental Concepts.

15. What software development methodology do you use, according to the


What programming paradigm is used?
16. What characterizes Grady Booch's Object-Oriented methodology?
17. What support does its development offer that facilitates debugging and updating?
of the same?

ACTIVITY 2.
Conceptual Map.

Develop a conceptual map of the theme of specific competency 1.

BIBLIOGRAFÍA

Julián Iranzo Pascual, Alpuente Frasnedo María (2007).-“Programming


Logic. Theory and practice. Pearson Education, S.A. Madrid.
Aho Alfred V., Lam Monica S., Sethi Ravi, Ullman Jeffrey D. (2008).-
Compilers. Principles, techniques, and tools. Pearson Addison
Wesley, Second Edition. Mexico.
Fokker Jeroen (1996).-"Functional Programming". Department of
Computer Science. University of Utrecht.
Pratt, Terrence W., Zelkowitz, Marvin V (1998).-"Languages of
programming, Prentice Hall Publishing, Third edition.
Rodríguez Artalejo Mario (2004).- Complutense University of Madrid.

44
FUNCTIONAL LOGIC PROGRAMMING
1. Fundamental Concepts.

Sanders Donald H. (1997) - 'Computing Present and Future'. Mc Graw Hill


third edition.

45

You might also like