<commit_before>{-# LANGUAGE FlexibleInstances, GADTs #-}
module Utilities where

import Test.QuickCheck

import qualified Data.Vector as DV
import qualified Data.Vector.Generic as DVG
import qualified Data.Vector.Primitive as DVP
import qualified Data.Vector.Fusion.Stream as S

import Data.List ( sortBy )


instance Show a => Show (S.Stream a) where
    show s = "Data.Vector.Fusion.Stream.fromList " ++ show (S.toList s)


instance Arbitrary a => Arbitrary (DV.Vector a) where
    arbitrary = fmap DV.fromList arbitrary

instance CoArbitrary a => CoArbitrary (DV.Vector a) where
    coarbitrary = coarbitrary . DV.toList

instance (Arbitrary a, DVP.Prim a) => Arbitrary (DVP.Vector a) where
    arbitrary = fmap DVP.fromList arbitrary

instance (CoArbitrary a, DVP.Prim a) => CoArbitrary (DVP.Vector a) where
    coarbitrary = coarbitrary . DVP.toList

instance Arbitrary a => Arbitrary (S.Stream a) where
    arbitrary = fmap S.fromList arbitrary

instance CoArbitrary a => CoArbitrary (S.Stream a) where
    coarbitrary = coarbitrary . S.toList

class (Testable (Pty a), Conclusion (Pty a)) => TestData a where
  type Pty a
  type Model a
  model :: a -> Model a
  unmodel :: Model a -> a

  equal :: a -> a -> Pty a

instance Eq a => TestData (DV.Vector a) where
  type Pty (DV.Vector a) = Property
  type Model (DV.Vector a) = [a]
  model = DV.toList
  unmodel = DV.fromList

  equal x y = property (x == y)

instance (Eq a, DVP.Prim a) => TestData (DVP.Vector a) where
  type Pty (DVP.Vector a) = Property
  type Model (DVP.Vector a) = [a]
  model = DVP.toList
  unmodel = DVP.fromList

  equal x y = property (x == y)

#define id_TestData(ty) \
instance TestData ty where { type Pty ty = Property; type Model ty = ty; model = id; unmodel = id; equal x y = property (x == y) }

id_TestData(Bool)
id_TestData(Int)
id_TestData(Float)
id_TestData(Double)
id_TestData(Ordering)

-- Functorish models
-- All of these need UndecidableInstances although they are actually well founded. Oh well.
instance (Eq a, TestData a) => TestData (Maybe a) where
  type Pty (Maybe a) = Property
  type Model (Maybe a) = Maybe (Model a)
  model = fmap model
  unmodel = fmap unmodel

  equal x y = property (x == y)

instance (Eq a, TestData a) => TestData [a] where
  type Pty [a] = Property
  type Model [a] = [Model a]
  model = fmap model
  unmodel = fmap unmodel

  equal x y = property (x == y)

instance (Eq a, Eq b, TestData a, TestData b) => TestData (a,b) where
  type Pty (a,b) = Property
  type Model (a,b) = (Model a, Model b)
  model (a,b) = (model a, model b)
  unmodel (a,b) = (unmodel a, unmodel b)

  equal x y = property (x == y)

instance (Eq a, Eq b, Eq c, TestData a, TestData b, TestData c) => TestData (a,b,c) where
  type Pty (a,b,c) = Property
  type Model (a,b,c) = (Model a, Model b, Model c)
  model (a,b,c) = (model a, model b, model c)
  unmodel (a,b,c) = (unmodel a, unmodel b, unmodel c)

  equal x y = property (x == y)

instance (Arbitrary a, Show a, TestData a, TestData b) => TestData (a -> b) where
  type Pty (a -> b) = a -> Pty b
  type Model (a -> b) = Model a -> Model b
  model f = model . f . unmodel
  unmodel f = unmodel . f . model

  equal f g x = equal (f x) (g x)

data P a where
  P :: TestData a => Pty a -> P a

unP :: P a -> Pty a
unP (P p) = p

instance Testable (P a) where
  property (P a) = property a

{-
class TestData a => EqTestable a p where
  equal :: a -> a -> p

instance (Eq a, TestData a) => EqTestable a Property where
  equal x y = property (x==y)

instance (Arbitrary a, Show a, TestData a, EqTestable b p) => EqTestable (a -> b) (a -> p) where
  equal f g = \x -> equal (f x) (g x)

infix 4 `eq`
eq :: EqTestable a (Pty a) => a -> Model a -> P a
eq x y = P (equal x (unmodel y))
-}

infix 4 `eq`
eq :: TestData a => a -> Model a -> P a
eq x y = P (equal x (unmodel y))

class Conclusion p where
  type Predicate p

  predicate :: Predicate p -> p -> p

instance Conclusion Property where
  type Predicate Property = Bool

  predicate = (==>)
<commit_msg>Delete dead code<commit_after>{-# LANGUAGE FlexibleInstances, GADTs #-}
module Utilities where

import Test.QuickCheck

import qualified Data.Vector as DV
import qualified Data.Vector.Generic as DVG
import qualified Data.Vector.Primitive as DVP
import qualified Data.Vector.Fusion.Stream as S

import Data.List ( sortBy )


instance Show a => Show (S.Stream a) where
    show s = "Data.Vector.Fusion.Stream.fromList " ++ show (S.toList s)


instance Arbitrary a => Arbitrary (DV.Vector a) where
    arbitrary = fmap DV.fromList arbitrary

instance CoArbitrary a => CoArbitrary (DV.Vector a) where
    coarbitrary = coarbitrary . DV.toList

instance (Arbitrary a, DVP.Prim a) => Arbitrary (DVP.Vector a) where
    arbitrary = fmap DVP.fromList arbitrary

instance (CoArbitrary a, DVP.Prim a) => CoArbitrary (DVP.Vector a) where
    coarbitrary = coarbitrary . DVP.toList

instance Arbitrary a => Arbitrary (S.Stream a) where
    arbitrary = fmap S.fromList arbitrary

instance CoArbitrary a => CoArbitrary (S.Stream a) where
    coarbitrary = coarbitrary . S.toList

class (Testable (Pty a), Conclusion (Pty a)) => TestData a where
  type Pty a
  type Model a
  model :: a -> Model a
  unmodel :: Model a -> a

  equal :: a -> a -> Pty a

instance Eq a => TestData (DV.Vector a) where
  type Pty (DV.Vector a) = Property
  type Model (DV.Vector a) = [a]
  model = DV.toList
  unmodel = DV.fromList

  equal x y = property (x == y)

instance (Eq a, DVP.Prim a) => TestData (DVP.Vector a) where
  type Pty (DVP.Vector a) = Property
  type Model (DVP.Vector a) = [a]
  model = DVP.toList
  unmodel = DVP.fromList

  equal x y = property (x == y)

#define id_TestData(ty) \
instance TestData ty where { type Pty ty = Property; type Model ty = ty; model = id; unmodel = id; equal x y = property (x == y) }

id_TestData(Bool)
id_TestData(Int)
id_TestData(Float)
id_TestData(Double)
id_TestData(Ordering)

-- Functorish models
-- All of these need UndecidableInstances although they are actually well founded. Oh well.
instance (Eq a, TestData a) => TestData (Maybe a) where
  type Pty (Maybe a) = Property
  type Model (Maybe a) = Maybe (Model a)
  model = fmap model
  unmodel = fmap unmodel

  equal x y = property (x == y)

instance (Eq a, TestData a) => TestData [a] where
  type Pty [a] = Property
  type Model [a] = [Model a]
  model = fmap model
  unmodel = fmap unmodel

  equal x y = property (x == y)

instance (Eq a, Eq b, TestData a, TestData b) => TestData (a,b) where
  type Pty (a,b) = Property
  type Model (a,b) = (Model a, Model b)
  model (a,b) = (model a, model b)
  unmodel (a,b) = (unmodel a, unmodel b)

  equal x y = property (x == y)

instance (Eq a, Eq b, Eq c, TestData a, TestData b, TestData c) => TestData (a,b,c) where
  type Pty (a,b,c) = Property
  type Model (a,b,c) = (Model a, Model b, Model c)
  model (a,b,c) = (model a, model b, model c)
  unmodel (a,b,c) = (unmodel a, unmodel b, unmodel c)

  equal x y = property (x == y)

instance (Arbitrary a, Show a, TestData a, TestData b) => TestData (a -> b) where
  type Pty (a -> b) = a -> Pty b
  type Model (a -> b) = Model a -> Model b
  model f = model . f . unmodel
  unmodel f = unmodel . f . model

  equal f g x = equal (f x) (g x)

data P a where
  P :: TestData a => Pty a -> P a

unP :: P a -> Pty a
unP (P p) = p

instance Testable (P a) where
  property (P a) = property a

infix 4 `eq`
eq :: TestData a => a -> Model a -> P a
eq x y = P (equal x (unmodel y))

class Conclusion p where
  type Predicate p

  predicate :: Predicate p -> p -> p

instance Conclusion Property where
  type Predicate Property = Bool

  predicate = (==>)

{'fraction_non_alphanumeric': 0.11758543930862904, 'fraction_numerical': 0.00039282440748985203, 'mean_word_length': 3.1783369803063457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7106189', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2740, 'n_words': 1097}