def test_profunctor_map(cls, data): # Draw types b = data.draw(testing.sample_eq_type()) c = data.draw(testing.sample_eq_type()) d = data.draw(testing.sample_type()) f = data.draw(cls.sample_profunctor_type_constructor()) fbc = f(b, c) # Draw values x = data.draw(fbc) g = data.draw(testing.sample_function(d)) cls.assert_profunctor_map(x, g, data=data) return
def test_bind_associativity(cls, data): a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_eq_type()) c = data.draw(testing.sample_type()) m = data.draw(cls.sample_bind_type_constructor()) ma = m(a) mb = m(b) mc = m(c) m = data.draw(ma) f = data.draw(testing.sample_function(mb)) g = data.draw(testing.sample_function(mc)) cls.assert_bind_associativity(m, f, g, data=data) return
def test_contravariant_composition(cls, data): # Draw types a = data.draw(testing.sample_type()) b = data.draw(testing.sample_eq_type()) c = data.draw(testing.sample_eq_type()) t = data.draw(cls.sample_contravariant_type_constructor()) ta = t(a) # Draw values v = data.draw(ta) f = data.draw(testing.sample_function(b)) g = data.draw(testing.sample_function(c)) cls.assert_contravariant_composition(v, f, g, data=data) return
def test_functor_composition(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_eq_type()) c = data.draw(testing.sample_type()) f = data.draw(cls.sample_functor_type_constructor()) fa = f(a) # Draw values v = data.draw(fa) g = data.draw(testing.sample_function(b)) h = data.draw(testing.sample_function(c)) cls.assert_functor_composition(v, g, h, data=data) return
class Endo(Monoid): """Endofunction monoid (a -> a)""" app_endo = attr.ib() @class_property def empty(cls): return cls(identity) def append(self, f): # Append by composing return Endo(lambda a: self.app_endo(f.app_endo(a))) def __repr__(self): return "Endo({})".format(self.app_endo) def __eq_test__(self, other, data, input_strategy=st.integers()): x = data.draw(input_strategy) return eq_test(self.app_endo(x), other.app_endo(x), data) @class_function def sample_value(cls, a): return testing.sample_function(a).map(lambda f: Endo(f)) sample_type = testing.create_type_sampler( testing.sample_eq_type(), ) sample_semigroup_type = sample_type sample_monoid_type = sample_type
class Identity(Monad, Eq): x = attr.ib() @class_function def pure(cls, x): return cls(x) def bind(self, f): """Identity a -> (a -> Identity b) -> Identity b""" return f(self.x) def __eq__(self, other): """Identity a -> Identity a -> bool""" return self.x == other.x def __repr__(self): return "Identity({})".format(repr(self.x)) def __eq_test__(self, other, data): return eq_test(self.x, other.x, data) @class_function def sample_value(cls, a): return a.map(Identity) sample_type = testing.create_type_sampler(testing.sample_type(), ) sample_functor_type_constructor = testing.create_type_constructor_sampler() # Some typeclass instances have constraints for the types inside sample_eq_type = testing.create_type_sampler(testing.sample_eq_type(), )
def test_foldable_foldl(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_eq_type()) f = data.draw(cls.sample_foldable_type_constructor()) fa = f(a) # Draw values xs = data.draw(fa) initial = data.draw(b) g = data.draw(testing.sample_function(testing.sample_function(b))) with catch_warnings(): filterwarnings("ignore", category=PerformanceWarning) cls.assert_foldable_foldl(xs, g, initial, data=data) return
def test_applicative_composition(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_eq_type()) c = data.draw(testing.sample_type()) f = data.draw(cls.sample_applicative_type_constructor()) fa = f(a) fab = f(testing.sample_function(b)) fbc = f(testing.sample_function(c)) # Draw values w = data.draw(fa) v = data.draw(fab) u = data.draw(fbc) cls.assert_applicative_composition(u, v, w, data=data) return
def test_cartesian_second(cls, data): # Draw types a1 = data.draw(testing.sample_eq_type()) a2 = data.draw(testing.sample_eq_type()) a = st.tuples(a1, a2) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_cartesian_type_constructor()) fab = f(a, b) # Draw values x = data.draw(fab) cls.assert_cartesian_second( x, data=data, input_strategy=a, ) return
def test_cocartesian_right(cls, data): from haskpy.types.either import Either # Draw types a1 = data.draw(testing.sample_eq_type()) a2 = data.draw(testing.sample_eq_type()) a = Either.sample_value(a1, a2) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_cocartesian_type_constructor()) fab = f(a, b) # Draw values x = data.draw(fab) cls.assert_cocartesian_right( x, data=data, input_strategy=a, ) return
def test_applicative_homomorphism(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) # Draw values x = data.draw(a) f = data.draw(testing.sample_function(b)) cls.assert_applicative_homomorphism(f, x, data=data) return
def test_profunctor_identity(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_profunctor_type_constructor()) fab = f(a, b) # Draw values x = data.draw(fab) cls.assert_profunctor_identity(x, data=data) return
def test_contravariant_contrareplace(cls, data): # Draw types a = data.draw(testing.sample_type()) b = data.draw(testing.sample_eq_type()) t = data.draw(cls.sample_contravariant_type_constructor()) ta = t(a) # Draw values v = data.draw(ta) x = data.draw(b) cls.assert_contravariant_contrareplace(v, x, data=data) return
def test_monad_map(cls, data): """Test consistency of ``map`` with the default implementation""" # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) m = data.draw(cls.sample_monad_type_constructor()) ma = m(a) u = data.draw(ma) f = data.draw(testing.sample_function(b)) cls.assert_monad_map(u, f, data=data) return
def test_monad_left_identity(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) m = data.draw(cls.sample_monad_type_constructor()) mb = m(b) # Draw values f = data.draw(testing.sample_function(mb)) x = data.draw(a) cls.assert_monad_left_identity(f, x, data=data) return
def test_functor_replace(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_functor_type_constructor()) fa = f(a) # Draw values v = data.draw(fa) x = data.draw(b) cls.assert_functor_replace(v, x, data=data) return
def test_functor_map(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_functor_type_constructor()) fa = f(a) # Draw values v = data.draw(fa) f = data.draw(testing.sample_function(b)) cls.assert_functor_map(v, f, data=data) return
def test_applicative_interchange(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_applicative_type_constructor()) fab = f(testing.sample_function(b)) # Draw values y = data.draw(a) u = data.draw(fab) cls.assert_applicative_interchange(u, y, data=data) return
def test_profunctor_contramap(cls, data): # Draw types b = data.draw(testing.sample_eq_type()) d = data.draw(testing.sample_type()) f = data.draw(cls.sample_profunctor_type_constructor()) fbd = f(b, d) # Draw values x = data.draw(fbd) f = data.draw(testing.sample_function(b)) cls.assert_profunctor_contramap(x, f, data=data) return
def test_applicative_map(cls, data): """Test consistency between Applicative and Functor implementations""" # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_applicative_type_constructor()) fa = f(a) # Draw values v = data.draw(fa) f = data.draw(testing.sample_function(b)) cls.assert_applicative_map(v, f, data=data) return
def test_apply_apply(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_apply_type_constructor()) fa = f(a) fab = f(testing.sample_function(b)) # Draw values v = data.draw(fa) u = data.draw(fab) cls.assert_apply_apply(u, v, data=data) return
def test_foldable_fold_map(cls, data): # Draw types from haskpy.typeclasses import Monoid monoid = data.draw(testing.sample_class(Monoid)) a = data.draw(testing.sample_eq_type()) b = data.draw(monoid.sample_monoid_type()) f = data.draw(cls.sample_foldable_type_constructor()) fa = f(a) # Draw values f = data.draw(testing.sample_function(b)) xs = data.draw(fa) cls.assert_foldable_fold_map(xs, monoid, f, data=data) return
def test_foldable_elem(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) f = data.draw(cls.sample_foldable_type_constructor()) fa = f(a) # Draw values e = data.draw(a) xs = data.draw(fa) with catch_warnings(): filterwarnings("ignore", category=PerformanceWarning) cls.assert_foldable_elem(xs, e, data=data) return
def test_bind_bind(cls, data): """Test consistency of ``bind`` with the default implementation""" # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) m = data.draw(cls.sample_bind_type_constructor()) ma = m(a) mb = m(b) # Draw values u = data.draw(ma) f = data.draw(testing.sample_function(mb)) cls.assert_bind_bind(u, f, data=data) return
def test_bind_apply(cls, data): """Test consistency ``apply`` with the default implementations""" # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) m = data.draw(cls.sample_bind_type_constructor()) ma = m(a) mab = m(testing.sample_function(b)) # Draw values v = data.draw(ma) u = data.draw(mab) cls.assert_bind_apply(u, v, data=data) return
def test_cartesian_identity(cls, data): # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_cartesian_type_constructor()) fab = f(a, b) # Draw values h = data.draw(fab) cls.assert_cartesian_identity( h, data=data, input_strategy=st.tuples(st.integers(), st.just(())), ) return
def test_cartesian_associativity(cls, data): # Draw types a = st.tuples(data.draw(testing.sample_eq_type()), st.tuples( st.just("foo"), st.just("bar"), )) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_cartesian_type_constructor()) fab = f(a, b) # Draw values h = data.draw(fab) cls.assert_cartesian_associativity(h, data=data, input_strategy=a) return
def test_cocartesian_unit(cls, data): from haskpy import Left # Draw types a = data.draw(testing.sample_eq_type()) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_cocartesian_type_constructor()) fab = f(a, b) # Draw values h = data.draw(fab) cls.assert_cocartesian_unit( h, data=data, input_strategy=a.map(Left), ) return
def test_eq_substitutivity(cls, data): """Test if ``x == y = True``, then ``f(x) == f(y) = True``""" # Draw types a = data.draw(cls.sample_eq_type()) b = data.draw(testing.sample_eq_type()) # Draw values x = data.draw(a) y = data.draw(a) f = data.draw(testing.sample_function(b)) # Note: the only requirement for arbitrary functions is that the input # variable has __eq__ implemented. And we have that for Eq type so this # test can always be run. cls.assert_eq_substitutivity(x, y, f) return
def test_cocartesian_associativity(cls, data): from haskpy.types.either import Either # Draw types a = Either.sample_value( data.draw(testing.sample_eq_type()), Either.sample_value( st.just("foo"), st.just("bar"), ) ) b = data.draw(testing.sample_type()) f = data.draw(cls.sample_cocartesian_type_constructor()) fab = f(a, b) # Draw values h = data.draw(fab) cls.assert_cocartesian_associativity(h, data=data, input_strategy=a) return