-- |
-- Module      : Harmonic.Lib
-- Description : The single import for live coding with theHarmonicAlgorithm
--
-- = Overview
--
-- One import gives a performance script everything it needs: the generative
-- engine, the music-theory primitives it reasons over, and the TidalCycles
-- bridge that turns its output into sound.
--
-- @import Harmonic.Lib@
--
-- = Quick start
--
-- Generation reads as a chain of modifiers applied to a generator. Each
-- modifier narrows what the engine may choose; the generator at the end of
-- the chain runs the walk:
--
-- @
-- tempo = 87
--
-- ctx = invSkip 1
--     $ hcOvertones \"E A D G\"
--     $ hcKey \"2#\"
--     $ hContext
--
-- start \<- lead \"C maj\"
--
-- s \<- seek \"*\" $ cue start $ tonal ctx $ len 8 $ entropy 0.4 $ attempt 3 12 $ gen
-- @
--
-- 'seek' picks the corpus — @\"*\"@ for all composers, @\"bach\"@ for one,
-- @\"bach:30 debussy:70\"@ for a weighted blend, @\"none\"@ to run offline with
-- no Neo4j. 'attempt' generates several candidates and keeps the best.
--
-- The result is then played by describing a form and handing it to
-- instruments:
--
-- @
-- form = [ at 0 1.0 1.0 s ]
--
-- do
--   let k = iK tempo form (warp \"[1 2 3 4]\/4\")
--   mapM_ id [ hush, setbpm tempo
--            , p \"strings\" $ stack
--                [ violin1    T (0,1) k voiceLines flow Soprano
--                , cello      T (0,1) k voiceLines flow Tenor8vb
--                , contrabass T (0,1) k voiceLines grid Bass8vb
--                ]
--            ]
-- @
--
-- = Verbosity
--
-- Every generator has three tiers, marked by the prime suffix — the same
-- convention used throughout the library for tiers and variants:
--
-- * @gen@ — the chord grid only
-- * @gen'@ — per-step musical context and the grid
-- * @gen''@ — full traces, the grid, and the multi-attempt scoreboard
--
-- The tiers have identical types, so switching verbosity never changes the
-- surrounding code. The same holds for @genP@ \/ @genP'@ \/ @genP''@,
-- @genFrom@ \/ @genFrom'@ \/ @genFrom''@, and the Roman numeral aliases.
--
-- = Where to go next
--
-- * "Harmonic.Framework.Builder" — the generation engine and every modifier
-- * "Harmonic.Interface.Tidal.Orchestra" — the 15 orchestral instruments
-- * "Harmonic.Interface.Tidal.Arranger" — voicing strategies and rearranging
-- * "Harmonic.Interface.Tidal.Form" — form and kinetics
--
-- = Legacy positional interface
--
-- Predating the modifier chain, 'genSilent', 'genStandard' and 'genVerbose'
-- take their arguments positionally and share one signature:
--
-- > genSilent :: CadenceState -> Int -> String -> Double -> HarmonicContext -> IO Progression
--
-- They remain supported, but note that 'initCadenceState' silently truncates
-- chords of more than three pitch classes to a triad; 'lead' is the
-- better-behaved way to build a starting state.
--

-- = Academic Lineage
--
-- This project originated from three academic documents:
--
-- 1. /The Harmonic Algorithm/ (South, 2016) — MA thesis: exhaustive
--    combinatorial analysis of overtone harmonics on the Electric Contrabass
--    Cittern (EAeGB\/EAeGC tunings) and standard bass (EADG). Charts all
--    3-note overtone combinations over 12 chromatic bass notes.
--
-- 2. /Harmonic Algorithm Reflections/ (South, 2016) — companion reflective
--    document. Contains \"The Parting Glass\" arrangement score and documents
--    technique development including Three Point Playing and Overtone 5
--    discovery.
--
-- 3. /Data Science In The Creative Process/ (South, 2018) — DBS Higher
--    Diploma. Implements the algorithm computationally in Haskell using
--    Wiggins' Creative Systems Framework \<R,T,E\>. Resolves \"Generative
--    Uninspiration\" via Markov model trained on the YCACL Bach chorales.
--
-- = References
--
-- * South, O. (2016). /The Harmonic Algorithm/. MA thesis.
-- * South, O. (2016). /Harmonic Algorithm Reflections/.
-- * South, O. (2018). /Data Science In The Creative Process/. DBS.
-- * Wiggins, G.A. (2006). /A preliminary framework for description, analysis and comparison of creative systems/. Knowledge-Based Systems 19(7). — the R\/T\/E framework and failure taxonomy.
-- * Hindemith, P. (1937). /The Craft of Musical Composition/.
-- * Pastorius, J. (2001). /Modern Electric Bass/. — harmonic vocabulary on bass.
-- * Manring, M. — Hipshot re-tuner technique, multiple D-Tuners.
-- * Bailey, S. & Wooten, V. (1993). /Bass Extremes/. — artificial harmonics.
-- * McLean, A. (2007). /Improvising with Synthesised Vocables/. — TidalCycles origins.

module Harmonic.Lib (
  -- * Primary interface for live coding
  -- | The modifier-based generation API. Each generator comes in three
  -- verbosity tiers, marked by the prime suffix:
  --
  -- * @gen@ — header + grid output
  -- * @gen'@ — compact summary
  -- * @gen''@ — verbose traces
  --
  -- Modifiers compose right-to-left onto a generator:
  --
  -- @s \<- seek "*" $ attempt 3 12 $ entropy 0.4 $ gen@

  -- ** Triadic generation
  gen, gen', gen'',
  gen4, gen4', gen4'', quad,
  genGrid, genFrom, genFrom', genFrom'',

  -- ** The genP paradigm (strata-first)
  -- | Three-layer generation (triad \/ strata \/ mode). The roman-numeral
  -- aliases pin the starting tristrata.
  genP, genP', genP'',
  genI,   genII,   genIII,   genIV,   genV,   genVI,   genVII,   genVIII,   genIX,   genX,   genXI,
  genI',  genII',  genIII',  genIV',  genV',  genVI',  genVII',  genVIII',  genIX',  genX',  genXI',
  genI'', genII'', genIII'', genIV'', genV'', genVI'', genVII'', genVIII'', genIX'', genX'', genXI'',

  -- ** Generation modifiers
  cue, len, seek, entropy, tonal,
  relStrata, absStrata,
  sameBoost, flipBoost, triBoost,
  attempt, viability,

  -- ** Generation types
  GenConfig(..), GenMode(..), Verbosity(..),
  defaultGenConfig, execGenConfig, execGenConfigPC,

  -- ** Positional generation (legacy)
  genSilent, genStandard, genVerbose,
  genPrint, genPrint', genPrint'',

  -- * Context and configuration
  -- | A 'HarmonicContext' constrains what the generator may choose. Build one
  -- by chaining modifiers onto 'hContext':
  --
  -- @ctx = invSkip 1 $ hcOvertones "E A D G" $ hcPedal "E?" $ hContext@
  HarmonicContext(..), harmonicContext, hContext,
  Drift(..), hcOvertones, hcKey, hcRoots, dissonant, consonant, invSkip, hcPedal, hcTristrata,
  GeneratorConfig(..), defaultConfig,

  -- * Core music types
  module Harmonic.Rules.Types.Pitch,
  module Harmonic.Rules.Types.Harmony,
  module Harmonic.Evaluation.Scoring.Dissonance,
  module Harmonic.Rules.Constraints.Overtone,

  -- ** Voice leading (cyclic DP paradigms)
  voiceLeadingCost, totalCost, cyclicCost,
  voiceMovement, minimalMovement,
  allVoicings, initialCompact,
  solveRoot, solveFlow,

  -- ** Progressions and scales
  module Harmonic.Rules.Types.Progression,
  module Harmonic.Rules.Types.Scale,
  module Harmonic.Rules.Types.ProgressionContext,

  -- * Interactive behaviour
  module Harmonic.Traversal.Probabilistic,

  -- * Filter functions
  -- ** String-friendly versions for TidalCycles
  overtones, Harmonic.Rules.Constraints.Filter.key, funds, tuning, wildcard,
  -- ** Text versions
  parseOvertones, parseKey, parseFunds, parseTuning, isWildcard,
  -- ** Overtone annotation support
  parseTuningNamed,

  -- * Database interface
  module Harmonic.Evaluation.Database.Query,
  connectNeo4j,

  -- * Ingestion pipeline
  -- | Corpus ingestion. Not needed to play — see "Harmonic.Rules.Import.CSV".
  module Harmonic.Rules.Import.CSV,
  module Harmonic.Rules.Import.Transform,

  -- * TidalCycles interface
  -- ** Pattern-level operations
  VoiceFunction, voiceRange,
  arrange, arrange', parallel, warp, rep, lookupChordAt,
  lookupChord, lookupProgression,
  overlapF,

  -- ** Form and kinetics
  FormNode(..), FormTime(..), Transition(..), Kinetics(..), IK,
  at, at', rh, rh', iK, lK, formK,
  ki, slate, withForm,

  -- ** Arranger functions (voicing paradigms)
  rotate, excerpt, insert, switch, clone, extract,
  transposeP, Harmonic.Interface.Tidal.Arranger.reverse, fuse, fuse2, interleave, expandP,
  progOverlap, progOverlapF, progOverlapB,
  grid, flow, lite, literal, root,

  -- ** Explicit progression construction
  fromChords, prog,

  -- ** Groove interface (drums and sub bass)
  subKick, fund, noteoff,

  -- ** Walking-bass line interface
  lineHarmony,

  -- ** Scale source (switch mechanism)
  ScaleSource(..), melodyStateFrom,

  -- ** Starting state construction
  lead, lead', parseLeadTokens, LeadToken(..),

  -- ** Instruments and orchestra
  module Harmonic.Interface.Tidal.Instruments,
  module Harmonic.Interface.Tidal.Orchestra,
  module Harmonic.Interface.Tidal.Utils,
  renderTristrataReport, genPReport,
  module Harmonic.Config,

  -- * Internal (advanced use only)
  -- | Tuple-returning versions for manual diagnostics extraction.
  generate, generateWith, genWith,
  generate', genWith',
  generate'', genWith'',
  printDiagnostics,
  StepDiagnostic(..), GenerationDiagnostics(..),
  TransformTrace(..), AdvanceTrace(..),
  genSilent', genStandard', genVerbose'
) where

-- Phase B: Core Music Types
import Harmonic.Rules.Types.Pitch
import Harmonic.Rules.Types.Harmony
import Harmonic.Evaluation.Scoring.Dissonance
import Harmonic.Rules.Constraints.Overtone
import Harmonic.Evaluation.Scoring.VoiceLeading (voiceLeadingCost, totalCost, cyclicCost, voiceMovement, minimalMovement, allVoicings, initialCompact, solveRoot, solveFlow)
import Harmonic.Rules.Types.Progression
import Harmonic.Rules.Types.Scale
import Harmonic.Rules.Types.ProgressionContext
-- Phase C: Interactive Behaviour
import Harmonic.Traversal.Probabilistic
import Harmonic.Framework.Builder (
    -- Modifier-based API
    gen, gen', gen'',
    gen4, gen4', gen4'', quad,
    genGrid, genFrom, genFrom', genFrom'',
    -- genP paradigm (strata-first)
    genP, genP', genP'',
    genI,   genII,   genIII,   genIV,   genV,   genVI,   genVII,   genVIII,   genIX,   genX,   genXI,
    genI',  genII',  genIII',  genIV',  genV',  genVI',  genVII',  genVIII',  genIX',  genX',  genXI',
    genI'', genII'', genIII'', genIV'', genV'', genVI'', genVII'', genVIII'', genIX'', genX'', genXI'',
    cue, len, seek, entropy, tonal,
    relStrata, absStrata,
    sameBoost, flipBoost, triBoost,
  attempt, viability,
    GenConfig(..), GenMode(..), Verbosity(..),
    defaultGenConfig, execGenConfig, execGenConfigPC,
    -- Positional API
    genPrint, genPrint', genPrint'',
    generate, generateWith, genWith,
    generate', genWith',
    generate'', genWith'',
    genSilent, genStandard, genVerbose,
    genSilent', genStandard', genVerbose',
    printDiagnostics,
    -- Context & types
    HarmonicContext(..), harmonicContext, hContext,
    Drift(..), hcOvertones, hcKey, hcRoots, dissonant, consonant, invSkip, hcPedal, hcTristrata,
    GeneratorConfig(..), defaultConfig,
    StepDiagnostic(..), GenerationDiagnostics(..), TransformTrace(..), AdvanceTrace(..)
  )
import Harmonic.Rules.Constraints.Filter (overtones, key, funds, tuning, wildcard, parseOvertones, parseKey, parseFunds, parseTuning, isWildcard, parseTuningNamed)
import Harmonic.Evaluation.Database.Query
-- Infrastructure (selective imports to avoid conflicts)
import Harmonic.Rules.Import.Graph (connectNeo4j)
import Harmonic.Rules.Import.CSV
import Harmonic.Rules.Import.Transform
import Harmonic.Interface.Tidal.Bridge (
    VoiceFunction, voiceRange,
    arrange, arrange', parallel, warp, rep, lookupChordAt,
    lookupChord, lookupProgression,
    overlapF
  )
import Harmonic.Interface.Tidal.Arranger (
    rotate, excerpt, insert, switch, clone, extract,
    transposeP, reverse, fuse, fuse2, interleave, expandP,
    progOverlap, progOverlapF, progOverlapB,
    grid, flow, lite, literal, root,
    fromChords, prog,
    ScaleSource(..), melodyStateFrom,
    lead, lead', parseLeadTokens, LeadToken(..)
  )
import Harmonic.Interface.Tidal.Groove (subKick, fund, noteoff)
import Harmonic.Interface.Tidal.LineHarmony (lineHarmony)
import Harmonic.Interface.Tidal.Form (
    FormNode(..), FormTime(..), Transition(..), Kinetics(..), IK,
    at, at', rh, rh', iK, lK, formK,
    ki, slate, withForm
  )
import Harmonic.Interface.Tidal.Instruments
import Harmonic.Interface.Tidal.Orchestra
import Harmonic.Interface.Tidal.Utils
import Harmonic.Interface.Tidal.OctatripentatonicT (renderTristrataReport, genPReport)
import Harmonic.Config