Replies: 10 comments 45 replies
-
Thank you for migrating this discussion @augb. Here is a summary of my thoughts on this topic. My viewpointI believe that Update: Below, @Mogball confirms that Mojo already supports the execution of arbitrary Mojo code at compile-time—with a caveat that the code cannot allocate memory yet. This is great to hear! Given this fact, the rest of my post should be understood as a proposal to have a syntax for mixing arbitrary pieces of run-time code and compile-time code within a function body. As I will demonstrate, such a syntax would make the Possible syntaxesThe goal is to have a syntax for indicating the time at which an arbitrary piece of code is executed. We will need to introduce a new keyword. I will use One of the syntaxes I have proposed is a
In this light, a
And maybe the
This brings us full-circle with the original topic of discussion: the Now, what about the
This is a (truncated) example from the Mojo docs. How could we describe the same computation using our
In my opinion, annotating both branches is better than just annotating the ConclusionMy core concern is not about the particular choice of keyword, it is about the model of compile-time execution that Mojo presents to its users. My assertion is that it would be conceptually simpler to offer a single set of constructs that can be used at both compile-time and run-time. Users shouldn't need to learn any additional concepts (like Addendum 1: Compile-time expressionsIn the previous discussion thread, @Janrupf proposed allowing individual expressions—including arbitrary function calls—to be annotated with If Mojo were to allow arbitrary functions to be executed at compile-time, they would likely need to obey certain restrictions, such as not being able to do I/O. Thankfully, this doesn't require any support from Mojo's type system: if an I/O call is made at compile time, it would be sufficient to just abort compilation. But this is a "stretch goal". It would be perfectly reasonable (as an MVP) to forbid the execution of arbitrary functions at compile-time, and only allow basic operations such as arithmetic. Update: I have been informed that Mojo already supports the execution of arbitrary functions. Nice! Addendum 2: Type aliasesCurrently, Mojo's |
Beta Was this translation helpful? Give feedback.
-
Allowed operationsMy hope for But as @nmsmith already said, that brings up the question of allowed operations. In case MLIR can fully be evaluated at compile time, that would allow for dynamically capturing data - something you usually need a complex build system for. For example, parsing some text format and translating it into binary, along with other similar tasks. Concrete (theoretical) example: from myprog.meta import CompiledMetadata, MetadataParser
from myprog.cmdline import parse_cmdline
fn parse_metadata(file: StringRef) -> CompiledMetadata:
with open(file) as f:
let parser = MetadataParser(f)
parser.validate()
return parser.compile()
comptime BUILTIN_METADATA = parse_metadata("resources/meta.dat")
let cmdline = parse_cmdline()
let metadata
if cmdline.data_override is not None:
metadata = parse_metadata(cmdline.data_override)
else:
metadata = BUILTIN_METADATA
# If we don't want to use the compiletime variable from above, this could also be written
# metadata = comptime(parse_metadata("resources/meta.dat")) This could even call into C libraries and so on and so on... Compiletime conditionsThe reason I personally prefer relying on the optimizer or guaranteeing that an A I would assume that an optimizer which can perform some static analysis and control flow tracking would also be capable of catching an Reasonably you could also expect that the following if statement is evaluated at compiletime (though no explicit guarantee is given): let a = 5
let b = 5
if a + b == 10:
print("Math works!")
else:
print("The compiler needs to go back to school!") If such an optimizer/analyzer feature is in place, |
Beta Was this translation helpful? Give feedback.
-
@nmsmith & @Janrupf, thank you for your ideas; I think y'all both make some interesting arguments for expanding things beyond the scope of this particular RFC. Would it make sense to continue that discussion in a separate thread? I ask since my purpose with this RFC is more about some (hopefully) relatively minor, tactical changes to bring clarity closer to the current design and implementation. I certainly see value in the broader discussion. |
Beta Was this translation helpful? Give feedback.
-
As a reminder to everyone, this RFC is about eight words:
While I am encouraged by the engaged response, substantive discussion about alternate approaches would be best served by creating a separate proposal. This RFC was inspired by @lattner's comment in the original issue, specifically:
I would be curious to hear from the Mojo team and others their thoughts on these eight words. |
Beta Was this translation helpful? Give feedback.
-
Thanks everyone for your contributions! The discussion has been quite helpful. Here is a summary of where this stands from my perspective:
Given that it may be a bit early to unify these into a single keyword, I wonder if there would still be value in renaming Given that an Given that I would be interested in y'all's thoughts and/or any corrections to my attempt at summarizing the state of this discussion thus far. |
Beta Was this translation helpful? Give feedback.
-
Also note that mojo has the less documented feature referred to as "result parameters" or "parameter results" for returning compile-time values: fn main():
fn f[a: Int, b: Int -> x: Int, y: Int]():
param_return[a * 2, b * 4]
alias a: Int
alias b: Int
f[2, 3 -> a, b]()
print(a, b)
main() The terminology here, and the |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I have decided as @augb requested to now split parts of this to #218, as it seems it may take way more discussion on the matter of how exactly this should work. To not block and interleave the discussion about |
Beta Was this translation helpful? Give feedback.
-
For what it's worth, the keyword |
Beta Was this translation helpful? Give feedback.
-
This is my current understanding of
If all of the above are true, that suggests the following:
Unless there is further discussion on this topic, I'll plan on closing this current discussion as outdated in the next day or so. Any substantive discussion on Updated: Italicized words were added to clarify original intent. |
Beta Was this translation helpful? Give feedback.
-
Picking up the discussion from #171 as suggested by @Mogball.
Beta Was this translation helpful? Give feedback.
All reactions