1 / 13

Using ECLiPSe Modules

Using ECLiPSe Modules. Joachim Schimpf. Why Modules?. Structuring non-trivial applications Information hiding libraries, packages Avoiding name clashes. What is under Visibility Control?. Predicate names Structure names Syntax settings operators macros character classes

toya
Download Presentation

Using ECLiPSe Modules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using ECLiPSe Modules Joachim Schimpf

  2. Why Modules? • Structuring • non-trivial applications • Information hiding • libraries, packages • Avoiding name clashes

  3. What is under Visibility Control? • Predicate names • Structure names • Syntax settings • operators • macros • character classes • Container names • records • non-logical variables and arrays • references

  4. :- module(m1). :- export q/3. q(X,Y,Z) :- Z is X*7+Y. p(hello). % local in m1 Predicate visibility :- module(m2). :- export p/2. :- import m1. p(A, B) :- q(A, 3, B).

  5. Visibility Rules for Predicate Names For a certain predicate name/arity: • Every module can contain at most one definition • this definition may be local or exported • In every module, at most one definition is visible • if there is a definition in a module, this is also the visible one • otherwise, if there is an import, this is the visible one • All exported definitions are accessible everywhere • using explict module qualification e.g. mod:pred(X,Y,Z)

  6. Qualified access via :/2 • Used to specify a lookup module for a predicate, e.g. • utilities : print_list([1,2,3]) • Use brackets if the prediate is in infix notation! • range : (X :: 1..5) • The called predicate must be an exported one

  7. Resolving name conflicts • Hiding an imported definition with a local one • :- import mod1. % where mod1 exports p/1 • :- local p/1. % local definition will hide the one from mod1 • p(99).

  8. Resolving import conflicts • Two modules export the same name • :- import m1. % m1 exports p/1 • :- import m2. % m2 exports p/1 • mypred(X) :- • m1:p(X). % explicitly specify the module

  9. Interface contol • import • :- import utilities. % everything the module exports • :- import p/1,q/3 from utilities. % only these predicate • export • :- export p/1,q/3. • reexport (import+export) • :- reexport utilities. • :- reexport utilities except p/1. • :- reexport p/1,q/3 from utilities.

  10. Making Modules from Modules m2 interface Extend: reexport reexport export m2 interface define m1 interface Restrict: reexport reexport except m1 interface m2 interface Modify: reexport reexport except export (re)define m1 interface m3 interface Combine: reexport reexport reexport reexport m1 interface m2 interface

  11. Tools - Motivation • Some predicates need to know from which module they were called • meta-predicates, i.e. predicates that have arguments which are goals • I/O predicates, in order to use the proper syntax settings • In ECLiPSe we call them tools • The system automatically adds a caller module argument when calling a tool

  12. Tools - Declaration and Example • Example: a predicate that calls a goal twice • Module-capable version: • :- tool(twice/1, twice/2). • twice(Goal, M) :- • call(Goal) @ M, • call(Goal) @ M. • A call to twice/1 is effectively replaced: • :- module(m). • ... • twice( p(X) ), • ... • That way, p/1 can be called by twice even if it is only visible in m • Non-module version • twice(Goal) :- • call(Goal), • call(Goal). twice( p(X), m)

  13. Lookup Module vs. Caller Module Definition of twice/1 is looked up in module: Caller module argument added if twice/1 is a tool: :- module(m). ...,twice(X),... m m ...,lm:twice(X),... lm m ...,twice(X)@cm,... m cm ...,lm:twice(X)@cm,... lm cm ...,call(twice(X))@cm,... cm cm • :/2 specifies the lookup module (to find the definition) • @/2 specifies the caller module (to know the context)

More Related