Utilize the embedded literal value information in `CodeType`
The initial goal was to optimize the GOOL by enhancing support for literal values and enabling more efficient operations, such as smartAdd and smartSub across different literal types (e.g., integers, floats, doubles, strings). https://github.com/JacquesCarette/Drasil/blob/8490ac28a5efc8fa6a0ce2c249444611d3295110/code/drasil-gool/lib/Drasil/GOOL/LanguageRenderer/LanguagePolymorphic.hs#L132-L148
-
Initial Approach Initially, we attempted to introduce a new data type
LitValueand replacedvalInt :: Maybe IntegerwithlitVal :: LitValue. The purpose ofLitValuewas to capture a wider range of literal values. However, this approach proved to be too generic, resulting in a loss of type information. Details of this attempt are documented in #3945. -
Current Approach The current approach focuses on modifying the
CodeTypedata structure to include an additional field to hold optional literal values (Maybe a). By adding this parameter,CodeTypecan store literals likeMaybe Int, Maybe Double, or Maybe String, specific to each type.The updated definition for
CodeTypeis as follows:data CodeType = Boolean (Maybe Bool) | Integer (Maybe Int) | Float (Maybe Float) | Double (Maybe Double) | Char (Maybe Char) | String (Maybe String) ...Since
cType :: CodeTypeis a field withinTypeData, this modification allowsTypeDatato carry literal value information as part of the type itself. This way, we can leverage the embedded literal values inCodeTypefor optimization in operations likesmartAddandsmartSub. -
Changes to
valFromDataWith the newCodeTypestructure, the signature ofvalFromDatawas modified to include an additionalMaybe aparameter that represents the literal value:valFromData :: Maybe Int -> Maybe a -> VSType r -> Doc -> SValue r- The main challenge was to properly insert the
Maybe avalue intoCodeType, which already contains optional literal values for specific types. After that, the updatedCodeType needed to be propagated into theTypeDataandValDatastructures effectively. This is where I am currently stuck.
- The main challenge was to properly insert the
Currently, functions like valueOf and inlineIf rely on valFromData by passing Nothing for the literal value parameter, as they don’t require this information to function correctly. This ensures compatibility with other parts of the code, such as listSlice.
Most of the time, the literal value information is not necessary except for certain optimizations like smartAdd and smartSub. Therefore, it’s important to consider if there are more straightforward ways to utilize these literal values effectively. For example, are there other operations or optimizations that could benefit from accessing literal values in CodeType?
I'm wondering if the problem has now moved to valFromData, i.e. it is general to too many types. I think maybe there should be different functions so that the new argument should be Maybe Bool for valFromBoolData, Maybe Int for valFromIntData, etc.
But maybe I misunderstand exactly where you are stuck in the propagation?
As to your last question: the main way for us to know how to use these more effectively is to look at Drasil-generated code and see if there are pieces of code that should have been simpler, i.e. where 'static computations' are present in the generated code.