The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

NAME

Language::Prolog::Sugar - Syntactic sugar for Prolog term constructors

SYNOPSIS

use Language::Prolog::Sugar vars => [qw(X Y Z)];
use Language::Prolog::Sugar functors =>{ equal => '=',
minus => '-' };
use Language::Prolog::Sugar functors =>[qw(is)];
use Language::Prolog::Sugar atoms =>[qw(foo bar)];
use Language::Prolog::Sugar atoms =>{ cut => '!' };
use Language::Prolog::Sugar chains =>{ andn => ',',
orn => ';' };
$term=andn( equal(X, foo),
orn( equal(Y, [4, bar]),
equal(Y, foo)),
cut,
is(Z, minus(34, Y)));

ABSTRACT

This module allows you to easily define constructor subs for Prolog terms.

DESCRIPTION

Language::Prolog::Sugar is able to export to the calling package a set of subrutines to create Prolog terms as defined in the Language::Prolog::Types module.

Perl programs using these constructors have the same look as real Prolog programs.

Unfortunately Prolog operators syntax could not be simulated in any way I know (well, something could be done using overloading, but just something).

EXPORT

Whatever you wants!

Language::Prolog::Sugar can create constructors for four Prolog types: atoms, functors, vars and chains.

The syntax to use it is as follows:

use Language::Prolog::Sugar $type1s=>{ $name1 => $prolog_name1,
$name2 => $prolog_name2,
... },
...

or

use Language::Prolog::Sugar $type2s=>[qw($name1 $name2 ...)],
...

$type1s, $type2s, ... are atoms, functors, vars or chains.

$name1, $name2, ... are the names of the subrutines exported to the caller package.

$prolog_name, $prolog_name2, ... are the names that the constructors use when making the Prolog terms.

i.e:

use Language::Prolog::Sugar atoms=>{ cut => '!' }

exports a subrutine cut that when called returns a Prolog atom !.

use Language::Prolog::Sugar functor=>{ equal => '=' }

exports a subrutine equal that when called returns a Prolog functor =.

equal(3,4)

returns

'='(3,4)

It should be noted that functor arity is inferred from the number of arguments:

equal(3, 4, 5, 6, 7)

returns

'='(3, 4, 5, 6, 7)

I call 'chain' the structure formed tipically by ','/2 or ';'/2 operators in Prolog programs. i.e., Prolog program

p, o, r, s.

is actually

','(p, ','(o, ','(r, s))).

using chains allows for a more easily composition of those structures:

use Language::Prolog::Sugar chains => { andn => ',' },
atoms => [qw(p o r s)];

and

andn(p, o, r, s)

generates the Prolog structure for the example program above.

Also, the tag auto_term can be used to install and AUTOLOAD sub on the caller module that would make a functor, term or variable for every undefined subroutine. For instance:

use Language::Prolog::Sugar 'auto_term';
swi_call(use_module(library(pce)));
swi_call(foo(hello, Hello))

The old auto_functor tag has been obsoleted.

SEE ALSO

Language::Prolog::Types, Language::Prolog::Types::Factory

COPYRIGHT AND LICENSE

Copyright 2002-2006 by Salvador Fandiño (sfandino@yahoo.com).

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.