constructive-algebra

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module A where
import Algebra.Structures.Module
import Algebra.Structures.CommutativeRing   
           (CommutativeRing, Ring(..), propCommutativeRing)
import Test.QuickCheck

newtype A = A [(Integer,String)] 
          deriving (Show, Eq, Arbitrary)

instance Ring A where 
  A xs <+> A ys = A (xs ++ ys)
  neg (A a) = A $ [((-k),c) | (k,c) <-  a]
  A x <*> A y = A [b | a <- x, b <- y ]
  one = A [] 
  zero = A []
instance CommutativeRing A
instance Module Integer A where
     r *> (A as) = A [(r <*> k,c) | (k,c) <- as] 

propACommutativeRing :: A -> A -> A -> Property
propACommutativeRing  = propCommutativeRing 
            -- from  Algebra.Structures.CommutativeRing

-- With the derived Eq, I get
     -- *A> quickCheck propACommutativeRing 
     -- *** Failed! Falsifiable (after 2 tests and 3 shrinks):    
     -- A [(0,"")]
     -- A []
     -- A []
     -- "propAddInv"

-- but if I drop the derived instance and add:
     -- instance Eq A where
     --   x == y = True
-- I of course get

-- *A> quickCheck propACommutativeRing 
-- +++ OK, passed 100 tests.
14:15: Warning: Redundant $
Found:
A $ [((-k), c) | (k, c) <- a]
Why not:
A [((-k), c) | (k, c) <- a]
14:20: Warning: Redundant bracket
Found:
((-k), c)
Why not:
(-k, c)