130 likes | 255 Views
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
E N D
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 • Container names • records • non-logical variables and arrays • references
:- 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).
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)
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
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).
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
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.
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
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
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)
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)