使用Haskell Math.Combinatorics.Species枚举分区

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

以下交互显示如何使用可用于Haskell的Math.Combinatorics.Species库的最新版本(版本0.4)枚举集合的子集。我想弄清楚如何使用相同的库来枚举集合的分区,但我无法理解并修复我得到的类型错误。更具体地说,我不了解分区的structureType,“Set Set”是什么意思?

vamsi@vamsi-laptop:~/learn/project_euler$ ghci -XNoImplicitPrelude
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
> import qualified Math.Combinatorics.Species as S
S> import qualified Data.Int as I
S I> S.structureType S.subset
"Set"
S I> S.enumerate S.subset [1,2,3] :: [S.Set I.Int]
[{},{3},{2},{2,3},{1},{1,3},{1,2},{1,2,3}]
S I> S.structureType S.partition
": Set Set"
S I> S.enumerate S.partition [1,2,3] :: [S.Set (S.Set I.Int)]

<interactive>:6:26: error:
    • No instance for (GHC.Num.Num (S.Set I.Int))
      arising from the literal ‘1’
    • In the expression: 1
      In the second argument of ‘S.enumerate’, namely ‘[1, 2, 3]’
      In the expression:
      S.enumerate S.partition [1, 2, 3] :: [S.Set (S.Set I.Int)]
S I> S.enumerate S.partition [1,2,3] :: [(S.Set I.Int)]
[{*** Exception: Structure type mismatch.
Expected: Set Int
Inferred: : Set Set Int
CallStack (from HasCallStack):
  error, called at ./Math/Combinatorics/Species/Enumerate.hs:176:33 in 
  species-0.4-DcNHk9r6nze3hHzRydgNb3:Math.Combinatorics.Species.Enumerate
haskell combinatorics
1个回答
1
投票

似乎这是structureType使用的漂亮打印机中的一个错误。我报告了问题here;与此同时,你应该在任何时候只看到:.:作为类型构造函数时使用:。所以,像这样:

S I> S.enumerate S.partition [1,2,3] :: [(S.Set S.:.: S.Set) I.Int]
[{{1,2,3}},{{2,3},{1}},{{2},{1,3}},{{3},{1,2}},{{3},{2},{1}}]
© www.soinside.com 2019 - 2024. All rights reserved.