Is there any algebraic structures used in functional programming other then monoid? -
i getting know functional programming (in haskell , scala). it's capabilities , elegance quite charming.
but when met monads, makes use of algebraic structure named monoid, surprised , glad see theoretic knowledge have been learning mathematics made use of in programming.
this observation brought question mind: can groups, fields or rings (see algebraic structures others) used in programming more abstraction , code reuse purposes , achieving mathematic-alike programming?
as know, language named fortress (which surely prefer on language once when compiler completed) defines these structure in library code. uses saw far numeric types, familiar with. there other uses of them?
best regards, ciun
you can model many structures. here's group:
class group mult :: -> -> identity :: inverse :: -> instance group integer mult = (+) identity = 0 inverse = negate -- s_3 (group of bijections of 3-element set) data s3 = abc | acb | bac | bca | cab | cba instance group s3 mult abc x = x ... -- boring code identity = abc inverse abc = abc ... -- remaining cases -- operations on groups. dual: data dual = dual { getdual :: } instance group => group (dual a) mult (dual x) (dual y) = dual (mult y x) identity = dual identity inverse (dual x) = dual (inverse x) -- product: instance (group a, group b) => group (a,b) mult (x,y) (z,t) = (x `mult` z, y `mult` t) identity = (identity, identity) inverse (x,y) = (inverse x, inverse y)
now, can write mult (dual cab, 5) (dual cba, 1)
, result. computation in group s3* ⨯ z. can add other groups, combine them in possible way , computations them.
similar things can done rings, fields, orderings, vector spaces, categories etc. haskell's numeric hierarchy unfortunately badly modeled, there's numeric prelude attempts fix that. there's docon takes extreme. tour of type classes (mainly motivated category theory), there's typeclassopedia has large list of examples , applications.
Comments
Post a Comment