text:
""" Generalized algebraic theories (GATs) in Julia.
"""
module GAT
export @theory, @signature, @instance, theory, invoke_term

using Base.Meta: ParseError
using Compat
using AutoHashEquals
using DataStructures: OrderedDict
using Logging
using MLStyle: @match

using ..Meta

# Data types
############

const Context = OrderedDict{Symbol,Expr0}

""" Type constructor in a GAT.
"""
@auto_hash_equals struct TypeConstructor
  name::Symbol
  params::Vector{Symbol}
  context::Context
  doc::Union{String,Nothing}

  function TypeConstructor(name::Symbol, params::Vector,
                           context::Context, doc=nothing)
    new(name, params, context, doc)
  end
end

""" Term constructor in a GAT.
"""
@auto_hash_equals struct TermConstructor
  name::Symbol
  params::Vector{Symbol}
  typ::Expr0
  context::Context
  doc::Union{String,Nothing}

  function TermConstructor(name::Symbol, params::Vector, typ::Expr0,
                           context::Context, doc=nothing)
    new(name, params, typ, context, doc)
  end
end

""" Axiom constructor in a GAT.
"""
@auto_hash_equals struct AxiomConstructor
  name::Symbol
  left::Expr0
  right::Expr0
  context::Context
  doc::Union{String,Nothing}

  function AxiomConstructor(name::Symbol, left::Expr0, right::Expr0,
                            context::Context, doc=nothing)
    new(name, left, right, context, doc)
  end
end

""" Data structure for a generalized algebraic theory (GAT).
"""
@auto_hash_equals struct Theory
  types::Vector{TypeConstructor}
  terms::Vector{TermConstructor}
  axioms::Vector{AxiomConstructor}
  aliases::Dict{Symbol,Symbol}
end

struct TheoryBinding
  name::Symbol
  params::Vector{Symbol}
end
struct TheoryHead
  main::TheoryBinding
  base::Vector{TheoryBinding}
  TheoryHead(main, base=[]) = new(main, base)
end

# Theories
##########

""" Define a generalized algebraic theory (GAT).

Four kinds of things can go in the theory body:

1. Type constructors, indicated by the special type `TYPE`, e.g.,
   `Hom(X::Ob,Y::Ob)::TYPE`
2. Term constructors, e.g.,
   `id(X::Ob)::Hom(X,X)`
3. Function aliases, e.g.,
   `@op Hom :→`
4. Equality axioms, e.g.,
   `f ⋅ id(B) == f ⊣ (A::Ob, B::Ob, f::(A → B))`

A theory can extend existing theories (at present only one).
"""
macro theory(head, body)
  theory_builder(head, body)
end

""" Define a signature for a generalized algebraic theory (GAT).

A signature is the same as a theory, except it may not contain axioms, and
therefore only three kinds of things can go in the signature body:

1. Type constructors, indicated by the special type `TYPE`, e.g.,
   `Hom(X::Ob,Y::Ob)::TYPE`
2. Term constructors, e.g.,
   `id(X::Ob)::Hom(X,X)`
3. Function aliases, e.g.,
   `@op Hom :→`

A signature can extend existing theories (at present only one).
"""
macro signature(head, body)
  theory_builder(head, body, signature=true)
end

""" Retrieve generalized algebraic theory associated with abstract type.

For example, if `Category` is imported from `Catlab.Theories`, then
`theory(Category)`returns the theory of a category.
"""
function theory end

""" Define how a theory is built, set up as a separate function to allow both
    the signature and theory macros to share code and throw an error if any
    axioms are defined in a signature.
"""
function theory_builder(head, body; signature=false)
  # Parse theory header.
  head = parse_theory_head(head)
  @assert all(param in head.main.params
              for base in head.base for param in base.params)
  @assert length(head.base) <= 1 "Multiple theory extension not supported"
  base_name = isempty(head.base) ? nothing : only(head.base).name

  # Parse theory body: GAT types/terms and function aliases.
  types, terms, axioms, aliases = parse_theory_body(body)
  if signature && length(axioms) > 0
    throw(ParseError("@signature macro does not allow axioms to be defined"))
  end
  theory = Theory(types, terms, axioms, aliases)

  # We must generate and evaluate the code at *run time* because the base
  # theory, if specified, is not available at *parse time*.
  expr = :(theory_code($head, $theory, $(esc(base_name))))
  Expr(:block,
    Expr(:call, esc(:eval), expr),
    :(Core.@__doc__ $(esc(head.main.name))))
end

function theory_code(head, theory, base_type)
  # Add types/terms/aliases from base theory, if provided.
  if !isnothing(base_type)
    base_theory = GAT.theory(base_type)
    base_params = [ type.name for type in base_theory.types ]
    bindings = Dict(zip(base_params, only(head.base).params))
    base_theory = replace_types(bindings, base_theory)
    theory = Theory([base_theory.types; theory.types],
                    [base_theory.terms; theory.terms],
                    [base_theory.axioms; theory.axioms],
                    merge(base_theory.aliases, theory.aliases))
  end
  theory = replace_types(theory.aliases, theory)

  # Names of generic functions in interface defined by theory.
  names = unique!(vcat(
    [ param for type in theory.types for param in type.params ], # Accessors.
    [ term.name for term in theory.terms ], # Term constructors.
    collect(keys(theory.aliases)) # Unicode aliases.
  ))

  # Generate block with abstract type definition, registration of theory,
  # and stubs for generic functions.
  Expr(:block,
    Expr(:abstract, head.main.name),
    Expr(:(=),
      Expr(:call, GlobalRef(GAT, :theory),
        Expr(:(::), Expr(:curly, :Type, head.main.name))),
      theory),
    (Expr(:function, name) for name in names)...,
  )
end

function parse_theory_head(expr::Expr)::TheoryHead
  parse = parse_theory_binding
  parse_jl = parse_theory_binding_jlstyle
  parse_either = parse_theory_binding_either
  @match expr begin
    (Expr(:call, :(=>), Expr(:tuple, bases), main)
      => TheoryHead(parse(main), map(parse, bases)))
    (Expr(:(<:), main, Expr(:tuple,bases))
      => TheoryHead(parse_jl(main), map(parse_jl, bases)))
    Expr(:call, :(=>), base, main) => TheoryHead(parse(main), [parse(base)])
    Expr(:(<:), main, base) => TheoryHead(parse_jl(main), [parse_jl(base)])
    _ => TheoryHead(parse_either(expr))
  end
end

function parse_theory_binding(expr::Expr)::TheoryBinding
  @warn "Using Haskell-style theory declaration with parentheses is deprecated," *
    " use Julia-style with curly braces."
  @match expr begin
    Expr(:call, name::Symbol, params...) => TheoryBinding(name, params)
    _ => throw(ParseError("Ill-formed theory binding $expr"))
  end
end

function parse_theory_binding_jlstyle(expr::Expr)::TheoryBinding
  @match expr begin
    Expr(:curly, name::Symbol, params...) => TheoryBinding(name, params)
    _ => throw(ParseError("Ill-formed theory binding $expr"))
  end
end

function parse_theory_binding_either(expr::Expr)::TheoryBinding
  @match expr begin
    Expr(:call, name::Symbol, params...) => TheoryBinding(name, params)
    Expr(:curly, name::Symbol, params...) => TheoryBinding(name, params)
    _ => throw(ParseError("Ill-formed theory binding $expr"))
  end
end

""" Parse the body of a GAT declaration.
"""
function parse_theory_body(expr::Expr)
  @assert expr.head == :block
  aliases = Dict{Symbol, Symbol}()
  types = OrderedDict{Symbol,TypeConstructor}()
  terms = TermConstructor[]
  axioms = AxiomConstructor[]
  for elem in strip_lines(expr).args
    elem = strip_lines(elem)
    head = last(parse_docstring(elem)).head
    if head in (:(::), :call, :comparison, :where)
      cons = parse_constructor(elem)
      if isa(cons, TypeConstructor)
        if haskey(types, cons.name)
          throw(ParseError("Duplicate type constructor $elem"))
        else
          types[cons.name] = cons
        end
      elseif isa(cons, TermConstructor)
        push!(terms, cons)
      else
        push!(axioms, cons)
      end
    elseif head == :macrocall && elem.args[1] == Symbol("@op")
      if elem.args[2].head == :(:=)
        aliases[elem.args[2].args[1]] = elem.args[2].args[2]
      elseif elem.args[2].head == :block
        merge!(aliases, Dict(map(x -> if x.head == :(:=)
                                        x.args[1] => x.args[2]
                                      else
                                        throw(ParseError("Ill-formed alias $x"))
                                      end, strip_lines(elem.args[2]).args)))
      else
        throw(ParseError("Ill-formed alias $elem"))
      end
    else
      throw(ParseError("Ill-formed theory element $elem"))
    end
  end
  return (collect(values(types)), terms, axioms, aliases)
end

""" Get type constructor by name.

Unlike term constructors, type constructors cannot be overloaded, so there is at
most one type constructor with a given name.
"""
function get_type(theory::Theory, name::Symbol)::TypeConstructor
  indices = findall(cons -> cons.name == name, theory.types)
  length(indices) < 1 && error("Malformed GAT definition type constructor for $name is missing")
  length(indices) > 1 && error("Malformed GAT definition type constructor for $name cannot be overloaded")
  theory.types[indices[1]]
end
function has_type(theory::Theory, name::Symbol)::Bool
  findfirst(cons -> cons.name == name, theory.types) != nothing
end

""" Add a type-valued first argument to a Julia function signature.

We need this to avoid ambiguity in method dispatch when a term constructor has
no arguments (e.g., `munit()`) or more generally has no arguments that are types
in the signature (e.g., object generators in a category).

The fundamental reason why these cases must be treated specially is that Julia
does not (yet?) support
[dispatching on return type](https://github.com/JuliaLang/julia/issues/19206).
"""
function add_type_dispatch(call_expr::Expr, type_expr::Expr0)::Expr
  @match call_expr begin
    (Expr(:call, name, args...) =>
      Expr(:call, name, :(::Type{$type_expr}), args...))
    _ => throw(ParseError("Ill-formed call expression $call_expr"))
  end
end

# GAT expressions
#################

""" Parse a raw expression in a GAT.

A "raw expression" is a just composition of function and constant symbols.
"""
function parse_raw_expr(expr)
  @match expr begin
    Expr(:call, args...) => map(parse_raw_expr, args)
    head::Symbol => nothing
    _ => throw(ParseError("Ill-formed raw expression $expr"))
  end
  expr # Return the expression unmodified. This function just checks syntax.
end

""" Parse context for term or type in a GAT.
"""
function parse_context(expr::Expr)::Context
  context = Context()
  args = expr.head == :tuple ? expr.args : [ expr ]
  for arg in args
    name, type = @match arg begin
      Expr(:(::), name::Symbol, type) => (name, parse_raw_expr(type))
      name::Symbol => (name, :Any)
      _ => throw(ParseError("Ill-formed context expression $expr"))
    end
    if haskey(context, name)
      throw(ParseError("Name $name already defined"))
    end
    context[name] = type
  end
  context
end

""" Parse type or term constructor in a GAT.
"""
function parse_constructor(expr::Expr)::Union{TypeConstructor,TermConstructor,
                                              AxiomConstructor}
  # Context is optional.
  doc, expr = parse_docstring(expr)
  cons_expr, context = @match expr begin
    Expr(:call, :<=, inner, context) => (inner, parse_context(context))
    Expr(:call, :⊣, inner, context) => (inner, parse_context(context))
    Expr(:comparison, cons_left, cons_sym, cons_right, :⊣, context) => (
      Expr(:call, cons_sym, cons_left, cons_right), parse_context(context))
    Expr(:where, inner, context) => (inner, parse_context(context))
    _ => (expr, Context())
  end

  # Allow abbreviated syntax where tail of context is included in parameters.
  function parse_param(param::Expr0)::Symbol
    name, type = @match param begin
      Expr(:(::), name::Symbol, type) => (name, parse_raw_expr(type))
      name::Symbol => (name, :Any)
      _ => throw(ParseError("Ill-formed type/term parameter $param"))
    end
    if !haskey(context, name)
      context[name] = type
    end
    name
  end

  @match cons_expr begin
    (Expr(:(::), name::Symbol, :TYPE)
      => TypeConstructor(name, [], context, doc))
    (Expr(:(::), Expr(:call, name::Symbol, params...), :TYPE)
      => TypeConstructor(name, map(parse_param, params), context, doc))
    (Expr(:(::), Expr(:call, name::Symbol, params...), type)
      => TermConstructor(name, map(parse_param, params), parse_raw_expr(type),
                         context, doc))
    (Expr(:call, :(==), left, right)
       => AxiomConstructor(:(==), left, right, context, doc))
    _ => throw(ParseError("Ill-formed type/term constructor $cons_expr"))
  end
end

""" Replace names of type constructors in a GAT.
"""
function replace_types(bindings::Dict, theory::Theory)::Theory
  Theory([ replace_types(bindings, t) for t in theory.types ],
         [ replace_types(bindings, t) for t in theory.terms ],
         [ replace_types(bindings, t) for t in theory.axioms ],
         replace_types(bindings, theory.aliases))
end
function replace_types(bindings::Dict, cons::TypeConstructor)::TypeConstructor
  TypeConstructor(replace_symbols(bindings, cons.name), cons.params,
                  replace_types(bindings, cons.context), cons.doc)
end
function replace_types(bindings::Dict, cons::TermConstructor)::TermConstructor
  TermConstructor(cons.name, cons.params,
                  replace_symbols(bindings, cons.typ),
                  replace_types(bindings, cons.context), cons.doc)
end
function replace_types(bindings::Dict, cons::AxiomConstructor)::AxiomConstructor
  AxiomConstructor(cons.name,
                   replace_symbols(bindings, cons.left),
                   replace_symbols(bindings, cons.right),
                   replace_types(bindings, cons.context), cons.doc)
end
function replace_types(bindings::Dict, aliases::Dict)::Dict
  Dict(a => replace_symbols(bindings, aliases[a])
       for a in keys(aliases))
end
function replace_types(bindings::Dict, context::Context)::Context
  GAT.Context(((name => @match expr begin
    (Expr(:call, sym::Symbol, args...) =>
      Expr(:call, replace_symbols(bindings, sym), args...))
    sym::Symbol => replace_symbols(bindings, sym)
  end) for (name, expr) in context))
end

""" Remove type parameters from dependent type.
"""
function strip_type(expr)::Symbol
  @match expr begin
    Expr(:call, head::Symbol, args...) => head
    sym::Symbol => sym
  end
end

# GAT expressions
#################

""" Expand context variables that occur implicitly in an expression.

Reference: (Cartmell, 1986, Sec 10: 'Informal syntax').
"""
function expand_in_context(expr, params::Vector{Symbol},
                           context::Context, theory::Theory)
  @match expr begin
    Expr(:call, name::Symbol, args...) => begin
      expanded = [expand_in_context(e, params, context, theory) for e in args]
      Expr(:call, name, expanded...)
    end
    name::Symbol => begin
      if name in params
        name
      elseif haskey(context, name)
        expand_symbol_in_context(name, params, context, theory)
      else
        error("Name $name missing from context $context")
      end
    end
    _ => throw(ParseError("Ill-formed raw expression $expr"))
  end
end
function expand_symbol_in_context(sym::Symbol, params::Vector{Symbol},
                                  context::Context, theory::Theory)
  # This code expands symbols that occur as direct arguments to type
  # constructors. If there are term constructors in between, it does not work:
  # indeed, it cannot work in general because the term constructors are not
  # necessarily injective. For example, we can expand :X in
  #   (:X => :Ob, :f => :(Hom(X)))
  # but not in
  #   (:X => :Ob, :Y => :Ob, :f => :(Hom(otimes(X,Y))))
  names = collect(keys(context))
  start = findfirst(names .== sym)
  for name in names[start+1:end]
    expr = context[name]
    if isa(expr, Expr) && expr.head == :call && sym in expr.args[2:end]
      cons = get_type(theory, expr.args[1])
      accessor = cons.params[findfirst(expr.args[2:end] .== sym)]
      expanded = Expr(:call, accessor, name)
      return expand_in_context(expanded, params, context, theory)
    end
  end
  error("Name $sym does not occur explicitly among $params in context $context")
end

""" Expand context variables that occur implicitly in the type expression
of a term constructor.
"""
function expand_term_type(cons::TermConstructor, theory::Theory)
  isa(cons.typ, Symbol) ? cons.typ :
    expand_in_context(cons.typ, cons.params, cons.context, theory)
end

""" Implicit equations defined by a context.

This function allows a generalized algebraic theory (GAT) to be expressed as
an essentially algebraic theory, i.e., as partial functions whose domains are
defined by equations.

References:
 - (Cartmell, 1986, Sec 6: "Essentially algebraic theories and categories with
    finite limits")
 - (Freyd, 1972, "Aspects of topoi")
"""
function equations(context::Context, theory::Theory)::Vector{Pair}
  # The same restrictions as `expand_symbol_in_context` apply here.
  eqs = Pair[]
  names = collect(keys(context))
  for (start, var) in enumerate(names)
    for name in names[start+1:end]
      expr = context[name]
      if isa(expr, Symbol) && !has_type(theory, expr)
        # If the constructor is a symbol and there isn't a matching type in
        # the theory, assume it's a Julia type. For now, these are
        # completely ignored by the syntax system.
        continue
      end
      expr = isa(expr, Symbol) ? Expr(:call, expr) : expr
      cons = get_type(theory, expr.args[1])
      accessors = cons.params[findall(expr.args[2:end] .== var)]
      append!(eqs, (Expr(:call, a, name) => var for a in accessors))
    end
  end
  eqs
end

""" Implicit equations defined by context, allowing for implicit variables.
"""
function equations(params::Vector{Symbol}, context::Context,
                   theory::Theory)::Vector{Pair}
  eqs = [ (expand_in_context(lhs, params, context, theory) =>
           expand_in_context(rhs, params, context, theory))
          for (lhs, rhs) in equations(context, theory) ]
  # Remove tautologies (expr == expr) resulting from expansions.
  # FIXME: Should we worry about redundancies from the symmetry of equality,
  # i.e., (expr1 == expr2) && (expr2 == expr1)?
  filter(eq -> eq.first != eq.second, eqs)
end

""" Implicit equations for term constructor.
"""
function equations(cons::TermConstructor, theory::Theory)::Vector{Pair}
  equations(cons.params, cons.context, theory)
end

# Instances
###########

""" Define an *instance* of a generalized algebraic theory (GAT).
"""
macro instance(head, body)
  # Parse the instance definition.
  head = parse_theory_binding_either(head)
  functions, ext_functions = parse_instance_body(body)

  # We must generate and evaluate the code at *run time* because the theory
  # type is not defined at *parse time*.
  # Also, we "throw away" any docstring.
  # FIXME: Is there a better place to put the docstring?
  expr = :(instance_code($(esc(head.name)), $(esc(head.params)), $functions, $ext_functions))
  Expr(:block,
    Expr(:call, esc(:eval), expr),
    :(Core.@__doc__ abstract type $(esc(gensym(:instance_doc))) end)) # /dev/null
end
function instance_code(theory_type, instance_types, instance_fns, external_fns)
  code = Expr(:block)
  theory = GAT.theory(theory_type)
  bindings = Dict(zip([type.name for type in theory.types], instance_types))
  bound_fns = [ replace_symbols(bindings, f) for f in interface(theory) ]
  bound_fns = OrderedDict(parse_function_sig(f) => f for f in bound_fns)
  instance_fns = Dict(parse_function_sig(f) => f for f in instance_fns)
  for (sig, f) in bound_fns
    if sig.name in external_fns
      continue
    elseif haskey(instance_fns, sig)
      f_impl = instance_fns[sig]
    elseif !isnothing(f.impl)
      f_impl = f
    else
      error("Method $(f.call_expr) not implemented in $(nameof(mod)) instance")
    end
    push!(code.args, generate_function(f_impl))
  end
  return code
end

""" Parse the body of a GAT instance definition.
"""
function parse_instance_body(expr::Expr)
  @assert expr.head == :block
  funs = JuliaFunction[]
  ext_funs = Symbol[]
  for elem in strip_lines(expr).args
    elem = strip_lines(elem)
    head = elem.head
    if head == :macrocall && elem.args[1] == Symbol("@import")
      ext_funs = @match elem.args[2] begin
        sym::Symbol => [ext_funs; [sym]]
        Expr(:tuple, args...) => [ext_funs; Symbol[args...]]
      end
    else
      push!(funs, parse_function(elem))
    end
  end
  return (funs, ext_funs)
end

""" Complete set of Julia functions for a theory.
"""
function interface(theory::Theory)::Vector{JuliaFunction}
  [ accessors(theory);
    constructors(theory);
    alias_functions(theory) ]
end

""" Julia functions for type parameter accessors.
"""
function accessors(theory::Theory)::Vector{JuliaFunction}
  vcat(map(accessors, theory.types)...)
end
function accessors(cons::TypeConstructor)::Vector{JuliaFunction}
  [ JuliaFunction(Expr(:call, param, Expr(:(::), cons.name)),
                  strip_type(cons.context[param]))
    for param in cons.params ]
end

""" Julia functions for term constructors of GAT.
"""
function constructors(theory::Theory)::Vector{JuliaFunction}
  [ constructor(cons, theory) for cons in theory.terms ]
end
function constructor(cons::Union{TypeConstructor,TermConstructor},
                     theory::Theory)::JuliaFunction
  arg_names = cons.params
  arg_types = [ strip_type(cons.context[name]) for name in arg_names ]
  args = [ Expr(:(::), name, typ) for (name,typ) in zip(arg_names, arg_types) ]
  return_type = cons isa TermConstructor ? strip_type(cons.typ) : cons.name

  call_expr = Expr(:call, cons.name, args...)
  if !any(has_type(theory, typ) for typ in arg_types)
    call_expr = add_type_dispatch(call_expr, return_type)
  end
  JuliaFunction(call_expr, return_type)
end

""" Julia functions for term and type aliases of GAT.
"""
function alias_functions(theory::Theory)::Vector{JuliaFunction}
  # collect all of the types and terms from the theory
  terms_types = [theory.types; theory.terms]
  # iterate over the specified aliases
  collect(Iterators.flatten(map(collect(theory.aliases)) do alias
    # collect all of the destination function definitions to alias
    # allows an alias to overite all the type definitions of a function
    dests = filter(i -> i.name == last(alias), map(x -> x, terms_types))
    # If there are no matching functions, throw a parse error
    if isempty(dests)
      throw(ParseError("Cannot alias undefined type or term $alias"))
    end
    # For each destination, create a Julia function
    map(dests) do dest
      fun = constructor(dest, theory)
      fun.call_expr.args[1] = first(alias)
      # Extract arguments from function header, handling special case of
      # created by `add_type_dispatch`.
      args = map(fun.call_expr.args[2:end]) do arg
        @match arg begin
          # Special case: dispatch on return type.
          Expr(:(::), Expr(:curly, :Type, type)) => type
          # Main case: typed parameter.
          Expr(:(::), param, type) => param
          _ => throw(ParseError("Cannot parse argument $arg for alias $alias"))
        end
      end
      body = Expr(:call, dest.name, args...)
      JuliaFunction(fun.call_expr, fun.return_type, body)
    end
  end))
end

""" Invoke a term constructor by name on an instance.

This method provides reflection for GATs. In everyday use the generic
method for the constructor should be called directly, not through this function.

Cf. Julia's builtin `invoke()` function.
"""
function invoke_term(theory_type::Type, instance_types::Tuple,
                     constructor_name::Symbol, args...)
  # Get the corresponding Julia method from the parent module.
  method = getfield(parentmodule(theory_type), constructor_name)
  args = collect(Any, args)

  # Add dispatch on return type, if necessary.
  if !any(typeof(arg) <: typ for typ in instance_types for arg in args)
    # Case 1: Name refers to type constructor, e.g., generator constructor
    # in syntax system.
    theory = GAT.theory(theory_type)
    index = findfirst(cons -> cons.name == constructor_name, theory.types)
    if isnothing(index)
      # Case 2: Name refers to term constructor.
      # FIXME: Terms constructors can be overloaded, so there may be multiple
      # term constructors with the same name. Distinguishing them requires type
      # inference. I am punting on that right now.
      constructor = theory.terms[
        findfirst(cons -> cons.name == constructor_name, theory.terms)]
      return_name = strip_type(constructor.typ)
      index = findfirst(cons -> cons.name == return_name, theory.types)
    end
    insert!(args, 1, instance_types[index])
  end

  # Invoke the method!
  method(args...)
end

end


text:
export ft_getdriverversion

function ft_getdriverversion(ft_handle::Culong)
  driverversion = Ref{Cuint}()
  ft_status = ccall((:FT_GetDriverVersion, d2xx),
                     Cuint,
                     (Culong, Ref{Cuint}),
                     ft_handle, driverversion)
  checkstatus(ft_status)
  build = (driverversion[] & 0x000000ff)
  minor = (driverversion[] & 0x0000ff00)>>8
  major = (driverversion[] & 0x00ff0000)>>16
  return VersionNumber(major,minor,build)
end

ext: jl


text:
#
# Tests for the Pacanowski-Philander module
#

function test_pp_basic()
    model = PacanowskiPhilander.Model(N=4, L=2)
    model.grid.N == 4
end

function test_pp_diffusion_cosine()
    parameters = PacanowskiPhilander.Parameters(Cκ₀=1.0, Cκ₁=0.0)
    model = PacanowskiPhilander.Model(N=100, L=π/2, parameters=parameters)
    z = model.grid.zc

    c_init(z) = cos(2z)
    c_ans(z, t) = exp(-4t) * c_init(z)

    model.solution.T = c_init

    dt = 1e-3
    iterate!(model, dt)

    # The error tolerance is a bit arbitrary.
    norm(c_ans.(z, time(model)) .- data(model.solution.T)) < model.grid.N*1e-6
end

@testset "Pacanowski-Philander" begin
    @test test_pp_basic()
    @test test_pp_diffusion_cosine()
end


text:
module AdaptiveFilter

    using LinearAlgebra

    function LMS(u, d, p, μ)

        # Initialize
        y = zeros(size(u, 1))
        e = zeros(size(u, 1))
        w = zeros(p)
        N = size(u, 1) - p - 1

        for n = 1:N
            x = reverse(u[n:n + p - 1])
            y[n] = x ⋅ w
            e[n] = d[n + p] .- y[n]

            w = w .+ μ .* x .* e[n]

            y[n] = x ⋅ w
        end

        return y, e, w
    end

    function NLMS(u, d, p, μ; ϵ = 0.001)

        # Initialize
        y = zeros(size(u, 1))
        e = zeros(size(u, 1))
        w = zeros(p)
        N = size(u, 1) - p - 1

        for n = 1:N
            x = reverse(u[n:n + p - 1])
            y[n] = x ⋅ w
            e[n] = d[n + p] .- y[n]

            normFactor = 1. / ((x ⋅ x) + ϵ)
            w = w .+ μ * normFactor .* x .* e[n]

            y[n] = x ⋅ w
        end

        return y, e, w
    end
end


text:
@warn "Opt.jl is not compatible with functors. See https://discourse.julialang.org/t/functor-usage-for-optim-jl/45778"

"""
Optimization module for finding minimum and maximum of given function with single input (f(x))
"""
module Opt

using Optim
using Parameters

export find_min, find_max, find_MinMax

@with_kw struct optRes
    f_opt
    x_arg
    summary
end

@with_kw struct minmax
    f_min
    f_max
    x_amin
    x_amax
    summary
end

"""
Find minimimum of function within constraint domain

# Argument
- f::`function`: function with single input. That is f(x) = y
- domain::`vector{Number, 2}`: searching domain [lower, upper].

# Return
- optRes
"""
function find_min(f, domain; method=BFGS())
    x_lower, x_upper = domain[1], domain[2]
    res = Optim.optimize(f, x_lower, x_upper, method)
    f_min = res.minimum
    x_amin = res.minimizer

    # Warning if the result is not converged
    if !res.converged
        @warn "Minimization failed. See summary\n$(res)"
    end

    return optRes(f_opt=f_min, x_arg=x_amin, summary=res)
end

"""
Find maximum of function within constraint domain

# Argument
- f::`function`: function with single input. That is f(x) = y
- domain::`vector{Number, 2}`: searching domain [lower, upper].
"""
function find_max(f, domain)
    inv_f(x; f=f) = f(x)*-1.0
    opres_min = find_min(inv_f, domain)
    opres_max = optRes(opres_min,
                       f_opt = -1.0 *opres_min.f_opt)
    return opres_max
end

function find_MinMax(f, domain)
    res_min = find_min(f, domain)
    res_max = find_max(f, domain)

    res = minmax(
            f_min=res_min.f_opt,
            f_max=res_max.f_opt,
            x_amin=res_min.x_arg,
            x_amax=res_max.x_arg,
            summary = @with_kw (min=res_min.summary, max=res_max.summary)
    )

    return res
end

end


text:
#
# Function to plot the energy function
#

using Printf
using Plots
using LaTeXStrings

function plotU(input :: MDInput, filename :: String; size=[400,400], xlim=[0.,-1.], ylim=[0.,-1.], n=100)

  ENV["GKSwstype"]="nul"

  if xlim[2] < 0.
    xlim[1] = input.sig/5.
    xlim[2] = input.cutoff
  end

  x = collect(xlim[1]:(xlim[2]-xlim[1])/n:xlim[2])
  U = zeros(length(x))

  for i in 1:n
    if input.uf == "uf_QUAD!"
      if x[i] < input.sig
        U[i] = input.eps*(input.sig-x[i])^2
      end
    else
      U[i] = input.eps^4*( input.sig^12 / x[i]^12 - input.sig^6 / x[i]^6 )
    end
  end

  if ylim[2] < 0.
    if input.uf == "uf_QUAD!"
      ymin = minimum(U)
      ymax = maximum(U)
      ylim = [ ymin-0.05*(ymax-ymin), ymin+1.05*(ymax-ymin) ]
    else
      ymin = minimum(U)
      ymax = -2*ymin
      ylim = [ ymin-0.05*(ymax-ymin), ymin+1.05*(ymax-ymin) ]
    end
  end

  plot(size=size,ylabel="U(r)",xlabel="r",framestyle=:box)
  plot!(x,U,label="",linewidth=2,xlim=xlim,ylim=ylim)

  fontsize=10
  x = 0.60*(xlim[2]-xlim[1]) + xlim[1]
  d = 0.05*(ymax-ymin) ; y = [ 0.90*ylim[2] + d ]
  if input.uf == "uf_QUAD!"
    uftype = "Quadratic"
    annotate!(x,yd!(y,d),text(uftype,:left,fontsize,:serif,:black))
    note = raw"\sigma = "*"$(input.sig)"
    annotate!(x,yd!(y,d),text(latexstring(note),:left,fontsize,:serif,:black))
  else
    uftype = "Lennard-Jones"
    annotate!(x,yd!(y,d),text(uftype,:left,fontsize,:serif,:black))
    note = raw"\sigma = "*"$(input.sig)"
    annotate!(x,yd!(y,d),text(latexstring(note),:left,fontsize,:serif,:black))
    note = raw"\varepsilon = "*"$(input.eps)"
    annotate!(x,yd!(y,d),text(latexstring(note),:left,fontsize,:serif,:black))
  end

  savefig(filename)

end


text:
# TheDancer.jl
# Simple Julia codes to play with 1D Polaron propagation
# By Jarvist Moore Frost and Rose Teague (2017--2018)

# These codes simulate the formation of a Polaron in a 1D Tight-Binding model.
# The model is an |N> site model, where the sites are expected to be ~molecular~ units in real space.
# The response of the lattice is modelled as a dipole response, for the polarisation of the dielectric modes.
# Electronic structure is by tight-binding, with site energies perturbed by the response of the lattice,
# and a parameterised 'transfer integral' for the kinetic energy between nearest neighbour sites =#

module TheDancer
println("\t\"He came riding fast, like a phoenix out of fire-flames.\" -- The Dancer, PJ Harvey" )

export nondispersive_wavepacket, planewave, randH, decompose_H
export SiteEnergyFromDipoles, FieldFromDensity, UpdateDipole, dipoles_by_relaxation
export AdiabaticPropagation, UnitaryPropagation, TimeDependentPropagation
export plot_S_psi_density_dipoles, plot_H, outputpng, overlap
export SCFthenUnitary, init!, prepare_model

const UsePlots=true
using Plots
gr()

include("initialise.jl")
include("models.jl")
include("propagators.jl")
include("simulations.jl")

end


text:
using ConvolutionTools
using Base.Test

import Base.isapprox

function isapprox{T, N}(a::Array{T, N}, b::Array{T, N}, eps=10e-10)
    if size(a) != size(b)
        return false
    end
    el_diff = sum(abs(a .- b)) / prod(size(a))
    return el_diff < eps
end

function test_pad_const_1d()
    a = rand(3)
    n = [1]
    c = 2.3
    res = pad_const(a, n, c)
    return length(res) == 5 && res[1] == c && res[end] == c && typeof(res) == typeof(a)
end

function test_pad_const_2d()
    a = rand(3, 2)
    n = [1, 3]
    res = pad_const(a, n)
    return size(res) == (5, 8)
end

function test_1d()
    a = rand((7))
    b = rand((2))
    res = ConvolutionTools.conv_valid(a, b)
    return ndims(res) == 1 && eltype(res) <: Number
end

function test_2d()
    a = rand((6, 7))
    b = rand((1, 2))
    res = ConvolutionTools.conv_valid(a, b)
    return ndims(res) == 2 && eltype(res) <: Number
end

function test_3d()
    a = rand((6, 7, 10))
    b = rand((1, 2, 3))
    res = ConvolutionTools.conv_valid(a, b)
    return ndims(res) == 3 && eltype(res) <: Number
end

function test_identity()
    a = [i^2 for i in 1:100]
    b = [1]
    res = ConvolutionTools.conv_valid(a, b)
    return all(a .== res)
end

function test_zero()
    a = float([i^2 for i in 1:100])
    b = float([0])
    res = ConvolutionTools.conv_valid(a, b)
    return all(res .== zero(eltype(res)))
end

function test_derivative()
    n = 10
    a = float([0.5 * i^2 for i in 1:n])
    b = float([1, 0, -1]) / 2
    res = ConvolutionTools.conv_valid(a, b)
    ref = float([i for i in 2:(n-1)])
    return isapprox(ref, res)
end

function test_integers()
    n = 10
    a = [i for i in 1:n]
    b = [1, 0, -1]
    res = ConvolutionTools.conv_valid(a, b)
    return all(res .== 2)
end

function test_mixed_input()
    a = [i for i in 1:10]
    b = float([1, 2, 3])
    res1 = ConvolutionTools.conv(a, b)
    res2 = ConvolutionTools.conv(b, a)
    return isapprox(res1, res2)
end

custom_handler(r::Test.Success) = print(".")
custom_handler(r::Test.Failure) = Test.default_handler(r)
custom_handler(r::Test.Error)   = Test.default_handler(r)

Test.with_handler(custom_handler) do
    @test test_pad_const_1d()
    @test test_pad_const_2d()
    @test test_1d()
    @test test_2d()
    @test test_3d()
    @test test_zero()
    @test test_identity()
    @test test_derivative()
    @test test_integers()
    @test test_mixed_input()
end

println("")


text:
# v0.6

using Tk

w = Toplevel("Example")

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
"""
$(TYPEDEF)
"""
struct LinearProblem{uType,isinplace,F,bType,P,K} <: AbstractLinearProblem{bType,isinplace}
    A::F
    b::bType
    u0::uType
    p::P
    kwargs::K
    @add_kwonly function LinearProblem{iip}(A,b,p=NullParameters();u0=nothing,
                                            kwargs...) where iip
        new{typeof(u0),iip,typeof(A),typeof(b),typeof(p),typeof(kwargs)}(
            A,b,u0,p,kwargs
        )
    end
end

function LinearProblem(A,b,args...;kwargs...)
    if A isa AbstractArray
        LinearProblem{true}(DiffEqArrayOperator(A),b,args...;kwargs...)
    else
        LinearProblem{isinplace(A, 4)}(A,b,args...;kwargs...)
    end
end

"""
$(TYPEDEF)
"""
struct NonlinearProblem{uType,isinplace,P,F,K} <: AbstractNonlinearProblem{uType,isinplace}
    f::F
    u0::uType
    p::P
    kwargs::K
    @add_kwonly function NonlinearProblem{iip}(f,u0,p=NullParameters();kwargs...) where iip
        new{typeof(u0),iip,typeof(p),typeof(f),typeof(kwargs)}(f,u0,p,kwargs)
    end
end

NonlinearProblem(f,u0,args...;kwargs...) = NonlinearProblem{isinplace(f, 3)}(f,u0,args...;kwargs...)

"""
$(TYPEDEF)
"""
struct QuadratureProblem{isinplace,P,F,L,U,K} <: AbstractQuadratureProblem{isinplace}
    f::F
    lb::L
    ub::U
    nout::Int
    p::P
    batch::Int
    kwargs::K
    @add_kwonly function QuadratureProblem{iip}(f,lb,ub,p=NullParameters();
                                                nout=1,
                                                batch = 0, kwargs...) where iip
        new{iip,typeof(p),typeof(f),typeof(lb),
            typeof(ub),typeof(kwargs)}(f,lb,ub,nout,p,batch,kwargs)
    end
end

QuadratureProblem(f,lb,ub,args...;kwargs...) = QuadratureProblem{isinplace(f, 3)}(f,lb,ub,args...;kwargs...)

"""
$(TYPEDEF)
"""
struct OptimizationProblem{isinplace,F,uType,BType,P,K} <: AbstractOptimizationProblem{isinplace}
    f::F
    u0::uType
    lb::BType
    ub::BType
    p::P
    kwargs::K

    @add_kwonly function OptimizationProblem{iip}(f,p=NullParameters();
                                                  u0 = nothing,
                                                  lb = nothing,
                                                  ub = nothing,
                                                  kwargs...) where iip
        new{iip,typeof(f),typeof(u0),typeof(lb),typeof(p),
            typeof(kwargs)}(f,u0,lb,ub,p,kwargs)
    end
end

OptimizationProblem(f,args...;kwargs...) = OptimizationProblem{false}(f,args...;kwargs...)


text:
struct SqWasserstein <: DistributionsSemiMetric end

# result type (e.g., for pairwise computations)
function Distances.result_type(
    ::SqWasserstein, ::Type{T1}, ::Type{T2}
) where {T1<:Real,T2<:Real}
    return promote_type(T1, T2)
end

# evaluations for normal distributions
function (::SqWasserstein)(a::Normal, b::Normal)
    μa, σa = params(a)
    μb, σb = params(b)
    return abs2(μa - μb) + abs2(σa - σb)
end

function (::SqWasserstein)(a::AbstractMvNormal, b::AbstractMvNormal)
    μ1 = mean(a)
    μ2 = mean(b)
    Σ1 = cov(a)
    Σ2 = cov(b)
    return Distances.sqeuclidean(μ1, μ2) + OT.sqbures(Σ1, Σ2)
end

function (::SqWasserstein)(a::MvNormal, b::MvNormal)
    μa, Σa = params(a)
    μb, Σb = params(b)
    return Distances.sqeuclidean(μa, μb) + OT.sqbures(Σa, Σb)
end

# evaluations for Laplace distributions
function (::SqWasserstein)(a::Laplace, b::Laplace)
    μa, βa = params(a)
    μb, βb = params(b)
    return abs2(μa - μb) + 2 * abs2(βa - βb)
end

# Wasserstein 2 distance
struct Wasserstein <: DistributionsMetric end

# result type (e.g., for pairwise computations)
function Distances.result_type(
    ::Wasserstein, ::Type{T1}, ::Type{T2}
) where {T1<:Real,T2<:Real}
    return float(promote_type(T1, T2))
end

function (::Wasserstein)(a::Distribution, b::Distribution)
    return sqrt(SqWasserstein()(a, b))
end

# Mixture Wasserstein distances
struct SqMixtureWasserstein{S} <: DistributionsSemiMetric
    lpsolver::S
end
struct MixtureWasserstein{S} <: DistributionsMetric
    lpsolver::S
end
SqMixtureWasserstein() = SqMixtureWasserstein(Tulip.Optimizer())
MixtureWasserstein() = MixtureWasserstein(Tulip.Optimizer())

# result type (e.g., for pairwise computations)
function Distances.result_type(
    ::SqMixtureWasserstein, ::Type{T1}, ::Type{T2}
) where {T1<:Real,T2<:Real}
    return promote_type(T1, T2)
end
function Distances.result_type(
    ::MixtureWasserstein, ::Type{T1}, ::Type{T2}
) where {T1<:Real,T2<:Real}
    return float(promote_type(T1, T2))
end

function (s::SqMixtureWasserstein)(a::AbstractMixtureModel, b::AbstractMixtureModel)
    C = Distances.pairwise(SqWasserstein(), components(a), components(b))
    return OT.emd2(probs(a), probs(b), C, deepcopy(s.lpsolver))
end

function (m::MixtureWasserstein)(a::AbstractMixtureModel, b::AbstractMixtureModel)
    return sqrt(SqMixtureWasserstein(m.lpsolver)(a, b))
end


text:
using LSH
using Base.Test

# A basic test for LSHTable with a dummy hash function

# To be explicit, we're constructing a test with the following
# parameters to make sure the basic plumbing work (this is not
# supposed to be useful)
#
# - T𝒫 === Int
# - H === DummyHash
# - RH === Uint32
# - DK === Uint32

const P = uint32(uint64(2)^32 - 5)

immutable AdditiveDummyHash <: HashFunction
    z::Int
end
call(T::AdditiveDummyHash,arg) = (T.z+arg)%Int32
LSH.dimension(T::AdditiveDummyHash) = 1

# Store 10 random integers in a table with k=1, L=10
LSHTable([AdditiveDummyHash(z) for z in rand(Int,10)],rand(Int,10),Int32,Int32)


text:
using GraphMatFun, LsqFit, LinearAlgebra, Random
include("exp_reset_all.jl");
#include("simulationtools.jl");
include("simulationtools.jl");

f=exp
m=5
target_n=200 # Measure error wrt this discr
n=50;        # Optimize wrt this discr
rho=1.9;  # Increased SID rho m=5

its=5;
droptol0=1e-10;
opt_kwargs=Dict(:logger=> 0,:γ0 => 0.5,:droptol => droptol0,
                :linlsqr => :real_svd,:maxit => its)
base = init_state_mult(f,rho,n,m; eltype=Complex{BigFloat})
base.params[:n]=n;
base.params[:target_n]=target_n;
base.params[:opt_kwargs]=opt_kwargs;

oldrho="1_68";
datadir=joinpath("..","..","data","exp");
# First the optimization free
filename=joinpath(datadir,"exp_sid_m$(m).cgr");
sid_org=init_state_file!(deepcopy(base),:sid,filename,showmeta=true)

filename=joinpath(datadir, "exp_bbc_m$(m).cgr");
bbc_org=init_state_file!(deepcopy(base),:bbc,filename,showmeta=true)

filename=joinpath(datadir,"exp_sastre_m$(m-1).cgr");
sastre_org=init_state_file!(deepcopy(base),:sastre,filename,showmeta=true,scale_and_square=true)

#sastre_org.cref=get_degopt_crefs(sastre_org.graph);

filename=joinpath(datadir,"exp_ps_m$(m)_rho$(oldrho).cgr");
ps_org=init_state_file!(deepcopy(base),:ps,filename,showmeta=true)

filename=joinpath(datadir,"exp_mono_m$(m)_rho$(oldrho).cgr");
mono_org=init_state_file!(deepcopy(base),:mono,filename,showmeta=true)

filename=joinpath(datadir,"exp_mono_m$(m)_opt_rho$oldrho.cgr");
mono0=init_state_file!(deepcopy(base),:mono,filename,showmeta=true)
(mono,simlist,commandlist)=
     run_sequence(mono0,"SddddddddddSSdddSsDDGGGSSdddSkSSzSSSsSddSggggggSSSSSSSSSSSSSGGGGGGSSSddSSSSkSKKKKKsssSzq")

filename=joinpath(datadir,"exp_ps_m$(m)_opt_rho$oldrho.cgr");
ps0=init_state_file!(deepcopy(base),:ps,filename,showmeta=true)
(ps,simlist,commandlist)=
     run_sequence(ps0,
     "sNsddddddddddddddsdddddSSzSkkSSSSSSSzdddSSGGGSSdddSdSggggggggkSSSGGGGGGGGSGSSzq");

filename=joinpath(datadir,"exp_sastre_m$(m)_opt_rho$oldrho.cgr");
sastre0=init_state_file!(deepcopy(base),:sastre,filename,showmeta=true)
(sastre,simlist,commandlist)=
run_sequence(sastre0,
             "skssssssssssdddddsssddddsssssssdddssssssssddddssssdddssssssssssdddsssssssssdddssssssssssszq");
#                  "skssssssssssdddddsssddddsssssssdddssssssssddddssssdddssssssssssdddsssssssssdddsssssssssssddssssssssssssssq");

filename=joinpath(datadir,"exp_sid_m$(m)_opt_rho$oldrho.cgr");
sid0=init_state_file!(deepcopy(base),:sid,filename,showmeta=true)
(sid,simlist,commandlist)=
     run_sequence(sid0,
     "sdddddsdddddssssssddddsssssssdddsdddsssssssssddddsddsssssNsssddssssssssssssszq")

filename=joinpath(datadir,"exp_bbc_m$(m)_opt_rho$oldrho.cgr");
bbc0=init_state_file!(deepcopy(base),:bbc,filename,showmeta=true)
(bbc,simlist,commandlist)=
     run_sequence(bbc0,
     "sdddddsdddddssssssddddsssssssddddsssssssssssssdddssssssssssddddssssssssdsggggggsdssssssssssssssssGGGGGsssGGGssszq")

include("exp_print_all.jl");
println("Run include(exp_save_all.jl) if you want to save");

max_stars_repo_name: matrixfunctions/GraphMatFunData

max_issues_repo_name: matrixfunctions/GraphMatFunData

max_forks_repo_name: matrixfunctions/GraphMatFunData


text:
@testset "Testing IO" begin
    # Create an InputHandler and an OutputHandler. Connect the two of them
    # together and make sure that data gets piped from the input to the output.

    fifo = AsapSim.DualClockFifo(Int64,10)

    input = AsapSim.InputHandler{Int64,typeof(fifo)}(output = fifo)
    output = AsapSim.OutputHandler{Int64,typeof(fifo)}(input = fifo)

    input.data = rand(Int64, 100)

    for i in 1:200
        update!(output)
        update!(input)
    end

    @test output.data == input.data
end


text:
# This file is auto-generated by AWSMetadata.jl
using AWS
using AWS.AWSServices: athena
using AWS.Compat
using AWS.UUIDs

"""
    batch_get_named_query(named_query_ids)
    batch_get_named_query(named_query_ids, params::Dict{String,<:Any})

Returns the details of a single named query or a list of up to 50 queries, which you
provide as an array of query ID strings. Requires you to have access to the workgroup in
which the queries were saved. Use ListNamedQueriesInput to get the list of named query IDs
in the specified workgroup. If information could not be retrieved for a submitted query ID,
information about the query ID submitted is listed under UnprocessedNamedQueryId. Named
queries differ from executed queries. Use BatchGetQueryExecutionInput to get details about
each unique query execution, and ListQueryExecutionsInput to get a list of query execution
IDs.

# Arguments
- `named_query_ids`: An array of query IDs.

"""
function batch_get_named_query(
    NamedQueryIds; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "BatchGetNamedQuery",
        Dict{String,Any}("NamedQueryIds" => NamedQueryIds);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function batch_get_named_query(
    NamedQueryIds,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "BatchGetNamedQuery",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("NamedQueryIds" => NamedQueryIds), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    batch_get_query_execution(query_execution_ids)
    batch_get_query_execution(query_execution_ids, params::Dict{String,<:Any})

Returns the details of a single query execution or a list of up to 50 query executions,
which you provide as an array of query execution ID strings. Requires you to have access to
the workgroup in which the queries ran. To get a list of query execution IDs, use
ListQueryExecutionsInputWorkGroup. Query executions differ from named (saved) queries. Use
BatchGetNamedQueryInput to get details about named queries.

# Arguments
- `query_execution_ids`: An array of query execution IDs.

"""
function batch_get_query_execution(
    QueryExecutionIds; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "BatchGetQueryExecution",
        Dict{String,Any}("QueryExecutionIds" => QueryExecutionIds);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function batch_get_query_execution(
    QueryExecutionIds,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "BatchGetQueryExecution",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("QueryExecutionIds" => QueryExecutionIds), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    create_data_catalog(name, type)
    create_data_catalog(name, type, params::Dict{String,<:Any})

Creates (registers) a data catalog with the specified name and properties. Catalogs created
are visible to all users of the same Amazon Web Services account.

# Arguments
- `name`: The name of the data catalog to create. The catalog name must be unique for the
  Amazon Web Services account and can use a maximum of 128 alphanumeric, underscore, at sign,
  or hyphen characters.
- `type`: The type of data catalog to create: LAMBDA for a federated catalog, HIVE for an
  external hive metastore, or GLUE for an Glue Data Catalog.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Description"`: A description of the data catalog to be created.
- `"Parameters"`: Specifies the Lambda function or functions to use for creating the data
  catalog. This is a mapping whose values depend on the catalog type.    For the HIVE data
  catalog type, use the following syntax. The metadata-function parameter is required. The
  sdk-version parameter is optional and defaults to the currently supported version.
  metadata-function=lambda_arn, sdk-version=version_number     For the LAMBDA data catalog
  type, use one of the following sets of required parameters, but not both.   If you have one
  Lambda function that processes metadata and another for reading the actual data, use the
  following syntax. Both parameters are required.  metadata-function=lambda_arn,
  record-function=lambda_arn      If you have a composite Lambda function that processes both
  metadata and data, use the following syntax to specify your Lambda function.
  function=lambda_arn       The GLUE type takes a catalog ID parameter and is required. The
  catalog_id  is the account ID of the Amazon Web Services account to which the Glue Data
  Catalog belongs.  catalog-id=catalog_id     The GLUE data catalog type also applies to the
  default AwsDataCatalog that already exists in your account, of which you can have only one
  and cannot modify.   Queries that specify a Glue Data Catalog other than the default
  AwsDataCatalog must be run on Athena engine version 2.   In Regions where Athena engine
  version 2 is not available, creating new Glue data catalogs results in an INVALID_INPUT
  error.
- `"Tags"`: A list of comma separated tags to add to the data catalog that is created.
"""
function create_data_catalog(Name, Type; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "CreateDataCatalog",
        Dict{String,Any}("Name" => Name, "Type" => Type);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function create_data_catalog(
    Name,
    Type,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "CreateDataCatalog",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("Name" => Name, "Type" => Type), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    create_named_query(database, name, query_string)
    create_named_query(database, name, query_string, params::Dict{String,<:Any})

Creates a named query in the specified workgroup. Requires that you have access to the
workgroup. For code samples using the Amazon Web Services SDK for Java, see Examples and
Code Samples in the Amazon Athena User Guide.

# Arguments
- `database`: The database to which the query belongs.
- `name`: The query name.
- `query_string`: The contents of the query with all query statements.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"ClientRequestToken"`: A unique case-sensitive string used to ensure the request to
  create the query is idempotent (executes only once). If another CreateNamedQuery request is
  received, the same response is returned and another query is not created. If a parameter
  has changed, for example, the QueryString, an error is returned.  This token is listed as
  not required because Amazon Web Services SDKs (for example the Amazon Web Services SDK for
  Java) auto-generate the token for users. If you are not using the Amazon Web Services SDK
  or the Amazon Web Services CLI, you must provide this token or the action will fail.
- `"Description"`: The query description.
- `"WorkGroup"`: The name of the workgroup in which the named query is being created.
"""
function create_named_query(
    Database, Name, QueryString; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "CreateNamedQuery",
        Dict{String,Any}(
            "Database" => Database,
            "Name" => Name,
            "QueryString" => QueryString,
            "ClientRequestToken" => string(uuid4()),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function create_named_query(
    Database,
    Name,
    QueryString,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "CreateNamedQuery",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "Database" => Database,
                    "Name" => Name,
                    "QueryString" => QueryString,
                    "ClientRequestToken" => string(uuid4()),
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    create_prepared_statement(query_statement, statement_name, work_group)
    create_prepared_statement(query_statement, statement_name, work_group, params::Dict{String,<:Any})

Creates a prepared statement for use with SQL queries in Athena.

# Arguments
- `query_statement`: The query string for the prepared statement.
- `statement_name`: The name of the prepared statement.
- `work_group`: The name of the workgroup to which the prepared statement belongs.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Description"`: The description of the prepared statement.
"""
function create_prepared_statement(
    QueryStatement,
    StatementName,
    WorkGroup;
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "CreatePreparedStatement",
        Dict{String,Any}(
            "QueryStatement" => QueryStatement,
            "StatementName" => StatementName,
            "WorkGroup" => WorkGroup,
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function create_prepared_statement(
    QueryStatement,
    StatementName,
    WorkGroup,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "CreatePreparedStatement",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "QueryStatement" => QueryStatement,
                    "StatementName" => StatementName,
                    "WorkGroup" => WorkGroup,
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    create_work_group(name)
    create_work_group(name, params::Dict{String,<:Any})

Creates a workgroup with the specified name.

# Arguments
- `name`: The workgroup name.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Configuration"`: The configuration for the workgroup, which includes the location in
  Amazon S3 where query results are stored, the encryption configuration, if any, used for
  encrypting query results, whether the Amazon CloudWatch Metrics are enabled for the
  workgroup, the limit for the amount of bytes scanned (cutoff) per query, if it is
  specified, and whether workgroup's settings (specified with EnforceWorkGroupConfiguration)
  in the WorkGroupConfiguration override client-side settings. See
  WorkGroupConfigurationEnforceWorkGroupConfiguration.
- `"Description"`: The workgroup description.
- `"Tags"`: A list of comma separated tags to add to the workgroup that is created.
"""
function create_work_group(Name; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "CreateWorkGroup",
        Dict{String,Any}("Name" => Name);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function create_work_group(
    Name, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "CreateWorkGroup",
        Dict{String,Any}(mergewith(_merge, Dict{String,Any}("Name" => Name), params));
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    delete_data_catalog(name)
    delete_data_catalog(name, params::Dict{String,<:Any})

Deletes a data catalog.

# Arguments
- `name`: The name of the data catalog to delete.

"""
function delete_data_catalog(Name; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "DeleteDataCatalog",
        Dict{String,Any}("Name" => Name);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function delete_data_catalog(
    Name, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "DeleteDataCatalog",
        Dict{String,Any}(mergewith(_merge, Dict{String,Any}("Name" => Name), params));
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    delete_named_query(named_query_id)
    delete_named_query(named_query_id, params::Dict{String,<:Any})

Deletes the named query if you have access to the workgroup in which the query was saved.
For code samples using the Amazon Web Services SDK for Java, see Examples and Code Samples
in the Amazon Athena User Guide.

# Arguments
- `named_query_id`: The unique ID of the query to delete.

"""
function delete_named_query(NamedQueryId; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "DeleteNamedQuery",
        Dict{String,Any}("NamedQueryId" => NamedQueryId);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function delete_named_query(
    NamedQueryId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "DeleteNamedQuery",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("NamedQueryId" => NamedQueryId), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    delete_prepared_statement(statement_name, work_group)
    delete_prepared_statement(statement_name, work_group, params::Dict{String,<:Any})

Deletes the prepared statement with the specified name from the specified workgroup.

# Arguments
- `statement_name`: The name of the prepared statement to delete.
- `work_group`: The workgroup to which the statement to be deleted belongs.

"""
function delete_prepared_statement(
    StatementName, WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "DeletePreparedStatement",
        Dict{String,Any}("StatementName" => StatementName, "WorkGroup" => WorkGroup);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function delete_prepared_statement(
    StatementName,
    WorkGroup,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "DeletePreparedStatement",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "StatementName" => StatementName, "WorkGroup" => WorkGroup
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    delete_work_group(work_group)
    delete_work_group(work_group, params::Dict{String,<:Any})

Deletes the workgroup with the specified name. The primary workgroup cannot be deleted.

# Arguments
- `work_group`: The unique name of the workgroup to delete.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"RecursiveDeleteOption"`: The option to delete the workgroup and its contents even if
  the workgroup contains any named queries or query executions.
"""
function delete_work_group(WorkGroup; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "DeleteWorkGroup",
        Dict{String,Any}("WorkGroup" => WorkGroup);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function delete_work_group(
    WorkGroup,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "DeleteWorkGroup",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("WorkGroup" => WorkGroup), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_data_catalog(name)
    get_data_catalog(name, params::Dict{String,<:Any})

Returns the specified data catalog.

# Arguments
- `name`: The name of the data catalog to return.

"""
function get_data_catalog(Name; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "GetDataCatalog",
        Dict{String,Any}("Name" => Name);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_data_catalog(
    Name, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "GetDataCatalog",
        Dict{String,Any}(mergewith(_merge, Dict{String,Any}("Name" => Name), params));
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_database(catalog_name, database_name)
    get_database(catalog_name, database_name, params::Dict{String,<:Any})

Returns a database object for the specified database and data catalog.

# Arguments
- `catalog_name`: The name of the data catalog that contains the database to return.
- `database_name`: The name of the database to return.

"""
function get_database(
    CatalogName, DatabaseName; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "GetDatabase",
        Dict{String,Any}("CatalogName" => CatalogName, "DatabaseName" => DatabaseName);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_database(
    CatalogName,
    DatabaseName,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "GetDatabase",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "CatalogName" => CatalogName, "DatabaseName" => DatabaseName
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_named_query(named_query_id)
    get_named_query(named_query_id, params::Dict{String,<:Any})

Returns information about a single query. Requires that you have access to the workgroup in
which the query was saved.

# Arguments
- `named_query_id`: The unique ID of the query. Use ListNamedQueries to get query IDs.

"""
function get_named_query(NamedQueryId; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "GetNamedQuery",
        Dict{String,Any}("NamedQueryId" => NamedQueryId);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_named_query(
    NamedQueryId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "GetNamedQuery",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("NamedQueryId" => NamedQueryId), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_prepared_statement(statement_name, work_group)
    get_prepared_statement(statement_name, work_group, params::Dict{String,<:Any})

Retrieves the prepared statement with the specified name from the specified workgroup.

# Arguments
- `statement_name`: The name of the prepared statement to retrieve.
- `work_group`: The workgroup to which the statement to be retrieved belongs.

"""
function get_prepared_statement(
    StatementName, WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "GetPreparedStatement",
        Dict{String,Any}("StatementName" => StatementName, "WorkGroup" => WorkGroup);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_prepared_statement(
    StatementName,
    WorkGroup,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "GetPreparedStatement",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "StatementName" => StatementName, "WorkGroup" => WorkGroup
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_query_execution(query_execution_id)
    get_query_execution(query_execution_id, params::Dict{String,<:Any})

Returns information about a single execution of a query if you have access to the workgroup
in which the query ran. Each time a query executes, information about the query execution
is saved with a unique ID.

# Arguments
- `query_execution_id`: The unique ID of the query execution.

"""
function get_query_execution(
    QueryExecutionId; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "GetQueryExecution",
        Dict{String,Any}("QueryExecutionId" => QueryExecutionId);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_query_execution(
    QueryExecutionId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "GetQueryExecution",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("QueryExecutionId" => QueryExecutionId), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_query_results(query_execution_id)
    get_query_results(query_execution_id, params::Dict{String,<:Any})

Streams the results of a single query execution specified by QueryExecutionId from the
Athena query results location in Amazon S3. For more information, see Query Results in the
Amazon Athena User Guide. This request does not execute the query but returns results. Use
StartQueryExecution to run a query. To stream query results successfully, the IAM principal
with permission to call GetQueryResults also must have permissions to the Amazon S3
GetObject action for the Athena query results location.  IAM principals with permission to
the Amazon S3 GetObject action for the query results location are able to retrieve query
results from Amazon S3 even if permission to the GetQueryResults action is denied. To
restrict user or role access, ensure that Amazon S3 permissions to the Athena query
location are denied.

# Arguments
- `query_execution_id`: The unique ID of the query execution.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of results (rows) to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
function get_query_results(
    QueryExecutionId; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "GetQueryResults",
        Dict{String,Any}("QueryExecutionId" => QueryExecutionId);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_query_results(
    QueryExecutionId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "GetQueryResults",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("QueryExecutionId" => QueryExecutionId), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_table_metadata(catalog_name, database_name, table_name)
    get_table_metadata(catalog_name, database_name, table_name, params::Dict{String,<:Any})

Returns table metadata for the specified catalog, database, and table.

# Arguments
- `catalog_name`: The name of the data catalog that contains the database and table
  metadata to return.
- `database_name`: The name of the database that contains the table metadata to return.
- `table_name`: The name of the table for which metadata is returned.

"""
function get_table_metadata(
    CatalogName, DatabaseName, TableName; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "GetTableMetadata",
        Dict{String,Any}(
            "CatalogName" => CatalogName,
            "DatabaseName" => DatabaseName,
            "TableName" => TableName,
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_table_metadata(
    CatalogName,
    DatabaseName,
    TableName,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "GetTableMetadata",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "CatalogName" => CatalogName,
                    "DatabaseName" => DatabaseName,
                    "TableName" => TableName,
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_work_group(work_group)
    get_work_group(work_group, params::Dict{String,<:Any})

Returns information about the workgroup with the specified name.

# Arguments
- `work_group`: The name of the workgroup.

"""
function get_work_group(WorkGroup; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "GetWorkGroup",
        Dict{String,Any}("WorkGroup" => WorkGroup);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_work_group(
    WorkGroup,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "GetWorkGroup",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("WorkGroup" => WorkGroup), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_data_catalogs()
    list_data_catalogs(params::Dict{String,<:Any})

Lists the data catalogs in the current Amazon Web Services account.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: Specifies the maximum number of data catalogs to return.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
function list_data_catalogs(; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "ListDataCatalogs"; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end
function list_data_catalogs(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "ListDataCatalogs", params; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end

"""
    list_databases(catalog_name)
    list_databases(catalog_name, params::Dict{String,<:Any})

Lists the databases in the specified data catalog.

# Arguments
- `catalog_name`: The name of the data catalog that contains the databases to return.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: Specifies the maximum number of results to return.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
function list_databases(CatalogName; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "ListDatabases",
        Dict{String,Any}("CatalogName" => CatalogName);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_databases(
    CatalogName,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "ListDatabases",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("CatalogName" => CatalogName), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_engine_versions()
    list_engine_versions(params::Dict{String,<:Any})

Returns a list of engine versions that are available to choose from, including the Auto
option.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of engine versions to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
function list_engine_versions(; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "ListEngineVersions"; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end
function list_engine_versions(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "ListEngineVersions", params; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end

"""
    list_named_queries()
    list_named_queries(params::Dict{String,<:Any})

Provides a list of available query IDs only for queries saved in the specified workgroup.
Requires that you have access to the specified workgroup. If a workgroup is not specified,
lists the saved queries for the primary workgroup. For code samples using the Amazon Web
Services SDK for Java, see Examples and Code Samples in the Amazon Athena User Guide.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of queries to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
- `"WorkGroup"`: The name of the workgroup from which the named queries are being returned.
  If a workgroup is not specified, the saved queries for the primary workgroup are returned.
"""
function list_named_queries(; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "ListNamedQueries"; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end
function list_named_queries(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "ListNamedQueries", params; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end

"""
    list_prepared_statements(work_group)
    list_prepared_statements(work_group, params::Dict{String,<:Any})

Lists the prepared statements in the specfied workgroup.

# Arguments
- `work_group`: The workgroup to list the prepared statements for.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of results to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
function list_prepared_statements(
    WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "ListPreparedStatements",
        Dict{String,Any}("WorkGroup" => WorkGroup);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_prepared_statements(
    WorkGroup,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "ListPreparedStatements",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("WorkGroup" => WorkGroup), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_query_executions()
    list_query_executions(params::Dict{String,<:Any})

Provides a list of available query execution IDs for the queries in the specified
workgroup. If a workgroup is not specified, returns a list of query execution IDs for the
primary workgroup. Requires you to have access to the workgroup in which the queries ran.
For code samples using the Amazon Web Services SDK for Java, see Examples and Code Samples
in the Amazon Athena User Guide.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of query executions to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
- `"WorkGroup"`: The name of the workgroup from which queries are being returned. If a
  workgroup is not specified, a list of available query execution IDs for the queries in the
  primary workgroup is returned.
"""
function list_query_executions(; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "ListQueryExecutions"; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end
function list_query_executions(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "ListQueryExecutions",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_table_metadata(catalog_name, database_name)
    list_table_metadata(catalog_name, database_name, params::Dict{String,<:Any})

Lists the metadata for the tables in the specified data catalog database.

# Arguments
- `catalog_name`: The name of the data catalog for which table metadata should be returned.
- `database_name`: The name of the database for which table metadata should be returned.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Expression"`: A regex filter that pattern-matches table names. If no expression is
  supplied, metadata for all tables are listed.
- `"MaxResults"`: Specifies the maximum number of results to return.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
function list_table_metadata(
    CatalogName, DatabaseName; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "ListTableMetadata",
        Dict{String,Any}("CatalogName" => CatalogName, "DatabaseName" => DatabaseName);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_table_metadata(
    CatalogName,
    DatabaseName,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "ListTableMetadata",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "CatalogName" => CatalogName, "DatabaseName" => DatabaseName
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_tags_for_resource(resource_arn)
    list_tags_for_resource(resource_arn, params::Dict{String,<:Any})

Lists the tags associated with an Athena workgroup or data catalog resource.

# Arguments
- `resource_arn`: Lists the tags for the resource with the specified ARN.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of results to be returned per request that lists the
  tags for the resource.
- `"NextToken"`: The token for the next set of results, or null if there are no additional
  results for this request, where the request lists the tags for the resource with the
  specified ARN.
"""
function list_tags_for_resource(
    ResourceARN; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "ListTagsForResource",
        Dict{String,Any}("ResourceARN" => ResourceARN);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_tags_for_resource(
    ResourceARN,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "ListTagsForResource",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("ResourceARN" => ResourceARN), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_work_groups()
    list_work_groups(params::Dict{String,<:Any})

Lists available workgroups for the account.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of workgroups to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
function list_work_groups(; aws_config::AbstractAWSConfig=global_aws_config())
    return athena("ListWorkGroups"; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET)
end
function list_work_groups(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "ListWorkGroups", params; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end

"""
    start_query_execution(query_string)
    start_query_execution(query_string, params::Dict{String,<:Any})

Runs the SQL query statements contained in the Query. Requires you to have access to the
workgroup in which the query ran. Running queries against an external catalog requires
GetDataCatalog permission to the catalog. For code samples using the Amazon Web Services
SDK for Java, see Examples and Code Samples in the Amazon Athena User Guide.

# Arguments
- `query_string`: The SQL query statements to be executed.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"ClientRequestToken"`: A unique case-sensitive string used to ensure the request to
  create the query is idempotent (executes only once). If another StartQueryExecution request
  is received, the same response is returned and another query is not created. If a parameter
  has changed, for example, the QueryString, an error is returned.  This token is listed as
  not required because Amazon Web Services SDKs (for example the Amazon Web Services SDK for
  Java) auto-generate the token for users. If you are not using the Amazon Web Services SDK
  or the Amazon Web Services CLI, you must provide this token or the action will fail.
- `"QueryExecutionContext"`: The database within which the query executes.
- `"ResultConfiguration"`: Specifies information about where and how to save the results of
  the query execution. If the query runs in a workgroup, then workgroup's settings may
  override query settings. This affects the query results location. The workgroup settings
  override is specified in EnforceWorkGroupConfiguration (true/false) in the
  WorkGroupConfiguration. See WorkGroupConfigurationEnforceWorkGroupConfiguration.
- `"WorkGroup"`: The name of the workgroup in which the query is being started.
"""
function start_query_execution(
    QueryString; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "StartQueryExecution",
        Dict{String,Any}(
            "QueryString" => QueryString, "ClientRequestToken" => string(uuid4())
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function start_query_execution(
    QueryString,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "StartQueryExecution",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "QueryString" => QueryString, "ClientRequestToken" => string(uuid4())
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    stop_query_execution(query_execution_id)
    stop_query_execution(query_execution_id, params::Dict{String,<:Any})

Stops a query execution. Requires you to have access to the workgroup in which the query
ran. For code samples using the Amazon Web Services SDK for Java, see Examples and Code
Samples in the Amazon Athena User Guide.

# Arguments
- `query_execution_id`: The unique ID of the query execution to stop.

"""
function stop_query_execution(
    QueryExecutionId; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "StopQueryExecution",
        Dict{String,Any}("QueryExecutionId" => QueryExecutionId);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function stop_query_execution(
    QueryExecutionId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "StopQueryExecution",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("QueryExecutionId" => QueryExecutionId), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    tag_resource(resource_arn, tags)
    tag_resource(resource_arn, tags, params::Dict{String,<:Any})

Adds one or more tags to an Athena resource. A tag is a label that you assign to a
resource. In Athena, a resource can be a workgroup or data catalog. Each tag consists of a
key and an optional value, both of which you define. For example, you can use tags to
categorize Athena workgroups or data catalogs by purpose, owner, or environment. Use a
consistent set of tag keys to make it easier to search and filter workgroups or data
catalogs in your account. For best practices, see Tagging Best Practices. Tag keys can be
from 1 to 128 UTF-8 Unicode characters, and tag values can be from 0 to 256 UTF-8 Unicode
characters. Tags can use letters and numbers representable in UTF-8, and the following
characters: + - = . _ : / @. Tag keys and values are case-sensitive. Tag keys must be
unique per resource. If you specify more than one tag, separate them by commas.

# Arguments
- `resource_arn`: Specifies the ARN of the Athena resource (workgroup or data catalog) to
  which tags are to be added.
- `tags`: A collection of one or more tags, separated by commas, to be added to an Athena
  workgroup or data catalog resource.

"""
function tag_resource(ResourceARN, Tags; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "TagResource",
        Dict{String,Any}("ResourceARN" => ResourceARN, "Tags" => Tags);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function tag_resource(
    ResourceARN,
    Tags,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "TagResource",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}("ResourceARN" => ResourceARN, "Tags" => Tags),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    untag_resource(resource_arn, tag_keys)
    untag_resource(resource_arn, tag_keys, params::Dict{String,<:Any})

Removes one or more tags from a data catalog or workgroup resource.

# Arguments
- `resource_arn`: Specifies the ARN of the resource from which tags are to be removed.
- `tag_keys`: A comma-separated list of one or more tag keys whose tags are to be removed
  from the specified resource.

"""
function untag_resource(
    ResourceARN, TagKeys; aws_config::AbstractAWSConfig=global_aws_config()
)
    return athena(
        "UntagResource",
        Dict{String,Any}("ResourceARN" => ResourceARN, "TagKeys" => TagKeys);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function untag_resource(
    ResourceARN,
    TagKeys,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "UntagResource",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}("ResourceARN" => ResourceARN, "TagKeys" => TagKeys),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    update_data_catalog(name, type)
    update_data_catalog(name, type, params::Dict{String,<:Any})

Updates the data catalog that has the specified name.

# Arguments
- `name`: The name of the data catalog to update. The catalog name must be unique for the
  Amazon Web Services account and can use a maximum of 128 alphanumeric, underscore, at sign,
  or hyphen characters.
- `type`: Specifies the type of data catalog to update. Specify LAMBDA for a federated
  catalog, HIVE for an external hive metastore, or GLUE for an Glue Data Catalog.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Description"`: New or modified text that describes the data catalog.
- `"Parameters"`: Specifies the Lambda function or functions to use for updating the data
  catalog. This is a mapping whose values depend on the catalog type.    For the HIVE data
  catalog type, use the following syntax. The metadata-function parameter is required. The
  sdk-version parameter is optional and defaults to the currently supported version.
  metadata-function=lambda_arn, sdk-version=version_number     For the LAMBDA data catalog
  type, use one of the following sets of required parameters, but not both.   If you have one
  Lambda function that processes metadata and another for reading the actual data, use the
  following syntax. Both parameters are required.  metadata-function=lambda_arn,
  record-function=lambda_arn      If you have a composite Lambda function that processes both
  metadata and data, use the following syntax to specify your Lambda function.
  function=lambda_arn
"""
function update_data_catalog(Name, Type; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "UpdateDataCatalog",
        Dict{String,Any}("Name" => Name, "Type" => Type);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function update_data_catalog(
    Name,
    Type,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "UpdateDataCatalog",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("Name" => Name, "Type" => Type), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    update_prepared_statement(query_statement, statement_name, work_group)
    update_prepared_statement(query_statement, statement_name, work_group, params::Dict{String,<:Any})

Updates a prepared statement.

# Arguments
- `query_statement`: The query string for the prepared statement.
- `statement_name`: The name of the prepared statement.
- `work_group`: The workgroup for the prepared statement.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Description"`: The description of the prepared statement.
"""
function update_prepared_statement(
    QueryStatement,
    StatementName,
    WorkGroup;
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "UpdatePreparedStatement",
        Dict{String,Any}(
            "QueryStatement" => QueryStatement,
            "StatementName" => StatementName,
            "WorkGroup" => WorkGroup,
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function update_prepared_statement(
    QueryStatement,
    StatementName,
    WorkGroup,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "UpdatePreparedStatement",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "QueryStatement" => QueryStatement,
                    "StatementName" => StatementName,
                    "WorkGroup" => WorkGroup,
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    update_work_group(work_group)
    update_work_group(work_group, params::Dict{String,<:Any})

Updates the workgroup with the specified name. The workgroup's name cannot be changed.

# Arguments
- `work_group`: The specified workgroup that will be updated.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"ConfigurationUpdates"`: The workgroup configuration that will be updated for the given
  workgroup.
- `"Description"`: The workgroup description.
- `"State"`: The workgroup state that will be updated for the given workgroup.
"""
function update_work_group(WorkGroup; aws_config::AbstractAWSConfig=global_aws_config())
    return athena(
        "UpdateWorkGroup",
        Dict{String,Any}("WorkGroup" => WorkGroup);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function update_work_group(
    WorkGroup,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return athena(
        "UpdateWorkGroup",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("WorkGroup" => WorkGroup), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end


text:
export
AABB,
overlaps,
inflate,
volume,
refit,
randAABB,
centre,
radius,
radius_squared

using Base.Iterators
using LinearAlgebra
import Base.union
import Base.isless

struct AABB{T}
  min::Vector3{T}
  max::Vector3{T}
end

AABB{T}() where T = AABB{T}(ones(Vector3{T}), ones(Vector3{T}))

function AABB( minx::T, maxx::T, miny::T, maxy::T, minz::T, maxz::T ) where {T}
  AABB(Vector3([minx, miny, minz]), Vector3([maxx, maxy, maxz]))
end

AABB( P::Vector{Point{T}} ) where {T} = reduce(union,(p->AABB(p,p)).(P))

function AABB( a::Point{T}, b::Point{T}, c::Point{T} ) where {T}
  @inbounds r = AABB(min(a[1], b[1], c[1]),
                     max(a[1], b[1], c[1]),
                     min(a[2], b[2], c[2]),
                     max(a[2], b[2], c[2]),
                     min(a[3], b[3], c[3]),
                     max(a[3], b[3], c[3]))
  r
end

function AABB( a::Vector3{T}, b::Vector3{T}, c::Vector3{T}, rest::Vector3{T}...) where {T}
  @inbounds r = AABB(min(a[1],b[1],c[1],map(p->p[1],rest)...), max(a[1],b[1],c[1],map(p->p[1],rest)...),
                     min(a[2],b[2],c[2],map(p->p[2],rest)...), max(a[2],b[2],c[2],map(p->p[2],rest)...),
                     min(a[3],b[3],c[3],map(p->p[3],rest)...), max(a[3],b[3],c[3],map(p->p[3],rest)...)); r
end

function refit( aabb::AABB{T} ) where T
  a = aabb.min
  b = aabb.max
  @inbounds AABB(Vector3(min(a[1],b[1]), min(a[2],b[2]), min(a[3],b[3])),
                 Vector3(max(a[1],b[1]), max(a[2],b[2]), max(a[3],b[3])))
end

randAABB(r = 0:0.001:1) = refit(AABB(rand(r), rand(r), rand(r), rand(r), rand(r), rand(r)))
Base.rand(::Type{AABB{Float64}}) = randAABB()

"""
"""
function union( a::AABB{T}, b::AABB{T} ) where {T}
  @inbounds @fastmath r = AABB(min(a.min[1],b.min[1]), max(a.max[1],b.max[1]),
                               min(a.min[2],b.min[2]), max(a.max[2],b.max[2]),
                               min(a.min[3],b.min[3]), max(a.max[3],b.max[3])); r
end

"""
check if all extents of a are < all extents of b.  if so, then disjoint.
"""
function isless(a::AABB{T}, b::AABB{T}) where {T}
  @inbounds @fastmath r = (a.max[1] < b.min[1]) || (a.max[2] < b.min[2]) || (a.max[3] < b.min[3]); r
end

function overlaps(a::AABB{T}, b::AABB{T}) where {T}
  @inbounds @fastmath r = !((a < b) || (b < a)); r
end

function contains(a::AABB{T}, b::AABB{T}) where {T}
  @inbounds @fastmath r = ((a.max[1] >= b.max[1]) && (a.min[1] <= b.min[1])) &&
  ((a.max[2] >= b.max[2]) && (a.min[2] <= b.min[2])) &&
  ((a.max[3] >= b.max[3]) && (a.min[3] <= b.min[3])); r
end

function inflate(a::AABB{T}, r::S) where {T,S}
  @inbounds @fastmath rv = Vector3([r,r,r])
  @inbounds @fastmath r = AABB( a.min-rv, a.max+rv ); r
end

function volume(a::AABB{T}) where {T}
  @inbounds @fastmath r = (a.max[1]-a.min[1])*(a.max[2]-a.min[2])*(a.max[3]-a.min[3]); r
end

function centre(a::AABB{T}) where {T}
  @inbounds @fastmath (a.max+a.min)*T(0.5)
end

function radius(a::AABB{T}) where {T}
  @inbounds @fastmath norm(a.max-a.min)*T(0.5)
end

function radius_squared(a::AABB{T}) where {T}
  @inbounds @fastmath LinearAlgebra.norm_sqr(a.max-a.min)*T(0.25)
end