Comparison of Data Values with Limits

Goals

  1. Prettyprinting Haskell values
    • Size limit for the output
    • Time limit for the computation
    • Escape exceptions
    • Do not escape unicode characters
  2. Comparing Haskell values
    • Highlight the first difference
    • Yes / No / Maybe results

Intended original use: Help students with automatically tested exercises.

Result

data-pprint package on HackageDB

The main module is Data.PPrint.

Two main functions:

pprint :: Data a => a -> IO Doc

(===)  :: Data a => a -> a -> IO Doc

Example: Size Limit

In GHCi:

Data.PPrint> pprint [1..]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, …, ……]

You can try it online!
Just edit the text after “Test>”! (pprint is added automatically)

Test>
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
 130, 131, 132, 133, 134, 135, …, ……]
:: [Integer]

Example: Size Limit for nested infinite values

Test>
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …,
  ……],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, …, ……],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, …, ……],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, …, ……],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, …, ……],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, …, ……],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …, ……],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, …, ……],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, …, ……],
 [1, 2, 3, 4, 5, 6, 7, 8, …, ……], [1, 2, 3, 4, 5, 6, 7, …, ……],
 [1, 2, 3, 4, 5, 6, …, ……], [1, 2, 3, 4, 5, …, ……],
 [1, 2, 3, 4, …, ……], [1, 2, 3, …, ……], [1, 2, …, ……], [1, …, ……],
 […, ……], …, ……]
:: [[Integer]]

Example: Size Limit for constructors

Test>
[1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
 1000000000, 10000000000, 100000000000, 1000000000000,
 10000000000000, 100000000000000, 1000000000000000,
 10000000000000000, 100000000000000000, 1000000000000000000,
 10000000000000000000, 100000000000000000000,
 1000000000000000000000, 10000000000000000000000,
 100000000000000000000000, 1000000000000000000000000,
 10000000000000000000000000, 100000000000000000000000000,
 1000000000000000000000000000, 10000000000000000000000000000,
 100000000000000000000000000000, 1000000000000000000000000000000,
 10000000000000000000000000000000,
 100000000000000000000000000000000,
 10000000000000000…000000000000000, 1000…000, …, ……]
:: [Integer]

Example: Time Limits

Test>
[⊥₁, ⊥₁, ⊥₂, 0, 0, ⊥₃] ++ ⊥₃ :: [Int]
⊥₁: timeout at 0%
⊥₂: timeout at 8%
⊥₃: timeout at 100%

Example: Escaping exceptions

Test>
[⊥₁, ⊥₂, 18, ⊥₁] :: [Integer]
⊥₁: "divide by zero"
⊥₂: "xxx"

Example: Nested errors

Test>
⊥₂ :: ()
⊥₁: timeout at 0%
⊥₂: "xx" ++ ⊥₁
Test>
⊥₂ :: ()
⊥₁: "yy"
⊥₂: "xx" ++ ⊥₁
Test>
⊥₁ :: ()
⊥₁:
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
++ [……]

Example: Strings

Test>
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮ"
++ […, ……]
:: [Char]
Test>
"hello" ++ [⊥₁] ++ "world!" :: [Char]
⊥₁: "Prelude.undefined"

Example: Comparison

In GHCi:

Data.PPrint> reverse [1..10] === [10,9..1]

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] === [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Data.PPrint> reverse [1..10] === [10..1]

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] =/= []

You can try (===) online but only in an implicit way…

Exercise: Define the first 10 square numbers!

Solution>

Example: Undecidable comparison

Data.PPrint> reverse [10..] === [1..]

    ⊥₁ =?= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] ++ ⊥₂
  ⊥₁: timeout at 0%
  ⊥₂: timeout at 100%

Example: Highlighting differences

Data.PPrint> [1..] === [1..99] ++ [101..]

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, …, ……, 99, 100, 101, 102,
     103, 104, 105, 106, 107, …, ……]
=/= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …, ……, 99, 101, 102, 103,
     104, 105, 106, 107, 108, …, ……]


Data.PPrint> ([1..], [1..]) === ([1..], [1..100]) 

    ([1, 2, 3, 4, 5, 6, 7, 8, …, ……],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, …, ……, 99, 100, 101, 102, 103, 104,
      105, 106, 107, …, ……])
=/= ([1, 2, 3, 4, 5, 6, 7, 8, …, ……],
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, …, ……, 99, 100])

Complex Example

Data.PPrint> (error "x", [1..]) === (0 `div` 0, reverse [1..])

    (⊥₁, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] ++ ⊥₂) =?= (⊥₃, ⊥₄)
  ⊥₁: x
  ⊥₂: timeout at 100%
  ⊥₃: divide by zero
  ⊥₄: timeout at 0%

Implementation Decisions

Why Data instance and not Show?

Back to the main page

Main page