unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Maxime Devos <maximedevos@telenet.be>
To: Stuart Hungerford <stuart.hungerford@gmail.com>, guile-user@gnu.org
Subject: Re: Idiomatic Guile for semigroup/monoid/group?
Date: Sun, 02 Jan 2022 10:46:43 +0000	[thread overview]
Message-ID: <7af1fd7e8bf7b0c84cdda3a431412bac31b45f18.camel@telenet.be> (raw)
In-Reply-To: <CAG+kMrFFpLv2O93DWE8Ea7m1qNRg=ZaQrdqoXOmk8Z-Q6jOZTA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2122 bytes --]

Stuart Hungerford schreef op zo 02-01-2022 om 10:45 [+1100]:
> In Haskell/Idris/Purescript/Swift/Rust I'd be looking to
> typeclasses/protocols or traits to model a semigroup, monoid or group.
> In OOP languages I'd be looking to some kind of abstract base class to
> model each structure.
> 
> What would the idiomatic Guile forms be to work with these structures?
> Would it involve GOOPS or records?

I wouldn't recommend using GOOPS methods, because a carrier set can
have many groups.

E.g., on the rational numbers (<rational> in GOOPS), both the
multiplication group and the addition group can be defined, but they
aren't isomorphic. (I'm ignoring 0 here)

My suggestion is to do as Haskell does, but make type class instances
explicit.

E.g., in Haskell one could define exponentiation (multiplicative) /
repeated addition (additive, not sure about standard terminology) as
something like

-- Not sure about the exact syntax, it has been a while!
exp :: Group G => Integer -> G -> G
exp 0 _ = identity
exp (+ 1 n) g = g * exp n g

This can be made more explicit:

-- first argument: identity element
-- second argument: function inverting an element
-- third argument: multiplication
data Group G = Group G (G -> G) (G -> G -> G)
identity :: Group G -> G
identity = _ -- maybe shorten to 'id'
mult :: Group G -> G -> G -> G -- maybe shorten to '*' in Scheme
mult = _
inverse :: Group G -> G -> G -- maybe shorten to '⁻¹' in Scheme

exp :: Group G -> Integer -> G -> G
exp gr 0 _ = identity gr
exp gr (+ 1 n) g = mult gr g (exp gr n g)

The downside is that passing all these 'Group G' objects around
might be a bit tedious.

Anyway, possibly things like this are already implemented in Theme-D
(not sure though): looking at the properties of Theme-D listed at
<https://www.tohoyn.fi/theme-d/>:

* [...]
* Static type system
* An object system
* A module system
* *Statically* and dynamically dispatched multimethods (emphasis mine)
* Parametrized (type parameters) classes, types, and procedures
* [...]
* Numeric tower
* [...]

Greetings,
Maxime.

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

  reply	other threads:[~2022-01-02 10:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-01 23:45 Idiomatic Guile for semigroup/monoid/group? Stuart Hungerford
2022-01-02 10:46 ` Maxime Devos [this message]
2022-01-02 23:46   ` Stuart Hungerford
2022-01-03 10:14     ` Maxime Devos
2022-01-03 21:02       ` Stuart Hungerford
2022-01-03 10:02 ` zimoun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7af1fd7e8bf7b0c84cdda3a431412bac31b45f18.camel@telenet.be \
    --to=maximedevos@telenet.be \
    --cc=guile-user@gnu.org \
    --cc=stuart.hungerford@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).