An instance error of several parameter classes

I am trying to write a base lexer using Haskell. To implement DFA and NFA, I decided to include some common functions in the FA and FAState classes.

-- |A class for defining the common functionality of all finite automatons.
class FA a b where
    mutateId :: a -> Int -> a               -- ^Returns a new FA by changing the sId of the input FA.
    mutateType :: a -> StateType -> a       -- ^Returns a new FA by changing the stateType of the input FA.
    addTransition :: a -> (b, a) -> a       -- ^Returns a new FA by adding a new transition to the input FA.


-- |A class for defining the common functionality of all finite automaton states.
class FA a b => FAState a b where
    sId :: a -> Int                         -- ^An unique identifier for the state(hence the prefix s).
    sType :: a -> StateType                 -- ^The type of the state.
    sTransitions :: a -> Transitions b a    -- ^The transitions that occur from this state.

Where

-- |A type which identifies different types of a FA state.
data StateType = Start | Normal | Final
    deriving (Show, Read, Eq)

-- |A type which represents a list of transitions on input a to b.
-- Eg. [(Char, DFA)] represents the transition on a Char input.
type Transitions a b = [(a, b)]

Therefore, b represents the data type for which the transitions occur. For DFA, b = Char, whereas for NFA, b = Char.

data Symbol = Alphabet Char | Epsilon
    deriving (Show, Read, Eq) 

DFA and NFA are defined as follows:

data DFA = DState Int StateType (Transitions Char DFA)
    deriving (Show, Read)
data NFA = NState Int StateType (Transitions Symbol NFA)
    deriving (Show, Read)

I had a problem with definitions of FA and FAState instances:

instance FA DFA Char where
    mutateId (DState i ty ts) new_i = DState new_i ty ts
    mutateType (DState i ty ts) new_ty = DState i new_ty ts
    addTransition (DState i ty ts) state = DState i ty (state:ts)

instance FAState DFA Char where
    sId (DState i t ts) = i
    sType (DState i t ts) = t
    sTransitions (DState i t ts) = ts

instance FA NFA Symbol where
    mutateId (NState i ty ts) new_i = NState new_i ty ts
    mutateType (NState i ty ts) new_ty = NState i new_ty ts
    addTransition (NState i ty ts) state = NState i ty (state:ts)

instance FAState NFA Symbol where
    sId (NState i t ts) = i
    sType (NState i t ts) = t
    sTransitions (NState i t ts) = ts

When I try to execute any of the functions, I get an instance error without an instance:

>>sId egNFA

<interactive>:15:1:
    No instance for (FAState NFA b0)
      arising from a use of `sId'
    Possible fix: add an instance declaration for (FAState NFA b0)
    In the expression: sId egNFA
    In an equation for `it': it = sId egNFA

I do not understand what is happening here.

+5
source share
1 answer

: , . " " : , ( " " ) , .

, , , : FAState NFA Symbol , , NFA; . , ( Symbol), : () FAState NFA Widget , . .

:

  • , , , . , : typeclass- sId :: FAState a b => a -> Int a b. , , GHC ( - ), ,.

    , sTransitions sId: , sTransitions nfa yes sTransitions nfa :: Transitions Symbol NFA, ( , , sTransitions nfa (sTransitions :: NFA -> Transitions Symbol NFA) dfa.)

  • . , , , . , GHC , :

    class FAState a b | a -> b where
        {- ...same as before -}
    -- instance declarations look the same as before, too
    

    : -, GHC, , a, , , b, -, GHC, , , , a, b.

  • () . , , , , . :

    class FAState a where
        type State a
        sId :: a -> Int
        sType :: a -> StateType
        sTransitions :: a -> Transitions (State a) a
    
    instance FAState NFA where
        type State NFA = Symbol
        -- methods are the same as before
    

    State ( ..). , , FAState, , .

  • . GHC , , ... , ( ). :

    -- class definition same as before
    instance b ~ Symbol => FAState NFA b where
        -- methods same as before
    

    ~ - GHC . , , , ( , ), , GHC, , NFA , , , , , Symbol.

!

+7

All Articles