Haskell: Processing Blocked Self-Reference Lists

Is there any useful reason why the GHC allows you to block forever:

list = 1 : tail list

It seems that the list iterator / generator is a bit more complicated, we should do something more useful:

  • Return error "Infinitely blocking list"
  • Return [1,1]

Explanation 2: it is possible that when entering the generator to get the element, Nwe could make all our own links inside the generator limited to the list, but ending in N-1(we notice read Ninside scope generate Nand return the end of the list). This is a kind of simple deadlock detection using areas.

Clearly, this is not so useful for the toy example above, but it may allow the use of more useful / elegant final definitions of a self-referential list, for example:

primes = filter (\x -> none ((==0).mod x) primes) [2..]

Note that either the change should only affect list generators, which currently lead to an infinite block, so they seem to be backward compatible language changes.

Ignoring the complexity of the GHC needed to make such a change for a moment, will it violate any existing language behavior that I am missing? Any other thoughts on the “elegance” of this change?

Also see another example of BFS, which may be useful below. For me, this seems more functional / elegant than some other solutions, since I only need to determine what bfsList is and not how to generate it (specifying a termination condition):

bfs :: (a -> Bool) -> (a -> [a]) -> [a] -> Maybe a
bfs predf expandf xs = find predf bfsList
    where bfsList = xs ++ concatMap expandf bfsList
+3
source share
2 answers

, list = 1 : ⊥.

-, . Haskell "", inolving & bot; ( "bottom" ) , .

  • , 1 : ⊥
  • 1 : ⊥ , 1 : 2 : 3 : []

,

  • 1 : ⊥ , 2 : 3 : ⊥, .

. 1 : ⊥ , , 1. Haskell.

.

list = 1 : tail list

, " ". :

list = ((1 :) . tail) list

, , list

list = f list

f = (1 :) . tail. Haskell .

. ⊥, , . , ( , - ).

⊥,

f ⊥ = ((1 :) . tail) ⊥ = 1 : tail ⊥

, ⊥ , ⊥ . , :

f (1 : tail ⊥) = ((1 :) . tail) (1 : tail ⊥)
               = 1 : tail (1 : tail ⊥)
               = 1 : tail ⊥

, , , .

, . [1,1] = 1:1:[] , :

f (1:1:[]) = ((1 :) . tail) (1:1:[]) 
           = 1 : tail (1:1:[])
           = 1:1:[]

, , , 1, , , . , 1:⊥ , , , , , .

+5

, list GHCi, , GHC, . :

list = 1 : tail list
main = print list

:

Loop: <<loop>>

primes.

, GHC . , , , , Haskell .

, ( "" ) [1,1], , :

list = 1 : tail list

. , "" ( "" _|_), , head [1,2,3] 1.

(, , list 1 : _|_, " ". , @Justin Li . , .)

, , , " ", Haskell ( , ) , , .

, , . list [1]? , "", n = 1 ( , ) tail list, list, n-1 = 0, [1] , [], , , ?

list = 1 : tail list
     = 1 : tail [1]   -- use list-so-far
     = 1 : []
     = [1]

()

list () , Haskell ( . ).

, tail, :

tail l = case l of _:xs -> xs
                   [] -> error "ack, you dummy!"

"" list Haskell:

-- evaluating `list` using definition of `list`
list = 1 : tail list

-- evaluating `tail list` using definition of `tail`
list = 1 : case list of _:xs -> xs
                        ...
-- evaluating case construct requires matching `list` to
-- a pattern, this requires evaluation of `list` using its defn
list = 1 : case (1 : tail list) of _:xs -> xs
                                   ...
-- case pattern match succeeds
list = 1 : let xs = tail list in xs    -- just to be clear
     = 1 : tail list

-- awesome, now all we need to do is evaluate:
list = 1 : tail list
-- ummm, Houston, we have a problem

- , " ".

. Haskell, Haskell. - , @luqui. , , " ", Haskell, , .

+2

Source: https://habr.com/ru/post/1686473/


All Articles