C'è un modo per fare in modo che GHC fornisca i vincoli della classe di tipo dei buchi tipizzati?


103

Comportamento attuale

Prelude> show _

<interactive>:7:6:
    Found hole ‘_’ with type: a0
    Where: ‘a0’ is an ambiguous type variable
    Relevant bindings include it :: String (bound at <interactive>:7:1)
    In the first argument of ‘show’, namely ‘_’
    In the expression: show _
    In an equation for ‘it’: it = show _

Comportamento desiderato

Sarebbe bello se GHC mi dicesse anche che il buco digitato ha il Showvincolo della classe di tipo.

Varie

Versione GHC 7.8.1


16
AFAIK, questo non è attualmente possibile, ma sarebbe sicuramente utile. Potrebbe valere la pena aprire una richiesta di funzionalità sul bug tracker GHC per questo.
— kosmikus

11
Sono d'accordo che questo sarebbe utile. L'ho segnalato come una richiesta di funzionalità su GHC trac: ghc.haskell.org/trac/ghc/ticket/9479
— Dominique Devriese

4
Per ora è possibile utilizzare pre-type-fori trucco: show (undefined :: () -> ()); GHC dirà di più nell'errore di controllo del tipo.
— phadej

1
È una richiesta di funzionalità o una domanda reale? Cioè, sai per certo che non c'è modo di creare GHC come desideri, o c'è la possibilità che tu possa ottenere ciò che vuoi con l'attuale compilatore, ma non sei sicuro di come?
— stakx - non contribuisce più il

1
@stakx È un po 'entrambe le cose. All'inizio, quando ho scritto questa domanda, ero confuso sul motivo per cui GHC non forniva i vincoli della classe di tipo e pensavo di usare i buchi digitati in modo sbagliato. Poi alcuni mi hanno detto che attualmente non è possibile farlo, ma potrebbe essere aggiunto a GHC. Quindi speravo che sarebbe stato aggiunto presto. Molti sembrano volerlo utilizzare. Il trucco di phadej sembra funzionare nel frattempo, ma non è così elegante o facile da usare come sarebbe una soluzione basata su un buco digitato.
— Wizek

Risposte:


2

Questo è ora risolto in GHC 8.0 grazie al ticket GHC di @ DominiqueDevriese .

A causa del default del tipo esteso , questo non è immediatamente ovvio in GHCi. Con il tuo esempio

> show _

  <interactive>:7:6: error:
    • Found hole: _h :: ()
      Or perhaps ‘_h’ is mis-spelled, or not in scope
    • In the first argument of ‘show’, namely ‘_h’
      In the expression: show _h
      In an equation for ‘it’: it = show _h
    • Relevant bindings include
        it :: String (bound at <interactive>:7:1)

il tipo di foro è predefinito (). Questo è apparentemente il comportamento desiderato , sebbene ci sia un argomento da sostenere che il default esteso non dovrebbe applicarsi ai buchi (poiché un uso comune per loro è far sì che il compilatore ti dica il tipo inferito).

Tuttavia, se compili con GHC o disabiliti le regole predefinite estese in GHCi (tramite :set -XNoExtendedDefaultRules), vediamo il risultato dei miglioramenti:

<interactive>:3:1: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘show’
      prevents the constraint ‘(Show a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Show Ordering -- Defined in ‘GHC.Show’
        instance Show Integer -- Defined in ‘GHC.Show’
        instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
        ...plus 22 others
        ...plus 11 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the expression: show _
      In an equation for ‘it’: it = show _

<interactive>:3:6: error:
    • Found hole: _ :: a0
      Where: ‘a0’ is an ambiguous type variable
    • In the first argument of ‘show’, namely ‘_’
      In the expression: show _
      In an equation for ‘it’: it = show _
    • Relevant bindings include
        it :: String (bound at <interactive>:3:1)

1

No, attualmente non è possibile, ma potrebbe essere aggiunto a GHC secondo le speculazioni.


Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.