Sugar(3)

πŸ“¦
version 0.07 Β· 3 versions
, in fedora 43, fedora 44

πŸ’– 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::Types
  • Language::Prolog::Types::Factory

go read those next, iykyk πŸ’œ


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