Nel seguente frammento di codice (ho sottratto tutte le altre parti banali)Haskell: le variabili di tipo nelle clausole "where" nello stesso spazio dei nomi con i loro genitori?
data T s = T (s -> s)
foo :: T s -> s -> s
foo (T f) x = bar x where
bar :: s -> s
bar a = f a
mi sono seguente errore
Couldn't match expected type `s1' with actual type `s'
`s1' is a rigid type variable bound by
the type signature for bar :: s1 -> s1 at /tmp/test.hs:5:12
`s' is a rigid type variable bound by
the type signature for foo :: T s -> s -> s at /tmp/test.hs:3:8
In the return type of a call of `f'
In the expression: f a
In an equation for `bar': bar a = f a
la mia ipotesi è che le variabili di tipo a firma bar
s' non condividono la namespace con foo
, quindi il compilatore non può dedurre che i due s
significhi effettivamente lo stesso tipo.
Quindi ho trovato questa pagina Scoped type variables, che suggerisce che posso utilizzare {-# LANGUAGE ScopedTypeVariables #-}
, che non ha aiutato. Ho avuto lo stesso errore.
Come gli esempi sul wiki (dovrebbe) implicare, è necessario un 'forall s' nella firma del tipo di' foo' per usarlo con 'ScopedTypeVariables'. –