[NumericPrelude] GCD as monoid

Paul McJones paul at mcjones.org
Sun Jul 4 22:59:44 EDT 2010


Would this be like calculating the alignment for a C struct that 
contains various members with individual alignment requirements, in 
which case wouldn't LCM be used, e.g. alignment for a 2-byte object and 
an 8-byte object would be 8 bytes?

In any case, it sounds like using the Writer monad with its Monoid  is 
convenient to organize your code, and 1 is the appropriate identity.


Paul


On 7/4/10 1:36 PM, Henning Thielemann wrote:
>
> On Sun, 4 Jul 2010, Paul McJones wrote:
>
>> Henning,
>>
>> I don't quite see where you're headed with this proposal. If the problem
>> of units is complicating things, would it be useful to define a
>> semigroup rather than a monoid?
>
> I encountered that problem when writing some pointer arithmetic in a 
> Writer monad/applicative functor. I used the Writer monad to maintain 
> the required alignment, which is the GCD of the alignments of all 
> atomic pointer accesses. A Writer monad uses the Monoid class for 
> "writing to something", where in my case "writing" means updating the 
> global alignment. In practice pointer alignments are always powers of 
> two, but using GCD we could handle any strange alignment. Then I 
> wanted to define a GCD Monoid generally, and encountered that 
> Prelude's GCD does not work, since:
>
> Prelude> gcd 0 (-1)
> 1
>




More information about the Numeric-Prelude mailing list