// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2020 The Board of Trustees of the Leland Stanford Junior University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>

import assert from 'assert';
import { Ast, Type } from 'thingtalk';
import * as C from '../ast_manip';
import ThingpediaLoader from '../load-thingpedia';
import type * as $Genie from "genie-toolkit";
export default async function($runtime : typeof $Genie.SentenceGeneratorRuntime, $ttUtils : typeof $Genie.ThingTalkUtils, $options : $Genie.SentenceGeneratorTypes.GrammarOptions, $locale : $Genie.I18n.LanguagePack, $grammar : $Genie.SentenceGenerator<any, any>, $loader : ThingpediaLoader) : Promise<void> {

    const HERE = new Ast.Value.Location(new Ast.Location.Relative('current_location'));
   $grammar.declareSymbol("projection_Measure_m");
   $grammar.declareSymbol("compute_question");
   $grammar.declareSymbol("with_arg_min_max_table");
   $grammar.declareSymbol("projection_Measure_m");
   if (!$options.flags.turking) {
       $grammar.addRule("projection_Measure_m", ["the distance", new $runtime.Choice(["of", "to"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["from here", ""])], (($0 : undefined, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), HERE], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["the distance", new $runtime.Choice(["of", "to"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["from here", ""])], (($0 : undefined, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), HERE], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "is", new $runtime.Choice(["from here", ""])], (($0 : undefined, $1 : undefined, table : Ast.Expression, $2 : undefined, $3 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), HERE], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "is", new $runtime.Choice(["from here", ""])], (($0 : undefined, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined, $4 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), HERE], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["the distance", new $runtime.Choice(["of", "to"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "from", new $runtime.NonTerminal("constant_Location", undefined)], (($0 : undefined, $1 : undefined, table : Ast.Expression, $2 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["the distance from", new $runtime.NonTerminal("constant_Location", undefined), new $runtime.Choice(["of", "to"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]])], (($0 : undefined, place : Ast.LocationValue, $1 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["the distance", new $runtime.Choice(["of", "to"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "from", new $runtime.NonTerminal("constant_Location", undefined)], (($0 : undefined, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["the distance from", new $runtime.NonTerminal("constant_Location", undefined), new $runtime.Choice(["of", "to"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]])], (($0 : undefined, place : Ast.LocationValue, $1 : undefined, $2 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), "from", new $runtime.NonTerminal("constant_Location", undefined), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "is"], (($0 : undefined, $1 : undefined, $2 : undefined, place : Ast.LocationValue, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "is from", new $runtime.NonTerminal("constant_Location", undefined)], (($0 : undefined, $1 : undefined, table : Ast.Expression, $2 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), "from", new $runtime.NonTerminal("constant_Location", undefined), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "is"], (($0 : undefined, $1 : undefined, $2 : undefined, place : Ast.LocationValue, $3 : undefined, table : Ast.Expression, $4 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "is from", new $runtime.NonTerminal("constant_Location", undefined)], (($0 : undefined, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
   }
   $grammar.declareSymbol("projection_Number");
   if (!$options.flags.turking) {
       $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["of", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)])], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isArray)
                return null;
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, Type.Number);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["of", "in"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)])], (($0 : undefined, p : C.ParamSlot, $1 : undefined, $2 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isArray)
                return null;
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, Type.Number);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), new $runtime.Choice(["of", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)])], (($0 : undefined, p : C.ParamSlot, $1 : undefined, filter : C.FilterSlot, $2 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            const ptype = table.schema!.out[p.name];
            if (!ptype || !(ptype instanceof Type.Array) || !(filter instanceof Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem as Type;
            if (!(elementType instanceof Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, Type.Number);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), new $runtime.Choice(["of", "in"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)])], (($0 : undefined, p : C.ParamSlot, $1 : undefined, filter : C.FilterSlot, $2 : undefined, $3 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            const ptype = table.schema!.out[p.name];
            if (!ptype || !(ptype instanceof Type.Array) || !(filter instanceof Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem as Type;
            if (!(elementType instanceof Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, Type.Number);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["of", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)])], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isNumber)
                return null;
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["of", "in"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)])], (($0 : undefined, p : C.ParamSlot, $1 : undefined, $2 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isNumber)
                return null;
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {  });
   }
   $grammar.declareSymbol("compute_question");
   if (!$options.flags.turking) {
       $grammar.addRule("compute_question", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.Choice(["is", "are"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["from here", ""]), new $runtime.Choice(["?", ""])], (($0 : undefined, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined, $4 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), HERE], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.Choice(["is", "are"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["from here", ""]), new $runtime.Choice(["?", ""])], (($0 : undefined, $1 : undefined, $2 : undefined, $3 : undefined, table : Ast.Expression, $4 : undefined, $5 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), HERE], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.Choice(["is", "are"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["from", "to"]), new $runtime.NonTerminal("constant_Location", undefined), new $runtime.Choice(["?", ""])], (($0 : undefined, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined, place : Ast.LocationValue, $4 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.Choice(["is", "are"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["from", "to"]), new $runtime.NonTerminal("constant_Location", undefined), new $runtime.Choice(["?", ""])], (($0 : undefined, $1 : undefined, $2 : undefined, $3 : undefined, table : Ast.Expression, $4 : undefined, place : Ast.LocationValue, $5 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'));
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["does", "do"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "have ?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), "are there", new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["does", "do"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "have ?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), "in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), "are there in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["does", "do"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "have ?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {

            assert (table.schema!.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, Type.Number);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {

            assert (table.schema!.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, Type.Number);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "are there", new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {

            assert (table.schema!.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, Type.Number);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["does", "do"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "have ?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {

            assert (table.schema!.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, Type.Number);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {

            assert (table.schema!.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, Type.Number);
        }), C.expressionKeyFn, {  });
       $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "are there in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {

            assert (table.schema!.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, Type.Number);
        }), C.expressionKeyFn, {  });
       if (!$options.flags.dialogues) {
           $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), new $runtime.Choice(["does", "do"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "have ?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, filter : C.FilterSlot, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {
            const ptype = table.schema!.out[p.name];
            if (!ptype || !(ptype instanceof Type.Array) || !(filter instanceof Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem as Type;
            if (!(elementType instanceof Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, Type.Number);
        }), C.expressionKeyFn, {  });
           $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, filter : C.FilterSlot, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {
            const ptype = table.schema!.out[p.name];
            if (!ptype || !(ptype instanceof Type.Array) || !(filter instanceof Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem as Type;
            if (!(elementType instanceof Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, Type.Number);
        }), C.expressionKeyFn, {  });
           $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "are there", new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, filter : C.FilterSlot, $2 : undefined, $3 : undefined, table : Ast.Expression, $4 : undefined) : (Ast.Expression)|null => {
            const ptype = table.schema!.out[p.name];
            if (!ptype || !(ptype instanceof Type.Array) || !(filter instanceof Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem as Type;
            if (!(elementType instanceof Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, Type.Number);
        }), C.expressionKeyFn, {  });
           $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), new $runtime.Choice(["does", "do"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "have ?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, filter : C.FilterSlot, $2 : undefined, $3 : undefined, table : Ast.Expression, $4 : undefined) : (Ast.Expression)|null => {
            const ptype = table.schema!.out[p.name];
            if (!ptype || !(ptype instanceof Type.Array) || !(filter instanceof Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem as Type;
            if (!(elementType instanceof Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, Type.Number);
        }), C.expressionKeyFn, {  });
           $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, filter : C.FilterSlot, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {
            const ptype = table.schema!.out[p.name];
            if (!ptype || !(ptype instanceof Type.Array) || !(filter instanceof Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem as Type;
            if (!(elementType instanceof Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, Type.Number);
        }), C.expressionKeyFn, {  });
           $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "are there in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, filter : C.FilterSlot, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {
            const ptype = table.schema!.out[p.name];
            if (!ptype || !(ptype instanceof Type.Array) || !(filter instanceof Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem as Type;
            if (!(elementType instanceof Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, Type.Number);
        }), C.expressionKeyFn, {  });
       }
       if ($options.flags.aggregation) {
           $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("with_filtered_table", ["is_list" as ("is_list" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["is_list"]]), "are there ?"], (($0 : undefined, table : Ast.Expression, $1 : undefined) : (Ast.Expression)|null => {
            assert(table.schema!.is_list);
            const newSchema = table.schema!.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT)
                .addArguments([new Ast.ArgumentDef(null, Ast.ArgDirection.OUT, 'count', Type.Number)]);
            newSchema.is_list = false;
            const agg = new Ast.AggregationExpression(null, table, '*', 'count', newSchema);
            return new Ast.ProjectionExpression(null, agg, ['count'], [], [], newSchema);
        }), C.expressionKeyFn, {  });
           $grammar.addRule("compute_question", [new $runtime.Choice(["please", ""]), "count the", new $runtime.NonTerminal("with_filtered_table", ["is_list" as ("is_list" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["is_list"]])], (($0 : undefined, $1 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            assert(table.schema!.is_list);
            const newSchema = table.schema!.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT)
                .addArguments([new Ast.ArgumentDef(null, Ast.ArgDirection.OUT, 'count', Type.Number)]);
            newSchema.is_list = false;
            const agg = new Ast.AggregationExpression(null, table, '*', 'count', newSchema);
            return new Ast.ProjectionExpression(null, agg, ['count'], [], [], newSchema);
        }), C.expressionKeyFn, {  });
           if (!$options.flags.schema_org) {
               $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]]), "are", new $runtime.Choice(["in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, $2 : undefined, table : Ast.Expression, $3 : undefined) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isNumeric())
                return null;
            if (!table.schema!.is_list)
                return null;

            const newSchema = table.schema!.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT || arg.name === p.name);
            newSchema.is_list = false;
            const agg = new Ast.AggregationExpression(null, table, p.name, 'sum', newSchema);
            return new Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
        }), C.expressionKeyFn, {  });
           }
           if (!$options.flags.schema_org) {
               $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]]), new $runtime.Choice(["do", "does"]), new $runtime.NonTerminal("with_filtered_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "have ?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined, table : Ast.Expression, $2 : undefined) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isNumeric())
                return null;
            if (!table.schema!.is_list)
                return null;

            const newSchema = table.schema!.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT || arg.name === p.name);
            newSchema.is_list = false;
            const agg = new Ast.AggregationExpression(null, table, p.name, 'sum', newSchema);
            return new Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
        }), C.expressionKeyFn, {  });
           }
       }
   }
   $grammar.declareSymbol("with_arg_min_max_table");
   if (!$options.flags.turking) {
       $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["nearest", "closest"]), new $runtime.NonTerminal("complete_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["", "from here", "to here", "to me"])], (($0 : undefined, table : Ast.Expression, $1 : undefined) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), HERE], new Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["nearest", "closest"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]])], (($0 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), HERE], new Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["farthest", "most far away"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]])], (($0 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), HERE], new Type.Measure('m'), 'desc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["nearest", "closest"]), new $runtime.NonTerminal("complete_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "to", new $runtime.NonTerminal("constant_Location", undefined)], (($0 : undefined, table : Ast.Expression, $1 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("complete_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["that is", "which is"]), new $runtime.Choice(["nearest", "closest"]), "to", new $runtime.NonTerminal("constant_Location", undefined)], ((table : Ast.Expression, $0 : undefined, $1 : undefined, $2 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("complete_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["nearest", "closest"]), "to", new $runtime.NonTerminal("constant_Location", undefined)], ((table : Ast.Expression, $0 : undefined, $1 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["nearest", "closest"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "to", new $runtime.NonTerminal("constant_Location", undefined)], (($0 : undefined, table : Ast.Expression, $1 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["that is", "which is"]), new $runtime.Choice(["nearest", "closest"]), "to", new $runtime.NonTerminal("constant_Location", undefined)], ((table : Ast.Expression, $0 : undefined, $1 : undefined, $2 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["nearest", "closest"]), "to", new $runtime.NonTerminal("constant_Location", undefined)], ((table : Ast.Expression, $0 : undefined, $1 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["farthest", "most far away"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), "from", new $runtime.NonTerminal("constant_Location", undefined)], (($0 : undefined, table : Ast.Expression, $1 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'), 'desc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["that is", "which is"]), new $runtime.Choice(["farthest", "most far away"]), "from", new $runtime.NonTerminal("constant_Location", undefined)], ((table : Ast.Expression, $0 : undefined, $1 : undefined, $2 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'), 'desc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", ["has_geo" as ("has_geo" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), (true) as ReturnType<typeof C.expressionKeyFn>["has_geo"]]), new $runtime.Choice(["farthest", "most far away"]), "from", new $runtime.NonTerminal("constant_Location", undefined)], ((table : Ast.Expression, $0 : undefined, $1 : undefined, place : Ast.LocationValue) : (Ast.Expression)|null => {
            assert (table.schema!.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new Ast.Value.VarRef('geo'), place], new Type.Measure('m'), 'desc');
        }), C.expressionKeyFn, {  });
       $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["most", "highest number of", "largest number of", "largest count of"]), new $runtime.NonTerminal("out_param_Array__Any", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((table : Ast.Expression, $0 : undefined, $1 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isArray)
                return null;
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeArgMaxMinTable(table, conflictParam, 'desc');
            return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, Type.Number, 'desc');
        }), C.expressionKeyFn, { weight: (0.33) });
       if (!$options.flags.turking) {
           $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["least", "fewest", "smallest number of", "tiniest number of", "smallest count of"]), new $runtime.NonTerminal("out_param_Array__Any", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((table : Ast.Expression, $0 : undefined, $1 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isArray)
                return null;
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeArgMaxMinTable(table, conflictParam, 'asc');
            return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, Type.Number, 'asc');
        }), C.expressionKeyFn, { weight: (0.33) });
       }
       if ($options.flags.turking) {
           $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["fewest", "smallest number of"]), new $runtime.NonTerminal("out_param_Array__Any", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((table : Ast.Expression, $0 : undefined, $1 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isArray)
                return null;
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeArgMaxMinTable(table, conflictParam, 'asc');
            return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, Type.Number, 'asc');
        }), C.expressionKeyFn, { weight: (0.33) });
       }
       $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["most", "highest number of", "largest number of", "largest count of"]), new $runtime.NonTerminal("out_param_ArrayCount", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((table : Ast.Expression, $0 : undefined, $1 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isNumber)
                return null;
            return C.makeArgMaxMinTable(table, p.name, 'desc');
        }), C.expressionKeyFn, { weight: (0.33) });
       if (!$options.flags.turking) {
           $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["least", "fewest", "smallest number of", "tiniest number of", "smallest count of"]), new $runtime.NonTerminal("out_param_ArrayCount", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((table : Ast.Expression, $0 : undefined, $1 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isNumber)
                return null;
            return C.makeArgMaxMinTable(table, p.name, 'asc');
        }), C.expressionKeyFn, { weight: (0.33) });
       }
       if ($options.flags.turking) {
           $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["fewest", "smallest number of"]), new $runtime.NonTerminal("out_param_ArrayCount", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((table : Ast.Expression, $0 : undefined, $1 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
            if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isNumber)
                return null;
            return C.makeArgMaxMinTable(table, p.name, 'asc');
        }), C.expressionKeyFn, { weight: (0.33) });
       }
   }
};
