JacquesCarette / Drasil

Generate all the things (focusing on research software)
https://jacquescarette.github.io/Drasil
BSD 2-Clause "Simplified" License
142 stars 26 forks source link

Code generation for function definitions doesn't work #3325

Open balacij opened 1 year ago

balacij commented 1 year ago

While trying to build my Beam Bending project for CAS 741, I came across what @JacquesCarette previously mentioned: generating code for defined functions is largely broken. It looks like there is a clash somewhere in how 'functions' are understood, and it results in odd translation.

For example, I was trying to build a function y_B (x)=e_B [note that it ignores the x variable], but the resultant code is largely confusing (ignoring the existing typing issues). The generated code is publicly available. For example,

C++ has declarations missing type signatures: https://github.com/balacij/Drasil/blob/e0c230521b3b2f2f182799711c6e762df94c3b8e/code/stable/bmbnd/src/cpp/Calculations.hpp#L8-L12

C# adds extras: https://github.com/balacij/Drasil/blob/e0c230521b3b2f2f182799711c6e762df94c3b8e/code/stable/bmbnd/src/csharp/Calculations.cs#L7-L13

Java is similar to C++ in that it doesn't have a well-defined type signature (missing return type): https://github.com/balacij/Drasil/blob/e0c230521b3b2f2f182799711c6e762df94c3b8e/code/stable/bmbnd/src/java/BmBnd/Calculations.java#L9-L15

Swift code tries to generate curried higher-order functions: https://github.com/balacij/Drasil/blob/e0c230521b3b2f2f182799711c6e762df94c3b8e/code/stable/bmbnd/src/swift/Calculations.swift#L5-L11

Additionally, Drasil currently complains when generate Python code with function definitions: image

JacquesCarette commented 1 year ago

Can you also show how you tried to build that function? Some of these things work, as it is used in GlassBR.

The issue with Python is likely to be different.

balacij commented 1 year ago

Yes, that would be good to have :sweat_smile:

I tried to build that function by defining y_b has an equational model: https://github.com/balacij/Drasil/blob/e0c230521b3b2f2f182799711c6e762df94c3b8e/code/drasil-example/bmbnd/lib/Drasil/BmBnd/InstanceModels.hs#L8-L26 . Afterwards, when I was trying to build it manually, similar to GlassBR, I came across #3321.

GlassBR does use function-typed things in code generation:

https://github.com/JacquesCarette/Drasil/blob/b73f49ff4a144322d1b1840d43a45b4ad64a074e/code/drasil-example/glassbr/lib/Drasil/GlassBR/Unitals.hs#L256-L257

But it is mostly done manually: https://github.com/JacquesCarette/Drasil/blob/b73f49ff4a144322d1b1840d43a45b4ad64a074e/code/drasil-example/glassbr/lib/Drasil/GlassBR/ModuleDefs.hs#L164-L219

JacquesCarette commented 1 year ago

This seems like a bunch of different, non-trivial bugs. Do you want to have a stab at them, or do you want me to look first?

balacij commented 1 year ago

I'll try to have a look at it as soon as I can, but I want to focus a bit of energy on #2873 first.