What is different for Visual Prolog 5 Users

From wiki.visual-prolog.com

Revision as of 13:57, 10 August 2007 by Thomas Linder Puls (talk | contribs) (Move from WEB)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This is not really a tutorial, but a quick guide to the differences between Visual Prolog 7 and Visual Prolog 5. The focus is on Visual Prolog 5 features that have been changed, rather than new features.

Dots

All declarations (i.e. constants, domains, predicates and facts) are terminated by a dot (i.e. ".").

Predicates

The syntax of predicate declarations has changed in several respects. This example illustrates most of the changes:

predicates
    ppp : (integer Input) procedure (i).
    qqq : (integer Input) -> integer Output procedure (i).
  • There is a colon between the predicate name (that is always the first word in the declaration) and the predicate type.
  • The return type (if any) has been moved to the end of the type expression after an arrow (i.e. "->"). There is no dash between the predicate type and the mode and flow patterns.
  • The format, where the predicate mode is written in front of the declaration, does not exist anymore.In a predicate declaration the omitted predicate flow means all arguments input. Special keyword anyflow can be used in local predicate declarations to specify any flow.

Predicate Domains

The syntax of predicate domain declarations has been changed similarly:

domains
    pppDom = (integer Input) procedure (i).
    qqqDom = (integer Input) -> integer Output procedure (i).

Reference Domains

Visual Prolog does not support reference domains since Visual Prolog 7.0 version.

See: How To Eliminate Reference Domains from a Project.

Function Clauses

The syntax of function clauses has been changed so that the return value is placed after an equal sign in the clause head:

clauses
    qqq(X) = 1 :-
        X < 3,
        !.
    qqq(X) = X.

Constants

Constants are no longer macros, but must be values of a certain type.

constants
    myList : integer_list = [14, 56, -3].

The type can be omitted for number, character and string constants.

Facts

The declarations are similar to predicate declarations:

facts - myFactDB
    fact1 : (integer Input) nondeterm.
  • Notice the colon, and that the mode declaration (i.e. nondeterm) is written in the end. Facts can still be single, determ and nondeterm.
  • Facts section can be used only inside implementation of a class.
  • Fact modifier nocopy is not used anymore. All facts are treated as being in nocopy mode.
  • The fact sections are identified with the keyword facts. The alternative database is no longer a keyword.

Fact Variables

Fact variables is a new notion. They are declared in facts sections:

facts
    myFactVariable : integer_list := [14, 56, -3].

Visual Prolog 5 users can consider this to be equivalent to the following single fact with accompanying initialization.

facts
    myFact : (integer_list Value) single.
clauses
    myFact([14, 56, -3]).

Fact variables are used like variables, as it is known from imperative languages:

clauses
    p() :-
        myFactVariable := [14, 56, -3],
        q(myFactVariable).

Visual Prolog 5 users can consider this to be equivalent to the following code:

clauses
    p() :-
        assert(myFact( [14, 56, -3])),
        myFact(Value), q(Value).

Nested Expressions and Functions

Expressions and functions can be nested practically everywhere:

clauses
    factorial(0) = 1 :-
        !.
    factorial(N) = N * factorial(N-1).

Above you can see that the return value in the second clause is an expression. This expression contains a function call, and the argument to this function is also an expression.

Compiler Directives

Compiler directives start with #. For example:

#include @"packageAaa\packageAaa.ph"

Strings

The @ symbol before a string literal means that an escape sequence is not used, i.e. @"\n" represents 2 symbols \ and n.

The \ symbol can be used only before n, t, r, \. Therefore "\x" does not mean "x" anymore.

Characters like '\013' should be written in Unicode (and hex) format '\u000D'.

Conditional Compilation

Conditional compilation can only be applied to sections in Visual Prolog 7.For example:

#if a::myconst = 1 #then
clauses
    p(0) = 1.
#endif

This Visual Prolog 5 code

clauses
    p(X):-
        ifdef debug_mode
        write("X",X),
        endif
        q(X).

corresponds to the following Visual Prolog 7 code:

    • Compile time constant debug_mode should be put in some class. Let us name it globalConstants.
    • An extra predicate is necessary to represent the part of the clause, which is inside conditional compilation.
class predicates
   writeX : (integer X).
#if globalConstants::debug_mode = 1 #then
clauses
   writeX(X):-
        stdIO::write("X=",X).
#else
clauses
   writeX(_).
#endif
clauses
    p(X):-
        writeX(X),
        q(X).

Input, Output and Domains "file", "db_selector"

The file domain is placed into the fileSelector class. The db_selector domain is placed into the chainDBSelector class. The files pfc\5xVIP\fileSelector.cl and pfc\ChainDB\chainDBSelector.cl should be copied to a project directory (into the appropriate sub-directory) before they are modified.

These domains are introduced only for backwards compatibility. The new style is to use objects instead.

IO handling is based on streams. Stream predicates use anonymous argument type and ellipsis.

Ellipsis and Anonymous Argument Type

If a number of arguments can vary, then ellipsis is used. Ellipsis is a special notation that means "zero or more arguments of any type". The syntax is:

class predicates
   p : (...).
clauses
   p(...):-
       stdio::write(...).

If a type of an argument is not known at compile time, anonymous argument type is used. The syntax is:

class predicates
   setProperty : (_).

Objects and Classes

There are significant differences between the object models in Visual Prolog 5 and Visual Prolog 7. The object model is described in Introduction to Classes and Objects.

Library Support

The Library support page describes Visual Prolog 7 equivalents for Visual Prolog 5 names.

Libraries are in general new, written according to object concept. There is a special library 5xVip including sub-folders. The library is intended for migrating Visual Prolog 5 code. New programs should not use 5xVip library.

References