mirror of
https://github.com/iritmaximus/haskell.git
synced 2026-10-12 19:13:39 +03:00
lecture 9: fix type of (==)
This commit is contained in:
+1
-1
@@ -589,7 +589,7 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni
|
||||
<p>By contrast, <em>ad hoc polymorphism</em> allows different types to have different implementations of the same function. Ad hoc polymorphism in Haskell can be achieved by defining a <em>type class</em> and then declaring <em>instances</em> of that type class for various types. Ad hoc polymorphism is a handy way for expressing a common set of operations, even when the implementation of the operations depends on the type they’re acting on.</p>
|
||||
<p>Functions that use ad hoc polymorphism have a <em>class constraint</em> in their types. Here are some examples:</p>
|
||||
<div class="sourceCode" id="cb24"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb24-1" data-line-number="1">negate<span class="ot"> ::</span> <span class="dt">Num</span> a <span class="ot">=></span> a <span class="ot">-></span> a</a>
|
||||
<a class="sourceLine" id="cb24-2" data-line-number="2"><span class="ot">(==) ::</span> <span class="dt">Eq</span> a <span class="ot">=></span> a <span class="ot">-></span> a <span class="ot">-></span> a</a>
|
||||
<a class="sourceLine" id="cb24-2" data-line-number="2"><span class="ot">(==) ::</span> <span class="dt">Eq</span> a <span class="ot">=></span> a <span class="ot">-></span> a <span class="ot">-></span> <span class="dt">Bool</span></a>
|
||||
<a class="sourceLine" id="cb24-3" data-line-number="3">sort<span class="ot"> ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a]</a></code></pre></div>
|
||||
<p>A type like <code>Num a => a -> a</code> means: for any type <code>X</code> that’s a member of the <code>Num</code> class, this function has type <code>X -> X</code>. In other words, we can invoke <code>negate</code> on any number type, but not on other types:</p>
|
||||
<div class="sourceCode" id="cb25"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb25-1" data-line-number="1"><span class="dt">Prelude</span><span class="fu">></span> negate <span class="dv">1</span></a>
|
||||
|
||||
Reference in New Issue
Block a user