Не удалось сопоставить тип с Возможно b0 - PullRequest
0 голосов
/ 04 ноября 2018

Здравствуйте, я столкнулся со следующей проблемой: Я определил класс типов:

* 1004 класс типов ** ** 1005 1006 * class TextEncode a where toText::a->Text fromText::Text->a И я хочу разобрать TCPFile:

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

Я не буду показывать типы Samples и Readme, поскольку они не имеют значения в этом случае. Я пытаюсь использовать метод 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. Что здесь не так?

Error

  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

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

^^^
...