Inconsistent types for record fields

After reading a little, it seems that the current situation with writing to Haskell is a bit sticky.

Take for example a new type StateT. Both code

newtype StateT s m a = StateT { runStateT :: s -> m (a,s) }

and documents

Constructors

StateT

runStateT :: s -> m (a, s)

say the type runStateTis equal s -> m (a,s). However, GHCi shows that the type is indeed

> :t runStateT
runStateT :: StateT s m a -> s -> m (a,s)

Are there any explanations for this discrepancy? Record identifier and function relate to two different things that the GHC magically resolves behind the scenes? Although I understand why it's nice to write s -> m (a,s)in notes, it just seems wrong.

+4
source share
3 answers

, , ,

  • , .
  • ,

GHCi , , , , .

data Foo = Foo {fooy :: Int}

fooy :: Foo -> Int
fooy Foo{fooy=fooy} = fooy
-- Equivalently: fooy Foo{fooy=bar} = bar

fooy - , , , Foo{fooy=...} .

+6

runStateT s -> m (a, s). runStateT - StateT s m a -> s -> m (a, s).

:

data Foo = Bar {foo :: Int}
  • foo (.. , Bar), Int.

  • foo - Foo -> Int.

:

Bar {foo = 5}

5, , a Int.

let x = foo (Bar 5) in ...

foo Bar 5, foo. , foo a foo 5 — Int.

+4

Field runStateTfor writing has type s -> m (a,s)but function also called runStateThas type StateT s m a -> s -> m (a,s)i.e. returns the value of the field (type s -> m (a,s)) for the record instance (type StateT s m a)

0
source

Source: https://habr.com/ru/post/1528785/


All Articles