Pergunta

My expected result as follows:

*Main> unify [Var "x",Var "x"] [Obj "a", Obj "a"] NoBindings

Bindings fromList [("x",a)]

However, this results in the following error:

Not in scope: data constructor H.HashMap

My code is as follows:

import qualified Data.List as L
import qualified Data.HashMap.Strict as H
import qualified Data.HashSet as S


data Pattern = Var String
         | GVar Int
         | Obj String
         | Funct String [Pattern]
         | Prim String
 deriving Eq

data Bindings = Fail
          | NoBindings
          | Bindings (H.HashMap String Pattern)
 deriving Show


unify :: [Pattern] -> [Pattern] ->Bindings -> Bindings
unify _ _ Fail              = Fail

unify [Var x,Var x1] [Obj a,Obj a1] NoBindings
        | x==x1     = Bindings (H.HashMap x a)
            | otherwise = error $show x1
Foi útil?

Solução

The Data.HashMap.Strict module exposes the data type HashMap, but doesn't export its data constructors. It exports the empty and singleton functions instead:

empty :: HashMap k v
singleton :: (Hashable k) => k -> v -> HashMap k v

The latter can be used in your case:

unify [Var x,Var x1] [Obj a,Obj a1] NoBindings
        | x==x1     = Bindings (H.singleton x a)
        | otherwise = error $show x1
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top