Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Why is the `Data.Int` type not a `Semigroup` in PureScript but `String` is?

+1
−0
> "lo" <> "fa"
"lofa"

> 1 <> 2
Error found:
in module $PSCI
at :1:1 - 1:7 (line 1, column 1 - line 1, column 7)

  No type class instance was found for
                                
    Data.Semigroup.Semigroup Int

Is this for purely technical reasons as there would have to be at least 2 Semigroup instances (for addition and multiplication), but type classes cannot be implemented multiple times for the same type?

Even if it could be done using instance chaining, one would have to create an instance of the entire "stack" of algebraic abstractions (Monoid, Group, Commutative) for each operation, right?

In contrast, Semiring is conveniently defined as a set with 2 binary operations1, which fits the bill perfectly:

A semiring (S, +, ) is a set S with two binary compositions such that (S, +) and (S, ) are semigroups and distributes over +.
source, pdf

So, technically, Int is a semigroup under addition (a commutative monoid even), but PureScript is not capable to "infer" that Semigroup's append (<>) and Semiring's add (+), in effect, do the same thing, but, for obvious reasons, cannot be used interchangeably.2

Then again, I'm quite new to pure functional programming, PureScript, and abstract algebra, so my statements above may not make much sense - and thanks in advance for dispelling my confusion!


[1]: Although, nLab states that "mathematicians disagree on the definition of a semiring"...

[2]: Are there programming languages where this level of granularity is achieved? (That is, provided if this is even a thing and if I didn't totally misinterpret these mathematical ideas.)

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+2
−0

Is this for purely technical reasons as there would have to be at least 2 Semigroup instances (for addition and multiplication), but type classes cannot be implemented multiple times for the same type?

Yes, basically this. When there is more than one plausible way to interpret a type as an instance of a class, and no reason to strongly favor one over the others, the typical thing to do is to create newtypes for each possible interpretation and define instances on the newtypes. That way the user can specify which instance to use by wrapping and unwrapping a particular newtype.

For Int (or any other type with a Semiring instance), you can use the Additive and Multiplicative types to get those two semigroups.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Works for me (1 comment)

Sign up to answer this question »