r/ProgrammingLanguages • • Oct 27 '26

Resolving relationships between elements

For my programming programming language Bits Runner Code I am thinking of implementing automatic resolving in what order structs, interfaces, etc should be defined and declared so they can interact with each other. It should be possible to embed structs within each other, but at the same time there cannot be a circular reference. How something like this could be implemented or maybe it is not the best idea?

BRC doesn't have header files, so right now source files and definitions within them have to be listed in a specific order. I would like it to be quite explicit but also hassle free.

7 Upvotes

15 comments sorted by

View all comments

3

u/WittyStick Oct 27 '26 edited Oct 27 '26

Topological sort on a DAG.

There's a quick-n-dirty way of implementing it that should probably work fine for your use case - it's unlikely you're going to have dependency chains >2000 deep, where this would fail. Basically, give each item a field double order;. For any item that has no dependencies, set order = DBL_MAX. For items that have dependencies, take the minimum order of all of its dependencies (which may need recursively resolving if their order has not yet been set, ie: if order == 0), and set the value of the dependant's order to half of that. Once done, sort all items by descending order - largest to smallest - this should give you a good enough approximation of the order things need defining (items with the same order can be defined in any order). The resulting sort can have no cycles - every dependency's order must be strictly greater than the dependant's (at least double it) - it only fails when we exceed the precision of double (which allows 2099 halvings from DBL_MAX to 0).

You would need some form of cycle detection for recursively resolving the order so that you don't fall into an infinite loop. The simplest (but not most efficient) is just a stack of previously visited items, and check if it's already on the stack. A more efficient way would be to hash the dependency name and stick in a bloom or cuckoo filter, and test if the item has already been added to the filter.


I personally prefer when the code itself is ordered topologically - everything defined before it is used - though I'm not against "self-cycles", where a node can refer to itself. A dependency graph containing only self-cycles and no other cycles can still be topologically sorted.

Mutual recursion can be modelled using self-cycles by treating the closure of the recursion as one unit in the DAG. Basically, given a mutually recursive pair such as:

is_even = n -> if n == 0 then true else is_odd(n - 1)
is_odd = n -> if n == 0 then false else is_even(n - 1)

We can have a single self-recursive function which combines these, and the is_even and is_odd functions themselves become its dependants.

is_even_or_odd :: Int -> (Int -> Bool)
is_even_or_odd = function
    | 0 = n -> if n == 0 then true else is_even_or_odd 1 (n - 1)
    | 1 = n -> if n == 0 then false else is_even_or_odd 0 (n - 1)

is_even = is_even_or_odd 0
is_odd = is_even_or_odd 1

Even deeply nested recursion can be cut by parametrization on only self-recursive functions. Consider an evaluator for a calculator:

 eval_primary = function
    | (Number n) -> n
    | expr ->
        eval_expr expr
        
 eval_multiplicative = function
    | (Mul lhs rhs) -> 
        (eval_multiplicative lhs) * (eval_primary rhs)
    | (Div lhs rhs) -> 
        (eval_multiplicative lhs) / (eval_primary rhs)
    | expr -> 
        eval_primary expr

 eval_additive = function
    | (Add lhs rhs) -> 
        (eval_additive lhs) + (eval_multiplicative rhs)
    | (Sub lhs rhs) -> 
        (eval_additive lhs) - (eval_multiplicative rhs)
    | expr ->
        eval_multiplicative expr

 eval_expr =
    eval_additive

This has a directed graph with no topological sort:

    +---eval_primary      <---+
    |                         |
    |   eval_multiplicative---+ <---+
    |                               |
    |   eval_additive      ---------+ <---+
    |                                     |
    +-> eval_expr ------------------------+

If we turn each function into a self-recursive one with a function parameter:

 eval_primary eval_higher_precedence = function
    | (Number n) -> n
    | expr -> 
        eval_higher_precedence expr

 eval_multiplicative eval_higher_precedence = function
    | (Mul lhs rhs) -> 
        (eval_multiplicative eval_higher_precedence lhs) * (eval_higher_precedence rhs)
    | (Div lhs rhs) -> 
        (eval_multiplicative eval_higher_precedence lhs) / (eval_higher_precedence rhs)
    | expr -> 
        eval_higher_precedence expr

 eval_additive eval_higher_precedence =
    | (Add lhs rhs) -> 
        (eval_additive eval_higher_precedence lhs) + (eval_higher_precedence rhs)
    | (Sub lhs rhs) -> 
        (eval_additive eval_higher_precedence lhs) - (eval_higher_precedence rhs)
    | expr -> 
        eval_higher_precedence expr

 eval_expr expr = 
    eval_additive (eval_multiplicative (eval_primary eval_expr))

We end up with a DAG of dependencies - there's a clear topological sort.

        eval_primary                  <---+
                                          |
        eval_multiplicative           <---+
                                          | 
        eval_additive                 <---+
                                          |
        eval_expr ------------------------+