applicative default structure

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

applicative default structure

Ovidiu Deac
This code is using Applicative Maybe. The structure is provided by Maybe and the value 1 is wrapped in this structure. No surprises here.

Prelude> pure 1 :: Maybe Int
Just 1
Prelude> :t (pure 1 :: Maybe Int)
(pure 1 :: Maybe Int) :: Maybe Int

...but can somebody explain the type of x below?

Prelude> x = pure 1
Prelude> x
1
Prelude> :t x
x :: (Applicative f, Num a) => f a

What is f here?

Thanks!

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: applicative default structure

Imants Cekusins
What is f here?

anything Applicative:

Prelude> let a1 = pure 1
Prelude> let a2 = pure 1

Prelude> (a1::Maybe Int) == a2
True
Prelude> (a1::Maybe Float) == a2
True
Prelude> (a1::Either String Float) == a2
True



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: applicative default structure

Ovidiu Deac
My understanding is that x can take any form required for type-inference. That's fine but what is the "default" structure if you don't specify any?

On Thu, Dec 22, 2016 at 5:13 PM, Imants Cekusins <[hidden email]> wrote:
What is f here?

anything Applicative:

Prelude> let a1 = pure 1
Prelude> let a2 = pure 1

Prelude> (a1::Maybe Int) == a2
True
Prelude> (a1::Maybe Float) == a2
True
Prelude> (a1::Either String Float) == a2
True



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: applicative default structure

David McBride
Rather than bailing with an instance error for both Applicative and Num, which is the technically the right way, and the way that it used to be in the dark ages of ghci.  Instead it chooses types which are probably what you wanted.  In this case it defaults f to IO and a to Int, and then runs it.

You can read more about type defaulting in ghci here: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html

On Thu, Dec 22, 2016 at 10:18 AM, Ovidiu Deac <[hidden email]> wrote:
My understanding is that x can take any form required for type-inference. That's fine but what is the "default" structure if you don't specify any?

On Thu, Dec 22, 2016 at 5:13 PM, Imants Cekusins <[hidden email]> wrote:
What is f here?

anything Applicative:

Prelude> let a1 = pure 1
Prelude> let a2 = pure 1

Prelude> (a1::Maybe Int) == a2
True
Prelude> (a1::Maybe Float) == a2
True
Prelude> (a1::Either String Float) == a2
True



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: applicative default structure

Imants Cekusins
In reply to this post by Ovidiu Deac
> what is the "default" structure if you don't specify any

similar to:

display::Show a => a -> String
display = show

fa::(Applicative f, Num a) => f a
fa = pure 1

f and a are bounded by Applicative and Num, so to say. No default. Or, it is typed however type is a bit broader than *


> ​ In this case it defaults f to IO and a to Int, 

does it though?


Prelude> let a1 = pure 1
Prelude> let a2 = pure 1
Prelude> a1 == a2

<interactive>:20:1: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘a1’
      prevents the constraint ‘(Num a0)’ from being solved.


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: applicative default structure

Imants Cekusins
> type is a bit broader than *
ignore this phrase. it is incorrect.

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: applicative default structure

David McBride
In reply to this post by Imants Cekusins
I get a Num ambiguous constraint error as well, but it is merely not giving you the rest of the errors.

Prelude> let a1 = pure (1::Int)
Prelude> let a2 = pure (1::Int)
Prelude> a1 == a2
   • Ambiguous type variable ‘f0’ arising from a use of ‘a1’
      prevents the constraint ‘(Applicative f0)’ from being solved.

Furthermore you can't compare an IO a with an IO a, so it will still cause an error.  But if you go:

Prelude>let a1 = 1
Prelude>let a2 = 1
Prelude>a1 == a2
True

Then it is totally fine with that.


On Thu, Dec 22, 2016 at 10:38 AM, Imants Cekusins <[hidden email]> wrote:
> what is the "default" structure if you don't specify any

similar to:

display::Show a => a -> String
display = show

fa::(Applicative f, Num a) => f a
fa = pure 1

f and a are bounded by Applicative and Num, so to say. No default. Or, it is typed however type is a bit broader than *


> ​ In this case it defaults f to IO and a to Int, 

does it though?


Prelude> let a1 = pure 1
Prelude> let a2 = pure 1
Prelude> a1 == a2

<interactive>:20:1: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘a1’
      prevents the constraint ‘(Num a0)’ from being solved.


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: applicative default structure

Ovidiu Deac
In reply to this post by David McBride
So normally it should fail but the default typing in ghci is the one who makes our life easier.

Thanks for the explanation!

On Thu, Dec 22, 2016 at 5:33 PM, David McBride <[hidden email]> wrote:
Rather than bailing with an instance error for both Applicative and Num, which is the technically the right way, and the way that it used to be in the dark ages of ghci.  Instead it chooses types which are probably what you wanted.  In this case it defaults f to IO and a to Int, and then runs it.

You can read more about type defaulting in ghci here: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html

On Thu, Dec 22, 2016 at 10:18 AM, Ovidiu Deac <[hidden email]> wrote:
My understanding is that x can take any form required for type-inference. That's fine but what is the "default" structure if you don't specify any?

On Thu, Dec 22, 2016 at 5:13 PM, Imants Cekusins <[hidden email]> wrote:
What is f here?

anything Applicative:

Prelude> let a1 = pure 1
Prelude> let a2 = pure 1

Prelude> (a1::Maybe Int) == a2
True
Prelude> (a1::Maybe Float) == a2
True
Prelude> (a1::Either String Float) == a2
True



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners