Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is .. defined?

I'm just wondering whether .. is actually defined in Haskell code anywhere, like in the Prelude? Is enumFromTo the same thing? I don't get which is the definition?

enumFromTo x y  =  map toEnum [fromEnum x .. fromEnum y]

[ e1 .. e3 ]  =  enumFromTo e1 e3
like image 861
js2010 Avatar asked Sep 01 '20 20:09

js2010


People also ask

How it is defined?

: the state of the situation. And that's how it is.

Where's the definition of define?

Definition of define transitive verb. 1a : to determine or identify the essential qualities or meaning of whatever defines us as human. b : to discover and set forth the meaning of (something, such as a word) how the dictionary defines "grotesque"

What is a example of where?

The definition of where is the place in, what or which place. An example of where is someone saying that he lives close to the house of his mother; He lives next door to where his mother lives. From what place or source? Where did you get your information?


1 Answers

It's a part of the syntax, specified in the Report, section 3.10.

Yes it gets translated into code using enumFrom etc. functions:

enter image description here

To your edit of the question: the definitions you show are "default definitions" in the Enum typeclass. It says so right there in the Prelude you linked:

class  Enum a  where
    succ, pred       :: a -> a
    toEnum           :: Int -> a
    fromEnum         :: a -> Int
    enumFrom         :: a -> [a]             -- [n..]
    enumFromThen     :: a -> a -> [a]        -- [n,n'..]
    enumFromTo       :: a -> a -> [a]        -- [n..m]
    enumFromThenTo   :: a -> a -> a -> [a]   -- [n,n'..m]

        -- Minimal complete definition:
        --      toEnum, fromEnum
--             _______                                      -- ____
-- NOTE: these default methods only make sense for types    -- **** NB
--   that map injectively into Int using fromEnum
--  and toEnum.
    succ             =  toEnum . (+1) . fromEnum
    pred             =  toEnum . (subtract 1) . fromEnum
    enumFrom x       =  map toEnum [fromEnum x ..]
    enumFromTo x y   =  map toEnum [fromEnum x .. fromEnum y]
    enumFromThen x y =  map toEnum [fromEnum x, fromEnum y ..]
    enumFromThenTo x y z = 
                        map toEnum [fromEnum x, fromEnum y .. fromEnum z]

So each type a that is in Enum must provide the definitions for at least toEnum :: Int -> a and fromEnum :: a -> Int. If it doesn't provide its own definition for e.g. enumFromTo :: a -> a -> [a] then its default definition will be used:

enumFromTo :: a -> a -> [a]
enumFromTo x  y   =  map  toEnum  [fromEnum x .. fromEnum y ]
--        └a┘└a┘                           └a┘           └a┘
--                                 └──Int────┘   └──Int────┘
--                       └Int->a┘ └─────────[Int]───────────┘
--                   └───────────────[a]────────────────────┘

And for the Int type itself there is a specific definition defined somewhere in the library, so the default definition of enumFromTo is not used for Int, thus there is no vicious circle.

like image 82
Will Ness Avatar answered Sep 23 '22 17:09

Will Ness