git.iko.soy/typeable/servant
on master help sign in
Make default ToSample instances productive

The default Generics-based ToSample instance now uses Omega type to productively produce distinct samples. The previous version was based on lists and hence left-recursive. This means that with previous versions the default toSamples for [Bool] would return an infinite list like this: [[],[False],[False,False],[False,False,False],... As you can see it would never produce a list with True in it. Omega handles this and produces a more diverse output: [[],[False],[False,False],[True],... This is still not the best possible case, but to do better we need to use Omega not only in GToSample, but in ToSample as well since GToSample uses ToSample instances recursively.

commit401b44ac1b3c8f3240400f2f56a0e143689af13e authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateSat 19 Sep 2015, 01:25 (11 years ago) parentc769800
3 files changed+7−4
servant-docs/servant-docs.cabal+1 servant-docs/src/Servant/Docs/Internal.hs+5−4 stack.yaml+1
servant-docs/servant-docs.cabal+1
@@ -42,6 +42,7 @@ library
4242 , string-conversions
4343 , text
4444 , unordered-containers
45+ , control-monad-omega == 0.3.*
4546 hs-source-dirs: src
4647 default-language: Haskell2010
4748 ghc-options: -Wall
servant-docs/src/Servant/Docs/Internal.hs+5−4
@@ -23,6 +23,7 @@ module Servant.Docs.Internal where
2323import Control.Applicative
2424import Control.Arrow (second)
2525import Control.Lens hiding (List, to, from)
26+import qualified Control.Monad.Omega as Omega
2627import Data.ByteString.Conversion (ToByteString, toByteString)
2728import Data.ByteString.Lazy.Char8 (ByteString)
2829import qualified Data.CaseInsensitive as CI
@@ -394,12 +395,12 @@ defaultSample :: forall a b. (Generic a, Generic b, GToSample (Rep a) (Rep b)) =
394395defaultSample _ = to <$> gtoSample (Proxy :: Proxy (Rep a))
395396
396397defaultSamples :: forall a b. (Generic a, Generic b, GToSample (Rep a) (Rep b)) => Proxy a -> [(Text, b)]
397−defaultSamples _ = second to <$> gtoSamples (Proxy :: Proxy (Rep a))
398+defaultSamples _ = Omega.runOmega $ second to <$> gtoSamples (Proxy :: Proxy (Rep a))
398399
399400class GToSample t s where
400401 gtoSample :: proxy t -> Maybe (s x)
401− gtoSample _ = snd <$> listToMaybe (gtoSamples (Proxy :: Proxy t))
402− gtoSamples :: proxy t -> [(Text, s x)]
402+ gtoSample _ = snd <$> listToMaybe (Omega.runOmega (gtoSamples (Proxy :: Proxy t)))
403+ gtoSamples :: proxy t -> Omega.Omega (Text, s x)
403404 gtoSamples _ = maybe empty (pure . ("",)) (gtoSample (Proxy :: Proxy t))
404405
405406instance GToSample U1 U1 where
@@ -426,7 +427,7 @@ instance (GToSample p p', GToSample q q') => GToSample (p :+: q) (p' :+: q') whe
426427
427428instance ToSample a b => GToSample (K1 i a) (K1 i b) where
428429 gtoSample _ = K1 <$> toSample (Proxy :: Proxy a)
429− gtoSamples _ = second K1 <$> toSamples (Proxy :: Proxy a)
430+ gtoSamples _ = second K1 <$> Omega.each (toSamples (Proxy :: Proxy a))
430431
431432instance (GToSample f g) => GToSample (M1 i a f) (M1 i a g) where
432433 gtoSample _ = M1 <$> gtoSample (Proxy :: Proxy f)
stack.yaml+1
@@ -14,4 +14,5 @@ packages:
1414- servant-server/
1515extra-deps:
1616- engine-io-wai-1.0.2
17+- control-monad-omega-0.3.1
1718resolver: nightly-2015-09-10