.Open 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 goal::= predicate"." -- input to intpereter at the "?-" prompt. command::=":-" predicate"." . Notice the periods and other punctuators! . Basics fact::=clause ".", clause::=predicate":-"predicate. query::=goal. . Predicates 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. structure::=prefix argument | argument postfix | argument infix argument | functor"(" arguments ")" | functor | array. Note that "," and ";" are also infix operators. array ::= "[]" | "[" arguments "]" | "[" arguments "|" arguments "]", arguments::= argument | argument "," arguments, argument::=constant | variable | structure, constant::= number | string | atom | byte_array, atom::=lower_case_word #("_" word), string::= "'" #char "'", byte_array::= double_quote #char double_quote, variable::= ("_"|"A".."Z")#(letters|digits|"_"). 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: .Box married(X, wife(X)). married(husband(X),X). .Close.Box However we never have to define what it means to evaluate .Box wife(X) husband(X). .Close.Box Prolog will automatically generate constants to fit the structure: .As_is ?- married(dick,Y). .As_is Y= wife(dick), for us. But `wife(dick)` will not match wife(X) with any structure that doesn't have 'wife' in front: .As_is married(dick, tricia)? .As_is *No Clever programming of special predicates (`para-modulation`) can express out intuitive understanding that .Box X = wife(Y) if and only if Y=husband(X). .Close.Box 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 program ::= sentence #( ". " sentences), sentence ::= head sentence_body, sentence_head ::= ( | goal), sentence_body ::= #goal, non_unit_clause ::= head_goal ":-" ( | #goal), goal ::=compund_term, unit_clause ::= head_goal, .Close Syntax .Open 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, last, leash, length, library_directory, limit_stack, line_count, line_position, list_to_set, listing, load_foreign, load_foreign_library, 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, qcompile, qload, qsave_program, qsave_program, read, read_clause, read_history, read_link, read_variables, recorda, recorded, recordz, redefine_system_predicate, rename_file, repeat, require, reset_profiler, restore, retract, retractall, reverse, 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, :-, ?-, -->, :-, .Close Quick reference lists . See also .See http://www/dick/samples/comp.ascii.html .See http://www/cs620/lab/sum.html