Salvador Fandiño García


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)),
              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,
                                        ... },


  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.


  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 =.




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)];


  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(foo(hello, Hello))

The old auto_functor tag has been obsoleted.


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


Copyright 2002-2006 by Salvador Fandiño (

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

Hosting generously
sponsored by Bytemark