unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Idiomatic Guile for semigroup/monoid/group?
@ 2022-01-01 23:45 Stuart Hungerford
  2022-01-02 10:46 ` Maxime Devos
  2022-01-03 10:02 ` zimoun
  0 siblings, 2 replies; 6+ messages in thread
From: Stuart Hungerford @ 2022-01-01 23:45 UTC (permalink / raw)
  To: guile-user

I'm new to Guile so I'm working my way through the Guile Reference
Manual and the Dybvig Scheme book.

I'd like to use Guile in my self-learning of abstract
algebra--semigroups, monoids and groups only for now. It seems that
with Guile's macros, access to C libraries and rich numeric tower, it
would be a good environment for this kind of programmatic exploration.

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?

For reference, I'm still to read:

https://www.reddit.com/r/scheme/comments/q824ps/is_there_a_portable_way_to_do_type_classes/

https://www.deinprogramm.de/scheme-2005/01-garcia/01-garcia.pdf

Any advice much appreciated,

Stu



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiomatic Guile for semigroup/monoid/group?
  2022-01-01 23:45 Idiomatic Guile for semigroup/monoid/group? Stuart Hungerford
@ 2022-01-02 10:46 ` Maxime Devos
  2022-01-02 23:46   ` Stuart Hungerford
  2022-01-03 10:02 ` zimoun
  1 sibling, 1 reply; 6+ messages in thread
From: Maxime Devos @ 2022-01-02 10:46 UTC (permalink / raw)
  To: Stuart Hungerford, guile-user

[-- 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 --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiomatic Guile for semigroup/monoid/group?
  2022-01-02 10:46 ` Maxime Devos
@ 2022-01-02 23:46   ` Stuart Hungerford
  2022-01-03 10:14     ` Maxime Devos
  0 siblings, 1 reply; 6+ messages in thread
From: Stuart Hungerford @ 2022-01-02 23:46 UTC (permalink / raw)
  To: Maxime Devos; +Cc: guile-user

On Sun, Jan 2, 2022 at 9:46 PM Maxime Devos <maximedevos@telenet.be> wrote:

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

Thanks for that advice -- much appreciated.

> 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/>:

That's very interesting. I'd been wondering if there was an extension
for Guile that did what Typed Racket does for Racket or what Coalton
does for Common Lisp.

Just out of interest, are there other multimethod/ad-hoc polymorphism
approaches for Guile?

Thanks,

Stu



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiomatic Guile for semigroup/monoid/group?
  2022-01-01 23:45 Idiomatic Guile for semigroup/monoid/group? Stuart Hungerford
  2022-01-02 10:46 ` Maxime Devos
@ 2022-01-03 10:02 ` zimoun
  1 sibling, 0 replies; 6+ messages in thread
From: zimoun @ 2022-01-03 10:02 UTC (permalink / raw)
  To: Stuart Hungerford, guile-user

Hi,

On Sun, 02 Jan 2022 at 10:45, Stuart Hungerford <stuart.hungerford@gmail.com> wrote:

> I'd like to use Guile in my self-learning of abstract
> algebra--semigroups, monoids and groups only for now. It seems that
> with Guile's macros, access to C libraries and rich numeric tower, it
> would be a good environment for this kind of programmatic exploration.

Well, I am not convinced that Guile is the correct language for
exploring these concepts…

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

…as you noted, they are naturally expressed in Haskell or friends.
While using Guile, you need first to learn Guile internals and probably
many hacks before the exploration itself.

> What would the idiomatic Guile forms be to work with these structures?
> Would it involve GOOPS or records?

That’s said, take my comment with a lot of salt, I would try to go via
the Guile module system.  Somehow as OCaml or Coq are doing; parametric
modules quickly said. :-)


Cheers,
simon



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiomatic Guile for semigroup/monoid/group?
  2022-01-02 23:46   ` Stuart Hungerford
@ 2022-01-03 10:14     ` Maxime Devos
  2022-01-03 21:02       ` Stuart Hungerford
  0 siblings, 1 reply; 6+ messages in thread
From: Maxime Devos @ 2022-01-03 10:14 UTC (permalink / raw)
  To: Stuart Hungerford; +Cc: guile-user

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

Stuart Hungerford schreef op ma 03-01-2022 om 10:46 [+1100]:
> Just out of interest, are there other multimethod/ad-hoc polymorphism
> approaches for Guile?

I only know of GOOPS and Theme-D -- Guix also has a
‘define-gexp-compiler’ but it's very ad-hoc.

It allows defining something like

(define (lower stuff system target)
  (cond ((derivation? stuff) stuff) ; done
        ((foo? stuff) (lower (lower-foo stuff system target)))
        ((bar? stuff) (lower (lower-bar stuff system target)))
        [...]))

(where 'lower-stuff' converts 'stuff' into something 'lowerable'),
but in an extensible way.

Greetings,
Maxime.

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiomatic Guile for semigroup/monoid/group?
  2022-01-03 10:14     ` Maxime Devos
@ 2022-01-03 21:02       ` Stuart Hungerford
  0 siblings, 0 replies; 6+ messages in thread
From: Stuart Hungerford @ 2022-01-03 21:02 UTC (permalink / raw)
  To: Maxime Devos; +Cc: guile-user

On Mon, Jan 3, 2022 at 9:14 PM Maxime Devos <maximedevos@telenet.be> wrote:

> Stuart Hungerford schreef op ma 03-01-2022 om 10:46 [+1100]:
> > Just out of interest, are there other multimethod/ad-hoc polymorphism
> > approaches for Guile?
>
> I only know of GOOPS and Theme-D -- Guix also has a
> ‘define-gexp-compiler’ but it's very ad-hoc.
>
> It allows defining something like
>
> (define (lower stuff system target)
>   (cond ((derivation? stuff) stuff) ; done
>         ((foo? stuff) (lower (lower-foo stuff system target)))
>         ((bar? stuff) (lower (lower-bar stuff system target)))
>         [...]))
>
> (where 'lower-stuff' converts 'stuff' into something 'lowerable'),
> but in an extensible way.

That's interesting -- thanks.

Stu



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-01-03 21:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-01 23:45 Idiomatic Guile for semigroup/monoid/group? Stuart Hungerford
2022-01-02 10:46 ` Maxime Devos
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

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