Add tour-example

This commit is contained in:
lyxia 2018-01-04 17:05:12 -05:00
parent f8ce9e2ba0
commit d202e04550
2 changed files with 57 additions and 0 deletions

View File

@ -25,3 +25,15 @@ executable text-example
text,
generic-random
default-language: Haskell2010
executable tour-example
main-is: tour.hs
ghc-options: -Wall
if impl(ghc < 7.10)
ghc-options: -fcontext-stack=30
build-depends:
base,
QuickCheck,
text,
generic-random
default-language: Haskell2010

45
examples/tour.hs Normal file
View File

@ -0,0 +1,45 @@
-- Just another toy example
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
import Control.Monad
import GHC.Generics
import Test.QuickCheck
import Generic.Random
data MyType
= OneThing Int
| TwoThings Double String
| ThreeThings (Maybe Integer) [()] (Bool -> Word)
deriving (Show, Generic)
custom :: GenList '[Maybe Integer]
custom = (Just <$> arbitrary) :@ Nil
instance Arbitrary MyType where
arbitrary :: Gen MyType
arbitrary = genericArbitraryG custom (1 % 4 % 4 % ())
-- arbitrary = frequency
-- [ (1, OneThing <$> arbitrary)
-- , (4, TwoThings <$> arbitrary <*> arbitrary)
-- , (4, ThreeThings <$> (Just <$> arbitrary) <*> arbitrary <*> arbitrary)
-- ]
main = do
-- Print some examples
sample (arbitrary @MyType)
-- Check the property that ThreeThings contains three things.
quickCheck $ \case
ThreeThings Nothing _ _ -> False
_ -> True
-- Ew. Sorry.
instance Show a => Show (Bool -> a) where
show f = "<True -> " ++ show (f True) ++ ",False -> " ++ show (f False) ++ ">"