haskell - Pattern matching in instances of Tofu [LYAH example] -


i'm playing weird tofu example lyah. i've little bit simplified eliminating record fields frank constructor, here it:

class tofu t     tofu :: j -> t j  data frank m = frank (m a) deriving (show)  instance tofu frank     tofu x = frank x 

it's working , rather clear. want make value of type a modified tofu function. i've started expansion of value of x in instance declaration:

instance tofu frank     tofu (m y) = frank (m y) 

as result i'm getting:

tofu.hs:13:15: parse error in pattern: m 

ok, next i've tried actual pattern matching inside instance declaration:

instance tofu frank     tofu (just y) = frank (just y) 

as result i'm getting:

tofu.hs:16:15:     couldn't match type `j' `maybe'       `j' rigid type variable bound           type signature tofu :: j -> frank j @ tofu.hs:16:9     expected type: j       actual type: maybe     in pattern: y     in equation `tofu': tofu (just y) = frank (just y)     in instance declaration `tofu frank' 

so, question is: how work value of type a in instance declaration of tofu? possible make failed examples working without modification of tofu class?

tl;dr: can not.

assume t satisfies tofu t. function type states

tofu :: j -> t j 

which means

tofu :: forall j a. j -> t j   -- t chosen class instance 

so, it's caller gets choose j , a are. caller can pass [int] or maybe char or either string bool (here j ~ either string , a ~ bool). function tofu can not assume specific case, , must job using "general" operations.

how work value of type in instance declaration of tofu

there might no a value. e.g.

data t = k int 

since can instantiate tofu as

tofu :: t -> t t 

we can call in tofu (k 6 :: t bool) if there's no bool around.

a similar argument holds for

data u = u (a -> int) 

here u bool contains function expecting bool, instead of providing or "containing" it, loosely speaking.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -