: f(X) is X**2+3. : f(5)? 28f is defined as a function on X which returns the value X^2+3. Not that the iProlog interpreter knows the difference between a function and a predicate, so to evaluate a function it is only necessary to type the functional expression and the resulting value is printed on the terminal.
To invoke a function from within a function is easy:
: g(X) is f(X) * 2. : g(5)? 56To evaluate a function within a clause, use the is predicate:
: p(X, Y) :- Y is g(X). : p(5, X)? X = 56Functions are really just a notational convenience, internally, the are implemented as if they were clauses whose last argument is always the output. Here is another example:
conc([], X) is X. conc([A|B], C) is [A|conc(B, C)]. rev([]) is []. rev([A|B]) is conc(rev(B), [A]). rev([1, 2, 3])? [3, 2, 1]
F is X :- Conditions. F is Y :- Conditions. .....The order of execution is:
op(850, xfx, -->)! rcib --> smir(20, 1). rcvp --> smir(4, 1). smir --> hype(300, 0.0001). hype --> fle(200, 0.0002).The rule states that the presence of rcib support the hypothesis that smir is present with sufficiency 20 and necessity 1. We need to define some prior probabilities:
The posterior probabilities are essentially the inputs:
posterior(rcib) is 1. posterior(rcvp) is 1.
The program for evaluating the probabilities is:
odds(P) is P / (1 - P). prob(O) is O / (1 + O). posterior(Consequent) is prob(M * odds(prior(Consequent))) :- Antecedent --> Consequent(LS, LN), M is odds_multiplier(prior(Antecedent), posterior(Antecedent), LS, LN). odds_multiplier(Prior, Posterior, LS, LN) is LS * (Posterior - Prior) / (1 - Prior) :- Prior < Posterior. odds_multiplier(Prior, Posterior, LS, LN) is LN * (Prior - Posterior) / Prior :- Prior > Posterior.The program is called as follows:
: posterior(fle)? 0.342818Click here to get the code.