[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] / [Samples] / prolog.syntax
Tue Sep 18 15:26:50 PDT 2007

Syntax of the Logic Programming language Prolog

Introduction

The syntax of Prolog is deceptively simple. The User inputs a series of predicates that have three different meanings - facts to be stored, goals to be searched for(queries), or commands(directives) which do one thing. There is also a special kind of rule called a clause which defines conditions underwhich a fact can be true.

Input to Interpretter

1. goal::= predicate"." -- input to intpereter at the "?-" prompt.

2. command::=":-" predicate"." . Notice the periods and other punctuators!

Basics

3. fact::=clause ".",
4. clause::=predicate":-"predicate.
5. query::=goal.

Predicates

6. predicate::=structure,

Structures

A structure looks like an expression in an imperative language but it is only evaluated onder special ccircumstances. Most of the time it is stored and treated as if it was a tree structure.

7. structure::=prefix argument | argument postfix | argument infix argument | functor"(" arguments ")" | functor | array.

Note that "," and ";" are also infix operators.

8. array::= "[]" | "[" arguments "]" | "[" arguments "|" arguments "]",
9. arguments::= argument | argument "," arguments,
10. argument::=constant | variable | structure,
11. constant::= number | string | atom | byte_array,
12. atom::=lower_case_word #("_" word),
13. string::= "'" #char "'",
14. byte_array::= double_quote #char double_quote,
15. variable::= ("_"|"A".."Z")#(letters|digits|"_").
16. functor::=atom.

A functor is a non-numeric constant that is written as if it was a function. It is called a funct_or because it is just a formula and may never be evaluated - it is just a way to label the kind of structure involved. For example here are two facts that might appear in a Prolog database and/or program:

1. married(X, wife(X)).
2. married(husband(X),X).

However we never have to define what it means to evaluate
1. wife(X)
2. husband(X).

Prolog will automatically generate constants to fit the structure:
` 	?- married(dick,Y).`
` 	Y= wife(dick),`
for us. But wife(dick) will not match wife(X) with any structure that doesn't have 'wife' in front:
` 	married(dick, tricia)?`
` 	*No`
Clever programming of special predicates (para-modulation) can express out intuitive understanding that
1. X = wife(Y) if and only if Y=husband(X).

But this goes beyond this simple introduction.

Some commands interpret some of the structures in special ways. For example, A predicate input as a fact is stored as piece of data, but input as a command or goal it becomes a call to a procedure. A clause is also stored by Prolog but then it can act as a subprogram that can be called as a command, or as part of another command.

Associated with each functor in a compound term is the number of arguments it has. This is called its arity([sic] "arity"). An atom can different definitions for different arities.

Logic Programs

17. program::= sentence #( ". " sentences),

20. sentence_body::= #goal,

21. non_unit_clause::= head_goal ":-" ( | #goal),

22. goal::=compund_term,

. . . . . . . . . ( end of section Syntax) <<Contents | End>>

Quick reference lists

Punctuation

comma(and), semicolon(or), exclamation(cut),-->(produces).

Relations

<, =, =.., =:=, =<, ==, =@=, =\=, >, >=, @<, @=<, @>, @>=, \+, \=, \==, \=@=,

Predicates

abolish, abort, absolute_file_name, absolute_file_name, access_file, append, append, apply, apropos, arg, arithmetic_function, assert, assert, asserta, asserta, assertz, assertz, at_halt, at_initialization, atom, atom_char, atom_chars, atom_length, atom_to_term, atomic, autoload,

bagof, between, block, break,

call, call, call_dll_function, call_shared_object_function, character_count, chdir, checklist, clause, clause, clause_property, close, close_dde_conversation, close_dll, close_shared_object, compare, compiling, compound, concat, concat_atom, consult, context_module, convert_time, copy_term, current_arithmetic_function, current_atom, current_flag, current_foreign_library, current_functor, current_input, current_key, current_module, current_module, current_op, current_output, current_predicate, current_stream,

dde_current_connection, dde_current_service, dde_execute, dde_register_service, dde_request, dde_unregister_service, debug, debugging, default_module, delete, delete_file, discontiguous, display, displayq, dwim_match, dwim_predicate, dynamic,

ed, ed, edit, edit, edit_source, ensure_loaded, erase, exception, exists_directory, exists_file, exit, expand_file_name, expand_file_search_path, expand_term, explain, explain, export, export_list,

fail, feature, file_base_name, file_directory_name, file_search_path, fileerrors, findall, flag, flatten, float, flush, flush_output, forall, foreign_file, format, free_variables, functor,

garbage_collect, gensym, get, get, get0, get_single_char, get_time, getenv, ground,

halt, hash_term, help, history_depth,

ignore, import, index, initialization, int_to_atom, integer, intersection, is, is_absolute_file_name, is_list, is_set,

keysort,

make, make_fat_filemap, make_library_index, maplist, member, merge, merge_set, module, module_transparent, msort, multifile,

name, nl, nodebug, nonvar, noprotocol, nospy, nospyall, not, notrace, nth0, nth1, nth_clause, number, number_chars, numbervars,

once, op, open, open_dde_conversation, open_null_stream, open_shared_object,

phrase, please, plus, portray, portray_clause, predicate_property, predsort, preprocessor, print, profile, profile_count, profiler, prolog, prolog_current_frame, prolog_frame_attribute, prolog_load_context, prolog_skip_level, prolog_to_os_filename, prolog_trace_interception, prompt1, prompt, proper_list, protocol, protocola, protocolling, put,

same_file, save, save_program, save_program, see, seeing, seen, select, set_feature, set_input, set_output, set_tty, setarg, setenv, setof, sformat, shell, show_profile, sleep, sort, source_file, source_location, spy, stack_parameter, statistics, stream_position, string, string_length, string_to_atom, string_to_list, style_check, sublist, subset, substring, subtract, succ, swritef,

tab, tell, telling, term_expansion, term_to_atom, time, time_file, tmp_file, told, trace, tracing, trim_stacks, true, tty_get_capability, tty_goto, tty_put, ttyflush,

union, unknown, unload_foreign_library, unsetenv, use_module, use_module,

var, visible, volatile

wait_for_input, wildcard_match, write, write_ln, writef, writeq,

Arithmetic

*, **, +, -, -, /, //, /\, <<, >>, 'C', \, \/, ^,

abs, acos, asin, atan, atan, ceil, ceiling, cos, cputime, e, exp, float, float_fractional_part, float_integer_part, floor, integer, log, log10, max, min, mod, random, rem, round, truncate, pi, sign, sin, sqrt, tan, xor,

Operators

discontiguous, dynamic, module_transparent, multifile, volatile, initialization, :-, ?-, -->, :-,

. . . . . . . . . ( end of section Quick reference lists) <<Contents | End>>