According to Wikipedia, the single data type in Prolog is the term. This text also mentions that "Prolog's single data taype is the term", but then proceeds to explain the "classification of data types in Prolog" (but I thought there was only one type...) Now, these slides mention different data types: "numbers, characters and strings".
So, what actually are the data types in Prolog?
The basic data structure in Prolog is term which is expressed in form name(arguments...). If the number of arguments is zero then we speak about atom. A special type of atom is number.
There are three kinds of terms in Prolog: A constant is an atom or a number. An atom is a quoted character string or a string of letters, digits, and underscores that starts with a lower-case letter. A number resembles the real or integer constants used in most programming languages.
There are only three basic constructs in Prolog: facts, rules, and queries. A collection of facts and rules is called a knowledge base (or a database) and Prolog programming is all about writing knowledge bases.
I'll bite: Prolog has a single data type term
. Not very useful answer, eh?
Terms are subdivided into
Variables. A placeholder, not unified to any specific term. Variables are identified by symbols matching the regular expression [A-Z_][A-Za-z_0-9]*
. The variable _
is special: it is the anonymous variable. Every occurrence of _
denotes a distinct variable. For instance, given the fact,
foo(1,2,3).
A test like foo(_,_,_).
will succeed whereas a test like foo(A,A,A).
will fail.
Once a variable is unified with (bound to) a value, however, it ceases to be variable: unless undone by backtracking it is forever and always what it unified with.
Numbers are either float
or integer
. Usual sorts of rules apply (e.g. -321
is an integer, -321.0
or something like -3.21e+02
is a float.
Atoms are names, usually denoted by by a word beginning with a lower-case letter (e.g. atom
), matching the regular rexpression [a-z][A-Za-z0-9_]*
. Alternatively, atoms may be delimited by apostrophes (e.g., 'atom'
), which conveniently allow the use of characters not otherwise allowable. The syntax for atom is somewhat more complex than that: essentially anything that doesn't fall into another category will form an atom (e.g., the special atom []
denoting the empty list and the comma (,
) denoting conjunction are all atoms.
Everything else is essentially a structure i.e. tuples of terms, tagged by a functor (a name matching the rules for an atom) with an arity (number of arguments). One can even treat atoms as structure of arity 0.
There is syntactic sugar poured on top of prolog's other "data types":
Lists are denoted by the structure ./2
, with the left-hand argument being the head of the list and the right-hand its tail. The empty list being denoted by the atom []
. For instance,
[a]
is internally represented as .(a,[])
,[a,b]
as .(a,.(b,[]))
and[a,b|[c]]
as .(a,.(b,.(c,[])))
.One should note that one can write a list using either notation: they will unify appropriately. You can see the attraction of using the bracketed list notation, however.
Similar syntactic sugar is applied to strings. A string can be written as a string of text delimited by double-quotes: "The cat and the hat"
. Internally however, strings are represented as lists of integers representing the code points for each of the characters in the implementations internal encoding. For instance, the string "cat"
is internally represented (in ASCII/UTF-8) as the list [99,97,116]
. "cat"
is easier to read, eh?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With