Type Conversion With Concepts

Viewed 20

I am implementing a compiler framework that allows for custom casts.

A concept in this situation is a set of types. For example, the Integer concept is the set of all signed and unsigned whole numbers, where as the Boolean concept consists of a single boolean type. It is known whether or not any given type is a member of a concept.

A custom cast is defined by an input concept and an output concept, meaning that any type that fits the input concept will be transformed to be a member of the output concept. Ex: I can define a cast that converts any Integer to a Boolean. I have it set up this way, as I don't have to define casts for every single member of Integer and rather just one.

In addition to these rules, casts can be chained. For example, int& can be converted to bool by first converting it to int and then to bool. However, the shortest "distance" is preferred. Meaning that if a cast is able to convert int& to bool directly, than it will be chosen (if multiple paths have this shortest distance than an error will be thrown, so this case shouldn't be considered).

The problem is that given a list of all possible casts and input type T and destination concept C, how do I find which casts to apply to T to get to C?

Set theory approach: Given an item T and list of transformations that allows a member of a given set to become a member of another one, how do I find the path of minimum transformations to get to set C?

My Ideas:

My first one was to create a graph with each unique concept as vertices and casts as edges and apply a path finding algorthm. The only problem is that I don't know how to define the start and goal nodes.

My second idea is to just brute force a path by going through each possible cast for each intermediate concept. While this would work, would there be a potential approach with dynamic programming?

0 Answers
Related