Haskell:数据多类型变量的实例?

时间:2016-08-05 20:52:46

标签: haskell typeclass

我想为各种类型的东西"创建一个类型类,所以像这样:

class Column c where
     at :: c a -> Int -> a

data ListColumn a = ListColumn {
     lcContent :: [a]
}

instance Column ListColumn where
  at c i = lcContent c !! i

现在我想从另一列派生一列。假设我有一列客户,我想从中派生出一列客户名称。所以我写道:

data DerivedColumn a b c = DerivedColumn {
    dcBase   :: c a, 
    dcDerive :: a -> b
}

a是客户,b上面示例中的客户名称。现在我想我可以这样写:

instance Column (DerivedColumn a b) where
    at c i = dcDerive c $ at (dcBase c) i

但是ghc似乎不喜欢它(Column should have kind * -> *, but DerivedColumn a b has kind (* -> *) -> *)。有人可以指出正确的方向......?

1 个答案:

答案 0 :(得分:2)

我认为你想要的是:

data DerivedColumn c a b = DerivedColumn {
    dcBase   :: c a, 
    dcDerive :: a -> b
}

instance Column c => Column (DerivedColumn c a) where
    at dc i = dcDerive $ at (dcBase dc) i

要进一步展开,如果查看Column课程,您会发现c* -> *a c a c DerivedColumn a b c 1}}是b包含的值的类型。我假设Column包含的列的值应该具有类型<script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/highcharts-more.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script><script src="https://code.highcharts.com/modules/exporting.js"></script> <div class="container"> <h1>Where's the plag?</h1> <div class="jumbotron"> <small> <p>Atom Type: paragraph </p> <p>Cluster Method: kmeans </p> <p>k: 2 </p> Stylistic Feature(s): <p>honore_r_measure </p> <p></p> </small> <br> <div id='chart' style="height: 300px; width: 20000px"></div> <br> <button type="button" class="btn btn-primary btn-sm" data-toggle="collapse" data-target="#full_table"> Hide/Show Table </button> <div class="row collapse in" id="full_table"> <div class="col-md-9"> <div class="table-responsive" style="font-size:12px;"> <table class="table table-condensed table-scrollable table-bordered"> <thead> <tr> <th>Start Index</th> <th>honore_r_measure</th> <th>Suspicion Score</th> </tr> </thead> <tbody> <tr class="passage_starting_at_"> <td class="passage_row"> 0</td> <td class="passage_row"> 2512.6585</td> <td class="passage_row">0.0000</td> </tr> <tr class="passage_starting_at_"> <td class="passage_row"> 196</td> <td class="passage_row"> 2115.8163</td> <td class="passage_row" bgcolor=#F60C0C> 1.0000</td> </tr> </tbody> </table> </div> </div> </div> <div class="row"> <br class="col-md-9"> <div class="panel panel-default"> <!-- Change the id of the following div to either column or boxplot, whichever is required--> <div id="boxplot" style="height: 300px; width: 750px"></div> <div class="panel-heading"> easy_source </div> <div class="panel-body" id="document_content"> <p> <div class="passage" features="&lt;p&gt;Span&lt;/p&gt;&lt;p&gt;0, 194&lt;/p&gt;&lt;hr size = &#34;10&#34;&lt;p&gt;Plag. conf.&lt;/p&gt;&lt;p&gt;0.0&lt;/p&gt;&lt;hr size = &#34;10&#34;&lt;p&gt;PLAG SPAN&lt;/p&gt;&lt;p&gt;No plag!&lt;/p&gt;&lt;hr size = &#34;10&#34;&lt;p&gt;honore_r_measure&lt;/p&gt;&lt;p&gt;2512.6585&lt;/p&gt;&lt;hr size = &#34;10&#34;" style="font-size:14px;display:inline;;color:rgb(0,0,0);" id='pass0'> This is a really short source that isn't all that super exciting. It should have some nice fingerprints that hopefully match its plagiarized copy which is named easy_test.txt. Pretty cool stuff. </div> <br/> <div class="passage" features="&lt;p&gt;Span&lt;/p&gt;&lt;p&gt;196, 379&lt;/p&gt;&lt;hr size = &#34;10&#34;&lt;p&gt;Plag. conf.&lt;/p&gt;&lt;p&gt;1.0&lt;/p&gt;&lt;hr size = &#34;10&#34;&lt;p&gt;PLAG SPAN&lt;/p&gt;&lt;p&gt;No plag!&lt;/p&gt;&lt;hr size = &#34;10&#34;&lt;p&gt;honore_r_measure&lt;/p&gt;&lt;p&gt;2115.8163&lt;/p&gt;&lt;hr size = &#34;10&#34;" style="font-size:14px;display:inline;color:rgb(255,0,0);" id='pass1'> It also has another couple of lines which are not in the test document but are also here and are very exciting. This will make jaccard similarity not quite 1.0 but probably about 0.6. </div> <br/> </div> </div> </div> </div> ,因此它应该是您传递给类型构造函数的最后一个值,以便您定义animation的实例