Jalog 1.5


Jalog Quick Reference

Built-in Predicates

assert(Fact)

Stores the fact Fact to the end of the database. Same as assertz.

Example:

assert(connection(7588, 9032))

asserta(Fact)

Stores the fact Fact to the beginning of the database.

Example:

asserta(address("Sqrt", $082A))

assertz(Fact)

Stores the fact Fact to the end of the database.

Example:

assertz(connection(7588, 9032))

bound(Var)

Succeeds, if the parameter Var is bound, otherwise fails.

closefile(SymbolicFileName)

Closes the named file. closefile succeeds even if the file has not been opened.

Example:

closefile(report)

comline_arg(Number, Option, Argument)

    Not implemented like other built-in predicates. Instead, each command line argument is stored to the database as a separate fact.

Returns information about one command line argument, if one can be found.

A command line example:

> java -jar jalog.jar myprogram.pro -foo=bar -up report.txt

    Note the equal sign separating an option and its argument.

Parameters of this command line are stored to the database prior to starting myprogram.pro as following facts:

comline_arg(1, "foo", "bar").
comline_arg(2, "up", "").
comline_arg(3, "", "report.txt").
concat(String1, String2, LongString)

Obtains LongString by concatenating String1 and String2

Example:

concat(S, ".pro", Filename)

consult(Filename)

Reads the rules and facts to the database and executes the goals from the file Filename.

If Filename does not specify full path, then the consult directory is used. Initially the consult directory is the directory containing the main program. The consult directory can be changed by the consult_dir predicate.

Example:

consult("game_library.pro")

The predicate name consult in another name of the predicate include.

consult_data(Filename, DatabaseName)
consult_data(Filename, Filter)

Reads the facts from the Filename to the end of the database.

If the second parameter is a symbol it is interpreted as a database name. In this case only facts that belong to this database is read.

If the second parameter is a list of strings it is interpreted as a filter. Each string of the filter must contain a predicate indicator in form name/arity . In this case only facts specified in the Filter are read.

Examples:

consult_data("samples.dat", samples)

consult_data("data.dat", ["cars/4", "houses/2"])

consult_dir(ConsultDirname)

The current consult directory is used by the consult predicate to locate files when no full path is specified. It is not used by consult_data or any other predicate.

If ConsultDirname is bound to a string sets current consult directory.

If ConsultDirname is open it will be bound to the current consult directory name. The directory name is absolute and always begins with "file:" or "res:". It can be used as an argument to set_consult_dir .

Examples:

"file:C:/data" - refers to the data directory at the root of the C disk.

"res:lib/routes" - refers to a resource directory.

"sub" - refers to the subdirectory sub of the current directory.

deletefile(FileName)

Deletes the file FileName.

Example:

deletefile("temp.dat")

dynamic(PredicateIndicator)
dynamic(PredicateIndicator, DatabaseName)

Prevents an error message, if no clause matching PredicateIndicator is found in the database. This is intended for predicates that can be asserted and retracted in runtime. This is intended to be used as a directive.

A PredicateIndicator must be a string of form Functor/Arity .

A DatabaseName, if present, specifies in which database these facts are stored. If absent default database dbasedom is used.

Examples:

:- dynamic("route/3").

:- dynamic("path/3", paths).

existfile(FileName)

Succeeds if the file FileName exists.

Example:

existfile("motor/params.txt")

exit

Terminates the program with exit code 0.

However, if exit is called (typically very indirectly) in execution of the first argument of trap then control is transferred to the trap predicate.

exit(Int_value)

Terminates the program with exit code Int_value.

However, if exit is called (typically very indirectly) in execution of the first argument of trap then control is transferred to the trap predicate.

fail

This call fails always.

file_str(FileName, StringVariable)

The contents of FileName is read into StringVariable. The file must be an ASCII or UTF-8 coded text file.

Example:

file_str("data.dat", String1)

findall(Variable, Atom, ListVariable)

Collects the values from backtracking into a list. Thus, if Atom is a predicate with its arguments represented by valid variable names, and Variable is the name of one of the variables in the predicate, ListVariable will be bound to the list of values for that variable that was obtained from instances when the predicate can succeed due to backtracking.

Example:

findall(P, substring("a, b, c, d", P, _, ","), L)
L will contain the positions of commas: [2, 5, 8]

foreach_(Var, List)

Binds the first variable Var in the first call to the first item of the list List in the second variable, and in recalls to the succeeding items.

foreach_(X, [1, 2, 3]), write(' ', X), fail. prints 1 2 3

free(Var)

Succeeds, if the parameter Var is open, otherwise fails.

frontchar(String, FrontChar, RestString)

Isolates the first character of String.

frontstr(NumberOfChars, String1, StartStr, String2)

Splits String1 into two parts. StartStr will contain the first NumberOfChars characters in String1 and String2 will contain the rest.

fronttoken(String, Token, RestString)

Isolates the first token of String. Whitespace in front of the token is skipped.

include(Filename)

Reads the rules and facts to the database and executes the goals from the file Filename.

If Filename does not specify full path, then the consult directory is used. Initially the consult directory is the directory containing the main program. The consult directory can be changed by the consult_dir predicate.

Example:

include("game_library.pro")

is_char(Var)

Succeeds, if the parameter Var is bound to a character value.

is_compound(Var)

Succeeds, if the parameter Var is bound to a compound structure.

is_integer(Var)

Succeeds, if the parameter Var is bound to an integer value.

is_list(Var)

Succeeds, if the parameter Var is bound to a list structure.

is_real(Var)

Succeeds, if the parameter Var is bound to a real value.

is_string(Var)

Succeeds, if the parameter Var is bound to a string value.

isname(StringParam)

Succeeds, if StringParam is a name.

member(Elem, List)

If Elem is bound, succeeds if Elemis a member of List. If Elem is free, picks one element of List and on backtracking next elements one by one, and fails at the end of List.

nl

Prints a newline character.

not(Predicate_call)

Fails, if Predicate_call succeeds, succeeds, if Predicate_call fails.

Example:

not(connection(7588, 9032)) fails, if connection(7588, 9032) is found from the database, otherwise succeeds.

openappend(SymbolicFileName, FileName)

Opens the disk file FileName for appending and attaches the symbolic FileName to that file. SymbolicFileName cannot be screen. The FileName must not begin with res: .

Example:

openappend(datafile, "data.dat")

openread(SymbolicFileName, FileName)

Opens the disk file FileName for reading and attaches the SymbolicFileName to that file. SymbolicFileName cannot be keyboard.

Example:

openead(parameters, "motor/params.txt")

openwrite(SymbolicFileName, FileName)

Opens the disk file FileName for writing and attaches the symbolic FileName to that file. If the file FileName alredy exists on the disk, it is deleted. SymbolicFileName cannot be screen. The FileName must not begin with res: .

Example:

openwrite(report, "summary.txt")

readdevice(SymbolicFileName)

If SymbolicFileName is bound, then reassigns the current read device to the file opened with the given SymbolicFileName. If the symbol is keyboard, then assigns the current read device to the standard input.

If SymbolicFileName is free, then binds SymbolicFileName to the current read device.

Example:

readdevice(Temp),

readdevice(parameters),

readln(StringVariable)

Reads the characters from the current read device (the keyboard until the devce is changed with readdevice) to the StringVariable until the end of the line

Example:

readln(Line)

retract(Fact)

Deletes the first fact in the database that matches the given Fact. The argument cannot be open. It must be a compound that can have open arguments.

Example:

retract(route("Moscow", _))
        
retractall(Fact)

Deletes all facts in the database that match the given Fact. The argument cannot be open. It must be a compound that can have open arguments.

Example:

retractall(route("Moscow", _))
        
save(FileName)
save(FileName, DatabaseName)

Saves all facts of a given database to a file FileName. If save is called with only one argument (no DatabaseName) it will save facts of the default dbasedom database to the file FileName. If save is called with two arguments (FileName and DatabaseName), it will save facts of the DatabaseName to the file FileName.

Example:

save("main_data.dat"), % all facts of dbasedom.

save("settings.dat", settings), % all facts of settings.

searchchar(Text, Character, Position)

Searches Character in Text. The first call finds the first Position. Each recall finds the next Position as long as there are any. Fails when no more found.

Example:

searchchar("A-B+C-D", '-', P).
Returns P = 2 on first call. If backtracked returns P = 6. If backtracked again fails.

searchstring(Text, String, Position)

Searches String in Text. The first call finds the first Position. Each recall finds the next Position as long as there are any. Fails when no more found.

Example:

searchstring("Apple,, Banana, Orange", ",,", P).
Returns P = 6 on first call. If backtracked fails.

str_char(StringParam, CharParam)

One of the parameters must be bound. Converts the bound parameter to other type. If both parameters are bound succeeds if both parameters are equivalent.

Example:

str_char("A", 'A').
Succeeds

str_int(StringParam, IntParam)

One of the parameters must be bound. If StringParam is bound it must be a decimal integer (possibly preceded by a minus sign). Converts the bound parameter to other type. If both parameters are bound succeeds if both parameters are equivalent.

Example:

str_int("-32768", -32768).
Succeeds

str_len(String, Length)

Length is set to number of characters in the String.

Example:

str_len("Very long string", Len).
Sets Len to 16.

subchar(String, Position, Char)

subchar returns the character at a given position in a string. The first character has position 1.

Example:

subchar("ABC", 2, Char) % Char is bound to 'B'.

substring(LongString, Pos, Len, SubString)

The parameter LongString must be bound to a string.

If Pos and Len are bound then Substring is bound to part of LongString that starts at Pos and is Len characters long.

if Pos is bound and Substring is bound and Len is open or bound to the length of Substring then SubString is searched in LongString. The first call finds the first Pos. Each recall finds the next Pos as long as there are any. The predicate fails when no more found.

Example:

substring("Programs/Games/Chess.exe", 10, 5, S).
Returns S = "Games".

term_str(Domain, Term, String)

Converts between terms and their string representations.

Example:

term_str(struct, a(3,"k"), S), % binds S to the string "a(3,\"k\")".

term_str(real, R, "3.141"), % binds R to the number 3.141.

trap(Predicate_call, Var_exit, Exception_handler)

Executes Predicate_call. If an exception happens or exit is called within the call, the exit code is bound to Var_exit, Exception_handler is called, and finally the whole trap predicate fails.

Example:

trap(analyze(Situation, Results), X, write("*** Exception ", X, '!'))
        
upper_lower(StringInUpperCase, StringInLowerCase)

One of the parameters must be bound.

If only StringInUpperCase is bound then converts the string to lower case and binds it to StringInLowerCase.

If only StringInLowerCase is bound then converts the string to upper case and binds it to StringInupperCase.

If both parameters are bound compares parameters ignoring the case, and fails if they are not equivalent.

Example:

upper_lower("String", A).
Returns A = "string"

write(parameters)

Writes the values of the parameters without punctuation or whitespace in between.

Example:

write('a', "bc", 3.142)
writes abc3.142

writedevice(SymbolicFileName)

If SymbolicFileName is bound, then reassigns the current write device to the file opened with the given SymbolicFileName. If the symbol is screen, then assigns the current write device to the standard output.

If SymbolicFileName is free, then binds SymbolicFileName to the current write device.

Example:

writedevice(Temp),

writedevice(report),

writeln(parameters)

As write but followed by nl

writeq(parameters)

Writes the values of the parameters in lexical notation so that they can be read back. Strings are enclosed in double quotes and characters are enclosed in single quotes.

Examples:

writeq('a', "bc", 3.142)
writes 'a'"bc"3.142

writeq(a('b', 'c'))
writes a('b','c')

Left = Right

Tries to unify Left and Right.

Left > Right
Left >= Right
Left < Right
Left <= Right
Left != Right

Compares arithmetically or lexicographically Left and Right.

Arithmetic Expressions

Operators

Operator Precedence

Arithmetic operators have their normal precedences. Expressions in parenthesis () are evaluated first. Operators multiplication, division, mod, and div have the same precedence and are evaluated from left to right.

Operators and Data Types

Symbol Operation Left Type Right Type Result Type Example Example Value
* multiplication integer integer integer 2 * 3 6
* multiplication integer real real 2 * 3.2 6.4
* multiplication real integer real 2.2 * 3 6.6
* multiplication real real real 2.2 * 3.2 7.04
/ division integer integer real 3 / 2 1.5
/ division integer real real 4 / 2.5 1.6
/ division real integer real 6.8 / 4 1.7
/ division real real real 14.7 / 4.2 3.5
div integer division integer integer integer 5 div 3 1
mod remainder integer integer integer 5 mod 3 2
+ addition integer integer integer 2 + 3 5
+ addition integer real real 2 + 3.2 5.2
+ addition real integer real 2.2 + 3 5.2
+ addition real real real 2.2 + 3.2 5.4
- subtraction integer integer integer 3 - 2 1
- subtraction integer real real 3 - 2.2 0.8
- subtraction real integer real 3.2 - 2 1.2
- subtraction real real real 3.2 - 2.2 1.0
+ identity function integer integer + 3 3
+ identity function real real + 3.2 3.2
- negation integer integer - 2 -2
- negation real real - 2.2 -2.2

Other Details

ConsultDirname

Directory names can refer to directories either in the file system or in the resource system. In order to specifically refer to the file system prepend the name with "file:". In order to refer to a directory in the resource system prepend the name with "res:". If neither is specified and the ConsultDirname is an absolute path it refers to the file system. A relative path refers to a subdirectory of the current work directory except in consult directory.
Examples:
"file:C:/data" - refers to the data directory at the root of the C disk.
"res:lib/routes" - refers to a resource directory.
"sub" - refers to the subdirectory sub of the current consult directory.

filename

File names can refer either to files in the file system or to resources of the jar package. In order to specifically refer to the file system prepend the name with "file:". In order to refer to a resource in the resource system prepend the name with "res:". If neither is specified and the filename is an absolute path it refers to the file system. A relative path refers to a file or resource in the current directory except that the predicates consult and include use current consult directory instead.
Examples:
"file:C:/data/trees.pro" - refers to a file in the file system.
"res:parameters.pro" - refers to a resource in the resource system.
"rules.pro" - refers to a file or a resource in the current directory.