Function Repository Resource:

# FlattenAmbiguityList

Restructure an expression with nested AmbiguityList expressions into a single flat AmbiguityList

Contributed by: Andrew Steinacher
 ResourceFunction["FlattenAmbiguityList"][expr] restructures expr as a single AmbiguityList. ResourceFunction["FlattenAmbiguityList"][expr, n] limits to no more than n possibilities. ResourceFunction["FlattenAmbiguityList"][expr, n, crit] limits to no more than n possibilities pi for which crit[pi] is True.

## Examples

### Basic Examples (2)

AmbiguityList is often only wrapped around the ambiguous part of an expression:

 In[1]:=
 Out[1]=

By moving the arguments inside the listed functions, all possible values can be computed directly:

 In[2]:=
 Out[2]=

Interpreter can return nested AmbiguityList expressions that may be difficult to reason about:

 In[3]:=
 Out[3]=

Flatten the expression to see all possibilities in a single flat list:

 In[4]:=
 Out[4]=

### Scope (3)

Ambiguous expressions can produce a large number of possibilities:

 In[5]:=
 Out[5]=
 In[6]:=
 Out[6]=

Limit the number of distinct possibilities produced:

 In[7]:=
 Out[7]=

Also limit to possibilities that pass a selection function:

 In[8]:=
 Out[8]=

### Options (3)

By default, only preexisting metadata is maintained during restructuring:

 In[9]:=
 Out[9]=

Add an entity type to descriptions for further clarity:

 In[10]:=
 Out[10]=

 In[11]:=
 Out[11]=

### Applications (3)

Find all interpretations for a food using an ambiguous unit of "cup":

 In[12]:=
 Out[12]=

Flatten the expression to see each interpretation:

 In[13]:=
 Out[13]=

Compare calorie amounts for different interpretations of the unit:

 In[14]:=
 Out[14]=

### Properties and Relations (5)

Unambiguous expressions are not restructured:

 In[15]:=
 Out[15]=

An already-flat AmbiguityList is not restructured:

 In[16]:=
 Out[16]=

Expressions containing AmbiguityList in deeper parts of an expression will be moved to the top level:

 In[17]:=
 Out[17]=

If ambiguity is present, a selection function that produces no matches returns an empty AmbiguityList:

 In[18]:=
 Out[18]=

When no ambiguity is present, the expression is left untouched:

 In[19]:=
 Out[19]=

### Possible Issues (1)

Sometimes ambiguous expressions can produce a large number of possibilities, which can take excessive memory and computation time:

 In[20]:=
 Out[21]=

## Publisher

Andrew Steinacher

## Version History

• 2.0.0 – 07 December 2020
• 1.0.0 – 13 November 2020

## Author Notes

The use of None in the second argument of AmbiguityList is not documented… but it was the only way to follow the documented forms where the third argument contains metadata, but no uniform single string can be put into the second argument. An empty AmbiguityList is also undocumented, but is a natural use case when no possibilities are found that meet the criteria.