[External] : Re: Question about circular references

Ron Pressler ron.pressler at oracle.com
Tue Jul 4 10:39:10 UTC 2023



> On 4 Jul 2023, at 02:42, David Alayachew <davidalayachew at gmail.com> wrote:
> 
> 
> Apologies, I was not clear here. My question to you is actually, do you see a problem with the fact that records cannot represent a circular reference to themselves?

No, because records are meant to represent tuples, and tuples don’t self-reference.

> 
> According to the definitions I cited earlier, tuples/product types have circular references in their domain of possible values

They don’t. Let’s say that the product type T is defined as T := Integer x T. Then a tuple of type T can self-reference if and only if T (the second component) can self-reference. It’s perfectly consistent, therefore, for tuples to not self reference, and Java, like most languages with algebraic data types, says tuples don’t self-reference. You can’t construct a self-referencing instance of T because you’d need a component value that is the instance you’re constructing. It also ensures that recursions over values always terminate.

> 
> Are you saying you can accomplish this without indirection, as in without a Map lookup or something equivalent? If so, please demonstrate.

A set of pairs *is* a map (actually, a “multi map", i.e. a map from elements to sets). You could implement a map from just tuples (as students learn in an introductory FP course: you represent the map as a list, and the list is a linked list of item-tail pairs, i.e. a cons list) but you’re better off just using an existing map implementation.

> 
> I understand your point, but this requires me to apply an ill fitting model because the language is unable to map the one I actually need.

But why did you decide that you need to do it *with records*? It’s not that the language isn’t able to represent the graph you want, but that you picked a language feature that is meant to represent tuples and you’re trying to use it to represent something that isn’t a tuple.

Maybe you did it because you want to use pattern-matching and records have pattern-matching, but we would like to offer pattern-matching for other classes, too.

— Ron



More information about the amber-dev mailing list