monoids

Use HSpec and QuickCheck to verify Data.Monoid properties

非 Y 不嫁゛ 提交于 2019-12-04 09:21:55
I'm trying to use HSpec and QuickCheck to verify properties of Monoids (associativity and identity element). I am going to verify particular instances, but would like to keep most of the code polymorphic. This is what I came up with after several hours: module Test where import Test.Hspec import Test.QuickCheck import Data.Monoid instance (Arbitrary a) => Arbitrary (Sum a) where arbitrary = fmap Sum arbitrary instance (Arbitrary a) => Arbitrary (Product a) where arbitrary = fmap Product arbitrary prop_Monoid_mappend_mempty_x x = mappend mempty x === x sumMonoidSpec = it "mappend mempty x = x"

A basic Monoid definition gives “No instance for (Semigroup MyMonoid) arising from the superclasses of an instance declaration”

烈酒焚心 提交于 2019-12-04 02:48:47
问题 I am attempting to define Haskell integer sets with the union operation as a Monoid. module MyMonoid where import qualified Data.IntSet as S data MyMonoid = MyMonoid S.IntSet instance Monoid MyMonoid where mempty = MyMonoid S.empty MyMonoid m1 `mappend` MyMonoid m2 = MyMonoid (S.union m1 m2) I get the error • No instance for (Semigroup Markup) arising from the superclasses of an instance declaration • In the instance declaration for ‘Monoid MyMonoid’ What am I doing wrong? This seems so

Higher Kinded Types in Scala [duplicate]

我只是一个虾纸丫 提交于 2019-12-03 20:40:47
This question already has an answer here: What is a higher kinded type in Scala? 4 answers I'm reading through the Functional Programming in Scala book and in the Monoids chapter, they talk about a Monoid interface that looks like this: trait Monoid[A] { def op(a1: A, a2: A): A def zero: A } Later on, they define specific Monoid instances by extending this interface. For example., val intMonoid = new Monoid[Int] { ... } val listMonoid = new Monoid[List[Int]] { ... } A couple more pages that I read through this chapter 10, I come across 'higher kinded types' which according to the book is any

Can you formulate the insertion sort as a monoid in Clojure?

旧城冷巷雨未停 提交于 2019-12-03 08:58:42
This is the code for an insertion sort in Clojure: (defn in-sort! [data] (letfn [(insert ([raw x](insert [] raw x)) ([sorted [y & raw] x] (if (nil? y) (conj sorted x) (if (<= x y ) (concat sorted [x,y] raw) (recur (conj sorted y) raw x )))))] (reduce insert [] data))) ;Usage:(in-sort! [6,8,5,9,3,2,1,4,7]) ;Returns: [1 2 3 4 5 6 7 8 9] This is the insertion sort formulated as a monoid in Haskell: newtype OL x = OL [x] instance Ord x => Monoid (OL x) where mempty = OL [] mappend (OL xs) (OL ys) = OL (merge xs ys) where merge [] ys = ys merge xs [] = xs merge xs@(x : xs') ys@(y : ys') | x <= y =

monoid vs monad in Scala

只愿长相守 提交于 2019-12-03 08:26:42
问题 I have recently tried to find a good source on the difference between monads and monoids. Could someone provide a link to a good resource on this or perhaps take one's time to elaborate on the similarities/differences? 回答1: Monads are monoids in the category of endofunctors. Therefore, a monad is just one example of monoid, which is a more general concept. And, though that might be technically true, the most simple answer is that monads and monoids are really nothing like each other, and you

How to use the maybe monoid and combine values with a custom operation, easily?

一笑奈何 提交于 2019-12-03 06:04:34
What I'm trying to do is trivial to define by hand, basically maybeCombine :: (a->a->a) -> Maybe a -> Maybe a -> Maybe a maybeCombine _ Nothing Nothing = Nothing maybeCombine _ (Just a) Nothing = Just a maybeCombine _ Nothing (Just a) = Just a maybeCombine f (Just a) (Just a') = Just $ f a a' It's not a big deal to define this locally when needed , but still cumbersone and being so basic and general it seems there should be a standard implementation, yet I can't seem to find one. Perhaps I'm just overlooking something. What I want seems quite unrelated on the behaviour of the maybe monad, so I

What is monoid homomorphism exactly?

匆匆过客 提交于 2019-12-03 04:04:09
问题 I've read about monoid homomorphism from Monoid Morphisms, Products, and Coproducts and could not understand 100%. The author says (emphasis original): The length function maps from String to Int while preserving the monoid structure . Such a function, that maps from one monoid to another in such a preserving way, is called a monoid homomorphism . In general, for monoids M and N , a homomorphism f: M => N , and all values x:M , y:M , the following equations hold: f(x |+| y) == (f(x) |+| f(y))

monoid vs monad in Scala

烈酒焚心 提交于 2019-12-02 22:08:20
I have recently tried to find a good source on the difference between monads and monoids. Could someone provide a link to a good resource on this or perhaps take one's time to elaborate on the similarities/differences? Daniel C. Sobral Monads are monoids in the category of endofunctors. Therefore, a monad is just one example of monoid, which is a more general concept. And, though that might be technically true, the most simple answer is that monads and monoids are really nothing like each other, and you shouldn't be trying to learn the difference between them, but just learn them. There's ton

Endofunction as Monoid

十年热恋 提交于 2019-12-01 07:37:37
I'm trying this (for learning purposes): {-# LANGUAGE FlexibleInstances #-} instance Monoid (a -> a) where mempty = id mappend f g = f . g expecting id <> id to be equal to id . id However, with (id <> id) 1 I receive this error: Non type-variable argument in the constraint: Monoid (a -> a) What should I change to run it? It's just to understand monoids and Haskell typeclasses better, not for any practical usage . This will need {-# OVERLAPPING #-} pragma since GHC.Base has an instance for Monoid (a -> b) when b is a Monoid: {-# LANGUAGE FlexibleInstances #-} import Data.Monoid (Monoid, mempty

Can you formulate the Bubble sort as a monoid or semigroup?

大城市里の小女人 提交于 2019-12-01 05:58:32
Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure Here is the code for Bubble Sort as Scala def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) { import o._ val consecutiveIndices = (arr.indices, arr.indices drop 1).zipped var hasChanged = true do { hasChanged = false consecutiveIndices