无法将类型与Mayb b0匹配

时间:2018-11-04 07:45:27

标签: haskell typeclass functor

您好,我遇到以下问题: 我定义了一个类型类:

类型类

class TextEncode  a where
        toText::a->Text
        fromText::Text->a

我想解析一个TCPFile

data TCPFile=Rfile (Maybe Readme) | Dfile  Samples | Empty 

我不会显示SamplesReadme类型,因为在这种情况下它们无关紧要。我正在尝试使用fromText方法来获取TCPFile

类型

data FileData=FileData{ header::Header,rawContent::Text}

data Header=Header { ftype::Char}

FileData用于存储文件的Header和将要解析的内容:

我将我定义的Functor用于链接操作:

(>>?)::Maybe a->(a->Maybe b)->Maybe b
    (Just t) >>? f=f t
    Nothing >>? _=Nothing

代码

instance TextEncode TCPFile where
        fromText txt = readHeader txt >>? (\h -> Just (FileData h txt)) >>? makeFile


    readHeader::Text->Maybe Header
    readHeader txt=case Data.Text.head txt of 
        'r' ->Just (Header{ ftype='r'})
        'd' ->Just (Header {ftype ='d'})
        _  ->Nothing



    makeFile::FileData->TCPFile
    makeFile fd= case ftype.header $ fd of
            'r'->Rfile (Just (fromText . rawContent $ fd))
            'd'->Dfile (fromText . rawContent $ fd)
            _  ->Empty 

我一直在makeFile方法中收到此错误,但我不明白为什么。我在makeFile中放的是Maybe FileData,它是通过>>?展开的。我提供了一个很好的输入参数,并且输出了一个与TCPFile TextEncode相匹配的typeclass。这是怎么回事?

错误

  TCPFile.hs:59:24: error:
        * Couldn't match expected type `TCPFile'
                      with actual type `Maybe b0'
        * In the expression:
            readHeader txt >>? (\ h -> Just (FileData h txt)) >>? makeFile
          In an equation for `fromText':
              fromText txt
                = readHeader txt >>? (\ h -> Just (FileData h txt)) >>? makeFile
          In the instance declaration for `TextEncode TCPFile'
       |
    59 |         fromText txt = readHeader txt >>? (\h -> Just (FileData h txt)) >>? makeFile

       |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

^^^

0 个答案:

没有答案