..在哪里定义? [英] Where is .. defined?
问题描述
我只是想知道 ..
是否实际上在任何地方的Haskell代码中定义,例如在
要对问题进行您显示的定义是默认定义",在 Enum
类型类中.它在您链接的前奏曲中说得很对:
class枚举a其中succ,pred :: a->一种toEnum :: Int->一种fromEnum :: a->诠释enumFrom :: a->[一个..]enumFromThen :: a->a->[a]-[n,n'..]enumFromTo :: a->a->[a]-[n..m]enumFromThenTo :: a->a->a->[a]-[n,n'.. m]-最小完整定义:-toEnum,来自Enum-_______-____-注意:这些默认方法仅对类型有意义-****注意-使用fromEnum射影映射到Int-和toEnum.succ = toEnum.(+1).来自枚举pred = toEnum.(减去1).来自枚举enumFrom x =映射到Enum [fromEnum x ..]enumFromTo x y =映射到Enum [fromEnum x .. fromEnum y]enumFromThen x y =映射到Enum [fromEnum x,fromEnum y ..]enumFromThenTo x y z =映射到Enum [fromEnum x,fromEnum y .. fromEnum z]
因此,中 Enum
中的每个 a
类型都必须至少提供 toEnum :: Int->的定义.a
和 fromEnum :: a->Int
.如果它没有提供自己的定义,例如 enumFromTo :: a->a->[a]
,则将使用其默认定义:
enumFromTo :: a->a->[一种]enumFromTo x y =映射到Enum [fromEnum x .. fromEnum y]-└a┘└a┘└a┘└a┘-└──Int────┘└──Int────┘-└Int->a┘└─────────[Int]─────────-└──────────────[a]────────────────┘
对于 Int
类型本身,库中的某个地方定义了 specific 定义,因此未使用 enumFromTo
的默认定义.表示 Int
,因此没有恶性循环.
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
It's a part of the syntax, specified in the Report, section 3.10.
Yes it gets translated into code using enumFrom
etc. functions:
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.
这篇关于..在哪里定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!