Skip to content

Commit

Permalink
Initial Commit: help/1 & dir/1
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Brown committed Jan 26, 2024
0 parents commit 5997ced
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# REPL Help

A couple of utility tools to aid in development by pretty-printing information
to the REPL.

## Loading

``` logtalk
{repl_help(loader)}.
```

## Use

Ensure the JSON library is loaded: `{json(loader)}`.

Find all the predicates for a current object at once:

``` logtalk
?- dir(json).
[generate/2,parse/2].
true.
```

Get information about a predicate:

``` logtalk
?- help(json::generate/2).
:- public(generate/2).
:- mode(generate(+compound,++term),one_or_error).
:- info(generate/2, [
comment is 'Generates the content using the representation specified in the first argument (``codes(List)``, ``stream(Stream)``, ``file(Path)``, ``chars(List)``, or ``atom(Atom)``) for the term in the second argument. Fails if this term cannot be processed.',
argnames is ['Sink','Term']
]).
true.
```

If you try to get information about an object or predicate that does not exist
an error will be thrown.
7 changes: 7 additions & 0 deletions loader.lgt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:- initialization((
logtalk_load([
repl_help
], [
optimize(on)
])
)).
59 changes: 59 additions & 0 deletions repl_help.lgt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
% Predicates for REPL help and information to aid development.

help(Obj::Predicate/Arity) :-
% Print the predicate definitions
guard_object_(Obj),
guard_predicate_(Obj, Predicate/Arity),
functor(Term, Predicate, Arity),
ignore(help_(scope, Obj, Term, Predicate, Arity)),
ignore(help_(mode, Obj, Term, Predicate, Arity)),
ignore(help_(info, Obj, Term, Predicate, Arity)).

dir(Obj) :-
% Print out all the current predicates for an Object
guard_object_(Obj),
findall(Predicate, Obj::current_predicate(Predicate), Predicates),
format('~n~q~n~n', [Predicates]).

% Rest of code is private
%
% % Guards
guard_object_(Obj) :-
current_object(Obj), !.
guard_object_(Obj) :-
throw(error(existance_error(object, Obj))).

guard_predicate_(Obj, Predicate) :-
Obj::current_predicate(Predicate), !.
guard_predicate_(_Obj, Predicate) :-
throw(error(existance_error(predicate_declaration, Predicate))).

% % Help info printers
help_(scope, Obj, Term, Predicate, Arity) :-
relaxed_predicate_property_(Obj, Term, scope(Scope)),
format('~n:- ~w(~q/~d).~n', [Scope, Predicate, Arity]).
help_(mode, Obj, Term, _Predicate, _Arity) :-
relaxed_predicate_property_(Obj, Term, mode(Args, Resp)),
format(':- ~q.~n', [mode(Args, Resp)]).
help_(info, Obj, Term, Predicate, Arity) :-
relaxed_predicate_property_(Obj, Term, info([Info|Infos])),
format(':- info(~q/~d, [~n', [Predicate, Arity]),
write_infos_(Infos, Info),
format(']).~n~n', []).

% % Make predicate_property/2 fail instead of throw
relaxed_predicate_property_(Obj, Term, Property) :-
catch(
Obj::predicate_property(Term, Property),
error(domain_error(predicate_property, _Term), _),
fail
).

% % Write out individual info/2 information
write_infos_([], Info) :-
Info =.. [Functor, Arg],
format(' ~q is ~q~n', [Functor, Arg]).
write_infos_([Next|Rest], Info) :-
Info =.. [Functor, Arg],
format(' ~q is ~q,~n', [Functor, Arg]),
write_infos_(Rest, Next).

0 comments on commit 5997ced

Please sign in to comment.