Logic programming tutorial (advanced article) [ID] dynamic combination/integrated dynamics (Integrated Dynamics) Minecra

This tutorial is set by the author to use the CC By-NC-SA protocol.

This tutorial will describe some of the advanced characteristics and usage of the operator. The most important thing is the custom operator. There are many words. It is recommended to read the basic article before reading this tutorial.

What is the amount of operational symbols?

Talking about the type of constant and variables in the basic tutorial. There is a special type of "operator". It is mainly used as the input parameters of certain operators, or it is used to run with the Apply (executing) operator.The running results are the same as using the operator directly.In addition, the computing constant is the operator after the corner of the corner. Its specific meaning will be introduced later. One of its functions is to achieve custom operators.

Get the constant quantity of operational symbols:

First select the "operator" label in the logical programming platform:

Then enter the name of the operator in the input box."The amount of operational symbols:

Use the Apply operator to run the operational quantity:

Logic programming Taichung Search "Apply" (can also search zhixing), and 3 Apply operators will appear:

The difference between these 3 Apply computing symbols is that the number of input parameters is different. Apply accepts 1 input parameter, Apply2 accepts 2 input parameters, Apply3 accepts 3 input parameters. It should be noted that the input parameters here referThe input parameters of the running operator instead of the input parameters of the Apply operator;

Assuming we want to run the operator constant of the "additional calculus" we just obtained, this operator requires 2 input parameters, so we need to choose the Apply2 operator to execute:

Place the additional calculatable variable card on the left, and place the parameter variable card in the two boxes on the right. The values ​​are 1 and 2, respectively. Put an air variable card in the lower right corner and then remove it.

Put this variable card in the display, and put the remaining variable cards in the variable box, you can observe the result of the addition of the additional operation:

What is Corry?

In computer science, Currying is a function that transforms functions that accept multiple parameters into a function that accepts a single parameter (the first parameter of the initial function), and returns to the new function of the remaining parameters and returns the result of the result of the result.technology.

The integrated motivation Corrihua is slightly different from the above. If a operator is chemical after a operator, the number of parameters accepts less than the number of parameters you need will return a new operator.Accept the remaining parameters. If all the parameters of the operator are accepted, the real operation results are returned.

In order to explain the concept of Curry, continue to take the "plus" operator as an example, select the "operator" label in the logical programming platform and search for "JIA", and the mouse moves to the position of "additional calculation character":

This xxx-> xxx-> xxx means:

It is usually used to indicate the parameter type and return value type of the operator (or function) after the Corrihua;

The last xxx is the type of return value of this operator. Other XXX is the type of input parameters;

It can be seen in the figure that the "Canadian Motor" has two Number type input parameters, and a Number type return value.

As mentioned earlier, the value of the computing symbol constant is the operator after Corrihua. Continue to use the above "additional calculation symbol". First, put the operator variable card in the display to observe the results:

The name of the first behavior operator, other rows represent its input parameter type and return value type. Now this operator has two parameters to be entered, and the type is Number.

Through the Apply operator, the result of a parameter (value is 1) and run after runtime:

It can be seen that the return value is still a operator, and there is an additional [INTEGER;] behind the operator name, indicating that it has been passed into a integer parameter. Now this operator still needs a parameter to complete the addition of the plus operation.

Continue to use the Apply operator, pass the parameter (value 2) and run:

In the end, the real operation results were obtained by using the Apply operator twice.

Custom operator:

The custom operator refers to the new operator formed by combining the built -in computing symbols in some way to achieve some custom functions, such as entering an integer, determining whether it is greater than 0, or obtaining all the number of all the number in the box.Items greater than 64.

Why do you customize the operator?

Generally speaking, you can customize the operator if you use some operators, such as MAP (traversal mapping), Filter (filter), contains_p (including), count_p (counting), and UNIQ_P (only the only condition).They all have a parameter of the operation symbol and must be a specific type of operator. In this case, a customized operator needs to be customized.

If you want to screen out the items with a stacking number of more than 10 in a list of items, you must use the Filter (filter) operator to filter a list of items:

To study carefully, two input parameters are required. The first parameter is a Predicate (assertion, which is also translated as a predicate) -n special operator, there is an arbitrary type of input parameter, a Boolean (Boolean) (Boolean)The return value of the type; the second parameter is a list.The Filter operator will pass on each element in the list as a parameter in the given operator. If this operator returns true, add the element to a new list. In the endThis new list returns.

Then the idea of ​​filtering items is probably: the operators that "determine whether the items are stacked greater than 10" and the list of items obtained from the item container reader is transmitted as the parameter into the Filter operator.The problem is that you will find that the operator of "judging whether the items are stacked greater than 10" can only be found in the logic programmer, and you can only define a operator yourself.

How to customize the operator?

Continue to use the above example, we must define an operator of "judging whether the items are stacked greater than 10". According to the built -in operator, we may think of using the "quantity" operator and the "greater" computing symbol to complete this function. IfIn order to complete the function of "judging whether the stacking of a certain items is greater than 10", it is enough to use these two operators, but it will be much more complicated to make this function into a operator:

Each box above shows a variable card. The arrow connection between the variable card indicates the reference relationship (for each variable card, the arrow is expressed as an input parameter, the arrow is insideThe order is up and down), the box with brackets indicates that the operator is used to obtain variables, and those without parentheses are constant.

The above flow chart is to define the complete process of this operator. The general description of the process (the serial number in the corresponding graph):

① First of all, we must obtain the stacking number of items, and then use this number as the parameter of the operating symbol of "whether the quantity is greater than 10". In the end, whether the number of stacking items is greater than 10;

② The follow -up is the operator of "whether the quantity is greater than 10". Here is a "less than" operator, because the original logic is "a certain number greater than 10", but here it must be passed to 10 to the operator, so the logic changes.It has become "10 less than a certain number"; according to the characteristics of Corride, the "less than" operator is passed into a parameter through the Apply computing operator.

③ Finally, the stacking number of items should be passed as the parameter into the "Whether the quantity is greater than 10" operators. Here is the "pipeline" operator.The second operator is executed, and the value returned by the "pipeline" computing symbol is still a operator; the number of stacking operators of the items and the "quantity greater than 10" operator will be used as the parameter of the "pipeline" operator, and it will eventually get it."Determine whether the stack of items is greater than 10" operators.

Design ideas for operators:

Determine the input parameter type and return value type of the operator before the design

When designing the first design, you can draw a picture to clarify the logical relationship of each variable card.

Some custom operators need to be used for built -in operators:

Its role is to combine one or more computing symbols into new operators

(Phacelet: Input parameters with any type, a bugle value of a BOOLEAN (Boolean) type)

Combat name name

Parameters (operators)

effect

. &&.

2

The return value of the two predicate words is "and" operation

. ||. (Separation of predicates)

2

Do "or" operation of the return value of the two predicate words

!. (Reverse)

1

Reverse the return value of the predicate, that is, "non -" operation

.(pipeline)

2

Create a new operator: first execute the first operator, and execute the return value as the first parameter of the second operator.

.2 (pipeline binary)

3

Create a new operator: first execute the first operator, and then execute the second operator, and the return values ​​of the two are used as the first two parameters of the third operator and execute.

FLIP (flip)

1

The passing order of the first two input parameters of the flip component.

Pixel description of each operator:

(The outermost frame represents the new operator, the arrows are inward inward, and the arrows represent the return value. The dotted line is optional parameter)

Written later: The easiest way to learn logic programming is practice. Most of the descriptions of the operators in the official manual are very vague. Only those who have learned programming have a certain programming experience.The relationship can be tested a few more by yourself, and you can understand it a few times. Of course, if you are interested, you can learn functional programming. After all, this is the core of this MOD.