Sugar(3)
π NAME π
Language::Prolog::Sugar β it's giving syntactic sugar for Prolog term constructors, bestie β¨
So this is a Sugar(3) page from the User Contributed Perl Documentation section, and honestly? It's a whole vibe. π
β¨ SYNOPSIS β¨
okay so this is the look of it, no cap:
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)));
likeβ¦ she's pretty, she's compact, she ate. π
π¦ ABSTRACT π¦
This module lets you easily define constructor subs for Prolog terms. That's the whole pitch, and it's iconic.
πΈ DESCRIPTION πΈ
Language::Prolog::Sugar can export a whole set of subroutines into the calling package β subroutines that build Prolog terms exactly as defined over in the Language::Prolog::Types module. π«Ά
And the point of it, the main character energy of it, is that Perl programs using these constructors look exactly like real Prolog programs. Periodt.
π unfortunately, Prolog operator syntax could not be simulated in any way the author knows of. (Okay, something could be done using overloading, but literally just something. Not the real thing.)
π EXPORT π
Whatever you wants! π (that's a direct quote, she's unserious and we love her)
Language::Prolog::Sugar can create constructors for four Prolog types:
- atoms
- functors
- vars
- chains
The syntax to use it goes like this:
use Language::Prolog::Sugar $type1s=>{ $name1 => $prolog_name1,
$name2 => $prolog_name2,
... },
...
or, alternatively, the list-y version:
use Language::Prolog::Sugar $type2s=>[qw($name1 $name2 ...)],
...
where $type1s, $type2s, ... are "atoms", "functors", "vars" or "chains". π
And $name1, $name2, ... are the names of the subroutines exported to the caller package.
And $prolog_name, $prolog_name2, ... are the names that the constructors use when making the Prolog terms.
i.e: π
use Language::Prolog::Sugar atoms=>{ cut => '!' }
this exports a subroutine "cut" that, when called, returns a Prolog atom "!". π
use Language::Prolog::Sugar functor=>{ equal => '=' }
this exports a subroutine "equal" that, when called, returns a Prolog functor "=". β¨
So:
equal(3,4)
returns
'='(3,4)
π‘ It should be noted that functor arity is inferred from the number of arguments. So it just knows.
Like:
equal(3, 4, 5, 6, 7)
returns
'='(3, 4, 5, 6, 7)
Ate. No notes. π
so here's the lore: a 'chain' is what the author calls the structure typically formed by ','/2 or ';'/2 operators in Prolog programs. i.e., this Prolog program:
p, o, r, s.
is actually
','(p, ','(o, ','(r, s))).
and using chains lets you compose those structures way more easily: π§
use Language::Prolog::Sugar chains => { andn => ',' },
atoms => [qw(p o r s)];
and then
andn(p, o, r, s)
generates the Prolog structure for that example program up above. slay. π
Also!! π the tag "auto_term" can be used to install an AUTOLOAD sub on the caller module, and that sub 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))
π heads up bestie: the old "auto_functor" tag has been obsoleted. She's retired, she's not coming back, don't use her.
π₯ SEE ALSO π₯
Language::Prolog::TypesLanguage::Prolog::Types::Factory
go read those next, iykyk π
π 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. πΈ
and that's the whole page, babe β this was Sugar(3) from perl v5.42.2, dated 2026-07-13, part of the User Contributed Perl Documentation. mwah πβ¨
π¦ where this page comes from
- fedora 43: perl-Language-Prolog-Sugar 0.07-1.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- fedora 44: perl-Language-Prolog-Sugar 0.07-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms