Title: ECLiPSe-specific Language features
1ECLiPSe-specific Language features
2Overview
- Structure notation
- Array notation
- Loops
- Matching
- Modules and Libraries
- Saving information across backtracking
- Input and Output
- Data-driven computation ? later
3Structures and Arrays
- In an untyped language, structure array
- PersonStruct p(tom, 24, accountant)
- TimeArray start(2.4, 5.6, 3.4, 7.1)
- ECLiPSe has special syntactic sugar for both
4Structure notation (I)
- Syntactic sugar for writing structures
- Declaration
- - local struct(book(author, title, year)).
- Structure syntax
- booktitle"Tom Sawyer", authortwain
- instead of
- book(twain, "Tom Sawyer", _)
- Use in any context, e.g.
- p(bookyearY) -
- ...
- Book bookauthortwain,yearY,
- ...
5Structure notation (II)
- Benefits of using structure notation
- Fields referred to by name, not position
- Fields can be written in any order
- Dont-care fields can be omitted
- Easy to add new fields
- - local struct(book(author, title, year,
publisher)). - Only need to change code where publisher is
relevant
6Structure notation (III)
- Two more primitives
- Access to field number
- year of book instead of 3
- e.g.
- arg(year of book, Book, Year)
- sort(year of book, lt, Books, OrderedBooks)
- Copying all-but-some fields, e.g.
- ?- Book bookauthortwain,title"Tom
Sawyer",year1876, - update_struct(book,authorharvey,year1976,
Book, MyBook). - Book book(twain, "Tom Sawyer", 1876)
- MyBook book(harvey, "Tom Sawyer", 1976)
- Yes
7Array Notation (I)
- Structures can be used as arrays
- By convention, we use the functor for
arrays - Vector (1-d array) ? normal structure
- A1 (1,2,3)
- A2 (_, apple, orange, _)
- Multidimensional array ? nested structures
- M ((1,2,3), (4,5,6))
- Within expressions, elements can be accessed via
subscripts - X is M1,2 M2,3.
- Example
- ?- M ((2, 3, 5), (1, 4, 7)),
- X is M1,2 M2,3.
- X 10
- Yes
8Array Notation (II)
- Creation (or dimension check)
- ?- dim(M, 2, 3).
- M ((_, _, _), (_, _, _))
- Yes
- ?- dim(M4, 3, 4, 2, 1).
- M4
- Yes
- ?- M ((1, 2, 3), (4, 5, 6)),
- dim(M, Dim).
- Dim 2, 3
- Yes
9Array Notation (III)
- Accessing sub-matrices
- (Note that the sub-matrices are lists)
- ?- Matrix ((1,2,3),(4,5,6),(7,8,9)),
- Row2 is Matrix2,1..3,
- SubRow2 is Matrix2,2..3,
- Column1 is Matrix1..3,1.
- Row2 4, 5, 6
- SubRow2 5, 6
- Column1 1, 4, 7
- Yes
10Array Notation (IV)
- Subscript-evaluation only done in
expression-context! - ?- Matrix ((1,2,3),(4,5,6),(7,8,9)),
- writeln(Matrix2,3).
- prints Matrix2,3 !!!
- ?- Matrix ((1,2,3),(4,5,6),(7,8,9)),
- X is Matrix2,3,
- writeln(X).
- prints 6
11Loops (I)
- In Prolog, the only way to express iteration is
to use recursion, which can be cumbersome - ECLiPSe provides do loops
- ( IterationSpecs do Goals )
- Programming idioms covered
- Fully
- Iteration
- Aggregation
- Mapping
- Partly
- Filtering
- While-loop
12Iteration (over list elements)
- With this
- write_list(List) -
- write("List "),
- ( foreach(X,List) do write(X)
- ).
- Replace this
- write_list(List) -
- write("List "),
- write_list1(List).
- write_list1().
- write_list1(XT) -
- write(X),
- write_list1(T).
13Iteration (over range of numbers)
- With this
- write_nat(N) -
- write(Numbers "),
- ( for(I,1,N) do
- write(I)
- ).
- Replace this
- write_nat(N) -
- write(Numbers "),
- write_nat1(0,N).
- write_nat1(N, N) - !.
- write_nat1(I0, N) -
- I is I01,
- write(I),
- write_nat1(I, N).
14Aggregation
- Replace this
- sumlist(Xs, S) -
- sumlist(Xs, 0, S).
- sumlist(, S, S).
- sumlist(XXs, S0, S) -
- S1 is S0X,
- sumlist(Xs, S1, S).
- With this
- sumlist(Xs, S) -
- ( foreach(X,Xs),
- fromto(0,S0,S1,S)
- do
- S1 is S0X
- ).
iterate
aggregate
15Mapping
- With this
- map - ,
- ( foreach(X,Xs),
- foreach(Y,Ys)
- do
- Y is X1,
- ), .
- Replace this
- map - ,
- add_one(Xs, Ys), .
- add_one(, ).
- add_one(XXs, YYs) -
- Y is X1,
- add_one(Xs, Ys).
iterate
construct
16General Form
( IterationSpecs do Body )
sequence of
- foreach(Elem, List) list iterator / aggregator
- foreacharg(Arg, Structure) structure/array
iterator - count( I, Min, Max) integer iterator /
aggregator - for( I, Min, Max ,Step) numeric iterator
- param(X1, X2, ) constant iterator
- fromto(First, In, Out, Last) generic iterator /
aggregator
17The generic fromto-iterator
- ( fromto(First, In, Out, Last) do In?Out )
- ( fromto(First, In, Out, Last) do In?Out )
( fromto(First, In, Out, Last) do In?Out )
Fromto can express all other iterators,
e.g. fromto(List, XXs, Xs, )
? foreach(X,List)
18Loop Example
factorial(N, Ans) - ( for(I, 1, N),
fromto(1, In, Out, Ans) do Out
is In I ).
1
2
N
I
I N
I
Ans
1!
N!
2!
1
...
Out
In
Out
In
In Out
Stop!
Out is 1 1
Out is 1 2
19Transformation scheme
- A goal
- ,( IterationSpecifiers do Body ),
- Is replaced by
- , PreCallGoals, µ(CallArgs),
- With an auxiliary predicate µ defined as
- µ(BaseArgs) - !.
- µ(HeadArgs) -
- PreBodyGoals,
- Body,
- µ(RecArgs).
20The module system
- In ECLiPSe, libraries are generally implemented
as modules - To use a user-defined module my_module, add
- - use_module(my_module).
- at the top of your code
- For an ECLiPSe library such as ic, use one of
- - use_module(library(ic)).
- - lib(ic).
- This gives access to all the facilities provided
by the module - Each module has its own name space
- Ambiguities resolved through module
qualification - icalldifferent(List)
21Solvers and Modules
- Different solver libraries can implement
different behaviours of the declaratively same
constraint - If a constraint has the same meaning, we use the
same name! - ?- lib(ic), lib(ic_global).
- ?- alldifferent(1, 2, 3).
- Ambiguous import of alldifferent / 1 from ic,
ic_global - in module eclipse
- calling an undefined procedure alldifferent(1,
2, 3) - in module eclipse
- Abort
- In case of ambiguity
- Resolve by explicit import - import
alldifferent/1 from ic. - Specify solver explicitly every time
icalldifferent(1,2,3)
22Specifying a solver explicitly
- Qualify constraint with solver module
- ic_globalalldifferent(List)
- ic(A B)
- Can pass constraint to more than one solver
- ic, eplex(A gt B)
- Solver can be a variable at compile time
- Solver(A gt B)
- Caution
- When used as constraints, gt, lt, gt, lt, and
\ should always be qualified because they
default to standard arithmetic (module
eclipse_language) even in the case of ambiguity!
23Saving information across backtracking
- Abstract container types
- Are identified by a handle
- Store copies of arbitrarily complex values
- Retain their contents across backtracking
- Contents accessed according to type
- Bags unordered bag enter one / retrieve all
- Shelves array set/get indexed element
- Stores hash map set/get keyed element
24Example
- Bag
- simple_findall(Goal, Solutions) -
- bag_create(Bag),
- (
- call(Goal),
- bag_enter(Bag, Goal),
- fail
-
- bag_retrieve(Bag, Solutions)
- ).
Shelf count_solutions(Goal, Total) -
shelf_create(count(0), Shelf), (
call(Goal), shelf_get(Shelf, 1,
Old), New is Old 1,
shelf_set(Shelf, 1, New), fail
shelf_get(Shelf, 1, Total)
).
A store is like a shelf, but with arbitrary keys
instead of integers
25Input / Output Formats
- Character-wise
- put get
- Arbitrary terms, various formats, human readable
- write, printf read_string, read_token
- Arbitrary terms, ECLiPSe syntax, can be read
back - writeq, write_canonical read
- Restricted terms, exchange with other languages
- write_exdr read_exdr
26I/O streams and devices
- I/O device How to open
- tty open by default (input, output, error)
- file open(FileName,Mode,Stream)
- pipe exec/2, exec/3 and exec_group/3
- socket socket/3, bind/2 and accept/3
- string open(string(String), Mode, Stream)
- queue open(queue(String), Mode, Stream)
- null open by default (null stream)
In memory
27Summary Prolog ? ECLiPSe
- Data structures
- Use named structures for record-style data
- Use arrays for indexed access
- Strings are a separate data type, not list of
characters - Control structures
- Use loops for iteration
- Saving information across backtracking
- Use bags, shelves or stores (dont use
assert/retract!) - Code structure
- Understand modules (libraries / serious
applications)
28Exercises
- Loops make a list containing N occurrences of C
- Loops Given a list of integers, make a list
containing only the positive ones - Arrays and loops make an array containing N
integers such that AI contains I - Arrays, lists and loops collect all elements of
a 2-d matrix into a list (row-wise) - Rewrite your magic-square program with matrices
and generalise to N x N