[Haskell-cafe] Re: Haskell Helper

Ben Franksen ben.franksen at online.de
Sun Oct 3 18:24:05 EDT 2010


c8h10n4o2 wrote:
> The problem is there. A function in Hai would be function-name,
> arg1,argn=body.
> Func stores function name,arguments and body as Strings(I was thinking to
> put Func String String String).
> The parser func that I wrote so far try to parse a function definition,
> not a function call.
> But when I try to store the function on my Map I get a error with somthing
> called 'functional dependencies'(which I don't know what is).

You mean:

hai1.hs:41:6:
    Couldn't match expected type `Hai' against inferred type `[Hai]'
      Expected type: Map.Map Hai Hai
      Inferred type: Map.Map [Hai] Hai
    When using functional dependencies to combine
      MonadState (Map.Map [Hai] Hai) m,
        arising from a use of `get' at hai1.hs:52:17-19
      MonadState (Map.Map Hai Hai) m,
        arising from a use of `get' at hai1.hs:47:16-18
    When generalising the type(s) for `w'

The type checker tells you that you are using the same Map with different
key types: at 52:17-19 the key has type [Hai], whereas at 47:16-18 it has
type Hai.

The latter is in your Func case:

  e <-return $ Map.insert (a :[b]) c d

where you use  a :[b]  which is the same as  [a,b]  for the key.

Everywhere else, the key has type Hai. This in itself is questionable: do
you really want to use arbitrary expressions as keys? Usually one would
have a

  Map String Hai

representing a map from variable (or function) names to expressions.

For functions you then want

  data Hai = ... | Func [String] Hai | ...

so that

  Func args body

represents the (anonymous) function with the formal arguments  args  and the
resulting expression  body . The function gets a name by inserting it into
the variable map. This means that a definition

  function-name,arg1,...,argn=body

actually defines a variable named  "function-name"  which, when it gets
looked up in the environment, yields the value  Func [arg1,...,argn] body .

Cheers
Ben



More information about the Haskell-Cafe mailing list