Skip to content

Commit a98630b

Browse files
xperiandriclaude
andcommitted
feat(quotations): tell the values of a quotation from its shape
Two runs of one query differ only in the values the quotation was written with, so a query has to be translated once, and a later run only has to find that translation and supply the values. The parameterizer tells the two apart for a quotation in normal form: - the values that become parameters, each at a numbered place, a slot; - the key, ShapeKey: every node of the quotation without those values, in a form that can be compared and hashed. A quotation cannot be its own key: it compares its variables by identity, and every evaluation of a quotation literal creates new ones; - the template, for a query that has no translation yet: the quotation with a variable at the place of every slot. The rules: - a literal is a part of the key: a boolean, a character, a string, an integer or floating-point number, a value of an enumeration, unit and null. The ADR made literals parameters and wrote a value declared with Literal into the text, but the compiler writes such a value into a quotation exactly as it writes 18, so the two cannot be told apart. A decimal is no literal here: the compiler writes 18m as a call that makes the decimal, so it is computed and is a slot, which keeps its digits; - a captured value is a slot named after its variable; - a part that reads nothing of the document, the largest one at its place, is computed by the evaluator and is a slot without a name; - a tuple, record, union case or array that the quotation builds stays what it is, because the translator has to see that Some limit is an option with a value and that a list is written item by item; - Query.constant and Query.parameter, two new public functions, say the opposite for their argument. The uses of one captured variable share a slot: same name, same type, same value. A value bound by let is written at every place it is used by the normalizer and is one node there, so it is computed once. A choice whose condition is known before the query runs computes only the parts of the branch it takes, as compiled code does: in filter.IsNone || p.Name = filter.Value.Trim () nothing is trimmed without a filter. The parts of the other branch are slots without a value, and the key does not depend on which branch is taken. A condition under Query.constant decides its branch the same way. A function value that would have to be a parameter or a constant is reported as CapturedFunctionValue, also when it is passed as an object and the type of the part does not say what it is. An error about a part that has no place in the source of its own, as a captured value has none, gets the place of the nearest node around it. The template is built only when it is asked for: building a node of a quotation costs microseconds, and a quotation whose translation is known needs no node built. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2322f3d commit a98630b

5 files changed

Lines changed: 978 additions & 4 deletions

File tree

‎.github/copilot-instructions.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
├── src/Cosmos.Quotations/ – translator of F# quotations into queries (FSharp.Azure.Cosmos.Quotations), built stage by stage and not packed yet
1616
│ ├── TranslationError.fs – translation errors and the place of a quotation in the source
1717
│ ├── Normalizer.fs – normal form of a quotation
18-
│ └── Evaluator.fs – value of a part of a quotation that reads no variable
18+
│ ├── Evaluator.fs – value of a part of a quotation that reads no variable
19+
│ ├── Query.fs – functions that say whether a value is a constant or a parameter of the query
20+
│ └── Parameterizer.fs – shape of a quotation, by which its translation is found again, and the values of its parameters
1921
├── src/Cosmos/ – main library (FSharp.Azure.Cosmos)
2022
│ ├── SubStatusCodes.fs – documented Cosmos DB sub-status codes
2123
│ ├── Cosmos.fs – core types and container extensions

‎src/Cosmos.Quotations/FSharp.Azure.Cosmos.Quotations.fsproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
<Compile Include="TranslationError.fs" />
2525
<Compile Include="Normalizer.fs" />
2626
<Compile Include="Evaluator.fs" />
27+
<Compile Include="Query.fs" />
28+
<Compile Include="Parameterizer.fs" />
2729
</ItemGroup>
2830

2931
<ItemGroup>

0 commit comments

Comments
 (0)