Questions tagged [tying-the-knot]

Tying the knot is a technique in which you can create circular data structures in the absence of mutation by referencing a yet to be produced value.

27 questions
46
votes
2 answers

Explanation of “tying the knot”

In reading Haskell-related stuff I sometimes come across the expression “tying the knot”, I think I understand what it does, but not how. So, are there any good, basic, and simple to understand explanations of this concept?
Magnus
  • 4,444
  • 1
  • 30
  • 47
40
votes
5 answers

Tying the Knot with a State monad

I'm working on a Haskell project that involves tying a big knot: I'm parsing a serialized representation of a graph, where each node is at some offset into the file, and may reference another node by its offset. So I need to build up a map from…
mergeconflict
  • 7,861
  • 31
  • 63
19
votes
2 answers

Using Cont to acquire values from the future and the past

I'm writing a brainfuck interpreter in Haskell, and I came up with what I believe to be a very interesting description of a program: data Program m = Instruction (m ()) (Program m) | Control (m (Program m)) |…
Dan Burton
  • 51,332
  • 25
  • 109
  • 190
16
votes
4 answers

Lazily Tying the Knot for 1 Dimensional Dynamic Programming

Several years ago I took an algorithms course where we were giving the following problem (or one like it): There is a building of n floors with an elevator that can only go up 2 floors at a time and down 3 floors at a time. Using dynamic…
16
votes
1 answer

Bug in Data.Map implementation?

I've stumbled upon something that I'm guessing is a bug in Data.Map, but which is also quite possibly a bug in my Haskell knowledge. Hoping somebody can clarify which it is :) Please reference this gist. I'm serializing a circular linked list…
mergeconflict
  • 7,861
  • 31
  • 63
11
votes
1 answer

Any methods for recovering enough laziness to tie the knot in a monad?

I want to write a slick bit of code (saving me much time to implement otherwise) by tying the knot. It goes roughly like this, n <- myinstr n x where in theory, myinstr should run x to get a value, which will become n. myinstr, which runs inside a…
gatoatigrado
  • 16,008
  • 13
  • 77
  • 136
11
votes
1 answer

Debugging unwanted strictness?

I have a problem that I don't know how to reason about. I was just about to ask if somebody could help me with the specific problem, but it dawned on me that I could ask a more general question and hopefully get a better general understanding as a…
mergeconflict
  • 7,861
  • 31
  • 63
11
votes
1 answer

letrec in Scala? (Immutable way to "Tie the knot?")

Suppose I have a stupid little case class like so: case class Foo(name: String, other: Foo) How can I define a and b immutably such that a.other is b, and b.other is a? Does scala provide some way to "tie the knot"? I'd like to do something like…
Dan Burton
  • 51,332
  • 25
  • 109
  • 190
10
votes
1 answer

Tying the knot on mutually recursive ADTs with well-typed error handling

(Note: this post is a literate-haskell file. You can copy-paste it into a text buffer, save it as someFile.lhs, and then run it using ghc.) Problem description: I want ot create a graph with two different node types that reference each other. The…
Aleksandar Dimitrov
  • 8,790
  • 3
  • 39
  • 46
8
votes
1 answer

Is there any way to convenient way to express graphs using the tying-the-knot strategy?

As explained on my previous question, it is impossible to differ two graphs made using the tying the knot strategy if you don't have some kind of unique label on your nodes. Using a two-edged graph as an example: data Node = Node Int Node…
MaiaVictor
  • 45,122
  • 42
  • 127
  • 254
8
votes
3 answers

Why doesn't `iterate` from the Prelude tie the knot?

Why isn't iterate defined like iterate :: (a -> a) -> a -> [a] iterate f x = xs where xs = x : map f xs in the Prelude?
user3237465
  • 12,116
  • 2
  • 15
  • 33
7
votes
1 answer

Patching a recursively-defined list without a <>

Context We all know the recursively-defined Fibonacci sequence: fibs = 1 : 1 : zipWith (+) fibs (tail fibs) λ> fibs [1,1,2,3,5,9,13,21,34,55,89... Question I'm trying to “patch” it in a few places, so that: the general recursive equation…
JB.
  • 34,745
  • 10
  • 79
  • 105
7
votes
1 answer

Equational reasoning with tying the knot

I'm trying to wrap my head around Cont and callCC, by reducing this function: s0 = (flip runContT) return $ do (k, n) <- callCC $ \k -> let f x = k (f, x) in return (f, 0) lift $ print n if n < 3 …
Ryba
  • 509
  • 2
  • 13
5
votes
2 answers

How can I avoid <> in Haskell?

The program below results in <> in GHC. ...Obviously. In hindsight. It happens because walk is computing a fixed point, but there are multiple possible fixed points. When the list comprehension reaches the end of the graph-walk, it "asks" for…
Jason Orendorff
  • 37,255
  • 3
  • 56
  • 91
5
votes
2 answers

How do you build an infinite grid like data structure in Haskell?

I am trying to form an infinite grid like data structure by tying the knot. This is my approach: import Control.Lens data Grid a = Grid {_val :: a, _left :: Grid a, _right :: Grid a, _down…
1
2