2016-04-11 17 views
4

GHC.Generics için hackage documentation'dan veri türleri için bir Genel "kodlama" işlevi oluşturmaya yönelik eğiticiyi izliyorum. Ben ettik kopyalanıp yapıştırılan kodu bölümünde "sarıcı ve jenerik varsayılan" için (ve dahil) kadar ancak aşağıdaki hatayı alıyorum şu şekildedir:Haskell/GHC Generics'in dökümantasyon örneği bir yazım hatası mı eksik?

Main.hs:35:14: 
    Could not deduce (Encode' (Rep a)) arising from a use of ‘encode'’ 
    from the context (Encode a) 
     bound by the class declaration for ‘Encode’ 
     at Main.hs:(32,1)-(35,29) 
    or from (Generic a) 
     bound by the type signature for encode :: Generic a => a -> [Bool] 
     at Main.hs:33:13-23 
    In the expression: encode' (from x) 
    In an equation for ‘encode’: encode x = encode' (from x) 
Failed, modules loaded: none. 

ben kopyaladıktan kodudur:

{-# LANGUAGE DeriveGeneriC#-} 
{-# LANGUAGE TypeOperators #-} 
{-# LANGUAGE DefaultSignatures #-} 
{-# LANGUAGE FlexibleContexts #-} 
{-# LANGUAGE DeriveAnyClass #-} 
module Main where 

import GHC.Generics 

class Encode' f where 
    encode' :: f p -> [Bool] 

instance Encode' V1 where 
    encode' x = undefined 

instance Encode' U1 where 
    encode' U1 = [] 

instance (Encode' f, Encode' g) => Encode' (f :+: g) where 
    encode' (L1 x) = False : encode' x 
    encode' (R1 x) = True : encode' x 

instance (Encode' f, Encode' g) => Encode' (f :*: g) where 
    encode' (x :*: y) = encode' x ++ encode' y 

instance (Encode c) => Encode' (K1 i c) where 
    encode' (K1 x) = encode x 

instance (Encode' f) => Encode' (M1 i t f) where 
    encode' (M1 x) = encode' x 

class Encode a where 
    encode :: a -> [Bool] 
    default encode :: (Generic a) => a -> [Bool] 
    encode x = encode' (from x) 

Sorunun son sınıf bildiriminde olduğunu düşünüyorum (class Encode a where ...).

class Encode a where 
    encode :: a -> [Bool]                                        
    default encode :: (Generic a, Encode' (Rep a)) => a -> [Bool] 
    encode x = encode' (from x) 

Bu reklamı olarak çalışmak gibi görünüyor - Ben Yeni veri türlerinin beyan ve onları DeriveAnyClass ve deriving ile encodable yapabilirsiniz: Fazladan kısıtlamayı ekleyerek çözdüm, bu olsun. Ancak, fikrimin neden gerekli olduğundan emin değilim. Benim sorum:

  • Dokümanlar yanlış mı?
  • Encode' (Rep a) kısıtlaması, hackage'deki örnek kodda mevcut mu?
  • Değilse, kodu çalıştırmak için ne eklemeliyim?

cevap

4

Evet, bence yanlışlar var. Kısıtlama zorunludur.

+0

Cool! Kiminle konuşacağım/onları düzeltmek için nereye giderim? – statusfailed

+1

@statusfailed Aslında, zaten sabit: http://downloads.haskell.org/~ghc/master/libraries/html/base/GHC-Generics.html#g:13 (Ben Gamari'ye teşekkürler). – kosmikus

İlgili konular