2016-06-29 9 views
0
{-# LANGUAGE TemplateHaskell, DeriveGeneric, DeriveAnyClass #-} 
module Main where 
import Flow 
import Control.Lens hiding ((|>)) 
import Data.Default 
import GHC.Generics 

main :: IO() 
main = putStrLn "hello world" 

append x = (++ [x]) 
equals = (==) 

data SectionedItems s i = SectionedItems{ 
    _section :: Maybe s, 
    _items :: [i], 
    _subsections :: [SectionedItems s i] 
} deriving (Show, Generic) 

instance Default (SectionedItems s i) where 
    def = SectionedItems { _section = Nothing, _items = [], _subsections = [] } 

makeLenses ''SectionedItems 

sectionedItems = SectionedItems{ 
    _section = Nothing, 
    _items = [], 
    _subsections = [] 
} 

data SectionedItemsElement s i = Section s | Item i 


addElementToSectionedItems :: SectionedItems s i -> SectionedItemsElement s i -> SectionedItems s i 
addElementToSectionedItems si (Section x) = 
    (def & section .~ Just x :: SectionedItems s i) -- Error is probably somewhere here 
    |> \subsec -> si & subsections %~ append subsec 

これを機能させるにはどうすればよいですか?私はsとiを試しましたが、Just xにエラーCould not match actual type s1 with expected type sがあります。関数本体から型sと型Iを参照するのに何が使えますか?関数本体の関数型宣言からの使用法のジェネリック

答えて

2

シンプル修正:

  • equalsに型シグネチャを追加します - それ以外の場合は単相性刺さをあなた
  • def & section .~ Just x上の不要な型注釈を削除する - タイプはとにかく推測されます。

ので:

... 

equals :: Eq a => a -> a -> Bool 
equals = (==) 
... 
addElementToSectionedItems :: SectionedItems s i -> SectionedItemsElement s i -> SectionedItems s i 
addElementToSectionedItems si (Section x) = 
    (def & section .~ Just x) 
    |> \subsec -> si & subsections %~ append subsec 
+0

どのように手動で型を指定するのでしょうか? – 2426021684

+1

[ScopedTypeVariables](https://wiki.haskell.org/Scoped_type_variables)は役に立ちました – Alec

+0

ScopeTypedVariablesが機能しました – 2426021684

関連する問題