Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.haskell > #639

Re: Types

From Jonathan Lamothe <jonathan@jlamothe.net>
Newsgroups comp.lang.haskell
Subject Re: Types
Date 2026-04-19 17:17 -0400
Organization A noiseless patient Spider
Message-ID <87y0ii7iq1.fsf@posteo.de> (permalink)
References <types-20260419194256@ram.dialup.fu-berlin.de>

Show all headers | View raw


ram@zedat.fu-berlin.de (Stefan Ram) writes:

>   Yes, I'm just reading a book by a man who said types are sets
>   and then goes on to "demonstrate" this using Haskell.
>
>   I know nothing about Haskell, but I was mistrustful immediately.
>
>   And here's the demo (my demo, not the book's demo):
>
> -- Both types have the exact same "extension" (two Integers)
> data Apple  = Apple Int Int
> data Orange = Orange Int Int
>
> -- A function that only accepts an Apple
> checkApple :: Apple -> String
> checkApple _ = "This is an Apple"
>
> main :: IO ()
> main = do
>     let myFruit = Orange 1 2
>     
>     -- The line below causes a COMPILE ERROR.
>     -- Even though Orange has the same structure as Apple, 
>     -- the names are different, so the types are NOT equal.
>     putStrLn (checkApple myFruit) 
>
>   . Both types have the same extension, but different names.
>   And Haskell does /not/ treat them to be equal.


I'm curious: what did *his* demo look like?  Perhaps he was doing
something with typeclasses?

e.g.:

data Apple a b = Apple a b
data Orange a b = Orange a b

class Fruit a where
  checkApple :: a Int Int -> String -- a Bool might be better here, but hey...
  checkApple _ = "This is not an apple" -- most fruit aren't apples

instance Fruit Apple where
  checkApple _ = "This is an apple" -- apples are apples

instance Fruit Orange where
  -- no need to override the default behaviour here

main :: IO ()
main = do
  let myFruit = Orange 1 2
  putStrLn (checkApple myFruit)

-- 
Regards,
Jonathan Lamothe
https://jlamothe.net

Back to comp.lang.haskell | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Types ram@zedat.fu-berlin.de (Stefan Ram) - 2026-04-19 18:51 +0000
  Re: Types Jonathan Lamothe <jonathan@jlamothe.net> - 2026-04-19 17:17 -0400
    Re: Types ram@zedat.fu-berlin.de (Stefan Ram) - 2026-04-20 10:32 +0000
      Re: Types Paul Rubin <no.email@nospam.invalid> - 2026-04-20 11:19 -0700
        Re: Types ram@zedat.fu-berlin.de (Stefan Ram) - 2026-04-20 19:32 +0000

csiph-web