如何从 REPL 检查给定的约束是否得到满足?

问题描述 投票:0回答:1

为了探索具有复杂类型和类型类实例的库,我想从 REPL 检查类型类约束是否满足(假设所有相关实例都在范围内)。

例如:在rel8 1.5.0中,是否满足约束

Table Expr (Expr Int, Expr Bool)
? (事实并非如此,因为
Int
缺少
DBType
实例,因此前提条件失败)。

非解决方案:

  • 使用

    :kind!
    。这仅检查约束是否格式良好,而不是是否满足。

    ghci> :kind! (Table Expr (Expr Int, Expr Bool))
    (Table Expr (Expr Int, Expr Bool)) :: Constraint
    = Table Expr (Expr Int, Expr Bool)
    
  • 使用

    :instances
    。似乎不适用于像
    Table
    这样的多参数类型类,而且它返回所有可能的实例,这可能很冗长。

    ghci> :instances Expr
    instance Reifiable Expr
    instance Nullifiable Expr
    instance GRecordable Expr
    
    ghci> :instances (Expr Int, Expr Bool)
    instance [safe] NotNull (Expr Int, Expr Bool)
    instance [safe] Nullable (Expr Int, Expr Bool)
    instance Show (Expr Int, Expr Bool) 
    instance GHC.Generics.Generic (Expr Int, Expr Bool)
    
haskell typeclass ghci
1个回答
0
投票

我在我的

.ghci
文件中定义了以下函数:

:set -XAllowAmbiguousTypes
:{
yep :: forall c . c => ()
yep = ()
:}

可以这样使用

ghci> yep @(Table Expr (Expr Int, Expr Bool))
<interactive>:4:1: error: [GHC-39999]
    • No instance for ‘DBType Int’ arising from a use of ‘yep’
    • In the expression: yep @(Table Expr (Expr Int, Expr Bool))
      In an equation for ‘it’:
          it = yep @(Table Expr (Expr Int, Expr Bool))

ghci> yep @(Table Expr (Expr Int32, Expr Bool))
()
© www.soinside.com 2019 - 2024. All rights reserved.