GADT for polymorphic list

I am parsing several operators of the form

v1 = expression1
v2 = expression2
...

I use State Monad, and my state must be a pair (String, Expr a), I really insist on typing expressions. I tried to implement the state as [PPair], where I define PPair GADT:

data PPair where
    PPair :: (String, Expr a) -> PPair

As soon as this line was passed by the compiler, I felt that I was doing something really wrong. I suppressed this thought and continued coding. When I came to write code that would extract the value of a variable from a state, I understood the problem:

evalVar k ((PPair (kk, v)):s) = if k == kk then v else evalVar k s

I get:

Inferred type is less polymorphic than expected

which is to be expected. How do I solve this problem? I know that I can solve it by breaking the type into all types of candidates a, but is there no way to lean?

+5
source share
1 answer

, evalVar:

evalVar :: String -> [PPair] -> Expr ?

? is a, , a. "an Expr " :

data SomeExpr where
  SomeExpr :: Expr a -> SomeExpr

, , RankNTypes, GADTs:

data SomeExpr = forall a. SomeExpr (Expr a)

. PPair SomeExpr:

data PPair = PPair String SomeExpr

evalVar :

evalVar k (PPair kk v : xs)
  | k == kk = v
  | otherwise = evalVar k xs

(, [(String,SomeExpr)] lookup.)

, , , Haskell, , , ; , Agda, , , , -, Haskell , , , , .

, , ; GADT. , , , , , , .

, , , ; Var a, , :

data PPair where
  PPair :: Var a -> Expr a -> PPair

evalVar :: Var a -> [PPair] -> Maybe (Expr a)

- vault; Key ST IO Vault . Map, . , Var a Key (Expr a) Vault [PPair]. ( : .)

, Key, Key , . ( , Var , , , .)

(, GADT, : data PPair where PPair :: String -> Expr a -> PPair.)

+9

All Articles