Language::Prolog::Sugar - Syntactic sugar for Prolog term constructors
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)));
This module allows you to easily define constructor subs for Prolog terms.
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).
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.
$type1s
$type2s
atoms
functors
vars
chains
$name1, $name2, ... are the names of the subrutines exported to the caller package.
$name1
$name2
$prolog_name, $prolog_name2, ... are the names that the constructors use when making the Prolog terms.
$prolog_name
$prolog_name2
i.e:
use Language::Prolog::Sugar atoms=>{ cut => '!' }
exports a subrutine cut that when called returns a Prolog atom !.
cut
!
use Language::Prolog::Sugar functor=>{ equal => '=' }
exports a subrutine equal that when called returns a Prolog functor =.
equal
=
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)
'='(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:
auto_term
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.
auto_functor
Language::Prolog::Types, Language::Prolog::Types::Factory
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.
To install Language::Prolog::Sugar, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Language::Prolog::Sugar
CPAN shell
perl -MCPAN -e shell install Language::Prolog::Sugar
For more information on module installation, please visit the detailed CPAN module installation guide.