Docstoc

prolog-types

Document Sample
prolog-types Powered By Docstoc
					     Prolog data types


 In Prolog, list literals are
delimited by brackets with
elements separated by
commas, e.g.,
[red, yellow, green]


  The empty list is
represented as [].
  The list obtained by
adding the item Head to the
beginning of the list Tail is
represented as
   [ Head | Tail ]


  A length predicate and
other predicates are
described in the online
manual.
  Prolog has a structure, or
compound term, data type.

  Literals of this type are
syntactically identical to
relational expressions, e.g.,

     fraction(3,4)

  The infix operator =..
can be used to convert
structures to lists, e.g.
     X =.. [f,3,4].
will bind X to f(3,4).
  The predicates functor
and arg can be used to
access components of a
structure. For example,

functor(f(3,4),X,Y).

will bind X to f and Y to 2.

   Here 2 is the arity of f.
-- the number of arguments
it takes. Conventionally the
notation f/2 refers to a
binary predicate f.
 Structures may be called
by the unary call (that is,
call/1) predicate.

 For example,

X=..[abs,-3,Y], call(X).

succeeds with X bound to
abs(-3,3), and Y to 3.




          Strings
  Prolog has two different
string types.

  The literals of the two
type are delimited by single
quotes and double quotes
respectively.

 Strings of the first type
may be used as functors
(e.g., as the first argument
of the functor predicate).
  Strings of the first type
are atoms. That it, they
satisfy the unary predicates
atom and atomic.

 Strings of the second type
are equivalent to lists of
character codes.

  Both types may appear as
the first argument of the
type conversion predicates
string_to_atom and
string_to_list.
  For numeric types, Prolog
has infix arithmetic
functions

    +, -, *, and /

  They are normally used in
conjunction with the infix
relation is, e.g.,

      X is 3 + 4.

which will bind X to 7.
  Prolog has infix
arithmetic predicates

<, >, =<, >=, =\=, and =:=

  Do not use = or == for
arithmetic equality.


  Prolog has a unary prefix
operator \+ for negation.
The equivalent prefix
function not is deprecated.
  The difference list
representation for lists can
be useful when frequent
appending needs to be
done, as in parsing.


  Here a list X is
represented as two lists Y
and Z such that appending
X to Z gives Y.

  In other words, X is the
difference of Y and Z.
 Conventionally a slash or
hyphen is placed between Y
and Z.

 So for example, the list

         [a, dog]

can be represented as

[a,dog,runs]/[runs]

or as

        [a,dog|X]/X
  So if V1 and V2 are
variables, appending two
lists L1/V1 and L2/V2 gives
L1/V2 by binding V1 to L2.

  This can be done in O(1)
time.

  The empty list can be
represented by X/X.

  An ordinary list L can be
represented as L/[].
  Prolog has many type-
checking predicates.

  See the "built-in
predicates | verify type of a
term" section of the SWI-
Prolog online manual for
examples.

  Except for the var
predicate, each of them
needs its argument to be
instantiated (i.e., not a
variable) to succeed.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:11/8/2012
language:Unknown
pages:13