Prolog logic programming. How to configure the SPG761 corrector to be able to receive heat supply parameters and archive data on a PC via the APS79 adapter


1. Commercial or technological accounting.

2. Specifications for the design of UUTE (if available).

3. Type of heating system.

4. Heat supply system parameters (Gmax; Gmin; Pmax; Pmin; Tmax; Tmin for each pipeline; pipeline diameters; coolant).

5. Allowable pressure losses when installing flow meters.

6. Installation location, environmental conditions.

7. Dispatching volume.

close

What information is needed to diagnose UTE?

1. A database of setting parameters obtained directly from the heat meter using the Configurator or Database programs.

2. Heat meter archives.

3. Automation scheme for UUTE.

4. Diagram of external connections of UUTE.

5. Copies of passports of flow, temperature, pressure sensors.

6. Specifications for the design of UUTE.

close

When using the ADS91 drive and the APS78 adapter purchased with the ADS90, it is not possible to read data from the SPT943.1

The APS78 adapter purchased together with the ADS90 cannot be used with the ADS91 drive. On the APS78 case compatible with ADS91 there is an inscription: “for work with ADS91”

close

It is not possible to obtain archived data from SPT941.20 using the ADS90 drive and the APS78 adapter.

Due to the discontinuation of ADS90 drives from production, drive support for new NPF LOGIC devices, in particular SPT941.20 and SPT943.1 with software versions 2.0.0.3.00 and higher, is not provided. The functions of reading data from the above devices are fully implemented in the new product of NPF LOGIKA - the ADS91 drive.

close

How can you organize data transfer on the Internet from heat meters SPT 943.2; SPT 961 and SPG 763 corrector with access to the Ethernet interface via the ARP-TCP/IP protocol?

To organize the transmission on the Internet of data received from energy metering devices in data collection systems, dispatching and monitoring of energy consumption and production facilities, ADS 99 adapters are designed, which ensure the integration of all modifications of heat meters SPT941, SPT943, SPT961, correctors SPG741, SPG742, SPG761, SPG762 , SPG763 and SPE542 adders with output to the Ethernet interface via the ARP-TCP/IP protocol.

close

How can you organize data transfer on the Internet from heat meters SPT 941.10; SPT 961.2 and LNG corrector 742 located at a distance of 160/120 m from each other via the PPP-TCP/IP protocol?

To organize the transmission on the Internet of data received from energy metering devices in data collection systems, dispatching and monitoring of energy consumption and production facilities, the ADS 98 adapter is designed, which ensures the integration of all modifications of heat meters SPT941, SPT943, SPT944, SPT961, SPT962, correctors SPG741, SPG742 , SPG761, SPG762, SPG763 and SPE542 adders with output to the RS232 interface via the PPP-TCP/IP protocol.

close

Is it possible to connect the SPT944 heat meter directly to a PC via the RS-232 interface, or is it necessary to use an adapter?

To directly connect single devices, for example SPT944, to the COM port of a computer via the RS-232 interface, use the APS77 adapter, which provides galvanic separation of the device and the computer. The adapter is installed in close proximity to the device. Adapterless connection to a computer COM port via the RS-232 interface is allowed only in laboratory conditions.

The need for this may arise when performing the procedure for updating the resident software device, or when performing preload database of the device before installing it at the place of permanent operation. If the computer is located at a significant distance from the device (hundreds of meters), it is advisable to install the second APS77 adapter in close proximity to the computer, ensuring its protection from industrial interference.

close

How to configure the SPG761 corrector to be able to receive heat supply parameters and archive data on a PC via the APS79 adapter?

The APS79 adapter is designed to work with a network of devices and allows you to connect a computer via the RS485 interface to a network consisting of SPT961, SPT961M, SPT962, SPG761, SPG762, SPG763 devices. Devices in the network are connected two-wire line communication, which at the hardware level complies with the RS485 standard, and in its logical organization is a bus with token access. The network configuration can be bus, star, or tree.

The network exchange speed is from 300 to 4800 bits per second. The total length of all segments can reach 10 km. The network provides information exchange between 30 subscribers. The adapter is programmable and allows you to transfer measured values ​​of individual parameters, for example, values ​​of temperature and cold water pressure, from one device to another within a network according to a given list. Exchange with devices via the APS79 adapter is supported by the SPSet®, PROLOG and OPC server "LOGIC" programs.

To connect a single device, just enter following settings: Specification of external LNG equipment 761.2 parameter 003=1050000025. Specification of external equipment APS 79 parameter 003=1050001025.

close

How to transfer archived data read by the ADS 90 drive to a PC that does not have a COM port?

1. Purchase a USB-RS232 interface converter.

2. Connect the USB-RS232 interface converter to the PC.

3. Install drivers for the USB-RS232 interface converter.

4. Make sure that the USB-RS232 interface converter is recognized by the computer as a COM port. Remember the COM port number.

5. Open the Prolog program (if the program is not installed, install it using the CD or the website). In the program settings “Service-Settings-Communication” set the COM port number.

6. To download data from the ADS90 drive, you need to connect it using the K228 connector to the USB-RS232 interface converter. Using the “Communication-Poll ADS90” command, new data will be loaded into the program archive.

close

At what maximum distance from the LNG corrector 761.2 and the heat meter SPT 941.20 are the ADS 98 and ADS 99 adapters installed?

The length of the Ethernet trunk should not exceed 200 m. The length of the RS485 and M4 trunks is determined by the number of subscribers connected to it, cable parameters, data transfer speed and ranges from 0.8 to 12 km. For local data exchange, a device equipped with an RS232C port can act as the leading subscriber; in this case, the length of the communication line should not exceed 100 m.

Detailed data on the length of the trunks are given in the documents "Trunk Protocol SPSet. Programmer's Guide" and "Trunk Protocol M4. Programmer's Guide", posted on www.logika.spb.ru.

close

How to automate the accounting of electricity consumption broken down by workshop and shift?

For automated accounting (commercial and technical) electrical energy and power at industrial and energy enterprises, the SPE542 Adder is designed. The adder is designed to work with electrical energy meters equipped with devices for converting the measured energy value into a pulse number signal (pulse sensors). The adder allows you to organize energy consumption metering under two-part tariffs, when not only electricity is measured, but also power during peak hours, as well as energy consumption metering under tariffs differentiated by day zones (for example, as is customary at the Federal Wholesale market of electric energy and power).

The device provides the ability to connect pulse sensors ADS68, E440, E440.01, E870, MX-1, Zh7AP1 and others. The adder allows servicing up to 128 channels of reference counters, which can be combined into groups with a total number of up to 32. Sixteen pulse channels can be connected directly to the SPE542, the rest through spatially separated ADS84 adapters connected to the SPE542 via the RS-485 interface. Up to 16 pulse channels can be connected to each ADS84 adapter. Instead of pulse sensors, telesignaling sensors can be connected to the corresponding inputs of the adder and ADS84 adapters. The adder and each ADS84 adapter can generate up to 4 output two-position signals intended for signaling and load control.

close

The parameters and quantities of which gas-liquid unstable hydrocarbon mixtures can be controlled and taken into account using LNG 763.2?

Correctors LNG 763.2 are used as part of measuring systems(complexes) for accounting:

  • liquid stable hydrocarbon gas condensates, including pentane and heavier hydrocarbons (C5H12+); liquid homogeneous hydrocarbon mixtures (products of processing of gas condensates and wide fractions of light hydrocarbons);
  • gas-liquid unstable gas condensates, including deethanized (without CH4 and C2H6 or with their total content not exceeding 1% of the mass) and debutanized (without CH4, C2H6, C3H8, C4H10), including, in addition to C5H12+, light hydrocarbons up to butane inclusive;
  • non-hydrocarbon components N2, CO2, H2S, RSH, CH3OH, mechanical impurities and H2O, gas-liquid unstable (raw) hydrocarbon mixtures with a total content of CH4 and C2H6 of no more than 3% by weight;
  • non-hydrocarbon components N2, CO2, H2S, RSH, CH3OH, mechanical impurities and H2O (broad fractions of light hydrocarbons);
  • gas mixtures containing hydrocarbon components up to heptane inclusive and non-hydrocarbon components N2, CO2, H2S, H2O, O2 (wet petroleum gas).
close

Why is he amazing? I know a couple of dozen languages ​​and it’s not a problem for me to learn another new one, I just don’t see the need anymore.

The prologue is unique. It is the only language that represents the declarative programming paradigm; it is a language that has hundreds of different implementations, but they are still called Prolog, adding only prefixes and suffixes to the name; it is a living language in which no significant changes have occurred for more than 20 years; This is probably the only programming language that is so popular that has no application in real programming. Why Prolog?

The prologue is unique in nature, it appeared due to a happy coincidence (the mysterious structure of the world). Once upon a time in the 60s, the theory of automatic theorem proving was developing very rapidly, and Robinson proposed a resolution algorithm that made it possible to prove any true theorem (derive it from axioms) in a finite time (for which it is not known). As it turned out later, this best solution general problem, it is impossible to prove the theorem in limited number operations. In simple words, the algorithm is a breadth-first traversal of a (generally infinite) graph. Naturally, the predictability of the algorithm’s operation is practically equal to 0; accordingly, this is absolutely not suitable for a Programming Language. And at that moment Kalmarow found a brilliant narrowing of the problem, thanks to which the proof of some theorems looked like a procedural execution of the program. It is worth noting that the class of provable theorems is quite wide and very well applicable to the class of programmable problems. That's how Prolog came into being in 1972.

In this article I will try to talk about Prolog as a tool for solving general logical problems. This topic will be of interest to those who already know Prolog syntax and want to understand it from the inside, as well as to those who absolutely do not know the syntax of the language, but want to understand its “zest” without spending extra time learning syntactic structures.


The main feature of Prolog is that it can be easy to read, but very difficult to write, which is fundamentally different from all mainstream languages, which say that writing has become even easier, one more step and you can write on a tablet, dragging working modules like friends on Google+ , we all know that the quality of the code itself suffers greatly from this. It seems that every line is clear, but how the system works is beyond understanding even for developers, as they say in Hindustan. It seems to me that in all books on teaching Prolog, they make the same mistake, starting a story about facts, relationships, queries, and a person develops an attitude towards the language as an Expert System or Database. It’s much more important to learn how to read programs correctly and read dozens of them that way :)

How to read prologue programs correctly

Reading programs is very easy, since there is very little in the language special characters and keywords and they are easily translated into natural language. The main mistake of a programmer is that he wants to immediately imagine how the program works, and not read what the program describes, so it seems to me to train an unfogged brain ordinary person, much easier than a programmer.
Concepts
There are 2 concepts in the language predicates(conditions) and objects(they are also variables and terms). Predicates express some condition, for example, an object is green or a number is prime, it is natural that the conditions have input parameters. For example green_object(Object), prime_number(Number). The number of parameters in a predicate determines the arity of the predicate. Objects- are terms, constants and variables. Constants- these are numbers and strings, variables- express an unknown object, possibly the one being sought, and are designated as lines with big letters. Let's leave the terms for now and consider the simplest program.
Program
A program is a set of rules of the form If condition1 and condition2 and... then the condition is true. Formally, these rules are combined through AND, but it is impossible to obtain a contradiction, since there is no logical negation in Prolog, and only one predicate (condition) can be present in the connective That.

A:- B_1, B_2. The % rule reads as: If B_1 and B_2, then A
odd_prime(Number) :- prime(Number), odd(Number).
% If "Number" is prime and odd, then "Number" is odd_prime

As you can see, the variable name has a scope - this is the rule. Mathematically correct, the rule sounds: for any variable - “Number”, if it is prime and odd, then it is prime_odd. Similarly, it can be rephrased as follows: If there is a “Number” that is odd and prime, then it is odd_prime. Therefore, the variable name is very important! If on the left side (before:-) we replace Number with Number2, then the rule will change its meaning: For any Number2 and Number, if Number is prime and odd, then Number2 is simple odd. It turns out that all numbers are prime_odd! This is the most common error in Prolog.

A:- B_1, B_2. The % rule reads as: If B_1 and B_2, then A odd_prime(Number) :- prime(Number), odd(Number). % If "Number" is prime and odd, then "Number" is odd_prime

Example - perfect numbers
perfect_number(N) :- number(N), sum_of_divisors_without_number(N, SumofDivisors), equal to(Sum_ofDivisors, H). perfect_number(1). equals(Object, Object). sum_of_divisors_without_number(1, 1). sum_of_divisors_without_number(Number, Sum) :- number_previous(Number, Previous), sum_of_divisors_of_number_to_number(Number, Sum, Previous). sum_of_number_divisors_to_number(Number, 1, 1). sum_of_number_divisors_to_number(Number, Sum, Divisor) :- divided_by(Number, Divisor), number_previous(Divisor, Previous), sum_of_number_divisors_to_number(Number, SumPrev, Previous), add(SumPrev, Divisor, Sum). sum_of_number_divisors_to_number(Number, Sum, Divisor) :- not_divisible_by(Number, Divisor), number_previous(Divisor, Previous), sum_of_number_divisors_to_number(Number, Sum, Previous).

First, let’s formally read what the rules mean:

  1. If “H” is a number and for “H” and “SumDivisors” the condition sum_divisors_without_number is met, in other words, SumDivisors is the sum of the divisors of the number “H”, and “H” is equal to “SumDivisors”, then “H” is a perfect number.
  2. 1 is a perfect number. Rules may not have conditions, in which case they are called facts.
  3. Every object "O" is equal to "O". In principle, there is a standard predicate "=", but you can completely replace it with your own.
  4. The fact that the sum_of_divisors_without_the number 1 is equal to 1.
  5. If the sum of the divisors "Number" up to the previous number "Number" is equal to "Sum", then this is the sum_of_divisors_without_number. This way it is expressed that the sum of the divisors of X is less than or equal to Y, since X is divisible by X, so we take Y = X - 1.
  6. Next, 3 predicates determine the sum of divisors number less than or equal to Y (Divisor), 1st case Y is equal to 1, 2nd case Number is divisible by Y, then sum_of_divisors(X, Y) = sum_of_divisors(X, Y-1) + Y , and 3rd case The number is not divisible by Y, then sum_of_divisors(X, Y) = sum_of_divisors(X, Y-1).
A program is like a set of definitions
There is a second way to read these rules, less mathematical and more natural, based on “definitions”. You can notice that in Prolog all the rules on the left (in the then part) contain only one condition, which is essentially a “definition” of this condition.
For example, the 1st rule is the definition of perfect numbers. "H" is a perfect number when "H" is a number and the sum of the divisors of "H" is equal to "H". Identical predicates are grouped by name using the “or” condition. That is, you can add to the definition: “H” is a perfect number when.., or when “H” is 1.

This reading method is widely used, as it allows predicates to be combined into homogeneous groups and helps to understand in what order the interpreter unwinds predicates in order to
check the truth of some statement. For example, it is obvious that if a predicate does not have a single definition, then it is impossible to prove the truth of a statement with it. In example No. 1, the predicate “divided by” has no definition.

An interesting fact is that in Prolog there are no loops, no variable assignments, no type declarations, and if we also remember about terms and clipping, the language becomes algorithmically complete.

Thermal Baths
Thermal Baths have a recursive definition as a named collection of objects. Term = "name"(object, object, ...), example person("Name", "Surname"), "+"(1, 2), person(address("Some address"), surname("Last name"), phone("Phone")). If we consider a term as a mathematical concept, then the term is a function, or more precisely a functor, that is, “+”(1, 2) means that there is an object that is equal to 1+2. This does not mean at all that 1+2 = 3; in Prolog this expression is not true, just as in the group of remainders modulo 2, there 3 does not exist at all. Again, from a mathematical point of view, Variables are connected by the word For All, and if in a statement the word exists, then a term (functor) is used for this purpose. For any number there is a factorial number: factorial(X, fact(X)).

From a programming point of view, terms can be explained much more simply: a term is an object with a set of attributes, the attributes can be other terms or constants or variables (that is, undefined). The main difference is that all objects in Prolog are immutable, that is, you cannot change the attributes in them, but there is a special state - a variable.

Example - Integer Arithmetic
nat(0). nat(number(Number)) :- nat(Number). plus(0, Number, Number). plus(number(N1), N2, number(Res)) :- plus(N1, N2, Res). multiply(0, Number, 0). multiply(number(Ch1), Ch2, Res2) :- multiply(Ch1, Ch2, Res), plus(Res, Ch2, Res2).
  1. Definition of the property nat (natural number). 0 is a natural number, if the Number is natural, then there is an object number(Number), which is also a natural number. Mathematically, the term “number” expresses the function +1; from a programming point of view, “number” is a recursive data structure, here are its elements: number(0), number(number(0)), number(number(number(0))).
  2. The plus ratio is 0 + Number = Number. If Ch1 + Ch2 = Res, then (Ch1+1) + Ch2 = (Res+1).
  3. The ratio to multiply is 0 * Number = 0. If Ch1 * Ch2 = Res and Res + Ch2 = Res2, then (Ch1+1) * Ch2 = Res2.
Obviously these statements are true for ordinary arithmetic, but then why didn’t we include the same obvious ones as Number + 0 = Number. The answer is simple: redundancy is very bad by any definition. Yes, this can help calculations, a kind of premature optimization, but the side effects can be contradictions in definitions, ambiguous output of a statement, and looping of the interpreter.

How Prolog understands predicates and how it proves statements

Of course, reading programs helps to get a feel for the Prolog style, but it does not make it clear why and how these definitions can be used. A full-fledged program, the examples given above cannot be named because there is not enough entry point. The entry point to Prolog is a query, analogous to a query to the database SQL data or similar call main function in functional programming. Examples of queries: nat(Number) - find a natural number, plus(0, 0, Result) - find the result of adding 0 and 0 in the Result variable, nat(0) - check whether 0 is a natural number, etc.

Of course, the results of queries are not difficult to predict from logical considerations, but it is extremely important to understand how the program obtained them. After all, Prolog is not a black box, but a programming language, and unlike a database, where a SQL plan is built and the query can be executed differently on different Databases, Prolog has a very specific execution order. The fact is that in the Database we completely know what answer we want to get based on the data in the table, unfortunately, looking at the Prolog of the program it is quite difficult to say which statements are logically deducible, so it is much easier to understand how the Prolog interpreter works.

Let's look at an example request plus(0, 0, Result) :
1. Find a match (a kind of pattern-matching, resolution) of this request with the left part is one of the rules. For this query, plus(0, Number, Number). Let's correlate all the query arguments with the rule one by one and get: 0 = 0, 0 = Number, Result = Number. These equations involve 2 variables (Number and Result), solving them we get that Number = Result = 0. Since of this rule no conditions, we received a response to asked question. Answer: yes and Result = 0.

Request nat(Number) :
1. We find the 1st match with the rule, the nat(0) rule, by solving the equations by correspondence, in other words, by finding the resolution, we get Number = 0. Answer: yes and Number = 0.

Request plus(Result, 0, number(0)) :
1. Find a resolution with the rule plus(0, Number, Number): Result = 0, 0 = Number, number(0) = Number, but (!) Number = 0 = number(0) - not possible since 0 is the same number (0). Therefore, we are looking for a resolution with the following rule.
2. Find the resolution with the rule plus(number(N1), Ch2, number(Res)), we get number(N1) = Result, Ch2 = 0, number(Res) = number(0), hence Res = 0. This rules, there are conditions that we must check, taking into account the results of the resolution (variable values), plus(Ch1, Ch2, Res) -> plus(Ch1, 0, 0). We remember the value of the variables on the stack and form new request plus(H1, 0, 0)
3*. Solving the query plus(Х1, 0, 0) we find a resolution with plus(0, Number, Number) and get Х1 = 0 and Number = 0.
4. We return along the stack to the previous variables Result = number(N1) = number(0). The answer was found number(0). Accordingly, the prolog machine has now solved the equation X + 0 = 1.

Competent drafting rules in the Prolog language, a very complex thing, but if you compose them compactly, you can get not only direct answers and solutions, but also reverse ones.

Example request plus(Number, Number, Number) : the answer is yes, Number = 0.

Example request plus(0, 0, 0) : the answer is no, on the first attempt all resolutions are not fulfilled.

Example request plus(Number, Number, number(Number)) : The answer is yes, Number = 1. Solving the equation X + X = X + 1.

Try outputting for multiply(Number, number(0), number(0)), this will require pushing variables onto the stack 2 times and calculating a new query. The essence of the Prolog machine is that you can reject the 1st result, then Prolog will return to previous state and continue the calculation. For example request nat(Number) , will first apply the 1st rule and output 0, and then apply the 2nd rule + 1st rule and output the number (0), you can repeat and get an infinite sequence of all natural numbers. Another example, request plus(Number, number(0), Number2) , will produce a sequence of all pairs of solutions to the equation X + 1 = Y.

Conclusion

Unfortunately, the reasonable size of the topic did not allow me to get close to the main topic, namely solving complex logical problems in Prolog, without having a strategy for solving them. Large chunks of Prolog code can scare off not only beginners, but even experienced programmers. The purpose of this article is to show that Prolog programs can in a simple way read on natural language , and executed by a simple interpreter.
The main feature of Prolog is that it is not a black box or a library that solves complex logical problems; you can enter an algebraic equation into Mathematica and it will produce a solution, but the sequence of steps performed is unknown. Prolog cannot solve general logical problems (it lacks logical “or” and “negation”), otherwise its output would be non-deterministic as a linear resolution. Prolog is the golden mean between a simple interpreter and a theorem proving machine; a shift in any direction leads to the loss of one of the properties.

In the next article I would like to talk about how sorting problems are solved, about the sequence of transfusions, Miss Manners and other well-known logical problems. For those who feel unsatisfied, I want to offer next task (the first person to solve the prize):
Write a predicate, which would generate an infinite sequence of natural numbers, starting with 3. These should be standard numbers in Prolog, operations on which are performed using the is predicate: X is 3 + 1 => X=4.

CHAPTER 10. PROLOGUE AND MATHEMATICAL LOGIC

The Prolog programming language was developed by a team led by Kolmerauer around 1970. This was the first attempt to develop a language that would allow the programmer to describe his tasks using mathematical logic, rather than using traditional programming constructs indicating What And When should do Calculating machine. This idea is reflected in the name of the programming language "Prolog" (the English name "Prolog" is an abbreviation for Programming in Logic.- Transl.).

This book focused on issues related to using Prolog as a tool for solving practical problems. At the same time, nothing was said about ways to achieve the final goal - the creation of a logic programming system, a step towards which is Prolog. In this chapter we intend to correct some of this discrepancy by briefly considering the relationship of Prolog with mathematical logic and the question of the extent to which Prolog programming corresponds to the idea of ​​logical programming.

From the book Applied Free Programs and Systems at School author Otstavnov Maxim

From the book Free Software and Systems at School author Otstavnov Maxim

0.3 Logic and sequence of mastering open source software The logic and sequence of presentation of the material in this course differs significantly from the logic in which most books on open source software are written. Most often, the authors proceed from the fact that the sequence of implementation of programs

From the book Let's Build a Compiler! by Crenshaw Jack

From the book Programming in Prolog author Kloksin U.

CHAPTER 8. DEBUGGING PROLOG PROGRAMS Using the above examples, you have already gained experience in using programs and learned how to change them, and also managed to write your own own programs. Now is the time to address the question: what to do when a program behaves differently than expected?

From the book Boost your website author Matsievsky Nikolay

Logic for a script that runs on a schedule Load and parse http://s3.amazonaws.com/application/?actions=loadlist . If the current server is not in the list, create empty file in the segment with the key servers/(IP address of the EC2 server). Find out whether the remaining servers recorded in the segment are accessible,

author Raymond Eric Stephen

From the book On Intelligence [another version of the book translation] by Jeff Hawkins

Prologue This book and my life are filled with two passions of mine. For 25 years I have been passionate mobile computers. In the world high technology In Silicon Valley, I am known as the founder of two projects - Palm Computing and Handspring, and as the developer of many handheld devices.

From the book The Phenomenon of Science. Cybernetic approach to evolution author Turchin Valentin Fedorovich

From the book The Art of Programming for Unix author Raymond Eric Stephen

From the book Mastery author Lebedev Artyom Andreevich

19.3. Licensing logic: how to choose a license Selection license agreement involves deciding what restrictions, if any, the author places on the use of the software he has created. If the developer does not want to limit the

From the book Programming in Prolog for artificial intelligence author Bratko Ivan

§ 109. Logic and aesthetics October 20, 2004Two basic concepts in design - logic and aesthetics. One designer, having solved all the logical problems, finally arrives at an aesthetic finale. The other is the opposite. (There is, of course, a third person whose senses were stepped on by the bear, but he didn’t

From the book Business e-mail correspondence. Five Rules for Success author Vorotyntseva Tamara

From the book SQL Help by the author

Chapter 2 Syntax and semantics of Prolog programs This chapter provides a systematic presentation of the syntax and semantics of the basic concepts of Prolog, and also introduces structural data objects. Topics covered: simple data objects (atoms, numbers,

From the book The King's New Mind [On computers, thinking and the laws of physics] by Penrose Roger

PREFACE

When using a computer to solve problems, two interrelated ways of representing knowledge can be distinguished:

1) procedural representation, i.e. definition of a data processing algorithm;

2) declarative representation, i.e., the definition of individual concepts, their state at specific points in time and the connections between them.

Traditional algorithmic languages(Pascal, C, Fortran) are procedural because their purpose is to describe the algorithm. But they also contain declarative components (variable descriptions).

In the PROLOG language (LOGICAL PROGRAMMING), on the contrary, the main one is the declarative component, so it is intended not so much for data processing as for processing facts and declarative rules. Facts are logical formulas. The knowledge base (KB) is specified by a set of such formulas. Logical methods ensure that new facts are obtained from the facts presented in the knowledge base.

In procedural languages, the programmer must describe to the computer the process of solving a problem step by step. In contrast, in the declarative language PROLOG, the programmer describes the problem itself and the basic rules for solving it, leaving the system to search for specific actions leading to a solution. Therefore, PROLOG is rightly considered a language more high level than C or Pascal. An external expression of this fact is that the text of a PROLOG program can be

V ten times shorter than the text of a Pascal program that solves the same problem (although, of course, there are problems that are inconvenient for PROLOG, but can be easily described in Pascal or another procedural language).

PROLOG can be used in development expert systems, as well as for the following tasks:

– proofs of theorems and derivation of solutions to problems;

– creating symbolic processing packages for solving equations, differentiation, integration, etc.;

– development of simplified versions of AI systems;

– creating natural language interfaces for existing programs;

translation of texts from one language to another, including from one programming language to another.

PROLOGUE originated in 1973, when a group of researchers at the University of Marseille, led by Alain Colmeroe, created a theorem-proving program that was implemented in Fortran. Subsequently, this product was called PROLOG. In the first years, PROLOG was not widely used practical application. However, in 1981, a Japanese project was announced to create a fifth-generation computer, the software of which was supposed to be based on logic programming. This project to create a computer for processing knowledge caused a great stir all over the world. Commercial implementations of PROLOG have appeared, such as CProlog, Quintus Prolog, Silogic Knowledge Workbench, Turbo Prolog, etc.

The most popular programming system in our country is Turbo Prolog, one of the products of the well-known company Borland. Turbo Prolog 2.0 was released in 1988 and became widely used. Borland distributed this version until 1990,

A PDC then acquired the rights to this product, called PDC Prolog. Visual Prolog was later released, equipped with standard features for quick interface design.

In December 1997, PDC released Visual Prolog 5.0, and in January 1999 began distributing version 5.1. Currently, anyone can copy for free via Internet latest version of the Visual Prolog 5.1 Personal Edition system, operating in Windows environments 3.1/95/98, NT, OS/2, SCO UNIX and Linux. Her boot file 20 MB capacity can be found at: http://www.visual-prolog.com/ vip/vipinfo/freeware version.htm, http://www.pdc.dk/vip/vipinfo/freeware version.htm. The Personal Edition is intended for non-commercial use and is posted in all applications created with it. In addition, Personal Edition owners are not provided with free technical support and they are not subject to benefits and discounts when purchasing new versions. IN latest version there were such improvements as a debugger, special tools, and examples of developing Web sites.

Turbo Prolog is quite suitable for educational purposes. All examples and tasks provided are designed to use this implementation of the language.

1. THEORETICAL PRINCIPLES OF PROLOGUE

PROLOG is a predicate calculus language. A predicate is a logical formula from one or more arguments. We can say that a predicate is a function that maps a set of arbitrary nature into a set (false, true).

The predicates F(x), F(x, y), F(x, y, z), etc. are denoted. The unary predicate F(x), defined on subject area

M is true if object x has property F, and false if it does not have this property.

A binary predicate F(x, y) is true if the objects x and y are in the relation F.

The multiplace predicate F(x1, x2, x3,..., xN) specifies the relationship between the elements x1, x2, ..., xN and is interpreted as a designation of the statement: “The elements x1, x2, xN are in relation to each other F.”

When developing logic programs, predicates are usually given names that correspond to the semantics of the subject area being described.

Examples of predicates:

predator (X)

spouses (X,Y)

Predicates that cannot be split into individual components, are called atomic. Complex formulas are constructed by combining atomic predicates with logical connectors AND, OR and NOT.

When a variable value is substituted into it, a unary predicate becomes “null-place,” i.e. a sentence that is true or false:

predator (tiger).

When a specific value is substituted into an n-place predicate, its locality becomes n–1. Thus, every statement

A statement is generated by a certain predicate, and each predicate corresponds to a set of statements.

The task of the PROLOG program is to prove whether a given target statement is a consequence of the existing facts and rules.

The problem to be solved is presented in the form of a set of statements (axioms), the goal of the problem is also written in the form of a statement. Then the solution to the problem is the clarification of the logical implication from the axioms

(A1 A2 A3 ... An )→ B.

In practice, it is convenient to use proof by contradiction, that is, to prove impossibility. If A → B is true, then A → B is false and AB is false.

This is the basis for the principle of resolution, which requires the representation of predicate calculus formulas as a set of disjuncts connected by the operation of conjunction [conjunctive normal form (CNF)].

The resolution rule has the form

(X A) (A Y)→ (X Y).

The resolution rule allows you to combine two formulas by removing A from one and A from the other. In this case, we say that A and A resolve.

The main idea of ​​the resolution method is to check whether the set of clauses R contains an empty (false) clause. If the answer is positive, the formula corresponding to R is unsatisfiable and the corresponding statement is contradictory. Thus, having proven the impossibility of the formula

(A1 A2 A3 ... An )→ B, we can conclude that B is true.

Let's look at an example of using the resolution method, using proof by contradiction.

Let it be necessary to prove the truth of the expression ((P Q) (P→ R) (Q→ S))→ (R S).

To use the resolution method, you need to consider the conjunction of a set of premises and the negation of the conclusion in conjunctive normal form. To do this, a number of steps are followed:

1. We bring the premises to normal form (without → and ↔ ) P Q;

P R;

Q S.

2. We write the negation of the conclusion in normal form

(R S) = R S ;

3. Consider the conjunction of five disjuncts

R (RP ) (P Q)S (Q S)

The first two clauses yield P, which when combined with the third clause yields Q. The fourth and fifth clauses yield Q.

Thus we have

Q Q = FALSE.

Thus, ((P Q) (P→ R) (Q→ S))→ (R S) is proven, since the opposite is not true.

Any PROLOG program can be considered as a database. The query processing mechanism in PROLOG is called unification. After the user enters a query, the interpreter begins to analyze the contents of the database, making valid substitutions of facts into the target statement to justify its truth.

2. STRUCTURE OF A PROLOGUE PROGRAM

The PROLOG program includes the following main sections: description of names and structures of objects (domains);

description of predicates – names of relations existing between objects (predicates);

section of goal statements, which may be missing; in this case, the program will ask for the target statement when launched;

description of facts and rules describing relationships (clauses). The names of objects (constants) in PROLOG are written with a small letter,

and the variables are large.

Let's consider how a problem from a certain subject area, for example geography, can be described in PROLOG.

Program 1

situ(city,country) CLAUSES

situ (london, england). situ (petersburg, russia). situ (kiev, ukraine). situ (Pekin, Asia).

situ (warszawa, poland). situ (berlin, europe).

situ (X, europe):- situ (X, russia). situ (X, europe):- situ (X, poland).

Expression

situ(kiev, ukraine)

describes the fact that the city of Kyiv is located in Ukraine. Earlier in the program, a corresponding predicate was introduced that works with objects of character type. The name of the city and country are constants here, therefore, according to the rules of PROLOG, they must be written with a small letter. Instead of a definition block:

city, country = symbol PREDICATES

situ(city,country)

one could simply describe the predicate: PREDICATES

situ(symbol, symbol)

Sometimes the first option is preferable because it allows one to evaluate the semantic meaning of the predicate arguments.

There are two rules described at the end of the clauses section of Program 1. The rule defines a new predicate using previously defined predicates. Rule 6

consists of a head (predicate) and a body - a sequence of predicates. The head is separated from the body by the symbol: -, which can be interpreted as the word “If”. Thus, the conclusion is the head of the rule, and the body of the rule consists of a set of premises.

The use of rules is the main way of representing knowledge in intelligent systems.

The meaning of the rule is that a goal, which is a head, will be true if the PROLOG interpreter can show that all expressions (subgoals) in the body of the rule are true.

In the rules, the letter X (or any other capital letter, or any word starting with capital letter) denotes a variable that can take on different values.

Yes, the rule

situ (X, europe):- situ (X, poland)

means that any Polish city is also a European city. By adding new rules, you can supplement and modify the task description. If we want to describe the fact that all cities in France are also European cities, then it is enough to add just one rule

situ (X, europe):- situ (X, france)

and you can still use all the other facts about European cities.

You can use composite objects in PROLOG. Composite objects allow you to describe hierarchical structures in which the description of one predicate includes the description of other predicates.

For example:

Program 2

DOMAINS personal_library=book(title,author,publication) publication= publication(publisher,year)

collector,title,author,publisher=symbol

year=integer PREDICATES

collection(collector, personal_library)

collection(“Ivanov”,book(“War and Peace”, “Leo Tolstoy”, publication(“Enlightenment”, 1990))).

When describing rules, it is often necessary to use logical connectives AND and OR. The AND connector is a comma, and the OR connector is a semicolon. For example:

gigant(X) :- growth(X,Y),Y>200.

star_or_mlad(X) :- X>70; X<10.

PROLOG has a large number of built-in predicates, i.e. automatically determined predicates. For example, the built-in predicate nl causes a line feed, and the built-in predicate write is used to display information on the screen. Built-in predicates are used in the same way as user-defined predicates, but a built-in predicate cannot be the head of a rule or appear in a fact.

Commonly used built-in predicates are = (unification) and logical negation not. For example:

student(X) :- X=”Petrov”; X=”Ivanov”.

xor_student(X) :- not(X=”Petrov”), not(X=”Ivanov”).

planeta(X) :- not(X=”sun”).

The statement not(X = Y) is equivalent to X<>Y.

Sometimes it is useful to use predicates that you know in advance whether they are true or false. For these purposes, the predicates true and fail are used. The predicate true is always true, while fail is always false. The last predicate is used to control the process of solving a problem in PROLOG.

A PROLOG program can use comments that do not affect the execution of the program, but may provide assistance to the person reading the program. PROLOG ignores any number of lines between /* and */. Anything between the % and the end of the line is also considered a comment:

/* Comment written here */

% This is also a comment

When describing a specific subject area, there is usually a set of initial facts and plausible assumptions on the basis of which rules are formulated.

Let's consider how to describe a problem about family relationships in PROLOG.

Let there be facts about paternity:

1) Ivan is Igor's father.

2) Ivan is Sidor's father.

3) Sidor is Lisa's father. Let us also introduce three predicates:

Male (x), meaning that x is a man, Consanguineous (x,y), meaning the same blood of x and y, Brother (x,y), meaning that x is the brother of y.

Obviously, the following rules are fair:

1) “All fathers are men.”

2) “If children have the same father, then they are half-blooded.”

3) “A brother is a half-blooded man.” Let's consider the conclusion of the solution when answering the question: “Does Igor have brothers?”

Program 3

person = symbol PREDICATES

otec(person,person)

brat(person,person) CLAUSES

man(X):-otec(X,_). brat(X,Y):-otec(Z,Y),otec(Z,X),man(X),X<>Y. otec(ivan,igor). otec(ivan,sidor). otec(sidor,lisa).

The second rule of the program contains the condition X<>Y. This allows the PROLOG program to describe the fact that a person cannot be his own brother.

After request

goal: brat(igor,X) The system will display

This corresponds to our ideas about the right decision. The examples given are primitive, but they allow us to imagine

the surprise and usefulness of the solutions that PROLOG can generate given a large number of facts and rules in a complex subject area.

3. DESCRIPTION OF ARITHMETIC OPERATIONS

IN PROLOG language uses a number of built-in functions to evaluate arithmetic expressions, some of which are listed

in table 1.

Table 1

Designation

Type of transaction

>,<,=,>=,<=,<>

Comparison Operations

Arithmetic operations

Remainder of X divided by Y

Divide X by Y

Absolute value of number X

Square root of X

sin(X), cos(X), tan(X), arctan(X)

Trigonometric functions

Exponentiation to the X power

Decimal logarithm (ln) of X

Natural logarithm of X

You can also use your own predicates to describe any arithmetic operation. For example:

Program 4

PREDICATES add(integer,integer) fadd(real,real) maximum(real,real,real)

add(X,Y):-Z=X+Y,write(“Sum= “,Z),nl. fadd(X,Y):-Z=X+Y,write(“FSum= “,Z),nl. maximum(X,X,X).







2024 gtavrl.ru.