[ ECLiPSe Prolog Environment built-in.|Group Index| Full Index]

local struct(++Prototype), export struct(++Prototype), global struct(++Prototype)

Declare a structure according to Prototype.

++Prototype
A structure with ground arguments.

Description

ECLiPSe structure notation provides a way to use structures with field names rather than positional arguments. Note that this is not a new data type, just a new syntax for normal compound terms. It is intended to make programs more readable and easier to modify, without compromising efficiency (it is implemented by macro expansion).

E.g. if a structure is declared by specifying the prototype

    book(author, title, year, publisher)
then subsequently book/4-terms can be written as follows:

    book with []
    book with title:'tom sawyer'
    book with [year:1886,title:'tom sawyer']
which will be completely equivalent to the usual

    book(_, _, _, _)
    book(_, 'tom sawyer', _, _)
    book(_, 'tom sawyer', 1886, _)
The advantage is that the order and position of the fields or the arity of the whole structure do not have to be known and can be changed by just changing the initial declaration.

The argument index of a field in a structure can be obtained using a term of the form

    FieldName of StructName
so instead of arg(3,B,Y) one can write

    arg(year of book, B, Y)
Structures can also be declared to contain other structures, e.g.

    :- local struct(film(based_on:book,director,year)).
This allows the fields of book to be accessed as if they were fields of film. Note that the declaration of the year-field in the film-structure hides the year-field in the book structure.

Fail Conditions

None.

Resatisfiable

No.

Exceptions

(4) Instantiation fault
Struct is not ground.
(5) Type error
Struct is neither variable nor structure.

Examples


    % A simple structure:

    [eclipse 1]: local struct(person(name,address,age)).

    yes.
    [eclipse 2]: John = person with [age:30,name:john],
            John = person with age:A,
            arg(name of person, John, N).

    John = person(john, _146, 30)
    A = 30
    N = john
    yes.


    % Example for structure inheritance:

    [eclipse 3]: local struct(employee(p:person,salary)).

    yes.
    [eclipse 4]: Emp = employee with [name:john,salary:2000].

    Emp = employee(person(john, _105, _106), 2000)
    yes.
    
    [eclipse 5]: Emp = employee with [name:john,salary:2000],
            Emp = employee with [p:Person,salary:S],
            arg(name of employee, Emp, N).

    Person = person(john, _169, _170)
    S = 2000
    Emp = employee(person(john, _169, _170), 2000)
    N = john
    yes.


    % Subscript syntax can be used with structures:

    [eclipse 6]: Emp = employee with [name:john,salary:2000],
             Cost is 5 * Emp[salary of employee].

     Cost = 10000
     Emp = employee(person(john, _137, _138), 2000)
     yes.



See Also

local / 1, export / 1, global / 1, current_struct / 1, arg / 3, subscript / 3