Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>(Update: Apparently my original answer wasn't very helpful for the OP... See below the second HR for an appendix.)</em></p> <hr> <p>In situations like this, what you really want to do is to launch <code>ghci</code> and use <code>:t</code> to find out the type of various expressions. E.g.</p> <pre><code>:t foldr map -- answer: foldr map :: [b] -&gt; [b -&gt; b] -&gt; [b] </code></pre> <p>If you need to define a name first, use <code>let</code> (which would not be needed in a Haskell source file):</p> <pre><code>let mys = undefined :: a -&gt; a :t map mys -- answer: [a] -&gt; [a] </code></pre> <p>Note the use of <code>undefined</code> with an explicit type signature. It's perfectly ok for finding out the type of expressions of various forms and might even be useful in actual code as a placeholder at early planning stages.</p> <p>The <code>Num a =&gt;</code> is a type class constraint on <code>a</code>; see e.g. <a href="http://www.haskell.org/tutorial/classes.html" rel="nofollow noreferrer">Type Classes and Overloading</a> from "A Gentle Introduction to Haskell, Version 98" or <a href="http://book.realworldhaskell.org/read/using-typeclasses.html" rel="nofollow noreferrer">Chapter 6. Using Typeclasses</a> from "Real World Haskell" for more information. (Basically, it does what you think it does. :-))</p> <hr> <p>The above should be useful for verifying your answers (and the resources on type classes are good). As for how to solve problems of this kind yourself:</p> <p>Type inference is an application of what is called "the unification algorithm". Google for "unification" for a number of resources; if you add a name of a programming language to your query, you'll likely find example implementations.</p> <p>As for how to approach the examples at hand...</p> <p>a. <code>map mys</code>: <code>map</code> takes a function of type <code>a -&gt; b</code> and returns a function of type <code>[a] -&gt; [b]</code>. In general, <code>a</code> can be different to <code>b</code>, but <code>mys</code> is of type <code>a -&gt; a</code>, so the returned function will be of type <code>[a] -&gt; [a]</code>.</p> <p>Here's some hand-unification:</p> <pre><code>(a -&gt; b) -&gt; [a] -&gt; [b]` (a -&gt; a)` ^-- at this point we unify a with b; when propagated to the return type, this produces [a] -&gt; [a] </code></pre> <p>b. <code>mys map</code>: <code>mys</code> is a function which takes an object of some type and returns an object of the same type. In particular, if you pass it an argument of type <code>(a -&gt; b) -&gt; [a] -&gt; [b]</code>, that will be the type of the return value.</p> <p>Incidentally, there is only one "interesting" (not <code>undefined</code>) function whose type signature is <code>a -&gt; a</code> (without type class constraints), namely <code>id</code>. See Philip Wadler's paper "Theorems for free!" (which you can download from <a href="http://homepages.inf.ed.ac.uk/wadler/topics/parametricity.html" rel="nofollow noreferrer">this page</a>) for an extended discussion.</p> <p>c. <code>foldr map</code>: Firstly, note that the <code>a</code>s and <code>b</code>s in <code>foldr</code>'s signature having nothing to do with those in <code>map</code>'s signature. It might be convenient to rename <code>map</code>'s <code>a</code> to <code>c</code> and <code>b</code> to <code>d</code> and use the signature <code>map :: (c -&gt; d) -&gt; [c] -&gt; [d]</code> to make this more readily apparent below. Also note that <code>(a -&gt; b -&gt; b)</code> is just a simpler way of writing <code>(a -&gt; (b -&gt; b))</code>.</p> <p>More hand-unification (explanation follows below):</p> <pre><code>(a -&gt; (b -&gt; b)) (c -&gt; d) -&gt; [c] -&gt; [d] </code></pre> <p>What happens here is that <code>foldr</code> takes a function argument of type <code>(a -&gt; (b -&gt; b))</code>. If you pass in <code>map</code> as that argument, the <code>a</code> gets unified with <code>c -&gt; d</code>, the <code>b</code> with <code>[c]</code> and then again with <code>[d]</code>, which means <code>c</code> equals <code>d</code>.</p> <p>The return value of <code>foldr</code> has type <code>b -&gt; [a] -&gt; b</code>; substituting the more specific types obtained in the previous paragraph we get</p> <pre><code>[c] -&gt; [c -&gt; c] -&gt; [c] ^-- c equals d, right? </code></pre> <p>The <code>c</code> comes from our changed signature for <code>map</code>; with the original <code>b</code>, this becomes</p> <pre><code>[b] -&gt; [b -&gt; b] -&gt; [b] </code></pre> <p>d. <code>foldr map . mys</code>: This is actually <code>(foldr map) . mys</code>, not <code>foldr (map . mys)</code> -- function application ("the invisible operator") binds the strongest! Combining a. and c. from above to solve this one is left as an exercise to the reader. ;-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload