Skip to content

Proposal 0028: units on the shopping list and in recipe references - #166

Draft
dubadub wants to merge 1 commit into
mainfrom
proposal/0028-shopping-list-units
Draft

dubadub wants to merge 1 commit into
mainfrom
proposal/0028-shopping-list-units

Conversation

@dubadub

@dubadub dubadub commented Nov 14, 2026

Copy link
Copy Markdown
Member

An opt-in setup that makes a shopping list show one amount per ingredient, and a rule for matching a recipe reference's unit to the referenced recipe's yield. No recipe syntax changes.

Rendered proposal

Summary

  • A setup file. units.conf with [shopping] units = "metric" (or "us customary") asks for converged totals. Without it, list totals stay as they are today.
  • Same kind of unit. Cups and millilitres, grams and pounds are added through a unit table, in the base unit, and rounded up. 1 cup, 200 ml becomes 437 ml. The result does not depend on the order of the recipes.
  • Weight against volume. Added only when a density is known for the ingredient, into the side the ingredient is bought by. The total is marked ≈ and the amounts as written stay available. No density is ever assumed; without one the list keeps two amounts.
  • The recipe view is not touched. The rule is for totals across recipes: shopping lists, menus, and pantry subtraction.
  • Recipe references. A reference is matched to yield by kind: {1%l} against yield: 500%ml scales by 2, and against yield: [500%ml, 350%g] the entry of the same kind is used. This part needs no setup and converts nothing that is shown.

Motivation

cook shopping-list on two recipes that measure milk in cups and in millilitres prints milk 1 cup, 200 ml. It also prints 1 cup, 2 cups, because amounts are added only when the unit text is identical. In random five-recipe lists from a collection of about 16,000 recipes, three shared ingredients in four come out as two or more amounts: 37% are the same unit spelled differently, 33% are different units of the same kind, 7% are weight against volume.

Tools also disagree. CookCLI adds by unit text since cooklang/cookcli#433. cooklang-rs with its bundled unit table adds into the unit of whichever recipe comes first, so the total changes with recipe order.

The same text comparison decides whether a reference is scaled. A reference for 1%l of a recipe with yield: 500%ml is not scaled, and the list form of yield is not read at all by CookCLI 0.38.1.

Addresses discussion #142. Related: cooklang/cookcli#673, cooklang/cookcli#199.

Things worth a reviewer's attention

  • A unit table in the conventions. Proposal: add unit system metadata #148 leaves conversion tables to each application. This proposal writes down the volume and mass units, their spellings and their ratios, because two tools need them to give the same total. Rounding and the choice of unit for a total are fixed here too and could be left to applications.
  • us customary, not imperial. The setting takes the values of the unit system key from Proposal: add unit system metadata #148. The volumes in the table are US ones; British imperial units are an open question.
  • Case and lbs. A unit is looked up as written and then in lower case (Tbsp, Cup), except C and F. lbs is added.
  • Crossing only to merge. An ingredient measured by one kind in every recipe is never converted to the other, whatever densities are known.
  • The density table. A small built-in table (liquids, fats, sugars, flours, grains) published with the conventions as a data file, with [density] in units.conf on top. It is not part of this PR. Ingredients whose cup weight depends on how they are cut are left out, and so is plain salt.
  • Rule 4 for references (the other kind, with a density the user wrote under the recipe's name) is the only approximate matching rule and may not be worth having.
  • This changes 0017 and the conventions: "only matching units are supported" becomes the matching rules, and "compatible units" in the shopping list format gets a definition.
  • Applications that add units today would, with no setup, go back to amounts as written.
  • Open questions in the proposal: opt-in or default (adding two spellings of one unit could be on for everyone), the unit table and Proposal: add unit system metadata #148, what a crossed total is shown in, where densities are kept, steps for US customary totals.

The two parts can be accepted separately: reference matching does not depend on the setup file.

This PR contains the proposal only; conventions and test updates follow once it is accepted.

- an opt-in setup file, units.conf, names the unit system for shopping
  list totals; with it a tool shows one amount per ingredient
- amounts of the same kind (cup and ml, g and lb) are added through a
  unit table, in the base unit, rounded up, independent of recipe order
- weight and volume are added only when a density is known for the
  ingredient; the total is marked as approximate and no density is
  ever assumed
- the recipe view is not converted; pantry amounts are subtracted
  across units by the same rule
- a recipe reference is matched to the referenced recipe's yield by
  kind: 1 l against a yield of 500 ml scales by 2, and a list of
  yields is matched by the entry of the same kind
- no recipe syntax changes
- addresses discussion #142
@Plommonsorbet

Copy link
Copy Markdown

I agree with where this is going, and the measurement makes the case on its own: only 23% of shared ingredients share a unit and a spelling, so a list that cannot add across units fails on the common case, not an edge case.

The split between reading and display is the important idea here, and I think it splits further. Six jobs, with different "owners":

  • Reading, recipe text to a unit. Needs the recipe's language. In Swedish msk is a metric tablespoon and tsk a metric teaspoon.
  • Conversion, a unit to another of the same kind. Needs exact ratios. One right answer.
  • Aggregation, adding amounts of one ingredient. Needs only conversion.
  • Preference, which units a total is shown in. Taste, so it is the reader's.
  • Writing, a unit back to text. Needs the language of whatever is being written: the reader's for a shopping list, the recipe's when the recipe itself is written back.
  • Density, converting between kinds, volume to mass. Needs the ingredient, and how the author measured it.

You already single out density:

A unit table solves the first two. The third needs the ingredient's density.

It is the only one of the six that needs data rather than definitions, so it looks like it should be its own proposal.

cooklang-rs with its bundled unit table, which other applications are built with, adds units of the same kind into the unit of whichever recipe comes first. The two recipes above give milk 1.845 c in one order and milk 436.588 ml in the other

So conversion and aggregation already work on cooklang-rs:main. What is missing is the preference, and with one the result stops depending on the order:

let c = Converter::bundled();
for order in [
    [(1.0, "cup"), (200.0, "ml")],
    [(200.0, "ml"), (1.0, "cup")],
] {
    let mut g = GroupedQuantity::empty();
    for (v, u) in order {
        g.add(&Quantity::new(v.into(), Some(u.into())), &c);
    }
    let mut total = g.iter().next().unwrap().clone();
    println!("merged:  {total}");
    total.convert(ConvertTo::Best(System::Metric), &c).unwrap();
    println!("metric:  {total}");
}

Output:

merged:  1.845 cup
metric:  436.588 ml
merged:  436.588 ml
metric:  436.588 ml

Either order lands on the 437 ml of your milk line, which is the requirement you set: the list is the same whichever recipe comes first. That changes one of the open questions:

Applications that add units today. With no setup they would go back to amounts as written, which removes a sum some people may rely on.

The reason they would have to stop is that today the total takes the unit of whichever recipe came first, so two tools can show different lists from the same files. Taking the unit from the reader's preference instead, as above, removes that without removing the sum: the total is then the same in any order and in every tool, and the applications that add today can keep adding.

Three suggestions:

  1. Reading needs a locale, not a system.

    When a recipe declares unit system (PR Proposal: add unit system metadata #148), its amounts are read in that system by a tool that has the units for it.

    None of unit system's three values says which language wrote the recipe, so unit system: metric cannot tell a tool what msk is. A locale tag says both: sv-SE names the language, and its region decides what a name like pint means, which is the job the three values are doing, so nothing the key does today is lost. The per-language locales I mentioned on Discord are keyed on that tag.

  2. Preference and writing are two settings, not one.

    units names the system that totals are shown in: metric or us customary

    That picks a system. It does not say which units within it a reader wants, nor which language they are written in. A Swedish shopping list saying 600 ml of milk makes me stop and convert: here it would be 6 dl, or 0.6 l. Both are the reader's, but they are separate choices. The locales keep them apart: a preferred unit per kind, and the reader's language for writing it.

  3. Leave the units.conf format out.

    units.conf, a TOML file kept where the tool keeps the aisle and pantry configuration

    The preference is a tool setting, and specifying the file it lives in makes a tool's configuration part of the language contract. Name the setting and leave the file to the tool.

For now I am only commenting on the open proposals. I will open one of my own for the locales likely in the coming week, rather than retell the design in each thread.

One request while I am here: the open questions are numbered, which makes them easy to answer. Could the motivation and design points be numbered too? Right now replying to one means quoting it, as above.

@dubadub

dubadub commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

The open questions from the proposal, as a list that can be answered by number. Each one says what the proposal does now.

Q1. Opt-in or on by default?
The proposal is opt-in: with no setup, list totals stay as written, because cooklang/cookcli#433 is recent and people expect to see what the recipe says. One part could be on for everyone. Adding two spellings of the same unit (1 cup and 2 cups, 1 tbsp and 2 tablespoons) converts nothing, and it is 37% of shared ingredients in the measurement.

Q2. Should the conventions hold a unit table?
#148 leaves conversion tables to each application. This proposal writes down the volume and mass units, their spellings and their ratios, because two tools need them to give the same total from the same files. Is that the right minimum? Rounding and the choice of unit for a total are fixed here too, and could be left to applications.

Q3. US customary and British imperial.
The table has US volumes. An imperial pint or fluid ounce differs, and a metric cup is 250 ml. Should the table get a second set of volume units, or wait for #148?

Q4. What is a total shown in when weight meets volume?
The proposal decides per ingredient: by weight, unless the density table says the ingredient is bought by volume (milk, oil). The alternatives are always weight, or the side that holds the larger share of the total.

Q5. Where are densities kept?
The proposal has a small built-in table published with the conventions as a data file, and [density] in the setup file on top of it. If an ingredient database with other properties is specified later, it becomes the main home and [density] stays as a short override. The alternatives are user-written densities only, or leaving the table to each application.

Q6. Is rule 4 for references worth having?
Rule 4 scales a reference of the other kind ({250%g} against yield: 500%ml) when the user has written a density under the recipe's name. It is the only matching rule that is approximate, and the only one that needs an "approximate" flag carried from scaling into list totals. Dropping it loses little: a second yield entry (yield: [500%ml, 500%g]) already covers the author who knows the weight.

Q7. What do applications that add units today do with no setup?
The proposal has them go back to amounts as written, so that every tool shows the same list. That removes a sum some people may rely on.

Q8. Which steps for US customary totals?
The proposal rounds up in quarters for every unit. Eighths for teaspoons, or whole ounces, may read better.

Four more came up in the first review comment above. They are not in the proposal text yet:

Q9. Should weight against volume be its own proposal?
It is the only part that needs data about ingredients. The unit table, the list rule for units of one kind, and reference matching rules 1 to 3 do not depend on it.

Q10. Should the setup file be specified, or only the setting?
The proposal names a file, units.conf, and its format. The suggestion is to name the setting and leave the file to each tool.

Q11. Is one display setting enough?
units picks a system. It does not say which units of that system a reader wants (6 dl or 600 ml), or which language the unit is written in.

Q12. How are unit names in other languages read?
msk and tsk are not in the table, and unit system does not name a language. The suggestion is a locale in place of a system.

On Q7, the same comment suggests an answer: keep adding, and take the unit from the reader's preference, so the total no longer depends on the order of the recipes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants