Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the data types in Prolog?

Tags:

types

prolog

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?

like image 273
josh Avatar asked Aug 20 '12 12:08

josh


People also ask

What are the data structures 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.

What are the three forms of a Prolog term?

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.

What are 3 basic constructs in Prolog in AI?

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.


1 Answers

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,

    • the list [a] is internally represented as .(a,[]),
    • the list [a,b] as .(a,.(b,[])) and
    • the list [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?

like image 102
Nicholas Carey Avatar answered Oct 12 '22 08:10

Nicholas Carey