Higher Order Functions and ST

I play with http://hackage.haskell.org/packages/archive/vault/0.2.0.0/doc/html/Data-Vault-ST.html and want to write functions like:

onVault f = runST (f <$> Vault.newKey) onVault2 f = runST (f <$> Vault.newKey <*> Vault.newKey) 

etc .. If I replace these functions with those that take no arguments and call a specific function instead of f, it works, but these higher-order functions will not introduce validation.

What is happening and can I fix it?

+4
source share
1 answer

You need to specify onVault and onVault2 level 2 classes .

 {-# LANGUAGE Rank2Types #-} -- RankNTypes would also work onVault :: (forall s. Key sa -> b) -> b onVault2 :: (forall s. Key sa -> Key sb -> c) -> c 

This is because runST :: (forall s. ST sa) -> a requires that the passed action be polymorphic in the state flow parameter s , which is a level type trick used to ensure cleanliness. See the ST monad article on the HaskellWiki for more information .

+4
source

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