How to define ord manually.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to define ord manually.

derek riemer
Hi,
I was playing around with haskell, and decided to implement a
RockPaperScissors type.

data RPS= Rock | Paper | Scissors
Then I manually derived show for fun.
instance Show RPS where
     show Rock = "rock"
     show Paper = "paper"
     show Scissors = "scissors"

But, I have a problem when defining ord.
How do I look in the sourcecode for ord?
In ghci I did
prelude> :i Ord
<snip>
          Ord (a, b, c, d, e, f, g, h, i, j)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h,
           Ord i) =>
          Ord (a, b, c, d, e, f, g, h, i)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
           Ord h) =>
          Ord (a, b, c, d, e, f, g, h)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g) =>
          Ord (a, b, c, d, e, f, g)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f) =>
          Ord (a, b, c, d, e, f)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d, Ord e) => Ord (a, b, c, d, e)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d) => Ord (a, b, c, d)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c) => Ord (a, b, c)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b) => Ord (a, b) -- Defined in `GHC.Classes'
instance Ord () -- Defined in `GHC.Classes'
Prelude>
Where is ghc.classes? I looked under the ghc directory and didn't find a
classes.hs file.

I tried defining it as such.

instance Ord RPS where
     ord Scissors `compare` Rock = LT
     ord Paper `compare` Scissors = LT
     ord Rock `compare` Paper = LT

But this fails with verry little errors, and I really don't know how I
define an instance of Ord for this.
I want rock to be less than paper, but greater than scissors,
scissors to be less than rock but greater than paper,
and paper to be greater than rock but less than scissors.
Kind of geeky, but it's one way of learning.

Also, What is the best way to write useful programs,
One example I tried is to make a simple guess what I am guessing game,
but I couldn't make heads or tails of the IO typeclasses, or the
System.Random module. How can I possibly get random numbers and do
things on them, and does this mean main must be imperative with a do
statement?
Thanks,
Derek
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to define ord manually.

Nikita Danilenko
Hi Derek,

you can find the desired functions and type classes using Hoogle [1].
Your attempt at an Ord instance is basically the right idea, but you
need to define the function "compare" only [2]. From your syntax it
looks like you are trying to define a function called "ord" that
additionally uses "compare" (in a way, it cannot be used). Removing
"ord" in all three cases fixes your problem, although the resulting
function "compare" is not total.

The documentation of the type classes usually contains a description of
what functions you need to define. The fact that the "Show" type class
instance can be obtained using a function called "show" is a coincidence.

Best regards,

Nikita

[1] https://www.haskell.org/hoogle/

[2] http://hackage.haskell.org/package/base-4.8.1.0/docs/Prelude.html#t:Ord

On 30/07/15 15:16, derek riemer wrote:

> Hi,
> I was playing around with haskell, and decided to implement a
> RockPaperScissors type.
>
> data RPS= Rock | Paper | Scissors
> Then I manually derived show for fun.
> instance Show RPS where
>     show Rock = "rock"
>     show Paper = "paper"
>     show Scissors = "scissors"
>
> But, I have a problem when defining ord.
> How do I look in the sourcecode for ord?
> In ghci I did
> prelude> :i Ord
> <snip>
>          Ord (a, b, c, d, e, f, g, h, i, j)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h,
>           Ord i) =>
>          Ord (a, b, c, d, e, f, g, h, i)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
>           Ord h) =>
>          Ord (a, b, c, d, e, f, g, h)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g) =>
>          Ord (a, b, c, d, e, f, g)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f) =>
>          Ord (a, b, c, d, e, f)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e) => Ord (a, b, c, d, e)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d) => Ord (a, b, c, d)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c) => Ord (a, b, c)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b) => Ord (a, b) -- Defined in `GHC.Classes'
> instance Ord () -- Defined in `GHC.Classes'
> Prelude>
> Where is ghc.classes? I looked under the ghc directory and didn't find a
> classes.hs file.
>
> I tried defining it as such.
>
> instance Ord RPS where
>     ord Scissors `compare` Rock = LT
>     ord Paper `compare` Scissors = LT
>     ord Rock `compare` Paper = LT
>
> But this fails with verry little errors, and I really don't know how I
> define an instance of Ord for this.
> I want rock to be less than paper, but greater than scissors,
> scissors to be less than rock but greater than paper,
> and paper to be greater than rock but less than scissors.
> Kind of geeky, but it's one way of learning.
>
> Also, What is the best way to write useful programs,
> One example I tried is to make a simple guess what I am guessing game,
> but I couldn't make heads or tails of the IO typeclasses, or the
> System.Random module. How can I possibly get random numbers and do
> things on them, and does this mean main must be imperative with a do
> statement?
> Thanks,
> Derek
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

--
Dipl.-Math. Nikita Danilenko
Research group:
Computer Aided Program Development
Kiel University
Olshausenstr. 40, D-24098 Kiel
Phone: +49 431 880 7275
URL: https://www.informatik.uni-kiel.de/index.php?id=nikita

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

0x76C229F0.asc (8K) Download Attachment
signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to define ord manually.

Marcin Mrotek
In reply to this post by derek riemer
The first Google result: https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Ord.html It says right there that you need to define (at least) either "compare" or "<=", and that there's no "ord" function.

Best regards,
Marcin Mrotek

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to define ord manually.

Imants Cekusins
Derek, if you face any questions, feel free to write here.

The search engines are there alright but humans do tend to
communicate. Even if it may be in the books somewhere.

Nothing wrong with that ;)
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to define ord manually.

Chaddaï Fouché
In reply to this post by Nikita Danilenko
While you may define this Ord instance by defining the compare method, you should probably not do this since Ord is supposed to follow some rules, in particular transitivity "a < b and b < c means a < c" is assumed but false in your case. Those rules are important for many functions to work correctly (sort, minimum, maximum and so on...).

You may simply define another function like winAgainst :

Rock `winAgainst` Scissors = True
Scissors `winAgainst` Paper = True
Paper `winAgainst` Rock = True
_ `winAgainst` _ = False

or maybe a battle function that returns a Win | Loss | Tie...

(NB : All functions can be used as infix by surrounding their name with ` (antiquotes))

Le jeu. 30 juil. 2015 à 15:34, Nikita Danilenko <[hidden email]> a écrit :
Hi Derek,

you can find the desired functions and type classes using Hoogle [1].
Your attempt at an Ord instance is basically the right idea, but you
need to define the function "compare" only [2]. From your syntax it
looks like you are trying to define a function called "ord" that
additionally uses "compare" (in a way, it cannot be used). Removing
"ord" in all three cases fixes your problem, although the resulting
function "compare" is not total.

The documentation of the type classes usually contains a description of
what functions you need to define. The fact that the "Show" type class
instance can be obtained using a function called "show" is a coincidence.

Best regards,

Nikita

[1] https://www.haskell.org/hoogle/

[2] http://hackage.haskell.org/package/base-4.8.1.0/docs/Prelude.html#t:Ord

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners