使用 Dump 库打印表达式失败

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

我想打印正在测试的属性以及导致失败的参数。所以,我试图通过使用

the 
Debug.Dump
中的 
dump
来解决问题的第二部分。这是一个最小的工作示例:

{-# LANGUAGE TemplateHaskell #-}

module Main where

import Debug.Dump

import Test.QuickCheck
import System.Exit

prop_ReverseLengthFail :: [Int] -> Property
prop_ReverseLengthFail xs = 
  counterexample [d|xs|] (length (reverse xs) === length xs + 1)

return []

main :: IO ()
main = do
  passed <- $quickCheckAll
  exitWith $ if passed
    then ExitSuccess
    else ExitFailure

但是,我收到以下错误:

tempCodeRunnerFile.haskell:12:18: error:
    • Couldn't match type ‘[template-haskell-2.19.0.0:Language.Haskell.TH.Syntax.Dec]’
                     with ‘Char’
      Expected: String
        Actual: [template-haskell-2.19.0.0:Language.Haskell.TH.Lib.Internal.Decs]
    • In the first argument of ‘counterexample’, namely
        ‘[d| |]
         pending(rn) [<splice, xs>]’
      In the expression:
        counterexample
          [d| |]
          pending(rn) [<splice, xs>]
          (length (reverse xs) === length xs + 1)
      In an equation for ‘prop_ReverseLengthFail’:
          prop_ReverseLengthFail xs
            = counterexample
                [d| |]
                pending(rn) [<splice, xs>]
                (length (reverse xs) === length xs + 1)
   |
12 |   counterexample [d|xs|] (length (reverse xs) === length xs + 1)

我有两个问题:

  • 如何解决错误并完成我的第二个目标(打印表达式)?
  • 如何打印正在计算的表达式,即“length (reverse xs) == length xs + 1”?
haskell quickcheck template-haskell
1个回答
0
投票

几个问题:

  1. Debug.Dump
    为您提供一个准引用器,需要使用
    QuasiQuotes
    扩展名。
    TemplateHaskell
    并未暗示该扩展名,因此您也需要指定它。
  2. d
    是 Template Haskell 的声明引用程序的名称,因此您不能在还需要使用 Template Haskell 的文件中使用该名称作为准引用程序(您可以使用
    $quickCheckAll
    来实现)。请使用其全名
    dump
    或其他简写
    dd
  3. ExitFailure
    需要
    Int
    作为参数,但您没有提供。

这是您的程序,所有这些问题都已修复:

{-# LANGUAGE QuasiQuotes, TemplateHaskell #-}

module Main where

import Debug.Dump

import Test.QuickCheck
import System.Exit

prop_ReverseLengthFail :: [Int] -> Property
prop_ReverseLengthFail xs = 
  counterexample [dd|xs|] (length (reverse xs) === length xs + 1)

return []

main :: IO ()
main = do
  passed <- $quickCheckAll
  exitWith $ if passed
    then ExitSuccess
    else ExitFailure 1

这是运行固定版本时的输出:

=== prop_ReverseLengthFail from tempCodeRunnerFile.haskell:10 ===
*** Failed! Falsified (after 1 test):  
[]
(xs) = []
0 /= 1

© www.soinside.com 2019 - 2024. All rights reserved.