Printing Dynamic Data

后端 未结 2 1750
青春惊慌失措
青春惊慌失措 2021-01-15 02:26

I have a system in haskell that uses Data.Dynamic and Type.Reflection to perform inference and calculations. I would like to be able to print the results.

Printing

2条回答
  •  无人及你
    2021-01-15 02:57

    I like the main idea of the other answer, but it seems to get where it's going in a fairly roundabout way. Here's how I would style the same idea:

    {-# LANGUAGE ViewPatterns #-}
    {-# LANGUAGE TypeApplications #-}
    {-# LANGUAGE GADTs #-}
    import Type.Reflection
    import Data.Dynamic
    
    showDyn :: Dynamic -> String
    showDyn (Dynamic (App (App (eqTypeRep (typeRep @(,)) -> Just HRefl) ta) tb) (va, vb))
        = concat [ "DynamicPair("
                 , showDyn (Dynamic ta va)
                 , ","
                 , showDyn (Dynamic tb vb)
                 , ")"
                 ]
    showDyn (Dynamic (eqTypeRep (typeRep @Integer) -> Just HRefl) n) = show n
    showDyn (Dynamic tr _) = show tr
    

    That first pattern match is quite a mouthful, but after playing with a few different ways of formatting it I'm convinced that there just is no way to make that look good. You can try it in ghci:

    > showDyn (toDyn ((3,4), (True, "hi")))
    "DynamicPair(DynamicPair(3,4),DynamicPair(Bool,[Char]))"
    

提交回复
热议问题