assignment 4, task 1

Benutzeravatar
Alp.traum
Mausschubser
Mausschubser
Beiträge: 85
Registriert: 3. Feb 2004 15:15
Wohnort: Darmstadt
Kontaktdaten:

assignment 4, task 1

Beitrag von Alp.traum »

I can't get to a real starting point. I tried to add a new datatype NumP which allows Int's and Rational's, but the testcase still throws this error:

Code: Alles auswählen

    Couldn't match expected type `NumP' against inferred type `Ratio t'
    In the first argument of `NumE', namely `(2 % 5)'
    In the second argument of `AddE', namely `(NumE (2 % 5))'
    In the second argument of `FunE', namely
        `(AddE (IdE "x") (NumE (2 % 5)))'
Further, i had to create several instance definitions, because the compiler cried about almost any missing function for my new datatype. Ist this intended for this task, or do i walk to the wrong direction?

Christoph-D
Computerversteher
Computerversteher
Beiträge: 325
Registriert: 11. Dez 2005 13:14
Wohnort: Darmstadt

Re: assignment 4, task 1

Beitrag von Christoph-D »

Alp.traum hat geschrieben:I can't get to a real starting point. I tried to add a new datatype NumP which allows Int's and Rational's, but the testcase still throws this error:

Code: Alles auswählen

    Couldn't match expected type `NumP' against inferred type `Ratio t'
    In the first argument of `NumE', namely `(2 % 5)'
    In the second argument of `AddE', namely `(NumE (2 % 5))'
    In the second argument of `FunE', namely
        `(AddE (IdE "x") (NumE (2 % 5)))'
Further, i had to create several instance definitions, because the compiler cried about almost any missing function for my new datatype. Ist this intended for this task, or do i walk to the wrong direction?
I think you're going the wrong way. The first assignment can actually be done by adding a few a's (this is the letter "a" as used for type variables) here and there and just one type constraint. No need to add any instances or new data types. Technically, you wouldn't even need that single additional type constraint if it weren't for the explicit type signature for the function "interp".

Maybe you're trying to allow *only* Ints and Rationals. Have you considered allowing arbitrary types? In Haskell it's often easier to restrict the types as little as possible. The compiler will figure out the necessary restrictions by itself. For example, the compiler has no trouble to figure out that (+) needs the restriction "Num". You don't have to write (Num a) yourself except in the case of explicit type signatures.
"I believe in the fundamental interconnectedness of all things." (Dirk Gently)

BastiS
Sonntagsinformatiker
Sonntagsinformatiker
Beiträge: 224
Registriert: 3. Nov 2005 19:12
Kontaktdaten:

Re: assignment 4, task 1

Beitrag von BastiS »

I do have problems with this task, too.
In the data-definition I tried something like:

Code: Alles auswählen

data FAE = NumE (Num a => a)
          | AddE FAE FAE
          | IdE String
          | FunE String FAE
          | AppE FAE FAE 
     deriving (Show, Eq)
I want to have "a" be an instance of Num, but this doesn't work this way because no constructor is defined for Num. Can someone give me a hint, how to check if a is a Num?

Christoph-D
Computerversteher
Computerversteher
Beiträge: 325
Registriert: 11. Dez 2005 13:14
Wohnort: Darmstadt

Re: assignment 4, task 1

Beitrag von Christoph-D »

BastiS hat geschrieben:I do have problems with this task, too.
In the data-definition I tried something like:

Code: Alles auswählen

data FAE = NumE (Num a => a)
          | AddE FAE FAE
          | IdE String
          | FunE String FAE
          | AppE FAE FAE 
     deriving (Show, Eq)
I want to have "a" be an instance of Num, but this doesn't work this way because no constructor is defined for Num. Can someone give me a hint, how to check if a is a Num?
You don't need any type constraints except in places where there is an explicit type signature. The only function in this assignment with an explicit type signature is "interp". There is really no need to add the "Num" constraint to the FAE type itself, it'll only make your work your more complicated.

You should put more trust in the Haskell compiler. It *will* figure out *by itself* all the places where you need a "Num" constraint. :)
"I believe in the fundamental interconnectedness of all things." (Dirk Gently)

Julius
Sonntagsinformatiker
Sonntagsinformatiker
Beiträge: 237
Registriert: 5. Okt 2005 15:52

Re: assignment 4, task 1

Beitrag von Julius »

But if I am not wrong, you have to replace the "Int" inside the two type definitions to be able to construct expressions like "NumE 3.5". Is there a special wildcard if we do not want to state the concrete type here?

Benutzeravatar
sewe
Sonntagsinformatiker
Sonntagsinformatiker
Beiträge: 295
Registriert: 16. Jan 2009 14:53
Kontaktdaten:

Re: assignment 4, task 1

Beitrag von sewe »

Just for the record: If you want to place a constraint upon your parameterized FAE, the syntax is wrong; it should be data [context =>] simpletype = constrs [deriving] as per the Haskell Report http://www.haskell.org/onlinereport/decls.html.

If I am not mistaken (cannot try it out at the moment), however, you can also place additional constraints upon the indivual constructor functions by providing an explicit function declaration for NumE, etc. (no definition; that is already handled by the algebraic type declaration) which is more restrictive than what Haskell would infer from the data declaration alone. Not that you need to for this task...

BastiS
Sonntagsinformatiker
Sonntagsinformatiker
Beiträge: 224
Registriert: 3. Nov 2005 19:12
Kontaktdaten:

Re: assignment 4, task 1

Beitrag von BastiS »

Thanks for your help so far....but....I'm getting more and more confused.
If I am not mistaken (cannot try it out at the moment), however, you can also place additional constraints upon the indivual constructor functions by providing an explicit function declaration for NumE, etc. (no definition; that is already handled by the algebraic type declaration) which is more restrictive than what Haskell would infer from the data declaration alone.
I tried it by adding

Code: Alles auswählen

NumE :: Num a => a
But the compiler complains about the "::"
Anywhere....you alread told, we don't need this here.

But I'm still stucking with the polymorphism. As Julius said, we need to replace the Int-thing with anything. Just replacing it with an "a" because "the compiler will figure it out by itself", does not work. So I conclude we have to put some constraint on the a.....but how? :roll:

Daniel S.
Mausschubser
Mausschubser
Beiträge: 87
Registriert: 25. Sep 2007 12:28
Wohnort: Mörfelden

Re: assignment 4, task 1

Beitrag von Daniel S. »

Just replacing it with an "a" because "the compiler will figure it out by itself", does not work.
To make it not to easy for you, here is a similar problem and its solution in C++.
Your code corresponds to

Code: Alles auswählen

 class FAE{  
    public:  
      T NumE;
      //...
 }; 
But you end up with Line 3 - Error: unknown type T

So you have to write

Code: Alles auswählen

template <class T> class FAE{
    public:  
      T NumE;
      //...
 }; 
The type T is now a parameter of the class.
Compare this to page 23 of the Introduction to Haskell slides.

I hope this doesn't confuse you any more ;)
Mit freundlichen Grüßen
Daniel

Julius
Sonntagsinformatiker
Sonntagsinformatiker
Beiträge: 237
Registriert: 5. Okt 2005 15:52

Re: assignment 4, task 1

Beitrag von Julius »

Thank you, that helped a lot to get startet. Defining the type as a paramter of the data-type was the important hint.

Nori
Erstie
Erstie
Beiträge: 22
Registriert: 15. Nov 2006 17:40
Kontaktdaten:

Re: assignment 4, task 1

Beitrag von Nori »

Hello, I'm also struggling with this task.
I added a type variable a to both FAE and FAEValue and now ghci complains I did paremeterize the FAEValues in the defintion of the environment (env). I think I should use a wildcard (live Java "?") here, but I don't know how to do that in haskell.
Am I even on the right track?

Christoph-D
Computerversteher
Computerversteher
Beiträge: 325
Registriert: 11. Dez 2005 13:14
Wohnort: Darmstadt

Re: assignment 4, task 1

Beitrag von Christoph-D »

Nori hat geschrieben:I added a type variable a to both FAE and FAEValue and now ghci complains I did paremeterize the FAEValues in the defintion of the environment (env). I think I should use a wildcard (live Java "?") here, but I don't know how to do that in haskell.
Am I even on the right track?
"a" is a wildcard: It stands for an arbitrary type. [edit: Well, depends on how you think about "wildcards"... comparing Haskell to Java is somewhat difficult, IMHO.]
Did you also add this type variable to Env (I assume you mean "Env" and not "env")?
"I believe in the fundamental interconnectedness of all things." (Dirk Gently)

Benutzeravatar
sewe
Sonntagsinformatiker
Sonntagsinformatiker
Beiträge: 295
Registriert: 16. Jan 2009 14:53
Kontaktdaten:

Re: assignment 4, task 1

Beitrag von sewe »

As Christoph-D already said, using a as a wildcard is perfectly fine. (Just make sure that you use a lowercase letter, as the Haskell parser uses case to disambiguate certain phrases.)

That being said: comparing Haskell's parametric polymorphism to Java's (both parametric and subtype) polymorphism is something that is planned for the next exercise's quiz.

Antworten

Zurück zu „Archiv“