lombardo2
lombardo2

Reputation: 141

Expand a function in Haskell

In my homework, we are given a regular expression. I have to return an e-NFA. I'm trying to build the delta function. So far I have:

module ConsENFA where

import Data.Set (Set, fromList, singleton, empty)
import RegEx
import ENFA

epsilon :: RegExp Char
epsilon = Symbol 'e'

deltaTest :: RegExp Char -> Int -> (Int -> Char -> Set Int)
deltaTest (Symbol sym) start = delta
    where
        delta :: Int -> Char -> Set Int
        delta start sym = singleton (start + 1)

deltaTest (Star re) start = delta
    where
        delta :: Int -> Char -> Set Int
        delta = deltaTest re (start + 1)
        delta start epsilon = fromList[1, 3]

I got the error

ConsENFA.hs:19:9: error:
Conflicting definitions for `delta'
Bound at: ConsENFA.hs:19:9-13
          ConsENFA.hs:20:9-13

which I assume means that I can't expand the pattern matching like that, I can't add more states.

I first define delta for a single label and then I add more definitions to the previously defined delta, but it's not working. What's the correct way to do it?

Upvotes: 1

Views: 714

Answers (1)

crockeea
crockeea

Reputation: 21811

All definitions of a function must have the same arity, i.e., the same number of function arguments. You define delta in three lines:

  1. The first line is a type signature.
  2. The second line is a definition of delta with arity zero (no arguments to the left of the =)
  3. The third line is another definition of delta with arity two (two arguments to the left of the =)

The two definitions have a different arity, so the compiler tells you there are conflicting definitions.

Ask yourself: What is the inteded behavior of delta? The compiler will look at the definitions of delta in the order they are defined, and choose the first one where a pattern match succeeds. Since there are no arguments in the first definition (and hence no patterns to match), it will always succeed and the second definition will never be called.

Upvotes: 7

Related Questions