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.
Post History
> "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 ...
#1: Initial revision
Why is the `Data.Int` type not a `Semigroup` in PureScript but `String` is?
``` > "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`][1] 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][2], one would have to create an instance of the entire "stack" of algebraic abstractions ([`Monoid`][3], [`Group`][4], [`Commutative`][5]) for each operation, right? In contrast, [`Semiring`][6] is conveniently defined as a set with 2 binary operations<sup><b>1</b></sup>, 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 `+`. > <sup>[source, pdf][7]</sup> So, technically, [`Int`][9] **is** a semigroup under addition ([a commutative monoid even][10]), but PureScript is not capable to "infer" that [`Semigroup`][1]'s `append` (`<>`) and [`Semiring`][6]'s `add` (`+`), in effect, do the same thing, but, for obvious reasons, cannot be used interchangeably.<sup><b>2</b></sup> 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! --- <sup>\[1]: Although, nLab states that "_[mathematicians disagree on the definition of a semiring][8]_"...</sup> <sup>\[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.)</sup> [1]: https://pursuit.purescript.org/packages/purescript-prelude/6.0.1/docs/Data.Semigroup [2]: https://stackoverflow.com/questions/50290695/how-to-use-instance-chain-to-solve-overlapping [3]: https://pursuit.purescript.org/packages/purescript-prelude/6.0.1/docs/Data.Monoid#t:Monoid [4]: https://pursuit.purescript.org/packages/purescript-group/4.1.1/docs/Data.Group [5]: https://pursuit.purescript.org/packages/purescript-group/4.1.1/docs/Data.Semigroup.Commutative [6]: https://pursuit.purescript.org/packages/purescript-prelude/6.0.1/docs/Data.Semiring [7]: https://projecteuclid.org/journals/journal-of-science-of-the-hiroshima-university-series-ai-mathematics/volume-30/issue-2/On-semigroups-semirings-and-rings-of-quotients/10.32917/hmj/1206139104.pdf [8]: https://ncatlab.org/nlab/show/semiring [9]: https://pursuit.purescript.org/packages/purescript-integers/6.0.0/docs/Data.Int [10]: https://pursuit.purescript.org/packages/purescript-prelude/6.0.1/docs/Data.Semiring#t:Semiring