User Tools

Site Tools


codesnippets:typeclasses

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
codesnippets:typeclasses [2021/04/10 15:50] f2b216codesnippets:typeclasses [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Type classes and its instances ====== +====== Type classes and its instances, and the deriving mechanism ======
-~~DISCUSSION~~+
  
   * A type class (keyword: ''class'') is a set of functions declarations that    * A type class (keyword: ''class'') is a set of functions declarations that 
Line 6: Line 5:
     * have instances (keyword: ''instance'') with type specific bindings.     * have instances (keyword: ''instance'') with type specific bindings.
   * module ''Prelude'' already consists of many type classes   * module ''Prelude'' already consists of many type classes
-    * {{:codesnippets:preludetypeclasses.png?400|Type classes in Prelude}}+    * {{:modules:preludetypeclasses.png?direct&300|}} 
 +      * picture from: Dirk Hünniger 
 +      * source: [[https://commons.wikimedia.org/wiki/File:HaskellClasses.svg]]
       * type ''class''es are in bold       * type ''class''es are in bold
       * below all types that have implemented instances       * below all types that have implemented instances
Line 14: Line 15:
         *'' Float''         *'' Float''
         * ''Double''         * ''Double''
-  * example, implementing a ''Functor'':<code Haskell>+  * additionally, there is many other libraries available with important type classes 
 +    * {{:codesnippets:typeclassopedia-diagram.png?direct&700|}} 
 +      * source: [[https://wiki.haskell.org/Typeclassopedia]] 
 +  * example, implementing an ''Enum'' type class, and an instance for a data type 
 +    * functions ''succ'', and ''pred'' have default implementations in ''class  Enum'',  
 +      * which have been overridden in the ''instance Enum MyInt'' of  
 +    * code:<code Haskell> 
 +import Prelude hiding (Enum(..)) 
 + 
 +main :: IO () 
 +main = putStrLn (show x1) 
 + 
 +class  Enum a  where 
 +    succ, pred     :: a -> a 
 +    succ            = toEnum . (+ 1) . fromEnum 
 +    pred            = toEnum . (subtract 1) . fromEnum 
 +    toEnum         :: Int -> a 
 +    fromEnum       :: a -> Int 
 + 
 +data MyInt = C0 | C1 | C2 | C3 
 + 
 +instance Enum MyInt where 
 +    toEnum 0 = C0 
 +    toEnum 1 = C1 
 +    toEnum 2 = C2 
 +    toEnum 3 = C3 
 +    fromEnum C0 = 0 
 +    fromEnum C1 = 1 
 +    fromEnum C2 = 2 
 +    fromEnum C3 = 3 
 +    succ C3 = C0 
 +    succ x = (toEnum . (+ 1) . fromEnum) x 
 +    pred C0 = C3 
 +    pred x = (toEnum . (subtract 1) . fromEnum) x 
 + 
 +x1 = fromEnum (succ (succ (succ C3))) 
 +</code> 
 +    * executes, with output:<code> 
 +
 +</code> 
 +  * example, implementing a ''Show'' type class, and an instance for a data type 
 +    * function ''show'' is implemented to covert every possible value of ''MyInt'' into a ''String'' 
 +    * code:<code Haskell> 
 +import Prelude hiding (Show(..)) 
 + 
 +main :: IO () 
 +main = putStrLn (show x1) 
 + 
 +class  Show a  where 
 +    show :: a -> String 
 + 
 +data MyInt = C0 | C1 | C2 | C3 
 + 
 +instance Show MyInt where 
 +    show C0 = "C0" 
 +    show C1 = "C1" 
 +    show C2 = "C2" 
 +    show C3 = "C3" 
 + 
 +x1 = C3 
 +</code> 
 +    * executes, with output:<code> 
 +C3 
 +</code> 
 +  * example, implementing an ''instance'' of the ''Prelude'' class ''Show'' for a data type 
 +    * function ''show'' is implemented again for ''MyInt'' 
 +    * code:<code Haskell> 
 +main :: IO () 
 +main = putStrLn (show x1) 
 + 
 +data MyInt = C0 | C1 | C2 | C3 
 + 
 +instance Show MyInt where 
 +    show C0 = "C0" 
 +    show C1 = "C1" 
 +    show C2 = "C2" 
 +    show C3 = "C3" 
 + 
 +x1 = C3 
 +</code> 
 +    * executes, again with output:<code> 
 +C3 
 +</code> 
 + 
 +===== Deriving mechanism ===== 
 + 
 +Looking to the examples above you may wonder, whether there an easier way to implement an ''instance'' of standard type classes. Yes, there is the ''deriving'' mechanism. 
 + 
 +  * example, where a data type applies ''deriving'' mechanism 
 +    * to implement all function of typeclass ''Show'', and ''Enum'' 
 +    * code:<code Haskell> 
 +main :: IO () 
 +main = putStrLn (show x1) 
 + 
 +data MyInt = C0 | C1 | C2 | C3 
 +    deriving (Show, Enum) 
 + 
 +x1 = succ C1 
 +</code> 
 +    * executes, again with output:<code> 
 +C2 
 +</code> 
 + 
 +===== Advanced type classes ===== 
 + 
 +  * example, implementing a ''Functor'' 
 +    * code:<code Haskell>
 import Prelude hiding (Maybe(..), Functor(..)) import Prelude hiding (Maybe(..), Functor(..))
  
Line 37: Line 144:
 Just 5 Just 5
 </code> </code>
-  * example, implementing a ''Functor'':<code Haskell> + 
-... + 
-</code> +===== ✎ ===== 
-    * executes, with output:<code> +~~DISCUSSION~~
-... +
-</code>+
codesnippets/typeclasses.1618062627.txt.gz · Last modified: (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki