If you are a newcomer to prolog, I hope you read this article first, so that you have an understanding of the overall prolog, this article will detail the prolog learning process programming ideas and prolog syntax details.
First, ask a basic question: We have seen many types of expressions in the Prolog program (for example, jody, playsAirGuitar(mia), and X), but these are just examples, it's time to go deeper, in fact, What constitutes rules and queries?
The answer is the terms. There are four types of statements in Prolog: atoms, numbers, variables, and complex statements (or structures). Atoms and numbers are collectively referred to as constants, and constants and variables are collectively referred to as simple statements.
First of all, we must clarify the scope of the basic characters: uppercase letters: A, B, ..., Z; lowercase letters: a, b, ..., z; numbers: 0, 1, 2, ..., 9. Also includes "_", which is an English underscore character; and some other special English characters, such as: +, -, *, /, ",", =,:,.,&,~; spaces are also characters, but are not commonly used. And not visible. A string is a sequence of characters that are not cut.
Atoms
An atom is one of the following:
1. A string of characters, including valid letters: uppercase letters, lowercase letters, numbers, and underscores, and lowercase letters as the first character. Some examples: butch, big_kahuna_burger, listen2Music, playsAirGuitar.
2. Character sequences enclosed in single quotes. For example: 'Vincent', 'The Gimp', 'Five_Dollar_Shake', '&^%%@# *',' '. The sequence of characters enclosed in single quotes is called the atomic name. Note that we have used the space character,
In fact, using single quotes encapsulation, one of the functions is to use special characters like white space characters accurately in the atom.
3. A string of special characters. For example: @=,====》,;,:-, etc. are all atoms. As we have seen, some special atoms, such as; (logical OR), :- (rules The symbol connecting the head and the trunk has a predefined meaning.
Numbers
In a typical Prolog program, real numbers are not very useful. So although most Prolog implementations support floating point numbers, this article does not discuss them.
But integers (such as: -2, -1, 0, 1, 2, ...) are very useful, such as when calculating the number of elements in the list, we will introduce in detail in Chapter 5. Prolog The representation of the numbers is very simple, nothing special, as follows:
23, 1001, 0, -365, etc.
Variables (Variables)
A variable is a string of uppercase letters, lowercase letters, numbers, and underscores, and the first letter must be an uppercase letter or an underscore. For example:
X, Y, Variable, _tag, X_526, List, List24, _head, Tail, _input, Output
Both are valid variables in Prolog. The variable "_" is a special case, it is called an anonymous variable, which we will introduce in Chapter 4.
Complex Terms
Constants, numbers, and variables are all modules that build statements. Now we learn how to make them into complex statements. Complex statements are also called structures.
A complex statement consists of a functor (functor, also known as a function name) and a sequence of arguments. The sequence of arguments is enclosed in parentheses, separated by commas, and placed after the functor. Note that the functor must be tight. With the sequence of parameters,
There can be no spaces in the middle. The functor must be an atom, that is, the variable cannot be used as a functor. On the other hand, the parameter sequence can be any type of statement.
From KB1 to KB5, we have seen many examples of complex statements. For example, playsAirGuitar(jody) is a complex statement, where playsAirGuitar is a functor, and jody is a sequence of parameters (only one parameter). Another
Examples are loves(vincent, mia), loves is a functor, vincent and mia are parameter sequences; another example is a variable containing: jalous(marsellus, W).
(Note: The functor and the predicate are different. My understanding is that the functor is the name of the predicate. The predicate contains the functor and its parameter sequence, which is the realization of the whole logic.)
However, the definition of complex statements can allow for more complex situations. In fact, in complex statements, other complex statements can be embedded (that is, complex statements allow recursion). For example:
Hide(X, father(father(father(butch)))).
It is a perfect complex statement that conforms to the definition. Its functor is hide, with two parameters: one is the variable X, the other is the complex statement, father(father(father(butch))). This complex statement consists of: The functor is father,
Another complex statement, father(father(butch)) is its only parameter. The parameters of the inner layer of the complex statement are still a complex statement: father(butch). But to the innermost level of the nest, the argument is a constant: Butch.
In fact, this nesting (recursive structure) allows us to describe many problems naturally, and the interaction between this recursive structure and the combination of variables is a powerful weapon for Prolog.
The number of parameters of a complex statement is called an arity. For example, woman(mia) is a complex statement with a number of 1 and loves(vincent, mia) is a complex statement with a cost of 2.
The number of elements is very important for Prolog. Prolog allows you to define complex statements with the same functors but different numbers of elements. For example, we can define the loves predicate of two parameters, loves(vincent, mia); we can also define the loves predicate of three parameters.
Loves(vincent, marsellus, mia). If we do this, Prolog will think that the two predicates are different. In Chapter 5, we will see specific applications that define the same functor but differ in the number of elements.
When we need to mention the defined predicates and how to use them (for example, in the documentation), the convention is the form of "concipher/meta". Back to KB2, we have three predicates, the previous expressions are as follows:
listen2Music
Happy
playsAirGuitar
Use formal writing as follows:
listen2Music/1
Happy/1
playsAirGuitar/1
Prolog won't be confused by defining two loves predicates, it will distinguish between loves/2 and loves/3 are different predicates.
Ring And Fork Type Insulated Terminals
Ring And Fork Type Insulated Terminals,High quality insulated terminal,copper tube terminal
Taixing Longyi Terminals Co.,Ltd. , https://www.longyicopperlugs.com