Pattern comparison in fromJust definition

The function fromJustin Data.Maybeis defined as follows:

fromJust          :: Maybe a -> a
fromJust Nothing  = error "Maybe.fromJust: Nothing"
fromJust (Just x) = x

According to my understanding of pattern matching (matching happens from top to bottom) I would change the order of the two definitions. Since usually the Nothing-part does not match in the it-is-sure-a-Just situation, but is always checked before the second definition is reached.

Can you clarify my mistake in reasoning? Thank.

Edit:

Example: Suppose I have a file with a million type numbers Intper line, and I need these numbers (like Int, not String) in my program for other things.

import qualified Data.ByteString.Lazy.Char8 as L

readInt = fst . fromJust . L.readInt
-- more stuff

With the above definition, fromJustit will take me more time to read the numbers, right?

+5
3

, . , Haskell ADT C switch.

, ADT "", , . , , Nothing 0 (null) Just 42, 1 (pointer to 42).

:

squash :: Maybe (Maybe Int) -> Int
squash (Just Nothing) = 0
squash Nothing = 0
squash (Just (Just x)) = x

:

squash x = 
   check tag of x:
       0 -> 0
       1 y -> check tag of y:
           0 -> 0
           1 z -> z

- , 0 1. , , .

, , ( , ). , fromJust :

fromJust x
    | isNothing x = error "fromJust: Nothing"
    | isJust x = case x of { Just y -> y }

, , , , . , , , - : -).

+9

: -, Haskell , Nothing, Just x. . -, GHC , , (details).

GHC . fromJust, :

        andq $7,%rax
        cmpq $2,%rax
        jae .LcO4

, , - (7 = 111 ). 2, , Just x. . , Nothing.

, , , Nothing. , . , GHC .

+6

, Nothing, Just. , .

If you go Just 5through fromJust, it Just 5does not match Nothing. But Just 5consistent Just x.

It will work the same way anyway. Things start to get sticky when the function doesn't match the pattern in one of the arguments.

0
source

All Articles