Re: What should the semantics of hierarchical modules be?
- From: "Brian Hulley" <brianh@xxxxxxxxxxxx>
- Date: 30 Apr 2005 14:05:47 -0700
tmp123 wrote:
> Brian Hulley wrote:
> > Hi -
> > I'm creating a new implementation of prolog with a hierarchical
> module
> > system. However I'm having a lot of trouble trying to decide what
the
> > semantics of the module system should be, and would be grateful for
> any
> > suggestions on the following:
> >
> > 1) Suppose there is a top level module 'a' with a public predicate
> > pred/0.
> > 2) Suppose 'a' contains a nested module 'b', which contains other
> > predicates.
> > 3) Suppose we are at the top level or in some other module outside
> this
> > hierarchy, and the following goal is given:
> >
> > ?- .a.b.pred .
> >
> > What should happen? (leading dot indicates an absolute path)
> >
> > I can think of two alternatives, but I can't decide which one would
> be
> > the most desirable. The first would be to say that a.b.pred means
> > "lookup pred/0 starting from the module found by traversing the
path
> > '.a.b' from the current module", in which case .a.pred would be
> called,
> > and the second would be to say "find the module given by .a.b then
> > execute this module's definition of pred/0".
> >
>
> If both modules has "pred": the first alternative goes always to
> .a.pred; the second one allows to select .a.pred or .b.pred.
[When b's pred/0 is private as it was in the example below]
Somewhere in the back of my mind I was still thinking in terms of the
Quintus/SICStus/SWI-Prolog (for example) approach of using the prefix
to denote "act as if you are now inside this module" instead of the
Ciao/ECLiPSe/Mercury (for example) approach, which is analogous to the
C++/Java/C# use of namespaces and means "use this module's definition
of the predicate"
> How would you define abstract classes?
The module system I'm creating is not intended to provide an object
oriented framework, only the equivalent of nested namespaces in C++/C#.
However some advantages of abstract classes can be gained by
introducing the concept of closures to Prolog. This also solves all
problems associated with user-defined meta predicates - in my Prolog
there are none! :-) The only meta-predicates are the control constructs
, ; ! -> \+ and built-ins like assert, retract and call, none of which
can be qualified by module prefixes or redefined.
For example:
[module(foo)]
hello(X) :- write(X),write(' says hello from inside module foo').
bye(X) :- write(bye), write(X), nl.
[public]
test :-
create_closure((hello(X), bye(X)),[X],Closure),
util.dotimes(5, Closure).
[endmodule]
[module(util)]
[public]
dotimes(N, Closure) :-
(
N > 0
->
closure(Closure, N), % Closure is a reference like #c1 etc
N1 is N-1,
dotimes(N1, Closure)
;
true
).
[endmodule]
?- test.
5 says hello from inside module foo bye 5
4 says...
This also has the advantage that the term (hello(X), bye(X)) is only
compiled once, not 5 times as in most other Prologs I know of, and of
course there is no need to pass any special hidden arguments because
the closure itself knows what module it was created in.
Regards, Brian.
.
- References:
- What should the semantics of hierarchical modules be?
- From: Brian Hulley
- Re: What should the semantics of hierarchical modules be?
- From: tmp123
- What should the semantics of hierarchical modules be?
- Prev by Date: Re: Non dominating queens problem
- Previous by thread: Re: What should the semantics of hierarchical modules be?
- Index(es):
Relevant Pages
|
|