In Haskell, some lists are cyclic:
ones = 1 : ones
Others are not:
nums = [1..]
And then there are things like this:
more_ones = f 1 where f x = x : f x
This denotes the same value as ones
, and certainly that value is a repeating sequence. But whether it's represented in memory as a cyclic data structure is doubtful. (An implementation could do so, but this answer explains that "it's unlikely that this will happen in practice".)
Suppose we take a Haskell implementation and hack into it a built-in function isCycle :: [a] -> Bool
that examines the structure of the in-memory representation of the argument. It returns True
if the list is physically cyclic and False
if the argument is of finite length. Otherwise, it will fail to terminate. (I imagine "hacking it in" because it's impossible to write that function in Haskell.)
Would the existence of this function break any interesting properties of the language?
f x = if isCycle x then 1 else 2
. This would return1
or2
for the valueones
depending on how you build that same list. This seems pretty big a breakage considering that one of the most obvious advantages of functional languages is that a function will always return the same result if given the same values in input... – SyversonIO
. You can writeisCycle :: [a] -> IO Bool
in terms of an explicit structure of the graph of a list obtained from data-reify which internally usesStableName
s. If you look too hard at memory withIO
you can do absolutely evil things, like break the purity of pure functions without resorting to anythingunsafe
. – RudeTrue
if the list is physically cyclic andFalse
if the argument is of finite length. Otherwise it will fail to terminate." So the functionf
you proposed certainly would not return2
for the valueones
. – Holism