Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the identity function sometimes change whether patterns are exhaustive?

Consider this GHCi session:

$ ghci
GHCi, version 8.6.5: http://www.haskell.org/ghc/  :? for help
Prelude> :set -Wincomplete-patterns -Wincomplete-uni-patterns
Prelude> foo t | (_, _) <- t = "Foo"
Prelude> bar t | (_, _) <- id t = "Foo"
Prelude> baz x | Just _ <- x = "Yes" | Nothing <- x = "No"
Prelude> qux x | Just _ <- id x = "Yes" | Nothing <- id x = "No"

<interactive>:3:1: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘qux’: Patterns not matched: _
Prelude>

Why is qux considered incomplete by GHC? Do we really know more about id x than we do about x? And why isn't bar also considered incomplete for the same reason that qux is?

Or for a possibly clearer example:

foo f x
  | Just _ <- f x = "Yes"
  | Nothing <- f x = "No"

bar f x = case f x of
  Just _ -> "Yes"
  Nothing -> "No"

As far as I know, those are exactly equivalent, but the former produces the warning and the latter does not.

like image 381
Joseph Sible-Reinstate Monica Avatar asked Dec 01 '19 17:12

Joseph Sible-Reinstate Monica


People also ask

How does culture impact on identity?

Culture is a defining feature of a person's identity, contributing to how they see themselves and the groups with which they identify. A person's understanding of their own and other's identities develops from birth and is shaped by the values and attitudes prevalent at home and in the surrounding community.

In what ways does globalization challenge identity?

Globalisation impacts on our identities by enabling us to experience a wider range of material cultures (such as food and music). How we make sense of these global cultural flows will modify on our sense of who we are. Our identities are complex and forever changing.

What determines identity diffusion?

Identity diffusion occurs when an individual hasn't committed to an identity and isn't working to form one. Many people experience, and eventually grow out of, a period of identity diffusion in childhood or early adolescence. However, long-term identity diffusion is possible.

What are the 4 types of identity status?

Marcia (1966) based his theory of adolescent identity development on Erikson's (1950/1980) theory of psychosocial identity development and identified four identity statuses: identity diffusion, identity foreclosure, identity moratorium, and identity achievement.


Video Answer


1 Answers

It looks like GHC doesn't consider identical expressions to be the same value in a pattern guard unless the expression is a single binding:

Prelude> f | Just x <- Just 1 = "foo" | Nothing <- Just 1 = "bar"

<interactive>:5:1: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘f’: Guards do not cover entire pattern space

This is even more silly, in that it's clear the patterns are exhaustive. It can't even branch.

I'd say that this is probably the right way to do things, even if it's weird. It encourages binding the expression to a name, which is helpful for ensuring the expression is only evaluated once. It's certainly a roundabout way of doing it, though.

As for why you're not getting that error in bar, it's because there's only one constructor for pairs. You're matching every possible output of id t, even if it's a computed expression. There's no alternative constructor you haven't matched.

like image 175
Carl Avatar answered Oct 12 '22 04:10

Carl