From borgauf at gmail.com Thu Sep 9 21:25:58 2021 From: borgauf at gmail.com (Galaxy Being) Date: Thu, 9 Sep 2021 16:25:58 -0500 Subject: [Haskell-beginners] Confusing Show/print error Message-ID: I've got this import Data.Tuple fswp :: (a, b) -> (b, a) fswp = Data.Tuple.swap and get this • No instance for (Show ((a0, b0) -> (b0, a0))) arising from a use of ‘print’ (maybe you haven't applied a function to enough arguments?) • In a stmt of an interactive GHCi command: print it Not sure why or what to do to correct it. ⨽ Lawrence Bottorff Grand Marais, MN, USA borgauf at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Thu Sep 9 21:51:38 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Thu, 9 Sep 2021 23:51:38 +0200 Subject: [Haskell-beginners] Confusing Show/print error In-Reply-To: References: Message-ID: Hello Lawrence, Il 09 settembre 2021 alle 16:25 Galaxy Being ha scritto: > import Data.Tuple > fswp :: (a, b) -> (b, a) > fswp = Data.Tuple.swap this typechecks without problem. maybe you invoked fswp without an argument? (i.e. `λ> fwsp` instead of `λ> fwsp (1,'a')`) From to_br at uni-bremen.de Thu Sep 9 21:56:58 2021 From: to_br at uni-bremen.de (Tobias Brandt) Date: Thu, 9 Sep 2021 23:56:58 +0200 Subject: [Haskell-beginners] Confusing Show/print error In-Reply-To: References: Message-ID: <4c9f37b6-c3d2-30f6-aa1a-2466cd01e8ad@uni-bremen.de> Hi Lawrence, it seems that you are trying to print your defined function. Most likely you just forgot to apply an argument to fswp. So 'fswp (1,2)' should just work fine. Cheers, Tobias On 9/9/21 11:25 PM, Galaxy Being wrote: > I've got this > > import Data.Tuple > fswp :: (a, b) -> (b, a) > fswp = Data.Tuple.swap > > and get this > > • No instance for (Show ((a0, b0) -> (b0, a0))) >         arising from a use of ‘print’ >         (maybe you haven't applied a function to enough arguments?) >     • In a stmt of an interactive GHCi command: print it > > Not sure why or what to do to correct it. > > ⨽ > Lawrence Bottorff > Grand Marais, MN, USA > borgauf at gmail.com > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From fernandobasso.br at gmail.com Sun Sep 12 10:57:38 2021 From: fernandobasso.br at gmail.com (Fernando Basso) Date: Sun, 12 Sep 2021 07:57:38 -0300 Subject: [Haskell-beginners] Confusing Show/print error In-Reply-To: References: Message-ID: <20210912105738.egomltlkquv5le5l@gmail.com> Hi Lawrence. Other people have given some answers, but perhaps saying it in a different way may also help. For starters, we have to remember that functions do not have instances of `Show' [1]: To exemplify, if we have a function and try to print it, we get an error: f :: a -> a f x = x $ ghci Prelude> :load fn.hs *Main> f :2:1: error: • No instance for (Show (a0 -> a0)) arising from a use of ‘print’ (maybe you haven't applied a function to enough arguments?) • In a stmt of an interactive GHCi command: print it *Main> This is just to exemplify that there is nothing wrong with *your* function. As you see, I produced the same error with a much simpler function. So, as others mentioned, it looks like you just tried to print your function, which is not possible. I would also like to add a note about emacs haskell-mode's REPL that is related to this topic. haskell-mode's REPL runs `:type' behind the scenes when we try to print functions. It might give the impression that we can actually print functions, or that functions have instance of `Show', which is not the case. In emacs + haskell-mode, the variable haskell-interactive-types-for-show-ambiguous is supposed to be `t' by default (instead of `nil'). In any case, you can play with it and try to print functions directly by just typing their name [2]: (custom-set-variables '(haskell-interactive-types-for-show-ambiguous t) References: • [1] https://wiki.haskell.org/Show_instance_for_functions • [2] https://haskell.github.io/haskell-mode/manual/latest/Interactive-Haskell.html#Type-of-expressions On Thu, Sep 09, 2021 at 04:25:58PM -0500, Galaxy Being wrote: > I've got this > > import Data.Tuple > fswp :: (a, b) -> (b, a) > fswp = Data.Tuple.swap > > and get this > > • No instance for (Show ((a0, b0) -> (b0, a0))) > arising from a use of ‘print’ > (maybe you haven't applied a function to enough arguments?) > • In a stmt of an interactive GHCi command: print it > > Not sure why or what to do to correct it. > > ⨽ > Lawrence Bottorff > Grand Marais, MN, USA > borgauf at gmail.com > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners From anthony.d.clayden at gmail.com Thu Sep 16 22:00:18 2021 From: anthony.d.clayden at gmail.com (Anthony Clayden) Date: Fri, 17 Sep 2021 10:00:18 +1200 Subject: [Haskell-beginners] A one- or two-page diagram of how Haskell works? In-Reply-To: References: Message-ID: Thank you Tarik, you're spot-on correct. > It's better to try to model execution using lambda calculus. > The most dominant factor in languages like Haskell is lambda calculus. > Think through lambda calculus for a mental model. I presume it's this advice that Michael is complaining about in his long comment on the cafe yesterday. In particular, Haskell is unusual even amongst functional programming languages in its lazy evaluation. So to try to correct Michael's misunderstanding: >> When I write C, or even C++, I have a mental model of how execution will proceed. Firstly, 'execution' in Haskell does not proceed in anything like the sequence-of-steps and update-of-locations/frame-on-the-stack of C or C++ (or Fortran or ALGOL or COBOL or ...). In particular, 'variables' in Haskell are not overwritable locations in the sense of a procedural language. Variables are just a name for an expression (might be a lambda expression). This is what we mean by 'referential transparency' that procedural languages do not have. We can replace any variable by the expression it denotes, or by any other equivalent expression. Secondly, there's an important theorem (Church-Rosser) in Lambda calculus [see wikipedia], that if Beta-reduction terminates, it'll produce the same 'reduced form' of the initial expression whether it proceeds inside-out or outside-in or both-sides-to-the-middle. You don't need to second-guess how the compiler proceeds, because you don't need to know. You do need to understand Beta-reduction (that's easy). You also need to understand why Alpha-renaming is needed hand-in-hand with Beta-reduction (not so easy, but work a few examples with a recursive function, such as stepping through a list). And you need to stop immediately trying to imagine Haskell semantics as some sort of crazy Turing machine: you'll go mad. Turing himself [1937] proved the semantics are equivalent; but that paper is impenetrable for ordinary mortals like me. -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Sat Sep 18 22:30:17 2021 From: borgauf at gmail.com (Galaxy Being) Date: Sat, 18 Sep 2021 17:30:17 -0500 Subject: [Haskell-beginners] Type signatures returned with :t Message-ID: > :t Just True Just True :: Maybe Bool > :t Left True Left True :: Either Bool b > :t Right False Right False :: Either a Bool What am I being told here? It seems data Maybe a = Just a | Nothing data Either a b = Left a | Right b are both straightforward parameterized types, but Maybe doesn't give me a type parameter back, while Either does, and in different order, different names (a becomes b; b becomes a) depending on which variable I invoke. What deeper lore am I not seeing here? -- ⨽ Lawrence Bottorff Grand Marais, MN, USA borgauf at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Sat Sep 18 22:41:58 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Sun, 19 Sep 2021 00:41:58 +0200 Subject: [Haskell-beginners] Type signatures returned with :t In-Reply-To: References: Message-ID: Il 18 settembre 2021 alle 17:30 Galaxy Being ha scritto: > > :t Just True > Just True :: Maybe Bool > > :t Left True > Left True :: Either Bool b > > :t Right False > Right False :: Either a Bool > > What am I being told here? It seems > are both straightforward parameterized types, but Maybe doesn't give me a > type parameter back, while Either does, and in different order, different > names (a becomes b; b becomes a) depending on which variable I invoke. What > deeper lore am I not seeing here? When you ask the type of λ> :t Just True the interpreter *knows* that that `Maybe` is not just a `Maybe a` (so type constructor and its type parameter) but the /concrete/ type `Maybe Bool`. This would not be the case if we did λ> :t Nothing Nothing :: Maybe a Same story with `Either`. Each of the two data constructors (`Left` and `Right`) let the interpreter infer just *one* of the type parameters (the `a` and `b` in `Either a b`). Does this answer your question? From borgauf at gmail.com Sun Sep 19 02:19:09 2021 From: borgauf at gmail.com (Galaxy Being) Date: Sat, 18 Sep 2021 21:19:09 -0500 Subject: [Haskell-beginners] Type signatures returned with :t In-Reply-To: References: Message-ID: Either returns with its parameters, reversed, but Maybe did not. That's my main question. On Sat, Sep 18, 2021 at 5:43 PM Francesco Ariis wrote: > Il 18 settembre 2021 alle 17:30 Galaxy Being ha scritto: > > > :t Just True > > Just True :: Maybe Bool > > > :t Left True > > Left True :: Either Bool b > > > :t Right False > > Right False :: Either a Bool > > > > What am I being told here? It seems > > are both straightforward parameterized types, but Maybe doesn't give me a > > type parameter back, while Either does, and in different order, different > > names (a becomes b; b becomes a) depending on which variable I invoke. > What > > deeper lore am I not seeing here? > > When you ask the type of > > λ> :t Just True > > the interpreter *knows* that that `Maybe` is not just a `Maybe a` (so > type constructor and its type parameter) but the /concrete/ type `Maybe > Bool`. This would not be the case if we did > > λ> :t Nothing > Nothing :: Maybe a > > Same story with `Either`. Each of the two data constructors (`Left` and > `Right`) let the interpreter infer just *one* of the type parameters > (the `a` and `b` in `Either a b`). > Does this answer your question? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- ⨽ Lawrence Bottorff Grand Marais, MN, USA borgauf at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From erik.dominikus71 at gmail.com Sun Sep 19 04:09:37 2021 From: erik.dominikus71 at gmail.com (Erik Dominikus) Date: Sun, 19 Sep 2021 11:09:37 +0700 Subject: [Haskell-beginners] Type signatures returned with :t In-Reply-To: References: Message-ID: > What deeper lore am I not seeing here? The "deeper lore" you are looking for is: - Type variables that look unbound are actually implicitly bound with a universal quantifier: - "Maybe a" is actually "forall a. Maybe a"; - "Either a b" is actually "forall a. forall b. Either a b", that is, "forall a. (forall b. Either a b)"; - "Either a (Either b c)" is actually "forall a. forall b. forall c. Either a (Either b c)"; - "a" is actually "forall a. a"; - "f a" is actually "forall f. forall a. f a". - alpha-equivalence: You can rename the bound variables of an expression without changing its meaning, as long as you don't change the bindings. - Example of changing the bindings: "forall x. forall y. x" (x is bound by the first/outer quantifier) is not alpha-equivalent to "forall y. forall x. x" (x is bound by the second/inner quantifier). Examples of alpha-equivalence (here "≡" reads "is alpha-equivalent to"): forall a. forall b. Either a b ≡ forall x. forall y. Either x y ≡ forall b. forall a. Either b a forall a. Either Bool a ≡ forall b. Either Bool b -------------- next part -------------- An HTML attachment was scrubbed... URL: From andipersti at gmail.com Sun Sep 19 04:46:35 2021 From: andipersti at gmail.com (Andreas Perstinger) Date: Sun, 19 Sep 2021 06:46:35 +0200 Subject: [Haskell-beginners] Type signatures returned with :t In-Reply-To: References: Message-ID: On 19.09.21 00:30, Galaxy Being wrote: >> :t Just True > Just True :: Maybe Bool What's the type of `Just`? > :t Just Just :: a -> Maybe a So `Just` is a constructor function that takes a value of type `a` and gives you back a value of type `Maybe a`. In your example you provide a concrete type for `a` (`True :: Bool`) so the type of the result is `Maybe Bool`. >> :t Left True > Left True :: Either Bool b As above what's the type of `Left`? > :t Left Left :: a -> Either a b Again, `Left` is a constructor function that takes a single value of type `a` but now returns a value of type `Either a b`, i.e. it has two type variables. If you specify the type of `a` (again `Bool` in your example), `b` is still unspecified/polymorphic and that's why the resulting type is `Either Bool b`. Bye, Andreas From erik.dominikus71 at gmail.com Sun Sep 19 12:54:30 2021 From: erik.dominikus71 at gmail.com (Erik Dominikus) Date: Sun, 19 Sep 2021 19:54:30 +0700 Subject: [Haskell-beginners] Type signatures returned with :t In-Reply-To: References: Message-ID: > are both straightforward parameterized types, but Maybe doesn't give me a type parameter back, while Either does, and in different order, different names (a becomes b; b becomes a) depending on which variable I invoke. Try these diagnostics: --- Diagnostic 1 Given that the type of "const" is "a -> b -> a" and the type of "True" is "Bool": :t const const :: a -> b -> a :t True True :: Bool What do you expect the type of "const True" to be? :t const True const True :: - If you answer "b -> Bool", you are correct, so you may be confused by the data type definition syntax (see below). - If you answer something else, you may be missing some basic understanding of the type system. --- Diagnostic 2 Consider this case: :t id id :: a -> a :t (id, id) (id, id) :: (a1 -> a1, a2 -> a2) - If you think that the type of "(id, id)" should be "(a -> a, a -> a)", you may be unaware of the implicit universal quantifiers. --- If you are confused by the data type definition syntax: data Either a b = Left a | Right b then, consider the GADT (generalized algebraic data type) definition syntax for saying the same thing: data Either a b where Left :: a -> Either a b Right :: b -> Either a b The problem becomes less confusing: The GADT definition syntax is screaming that "Left" is a function like "id", "const", etc. --- If you're still unsure, try thinking aloud here: Write your expectations and why you think so (your thought process, what steps you took to arrive at your expectations). -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.neely at gmail.com Sun Sep 19 17:13:59 2021 From: joel.neely at gmail.com (Joel Neely) Date: Sun, 19 Sep 2021 12:13:59 -0500 Subject: [Haskell-beginners] Type signatures returned with :t In-Reply-To: References: Message-ID: I hope the wizards will forgive a down-to-earth analogy. Either a b is a knapsack with two pockets, the one on the Left (which must hold an "a") and the one on the Right (which must hold a "b"). But you can only use one pocket at a time. So when there's a specific case of Either a b such as Left True, all that can be concluded is that this is a case of an Either whose left pocket must be able to handle a Bool; there's not enough information to know what could have been put in the Right pocket. So only the left-pocket type ("a") can be replaced with something specific. Similarly, for a specific case of Right False, it's clear that the right pocket holds a value of type Bool (replacing the general "b"), but there's no information to identify what type might be in the left pocket. So it remains unspecified. I hope that helps. -jn- On Sat, Sep 18, 2021 at 9:21 PM Galaxy Being wrote: > Either returns with its parameters, reversed, but Maybe did not. That's my > main question. > > On Sat, Sep 18, 2021 at 5:43 PM Francesco Ariis wrote: > >> Il 18 settembre 2021 alle 17:30 Galaxy Being ha scritto: >> > > :t Just True >> > Just True :: Maybe Bool >> > > :t Left True >> > Left True :: Either Bool b >> > > :t Right False >> > Right False :: Either a Bool >> > >> > What am I being told here? It seems >> > are both straightforward parameterized types, but Maybe doesn't give me >> a >> > type parameter back, while Either does, and in different order, >> different >> > names (a becomes b; b becomes a) depending on which variable I invoke. >> What >> > deeper lore am I not seeing here? >> >> When you ask the type of >> >> λ> :t Just True >> >> the interpreter *knows* that that `Maybe` is not just a `Maybe a` (so >> type constructor and its type parameter) but the /concrete/ type `Maybe >> Bool`. This would not be the case if we did >> >> λ> :t Nothing >> Nothing :: Maybe a >> >> Same story with `Either`. Each of the two data constructors (`Left` and >> `Right`) let the interpreter infer just *one* of the type parameters >> (the `a` and `b` in `Either a b`). >> Does this answer your question? >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > > > -- > ⨽ > Lawrence Bottorff > Grand Marais, MN, USA > borgauf at gmail.com > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: