[Haskell-beginners] Nullable attribute of a language grammar howto

Daniel Fischer daniel.is.fischer at web.de
Mon Sep 1 14:21:12 EDT 2008


Jason beat me, but I can elaborate on the matter:

Am Montag, 1. September 2008 18:22 schrieb Larry Evans:
>
> expr2null :: (GramExpr inp_type var_type) -> (GramNull inp_type var_type)
> {-
>   expr2null GramExpr
>   returns a GramNull expression which indicates whether the GramExpr
>   can derive the empty string.
> -}
>
> expr2null NullExpr = OneNull
> expr2null (InpExpr inp_valu) = ZeroNull::(GramNull inp_type var_type)
> expr2null (VarExpr var_valu) = (VarNull var_valu)::(GramNull inp_type
> var_type)
>
The type variables from the pattern signatures are NOT those from the type 
signature of expr2null, they are fresh type variables. So your pattern 
signatures actually say that
expr2null (InpExpr inp_valu) has type GramNull a b, for all types a and b.
If you omit the patterns signatures, the type checker determines that they 
have the correct type. If you want to keep the pattern signatures 
(superfluous in this case, but there are situations where it's necessary), 
you must bring the type variables into scope. That is done by the 
ScopedTypeVariables language extension, best included as a LANGUAGE pragma in 
the source and the -then- keyword forall:

expr2null :: forall inp_type var_type. GramExpr inp_type var_type -> GramNull 
inp_type var_type

Cheers,
Daniel


More information about the Beginners mailing list