Trouble with DataKinds

后端 未结 2 735
-上瘾入骨i
-上瘾入骨i 2021-01-21 13:03

I have created a very simple example of a problem I\'m having using GADTs and DataKinds. My real application is obviously more complicated but this captures the essence of my si

2条回答
  •  孤街浪徒
    2021-01-21 13:48

    You have two options here. Either you can infer the type of the return value from the types of arguments or you can't.

    In the former case, you refine the type:

    data Which :: TIdx -> * where
      Fst :: Which TI
      Snd :: Which TD
    
    prob :: Which i -> T1 -> T2 -> Test i
    prob Fst x y = x
    prob Snd x y = y
    

    In the latter case, you have to erase the type information:

    prob :: Bool -> T1 -> T2 -> Either Int Double
    prob True (T1 x) y = Left x
    prob False x (T2 y) = Right y
    

    You can also erase the type information by using an existential type:

    data SomeTest = forall i . SomeTest (Test i)
    
    prob :: Bool -> T1 -> T2 -> SomeTest
    prob True x y = SomeTest x
    prob False x y = SomeTest y
    

    In this case, you cannot do anything interesting with a value of SomeTest, but you might be able in your real example.

提交回复
热议问题