haskell - Combining generator for different datatypes in Quickcheck
问题描述
I would like to combine two custom generators of different data type, but which are grouped together in another datatype.
In the following example, I would like to use the generator for Legumes
and AnimalProteins
to create another one for Proteins. choose
and elements
does not work as the type are different. Casting the generators as gen Proteins
does not work either.
data AnimalProteins = Beef | Chicken | Fish
data Legumes = WhiteBeans | RedBeans | Lentils | Chickpeas
data Proteins = AnimalProteins | Legumes deriving (Show)
rAnimalProteins :: Gen AnimalProteins
rAnimalProteins = elements [Beef , Chicken , Fish]
rLegumes :: Gen Legumes
rLegumes = elements [WhiteBeans , RedBeans , Lentils , Chickpeas]
-- This does not work !
rProteins :: Gen Proteins
rProteins = choose (rLegumes, rAnimalProteins)
The solution may be simple but I'm quite stuck here as a beginner. Thanks !
解决方案
The problem is that your Proteins
data type doesn't actually contain any AnimalProteins
or Legumes
! Yes, you've named the constructors of Proteins
the same way, but constructors live in the term language, not in the type language, so the compiler doesn't automatically associate them to the AnimalProteins
type.
To do that, you need to be explicit:
data Proteins = AnimalProteins AnimalProteins | Legumes Legumes
推荐阅读
- javascript - 有没有办法同步解决一个承诺?(或可以的替代库)
- rust - 替代退货选项
]>> 从 Rust fn 到 Wasm - javascript - 使用 jQuery 包装展开的 textNode
- tableau-api - 如何在 Tableau 中计算累计和?
- sql - 选择重复时状态值相同的记录
- python - 如何检查我的 python 守护程序是否实际运行,即获取进程的日志?
- python-3.x - 停止在无头浏览器中播放音乐
- excel - 将数据从用户输入的范围复制到确定的模板
- google-cloud-platform - 部署在 AI 平台中的 Torch-Model 生成“预测失败:未知错误”。
- ansible - 在混合环境中运行 ansible 命令