# Coin This example shows the basic concepts of _Logic Programs with Annotated Disjunctions_ and how to use the tool *cplint on SWISH* to perform exact inference or simply inference. Let us suppose that we have one coin that we are not sure if it is biased. Now, if we toss the coin, what is the probability that it will land on head? ### Writing the program step by step In our example we want to write a rule which states that if the coin is fair (not biased) and we toss it, it will land on heads with 50% probability and on tail with 50% probability. We can write such a rule as below: == heads(Coin): 1/2; tails(Coin) : 1/2 :- toss(Coin),\+biased(Coin). == Note that _|1/2 + 1/2 = 1|_. Now we want to write down a second rule which states that if the coin is biased it will land on heads with 60% probability and on tails with 40% probability. == heads(Coin): 0.6 ; tails(Coin) : 0.4 :- toss(Coin), biased(Coin). == We are not sure if the coin is biased or not, we know that there is a 90% probability that the coin is fair and a 10% probability that the coin is biased. Therefore we can write the following probabilistic fact (an LPAD rule without body). == fair(Coin):0.9 ; biased(Coin):0.1. == Finally we state the fact that we will certainly toss the coin. *Note:* this fact is ground. == toss(coin): 1. == If the head of a rule contains only one element =|h : 1|=, we can simpy write this element as =h=. Therefore we can write the previous fact (a fact can be seen as a rule without body) in this way: == toss(coin). ==
### Choosing the editor Before starting write our program, we need to choose an editor between "Prolog" or "LPAD". If we choose the "Prolog" editor we need to add some commands before and after our program (see subsection *Full program with Prolog editor*). Moreover the syntax of the query is different between these two editors. In this section we will see both editors (subsections *Full program with Prolog editor* and *Full program with LPAD editor*). However in the next examples of the tutorial we will use only the "Prolog" editor. ### Full program with the Prolog editor {#prolog_editor} The program is almost complete, what we need now is to load the library =pita= in order to perform exact inference (if we want to perform approximate inference, instead, we need to load =mcintyre=, see [Coin variant](tutorial/inference/coinmc.swinb). Therefore we import this library with the built-in predicate use_module/1. So we need to write == :- use_module(library(pita)). == Moreover, after =|:- use_module(library(pita))|= we need to write =|:- pita.|= in order to initalize the library and the program should be enclosed by =|:- begin_lpad.|= and =|:- end_lpad.|= (respectively at the begin and at the end of the program). These goals are mandatory to initialize the inference system. The full LPAD of this example is shown below.
% load the 'pita' library to perform inference :- use_module(library(pita)). :- pita. % to be written before the program :- begin_lpad. % Rules heads(Coin): 1/2; tails(Coin) : 1/2 :- toss(Coin),\+biased(Coin). heads(Coin): 0.6 ; tails(Coin) : 0.4 :- toss(Coin),biased(Coin). % Facts fair(Coin): 0.9 ; biased(Coin): 0.1. toss(coin). % to be written after the program :- end_lpad.
#### How to execute a query Ok, we have our program, now what?! Now it's time to submit some queries! To query a program you must use the prob/2 predicate with the following syntax == prob(:Atom, -P). == =Atom= is the query that we want to perform, while =P= is the variable that will contain the probability that =Atom= is true (a float between 0 and 1). For instance, let us ask for the probability that the tossed coin will land on head. We can do it with the following query
prob(heads(coin),P).
To try this example click on the triangle icon next to the query. We will see that the coin will land on heads with 51% probability. In fact _|P(heads) = P(heads, fair) + P(heads, biased) = P(heads|fair)P(fair) + P(heads|biased)P(biased) = 0.5*0.9 + 0.6*0.1 = 0.51|_. -- Complete example: [coin.pl](example/inference/coin.pl) -- ### Full program with the LPAD editor {#lpad-editor} As mentioned before, if we use the LPAD editor, our program will be slightly different. We shall not load the library =pita= and the program shall not be enclosed by the goals =|:- begin_lpad.|= and =|:- end_lpad.|=. Therefore our program with the LPAD editor is
% Rules heads(Coin): 1/2; tails(Coin) : 1/2 :- toss(Coin),\+biased(Coin). heads(Coin): 0.6 ; tails(Coin) : 0.4 :- toss(Coin),biased(Coin). % Facts fair(Coin):0.9 ; biased(Coin):0.1. toss(coin).
#### How to execute a query In order to perform a query with the "LPAD" editor, we simply write the atom we want to query == ?- Atom. == In our example, if we want to know the probability that the tossed coin will land on head, we have to write the following query
heads(coin).
-- Complete example with the LPAD editor: [coin.cpl](example/inference/coin.cpl) -- - Reference: J. Vennekens, S. Verbaeten, and M. Bruynooghe. _Logic programs with annotated disjunctions_. In International Conference on Logic Programming, volume 3131 of LNCS, pages 195-209. Springer, 2004.
-- [Back to Tutorial](tutorial/tutorial.swinb)