nanaxipad.blogg.se

Visual prolog 7.5 tutorial
Visual prolog 7.5 tutorial








visual prolog 7.5 tutorial
  1. VISUAL PROLOG 7.5 TUTORIAL CODE
  2. VISUAL PROLOG 7.5 TUTORIAL PLUS

VISUAL PROLOG 7.5 TUTORIAL PLUS

% Note: on this occasion it works to pass two variables to plus because % only Y is free (X is bound by magicNumber). ?- magicNumber ( X ), plus ( X, Y, 100 ). % When a predicate such as magicNumber can give several solutions, the % overall compound goal including it may have several solutions too. % Error - although this could be solved, % the number of solutions is infinite, % which most predicates try to avoid. For % example, Prolog has a built in predicate plus which represents % arithmetic addition but can reverse simple additions. % Any unification, and thus any predicate in Prolog, can either: % Succeed (return True) without changing anything, % because an equality-style unification was true % Succeed (return True) and bind one or more variables in the process, % because an assignment-style unification was made true % or Fail (return False) % because an equality-style unification was false % (Failure can never bind variables) % The ideal of being able to give any predicate as a goal and have it % made true is not always possible, but can be worked toward. Therefore assigning X will also % assign Y. % X = Y are both free, so Prolog remembers % it. Unlike =, the right hand side of IS % must always be bound, thus guaranteeing % no attempt to solve an equation. % This is why = can't do arithmetic - % because Prolog can't solve equations ?- 5 is X + 2. % X = 3+2 - unification can't do arithmetic ?- X is 3 + 2. % False % First acts as assignment and binds X=3 % Second acts as equality because X is bound % Since 3 does not equal 2, gives False % Thus in Prolog variables are immutable ?- X = 3 + 2. % X = Y = 2 - two assignments % Note Y is assigned too, even though it is % on the right hand side, because it is free ?- X = 3, X = 2. % % The = sign in Prolog represents unification, so: ?- 2 = 3. With some luck, % one of the two sides will eventually be bound, but this isn't % necessary. % If both sides are free, the assignment is remembered. % If one side is free (ie, undefined), assign to match the other side. Unification is % essentially a combination of assignment and equality! It works as % follows: % If both sides are bound (ie, defined), check equality. % This is Prolog's central operation: unification. You can accept an earlier solution by pressing.

visual prolog 7.5 tutorial

Pressing again forces it to try % the last one, 42, after which it no longer accepts input because this % is the last solution. % By pressing in interactive mode you can reject that solution and % force it to assign the next one, 9. % Prolog makes magicNumber true by assigning one of the valid numbers to % the undefined variable Presto. Any name % starting with a capital letter is a variable in Prolog. % What makes Prolog unusual is that we can also tell Prolog to _make_ % magicNumber true, by passing it an undefined variable. % True % Some older Prologs may display "Yes" and "No" instead of True and % False. We can now use % interactive mode to ask if it is true for different values: ?- magicNumber ( 7 ). Note that % predicate names must start with lower case letters. % This introduces magicNumber as a predicate and says that it is true % with parameter 7, 9, or 42, but no other parameter. % As an example, here is a definition of the simplest kind of predicate: % a fact. % A command (called a goal) tells Prolog to make that state of the world % come true, if possible.

visual prolog 7.5 tutorial

% A subprogram (called a predicate) represents a state of the world. % Prolog is based on the ideal of logic programming. Different Prologs may behave % differently. % A bunch of errors and warnings will trigger when you load this file % due to the examples which are supposed to fail - they can be safely % ignored. % Lines that begin with ?- can be typed in interactive mode.

VISUAL PROLOG 7.5 TUTORIAL CODE

% This code must be loaded from a file to work as intended. % Prolog treats code entered in interactive mode differently % to code entered in a file and loaded ("consulted").










Visual prolog 7.5 tutorial