{-# LANGUAGE ConstraintKinds       #-}
{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE DeriveAnyClass        #-}
{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE DerivingVia           #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds             #-}
{-# LANGUAGE PatternSynonyms       #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE StandaloneDeriving    #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE UndecidableInstances  #-}

module Brassica.SoundChange.Types
       (
       -- * Words and graphemes
         Grapheme(..)
       , PWord
       , addBoundaries
       , removeBoundaries
       , concatWithBoundary
       -- * Lexemes
       , Lexeme(..)
       , pattern Boundary
       , LexemeType(..)
       , generalise
       -- * Categories
       , mapCategory
       , mapCategoryA
       , Expanded(..)
       , generaliseExpanded
       -- * Rules
       , Rule(..)
       , Environment
       , Direction(..)
       , Sporadicity(..)
       , Flags(..)
       , defFlags
       -- * Statements
       , Filter(..)
       , Statement(..)
       , plaintext'
       , SoundChanges
       -- * Directives
       , CategoryModification(..)
       , CategorySpec(..)
       , FeatureSpec(..)
       , CategoryDefinition(..)
       , Directive(..)
       ) where

import Control.DeepSeq (NFData(..))
import Data.String (IsString(..))
import GHC.Generics (Generic)
import GHC.OldList (dropWhileEnd)

-- | The type of graphemes within a word.
data Grapheme
    = GMulti [Char]  -- ^ A multigraph: for instance @GMulti "a", GMulti "ch", GMulti "c̓" :: t'Grapheme'@.
    | GBoundary      -- ^ A non-letter element representing a word boundary which sound changes can manipulate
    deriving (Grapheme -> Grapheme -> Bool
(Grapheme -> Grapheme -> Bool)
-> (Grapheme -> Grapheme -> Bool) -> Eq Grapheme
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Grapheme -> Grapheme -> Bool
== :: Grapheme -> Grapheme -> Bool
$c/= :: Grapheme -> Grapheme -> Bool
/= :: Grapheme -> Grapheme -> Bool
Eq, Eq Grapheme
Eq Grapheme =>
(Grapheme -> Grapheme -> Ordering)
-> (Grapheme -> Grapheme -> Bool)
-> (Grapheme -> Grapheme -> Bool)
-> (Grapheme -> Grapheme -> Bool)
-> (Grapheme -> Grapheme -> Bool)
-> (Grapheme -> Grapheme -> Grapheme)
-> (Grapheme -> Grapheme -> Grapheme)
-> Ord Grapheme
Grapheme -> Grapheme -> Bool
Grapheme -> Grapheme -> Ordering
Grapheme -> Grapheme -> Grapheme
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Grapheme -> Grapheme -> Ordering
compare :: Grapheme -> Grapheme -> Ordering
$c< :: Grapheme -> Grapheme -> Bool
< :: Grapheme -> Grapheme -> Bool
$c<= :: Grapheme -> Grapheme -> Bool
<= :: Grapheme -> Grapheme -> Bool
$c> :: Grapheme -> Grapheme -> Bool
> :: Grapheme -> Grapheme -> Bool
$c>= :: Grapheme -> Grapheme -> Bool
>= :: Grapheme -> Grapheme -> Bool
$cmax :: Grapheme -> Grapheme -> Grapheme
max :: Grapheme -> Grapheme -> Grapheme
$cmin :: Grapheme -> Grapheme -> Grapheme
min :: Grapheme -> Grapheme -> Grapheme
Ord, Int -> Grapheme -> ShowS
[Grapheme] -> ShowS
Grapheme -> [Char]
(Int -> Grapheme -> ShowS)
-> (Grapheme -> [Char]) -> ([Grapheme] -> ShowS) -> Show Grapheme
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Grapheme -> ShowS
showsPrec :: Int -> Grapheme -> ShowS
$cshow :: Grapheme -> [Char]
show :: Grapheme -> [Char]
$cshowList :: [Grapheme] -> ShowS
showList :: [Grapheme] -> ShowS
Show, (forall x. Grapheme -> Rep Grapheme x)
-> (forall x. Rep Grapheme x -> Grapheme) -> Generic Grapheme
forall x. Rep Grapheme x -> Grapheme
forall x. Grapheme -> Rep Grapheme x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Grapheme -> Rep Grapheme x
from :: forall x. Grapheme -> Rep Grapheme x
$cto :: forall x. Rep Grapheme x -> Grapheme
to :: forall x. Rep Grapheme x -> Grapheme
Generic, Grapheme -> ()
(Grapheme -> ()) -> NFData Grapheme
forall a. (a -> ()) -> NFData a
$crnf :: Grapheme -> ()
rnf :: Grapheme -> ()
NFData)

instance IsString Grapheme where
    fromString :: [Char] -> Grapheme
fromString = [Char] -> Grapheme
GMulti

-- | A word (or a subsequence of one) can be viewed as a list of
-- @Grapheme@s: e.g. Portuguese "filha" becomes
-- @["f", "i", "lh", "a"] :: 'PWord'@.
--
-- (The name 'PWord' is from ‘phonological word’, these being what a
-- SCA typically manipulates; this name was chosen to avoid a clash
-- with @Prelude.'Prelude.Word'@.)
type PWord = [Grapheme]

-- Add a 'GBoundary' at the beginning and end of the 'PWord'.
addBoundaries :: PWord -> PWord
addBoundaries :: [Grapheme] -> [Grapheme]
addBoundaries [Grapheme]
w = Grapheme
GBoundary Grapheme -> [Grapheme] -> [Grapheme]
forall a. a -> [a] -> [a]
: [Grapheme]
w [Grapheme] -> [Grapheme] -> [Grapheme]
forall a. [a] -> [a] -> [a]
++ [Grapheme
GBoundary]

-- Remove 'GBoundary's from the beginning and end of the 'PWord'.
removeBoundaries :: PWord -> PWord
removeBoundaries :: [Grapheme] -> [Grapheme]
removeBoundaries = (Grapheme -> Bool) -> [Grapheme] -> [Grapheme]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Grapheme -> Grapheme -> Bool
forall a. Eq a => a -> a -> Bool
==Grapheme
GBoundary) ([Grapheme] -> [Grapheme])
-> ([Grapheme] -> [Grapheme]) -> [Grapheme] -> [Grapheme]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Grapheme -> Bool) -> [Grapheme] -> [Grapheme]
forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd (Grapheme -> Grapheme -> Bool
forall a. Eq a => a -> a -> Bool
==Grapheme
GBoundary)

-- | Render a 'PWord' as a 'String'. Very much like 'concat', but
-- treating 'GBoundary's specially. Word-external boundaries are
-- deleted, while word-internal boundaries are converted to @"#"@.
concatWithBoundary :: PWord -> String
concatWithBoundary :: [Grapheme] -> [Char]
concatWithBoundary = [Grapheme] -> [Char]
go ([Grapheme] -> [Char])
-> ([Grapheme] -> [Grapheme]) -> [Grapheme] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Grapheme] -> [Grapheme]
removeBoundaries
  where
    go :: [Grapheme] -> [Char]
go = (Grapheme -> [Char]) -> [Grapheme] -> [Char]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((Grapheme -> [Char]) -> [Grapheme] -> [Char])
-> (Grapheme -> [Char]) -> [Grapheme] -> [Char]
forall a b. (a -> b) -> a -> b
$ \case
        GMulti [Char]
g -> [Char]
g
        Grapheme
GBoundary -> [Char]
"#"

-- | The part of a 'Rule' in which a 'Lexeme' may occur: in a matched
-- part (target or environment), in replacement, or in either of
-- those.
data LexemeType = Matched | Replacement | AnyPart

-- | A 'Lexeme' is the smallest part of a sound change. Both matches
-- and replacements are made up of 'Lexeme's: the phantom type
-- variable @a@ specifies where each different variety of 'Lexeme' may
-- occur. 'Lexeme's are also parameterised by their category type,
-- which may be 'Expanded' or something else.
data Lexeme category (a :: LexemeType) where
    -- | In Brassica sound-change syntax, one or more letters without intervening whitespace,
    -- or a word boundary specified as @#@
    Grapheme :: Grapheme -> Lexeme category a
    -- | In Brassica sound-change syntax, delimited by square brackets
    Category :: category a -> Lexeme category a
    -- | In Brassica sound-change syntax, delimited by parentheses
    Optional :: [Lexeme category a] -> Lexeme category a
    -- | In Brassica sound-change syntax, specified as @\@
    Metathesis :: Lexeme category 'Replacement
    -- | In Brassica sound-change syntax, specified as @>@
    Geminate :: Lexeme category a
    -- | In Brassica sound-change syntax, specified as @^@ before another 'Lexeme'
    Wildcard :: Lexeme category a -> Lexeme category a
    -- | In Brassica sound-change syntax, specified as @*@ after another 'Lexeme'
    Kleene   :: Lexeme category a -> Lexeme category a
    -- | In Brassica sound-change syntax, specified as @~@
    Discard  :: Lexeme category 'Replacement
    -- | In Brassica sound-change syntax, specified as \@i before a category
    Backreference :: Int -> category a -> Lexeme category a
    -- | In Brassica sound-change syntax, specified as \@? before a category
    Multiple :: category 'Replacement -> Lexeme category 'Replacement

mapCategory :: (forall x. c x -> c' x) -> Lexeme c a -> Lexeme c' a
mapCategory :: forall (c :: LexemeType -> *) (c' :: LexemeType -> *)
       (a :: LexemeType).
(forall (x :: LexemeType). c x -> c' x)
-> Lexeme c a -> Lexeme c' a
mapCategory forall (x :: LexemeType). c x -> c' x
_ (Grapheme Grapheme
g) = Grapheme -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Grapheme -> Lexeme category a
Grapheme Grapheme
g
mapCategory forall (x :: LexemeType). c x -> c' x
f (Category c a
c) = c' a -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
category a -> Lexeme category a
Category (c a -> c' a
forall (x :: LexemeType). c x -> c' x
f c a
c)
mapCategory forall (x :: LexemeType). c x -> c' x
f (Optional [Lexeme c a]
ls) = [Lexeme c' a] -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
[Lexeme category a] -> Lexeme category a
Optional ((forall (x :: LexemeType). c x -> c' x)
-> Lexeme c a -> Lexeme c' a
forall (c :: LexemeType -> *) (c' :: LexemeType -> *)
       (a :: LexemeType).
(forall (x :: LexemeType). c x -> c' x)
-> Lexeme c a -> Lexeme c' a
mapCategory c x -> c' x
forall (x :: LexemeType). c x -> c' x
f (Lexeme c a -> Lexeme c' a) -> [Lexeme c a] -> [Lexeme c' a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Lexeme c a]
ls)
mapCategory forall (x :: LexemeType). c x -> c' x
_ Lexeme c a
Metathesis = Lexeme c' a
Lexeme c' 'Replacement
forall (category :: LexemeType -> *). Lexeme category 'Replacement
Metathesis
mapCategory forall (x :: LexemeType). c x -> c' x
_ Lexeme c a
Geminate = Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a
Geminate
mapCategory forall (x :: LexemeType). c x -> c' x
f (Wildcard Lexeme c a
l) = Lexeme c' a -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a -> Lexeme category a
Wildcard ((forall (x :: LexemeType). c x -> c' x)
-> Lexeme c a -> Lexeme c' a
forall (c :: LexemeType -> *) (c' :: LexemeType -> *)
       (a :: LexemeType).
(forall (x :: LexemeType). c x -> c' x)
-> Lexeme c a -> Lexeme c' a
mapCategory c x -> c' x
forall (x :: LexemeType). c x -> c' x
f Lexeme c a
l)
mapCategory forall (x :: LexemeType). c x -> c' x
f (Kleene Lexeme c a
l) = Lexeme c' a -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a -> Lexeme category a
Kleene ((forall (x :: LexemeType). c x -> c' x)
-> Lexeme c a -> Lexeme c' a
forall (c :: LexemeType -> *) (c' :: LexemeType -> *)
       (a :: LexemeType).
(forall (x :: LexemeType). c x -> c' x)
-> Lexeme c a -> Lexeme c' a
mapCategory c x -> c' x
forall (x :: LexemeType). c x -> c' x
f Lexeme c a
l)
mapCategory forall (x :: LexemeType). c x -> c' x
_ Lexeme c a
Discard = Lexeme c' a
Lexeme c' 'Replacement
forall (category :: LexemeType -> *). Lexeme category 'Replacement
Discard
mapCategory forall (x :: LexemeType). c x -> c' x
f (Backreference Int
i c a
c) = Int -> c' a -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Int -> category a -> Lexeme category a
Backreference Int
i (c a -> c' a
forall (x :: LexemeType). c x -> c' x
f c a
c)
mapCategory forall (x :: LexemeType). c x -> c' x
f (Multiple c 'Replacement
c) = c' 'Replacement -> Lexeme c' 'Replacement
forall (category :: LexemeType -> *).
category 'Replacement -> Lexeme category 'Replacement
Multiple (c 'Replacement -> c' 'Replacement
forall (x :: LexemeType). c x -> c' x
f c 'Replacement
c)

mapCategoryA
    :: Applicative t
    => (forall x. c x -> t (c' x))
    -> Lexeme c a
    -> t (Lexeme c' a)
mapCategoryA :: forall (t :: * -> *) (c :: LexemeType -> *) (c' :: LexemeType -> *)
       (a :: LexemeType).
Applicative t =>
(forall (x :: LexemeType). c x -> t (c' x))
-> Lexeme c a -> t (Lexeme c' a)
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
_ (Grapheme Grapheme
g) = Lexeme c' a -> t (Lexeme c' a)
forall a. a -> t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Lexeme c' a -> t (Lexeme c' a)) -> Lexeme c' a -> t (Lexeme c' a)
forall a b. (a -> b) -> a -> b
$ Grapheme -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Grapheme -> Lexeme category a
Grapheme Grapheme
g
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
f (Category c a
c) = c' a -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
category a -> Lexeme category a
Category (c' a -> Lexeme c' a) -> t (c' a) -> t (Lexeme c' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> c a -> t (c' a)
forall (x :: LexemeType). c x -> t (c' x)
f c a
c
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
f (Optional [Lexeme c a]
ls) = [Lexeme c' a] -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
[Lexeme category a] -> Lexeme category a
Optional ([Lexeme c' a] -> Lexeme c' a)
-> t [Lexeme c' a] -> t (Lexeme c' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Lexeme c a -> t (Lexeme c' a)) -> [Lexeme c a] -> t [Lexeme c' a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ((forall (x :: LexemeType). c x -> t (c' x))
-> Lexeme c a -> t (Lexeme c' a)
forall (t :: * -> *) (c :: LexemeType -> *) (c' :: LexemeType -> *)
       (a :: LexemeType).
Applicative t =>
(forall (x :: LexemeType). c x -> t (c' x))
-> Lexeme c a -> t (Lexeme c' a)
mapCategoryA c x -> t (c' x)
forall (x :: LexemeType). c x -> t (c' x)
f) [Lexeme c a]
ls
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
_ Lexeme c a
Metathesis = Lexeme c' a -> t (Lexeme c' a)
forall a. a -> t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Lexeme c' a
Lexeme c' 'Replacement
forall (category :: LexemeType -> *). Lexeme category 'Replacement
Metathesis
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
_ Lexeme c a
Geminate = Lexeme c' a -> t (Lexeme c' a)
forall a. a -> t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a
Geminate
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
f (Wildcard Lexeme c a
l) = Lexeme c' a -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a -> Lexeme category a
Wildcard (Lexeme c' a -> Lexeme c' a) -> t (Lexeme c' a) -> t (Lexeme c' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall (x :: LexemeType). c x -> t (c' x))
-> Lexeme c a -> t (Lexeme c' a)
forall (t :: * -> *) (c :: LexemeType -> *) (c' :: LexemeType -> *)
       (a :: LexemeType).
Applicative t =>
(forall (x :: LexemeType). c x -> t (c' x))
-> Lexeme c a -> t (Lexeme c' a)
mapCategoryA c x -> t (c' x)
forall (x :: LexemeType). c x -> t (c' x)
f Lexeme c a
l
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
f (Kleene Lexeme c a
l) = Lexeme c' a -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a -> Lexeme category a
Kleene (Lexeme c' a -> Lexeme c' a) -> t (Lexeme c' a) -> t (Lexeme c' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall (x :: LexemeType). c x -> t (c' x))
-> Lexeme c a -> t (Lexeme c' a)
forall (t :: * -> *) (c :: LexemeType -> *) (c' :: LexemeType -> *)
       (a :: LexemeType).
Applicative t =>
(forall (x :: LexemeType). c x -> t (c' x))
-> Lexeme c a -> t (Lexeme c' a)
mapCategoryA c x -> t (c' x)
forall (x :: LexemeType). c x -> t (c' x)
f Lexeme c a
l
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
_ Lexeme c a
Discard = Lexeme c' a -> t (Lexeme c' a)
forall a. a -> t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Lexeme c' a
Lexeme c' 'Replacement
forall (category :: LexemeType -> *). Lexeme category 'Replacement
Discard
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
f (Backreference Int
i c a
c) = Int -> c' a -> Lexeme c' a
forall (category :: LexemeType -> *) (a :: LexemeType).
Int -> category a -> Lexeme category a
Backreference Int
i (c' a -> Lexeme c' a) -> t (c' a) -> t (Lexeme c' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> c a -> t (c' a)
forall (x :: LexemeType). c x -> t (c' x)
f c a
c
mapCategoryA forall (x :: LexemeType). c x -> t (c' x)
f (Multiple c 'Replacement
c) = c' 'Replacement -> Lexeme c' a
c' 'Replacement -> Lexeme c' 'Replacement
forall (category :: LexemeType -> *).
category 'Replacement -> Lexeme category 'Replacement
Multiple (c' 'Replacement -> Lexeme c' a)
-> t (c' 'Replacement) -> t (Lexeme c' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> c 'Replacement -> t (c' 'Replacement)
forall (x :: LexemeType). c x -> t (c' x)
f c 'Replacement
c

-- | The type of a category after expansion.
newtype Expanded a = FromElements { forall (a :: LexemeType).
Expanded a -> [Either Grapheme [Lexeme Expanded a]]
elements :: [Either Grapheme [Lexeme Expanded a]] }
    deriving (Expanded a -> Expanded a -> Bool
(Expanded a -> Expanded a -> Bool)
-> (Expanded a -> Expanded a -> Bool) -> Eq (Expanded a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (a :: LexemeType). Expanded a -> Expanded a -> Bool
$c== :: forall (a :: LexemeType). Expanded a -> Expanded a -> Bool
== :: Expanded a -> Expanded a -> Bool
$c/= :: forall (a :: LexemeType). Expanded a -> Expanded a -> Bool
/= :: Expanded a -> Expanded a -> Bool
Eq, Eq (Expanded a)
Eq (Expanded a) =>
(Expanded a -> Expanded a -> Ordering)
-> (Expanded a -> Expanded a -> Bool)
-> (Expanded a -> Expanded a -> Bool)
-> (Expanded a -> Expanded a -> Bool)
-> (Expanded a -> Expanded a -> Bool)
-> (Expanded a -> Expanded a -> Expanded a)
-> (Expanded a -> Expanded a -> Expanded a)
-> Ord (Expanded a)
Expanded a -> Expanded a -> Bool
Expanded a -> Expanded a -> Ordering
Expanded a -> Expanded a -> Expanded a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (x :: LexemeType). Eq (Expanded x)
forall (a :: LexemeType). Expanded a -> Expanded a -> Bool
forall (a :: LexemeType). Expanded a -> Expanded a -> Ordering
forall (a :: LexemeType). Expanded a -> Expanded a -> Expanded a
$ccompare :: forall (a :: LexemeType). Expanded a -> Expanded a -> Ordering
compare :: Expanded a -> Expanded a -> Ordering
$c< :: forall (a :: LexemeType). Expanded a -> Expanded a -> Bool
< :: Expanded a -> Expanded a -> Bool
$c<= :: forall (a :: LexemeType). Expanded a -> Expanded a -> Bool
<= :: Expanded a -> Expanded a -> Bool
$c> :: forall (a :: LexemeType). Expanded a -> Expanded a -> Bool
> :: Expanded a -> Expanded a -> Bool
$c>= :: forall (a :: LexemeType). Expanded a -> Expanded a -> Bool
>= :: Expanded a -> Expanded a -> Bool
$cmax :: forall (a :: LexemeType). Expanded a -> Expanded a -> Expanded a
max :: Expanded a -> Expanded a -> Expanded a
$cmin :: forall (a :: LexemeType). Expanded a -> Expanded a -> Expanded a
min :: Expanded a -> Expanded a -> Expanded a
Ord, Int -> Expanded a -> ShowS
[Expanded a] -> ShowS
Expanded a -> [Char]
(Int -> Expanded a -> ShowS)
-> (Expanded a -> [Char])
-> ([Expanded a] -> ShowS)
-> Show (Expanded a)
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
forall (a :: LexemeType). Int -> Expanded a -> ShowS
forall (a :: LexemeType). [Expanded a] -> ShowS
forall (a :: LexemeType). Expanded a -> [Char]
$cshowsPrec :: forall (a :: LexemeType). Int -> Expanded a -> ShowS
showsPrec :: Int -> Expanded a -> ShowS
$cshow :: forall (a :: LexemeType). Expanded a -> [Char]
show :: Expanded a -> [Char]
$cshowList :: forall (a :: LexemeType). [Expanded a] -> ShowS
showList :: [Expanded a] -> ShowS
Show, (forall x. Expanded a -> Rep (Expanded a) x)
-> (forall x. Rep (Expanded a) x -> Expanded a)
-> Generic (Expanded a)
forall x. Rep (Expanded a) x -> Expanded a
forall x. Expanded a -> Rep (Expanded a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (a :: LexemeType) x. Rep (Expanded a) x -> Expanded a
forall (a :: LexemeType) x. Expanded a -> Rep (Expanded a) x
$cfrom :: forall (a :: LexemeType) x. Expanded a -> Rep (Expanded a) x
from :: forall x. Expanded a -> Rep (Expanded a) x
$cto :: forall (a :: LexemeType) x. Rep (Expanded a) x -> Expanded a
to :: forall x. Rep (Expanded a) x -> Expanded a
Generic, Expanded a -> ()
(Expanded a -> ()) -> NFData (Expanded a)
forall a. (a -> ()) -> NFData a
forall (a :: LexemeType). Expanded a -> ()
$crnf :: forall (a :: LexemeType). Expanded a -> ()
rnf :: Expanded a -> ()
NFData)

instance Semigroup (Expanded a) where
    (FromElements [Either Grapheme [Lexeme Expanded a]]
es) <> :: Expanded a -> Expanded a -> Expanded a
<> (FromElements [Either Grapheme [Lexeme Expanded a]]
es') = [Either Grapheme [Lexeme Expanded a]] -> Expanded a
forall (a :: LexemeType).
[Either Grapheme [Lexeme Expanded a]] -> Expanded a
FromElements ([Either Grapheme [Lexeme Expanded a]]
es [Either Grapheme [Lexeme Expanded a]]
-> [Either Grapheme [Lexeme Expanded a]]
-> [Either Grapheme [Lexeme Expanded a]]
forall a. Semigroup a => a -> a -> a
<> [Either Grapheme [Lexeme Expanded a]]
es')

instance Monoid (Expanded a) where
    mempty :: Expanded a
mempty = [Either Grapheme [Lexeme Expanded a]] -> Expanded a
forall (a :: LexemeType).
[Either Grapheme [Lexeme Expanded a]] -> Expanded a
FromElements []

generalise :: (c 'AnyPart -> c a) -> Lexeme c 'AnyPart -> Lexeme c a
generalise :: forall (c :: LexemeType -> *) (a :: LexemeType).
(c 'AnyPart -> c a) -> Lexeme c 'AnyPart -> Lexeme c a
generalise c 'AnyPart -> c a
_ (Grapheme Grapheme
g) = Grapheme -> Lexeme c a
forall (category :: LexemeType -> *) (a :: LexemeType).
Grapheme -> Lexeme category a
Grapheme Grapheme
g
generalise c 'AnyPart -> c a
f (Category c 'AnyPart
es) = c a -> Lexeme c a
forall (category :: LexemeType -> *) (a :: LexemeType).
category a -> Lexeme category a
Category (c a -> Lexeme c a) -> c a -> Lexeme c a
forall a b. (a -> b) -> a -> b
$ c 'AnyPart -> c a
f c 'AnyPart
es
generalise c 'AnyPart -> c a
f (Optional [Lexeme c 'AnyPart]
ls) = [Lexeme c a] -> Lexeme c a
forall (category :: LexemeType -> *) (a :: LexemeType).
[Lexeme category a] -> Lexeme category a
Optional ([Lexeme c a] -> Lexeme c a) -> [Lexeme c a] -> Lexeme c a
forall a b. (a -> b) -> a -> b
$ (c 'AnyPart -> c a) -> Lexeme c 'AnyPart -> Lexeme c a
forall (c :: LexemeType -> *) (a :: LexemeType).
(c 'AnyPart -> c a) -> Lexeme c 'AnyPart -> Lexeme c a
generalise c 'AnyPart -> c a
f (Lexeme c 'AnyPart -> Lexeme c a)
-> [Lexeme c 'AnyPart] -> [Lexeme c a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Lexeme c 'AnyPart]
ls
generalise c 'AnyPart -> c a
_ Lexeme c 'AnyPart
Geminate = Lexeme c a
forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a
Geminate
generalise c 'AnyPart -> c a
f (Backreference Int
i c 'AnyPart
es) = Int -> c a -> Lexeme c a
forall (category :: LexemeType -> *) (a :: LexemeType).
Int -> category a -> Lexeme category a
Backreference Int
i (c a -> Lexeme c a) -> c a -> Lexeme c a
forall a b. (a -> b) -> a -> b
$ c 'AnyPart -> c a
f c 'AnyPart
es
generalise c 'AnyPart -> c a
f (Wildcard Lexeme c 'AnyPart
l) = Lexeme c a -> Lexeme c a
forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a -> Lexeme category a
Wildcard (Lexeme c a -> Lexeme c a) -> Lexeme c a -> Lexeme c a
forall a b. (a -> b) -> a -> b
$ (c 'AnyPart -> c a) -> Lexeme c 'AnyPart -> Lexeme c a
forall (c :: LexemeType -> *) (a :: LexemeType).
(c 'AnyPart -> c a) -> Lexeme c 'AnyPart -> Lexeme c a
generalise c 'AnyPart -> c a
f Lexeme c 'AnyPart
l
generalise c 'AnyPart -> c a
f (Kleene Lexeme c 'AnyPart
l) = Lexeme c a -> Lexeme c a
forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a -> Lexeme category a
Kleene (Lexeme c a -> Lexeme c a) -> Lexeme c a -> Lexeme c a
forall a b. (a -> b) -> a -> b
$ (c 'AnyPart -> c a) -> Lexeme c 'AnyPart -> Lexeme c a
forall (c :: LexemeType -> *) (a :: LexemeType).
(c 'AnyPart -> c a) -> Lexeme c 'AnyPart -> Lexeme c a
generalise c 'AnyPart -> c a
f Lexeme c 'AnyPart
l

generaliseExpanded :: Expanded 'AnyPart -> Expanded a
generaliseExpanded :: forall (a :: LexemeType). Expanded 'AnyPart -> Expanded a
generaliseExpanded = [Either Grapheme [Lexeme Expanded a]] -> Expanded a
forall (a :: LexemeType).
[Either Grapheme [Lexeme Expanded a]] -> Expanded a
FromElements ([Either Grapheme [Lexeme Expanded a]] -> Expanded a)
-> (Expanded 'AnyPart -> [Either Grapheme [Lexeme Expanded a]])
-> Expanded 'AnyPart
-> Expanded a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Either Grapheme [Lexeme Expanded 'AnyPart]
 -> Either Grapheme [Lexeme Expanded a])
-> [Either Grapheme [Lexeme Expanded 'AnyPart]]
-> [Either Grapheme [Lexeme Expanded a]]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap((Either Grapheme [Lexeme Expanded 'AnyPart]
  -> Either Grapheme [Lexeme Expanded a])
 -> [Either Grapheme [Lexeme Expanded 'AnyPart]]
 -> [Either Grapheme [Lexeme Expanded a]])
-> ((Lexeme Expanded 'AnyPart -> Lexeme Expanded a)
    -> Either Grapheme [Lexeme Expanded 'AnyPart]
    -> Either Grapheme [Lexeme Expanded a])
-> (Lexeme Expanded 'AnyPart -> Lexeme Expanded a)
-> [Either Grapheme [Lexeme Expanded 'AnyPart]]
-> [Either Grapheme [Lexeme Expanded a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.([Lexeme Expanded 'AnyPart] -> [Lexeme Expanded a])
-> Either Grapheme [Lexeme Expanded 'AnyPart]
-> Either Grapheme [Lexeme Expanded a]
forall a b. (a -> b) -> Either Grapheme a -> Either Grapheme b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap(([Lexeme Expanded 'AnyPart] -> [Lexeme Expanded a])
 -> Either Grapheme [Lexeme Expanded 'AnyPart]
 -> Either Grapheme [Lexeme Expanded a])
-> ((Lexeme Expanded 'AnyPart -> Lexeme Expanded a)
    -> [Lexeme Expanded 'AnyPart] -> [Lexeme Expanded a])
-> (Lexeme Expanded 'AnyPart -> Lexeme Expanded a)
-> Either Grapheme [Lexeme Expanded 'AnyPart]
-> Either Grapheme [Lexeme Expanded a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Lexeme Expanded 'AnyPart -> Lexeme Expanded a)
-> [Lexeme Expanded 'AnyPart] -> [Lexeme Expanded a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) ((Expanded 'AnyPart -> Expanded a)
-> Lexeme Expanded 'AnyPart -> Lexeme Expanded a
forall (c :: LexemeType -> *) (a :: LexemeType).
(c 'AnyPart -> c a) -> Lexeme c 'AnyPart -> Lexeme c a
generalise Expanded 'AnyPart -> Expanded a
forall (a :: LexemeType). Expanded 'AnyPart -> Expanded a
generaliseExpanded) ([Either Grapheme [Lexeme Expanded 'AnyPart]]
 -> [Either Grapheme [Lexeme Expanded a]])
-> (Expanded 'AnyPart
    -> [Either Grapheme [Lexeme Expanded 'AnyPart]])
-> Expanded 'AnyPart
-> [Either Grapheme [Lexeme Expanded a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expanded 'AnyPart -> [Either Grapheme [Lexeme Expanded 'AnyPart]]
forall (a :: LexemeType).
Expanded a -> [Either Grapheme [Lexeme Expanded a]]
elements

-- | A 'Lexeme' matching a single word boundary, specified as @#@ in Brassica syntax.
pattern Boundary :: Lexeme c a
pattern $mBoundary :: forall {r} {c :: LexemeType -> *} {a :: LexemeType}.
Lexeme c a -> ((# #) -> r) -> ((# #) -> r) -> r
$bBoundary :: forall (category :: LexemeType -> *) (a :: LexemeType).
Lexeme category a
Boundary = Grapheme GBoundary

deriving instance (forall x. Show (c x)) => Show (Lexeme c a)
deriving instance (forall x. Eq (c x)) => Eq (Lexeme c a)
deriving instance (forall x. Ord (c x)) => Ord (Lexeme c a)

instance (forall x. NFData (c x)) => NFData (Lexeme c a) where
    rnf :: Lexeme c a -> ()
rnf (Grapheme Grapheme
g) = Grapheme -> ()
forall a. NFData a => a -> ()
rnf Grapheme
g
    rnf (Category c a
cs) = c a -> ()
forall a. NFData a => a -> ()
rnf c a
cs
    rnf (Optional [Lexeme c a]
ls) = [Lexeme c a] -> ()
forall a. NFData a => a -> ()
rnf [Lexeme c a]
ls
    rnf Lexeme c a
Metathesis = ()
    rnf Lexeme c a
Geminate = ()
    rnf (Wildcard Lexeme c a
l) = Lexeme c a -> ()
forall a. NFData a => a -> ()
rnf Lexeme c a
l
    rnf (Kleene Lexeme c a
l) = Lexeme c a -> ()
forall a. NFData a => a -> ()
rnf Lexeme c a
l
    rnf Lexeme c a
Discard = ()
    rnf (Backreference Int
i c a
l) = Int -> () -> ()
forall a b. a -> b -> b
seq Int
i (() -> ()) -> () -> ()
forall a b. (a -> b) -> a -> b
$ c a -> ()
forall a. NFData a => a -> ()
rnf c a
l
    rnf (Multiple c 'Replacement
l) = c 'Replacement -> ()
forall a. NFData a => a -> ()
rnf c 'Replacement
l

-- | An 'Environment' is a tuple of @(before, after)@ components,
-- corresponding to a ‘/ before _ after’ component of a sound change.
--
-- Note that an empty environment is just @([], [])@.
type Environment c = ([Lexeme c 'Matched], [Lexeme c 'Matched])

-- | Specifies application direction of rule — either left-to-right or right-to-left.
data Direction = LTR | RTL
    deriving (Direction -> Direction -> Bool
(Direction -> Direction -> Bool)
-> (Direction -> Direction -> Bool) -> Eq Direction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Direction -> Direction -> Bool
== :: Direction -> Direction -> Bool
$c/= :: Direction -> Direction -> Bool
/= :: Direction -> Direction -> Bool
Eq, Int -> Direction -> ShowS
[Direction] -> ShowS
Direction -> [Char]
(Int -> Direction -> ShowS)
-> (Direction -> [Char])
-> ([Direction] -> ShowS)
-> Show Direction
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Direction -> ShowS
showsPrec :: Int -> Direction -> ShowS
$cshow :: Direction -> [Char]
show :: Direction -> [Char]
$cshowList :: [Direction] -> ShowS
showList :: [Direction] -> ShowS
Show, (forall x. Direction -> Rep Direction x)
-> (forall x. Rep Direction x -> Direction) -> Generic Direction
forall x. Rep Direction x -> Direction
forall x. Direction -> Rep Direction x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Direction -> Rep Direction x
from :: forall x. Direction -> Rep Direction x
$cto :: forall x. Rep Direction x -> Direction
to :: forall x. Rep Direction x -> Direction
Generic, Direction -> ()
(Direction -> ()) -> NFData Direction
forall a. (a -> ()) -> NFData a
$crnf :: Direction -> ()
rnf :: Direction -> ()
NFData)

-- | Specifies how regularly a rule should be applied.
data Sporadicity
    = ApplyAlways
    -- ^ Always apply the rule
    | PerWord
    -- ^ Apply sporadically, either to the whole word or to none of the word
    | PerApplication
    -- ^ Apply sporadically, at each application site
    deriving (Sporadicity -> Sporadicity -> Bool
(Sporadicity -> Sporadicity -> Bool)
-> (Sporadicity -> Sporadicity -> Bool) -> Eq Sporadicity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Sporadicity -> Sporadicity -> Bool
== :: Sporadicity -> Sporadicity -> Bool
$c/= :: Sporadicity -> Sporadicity -> Bool
/= :: Sporadicity -> Sporadicity -> Bool
Eq, Int -> Sporadicity -> ShowS
[Sporadicity] -> ShowS
Sporadicity -> [Char]
(Int -> Sporadicity -> ShowS)
-> (Sporadicity -> [Char])
-> ([Sporadicity] -> ShowS)
-> Show Sporadicity
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Sporadicity -> ShowS
showsPrec :: Int -> Sporadicity -> ShowS
$cshow :: Sporadicity -> [Char]
show :: Sporadicity -> [Char]
$cshowList :: [Sporadicity] -> ShowS
showList :: [Sporadicity] -> ShowS
Show, (forall x. Sporadicity -> Rep Sporadicity x)
-> (forall x. Rep Sporadicity x -> Sporadicity)
-> Generic Sporadicity
forall x. Rep Sporadicity x -> Sporadicity
forall x. Sporadicity -> Rep Sporadicity x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Sporadicity -> Rep Sporadicity x
from :: forall x. Sporadicity -> Rep Sporadicity x
$cto :: forall x. Rep Sporadicity x -> Sporadicity
to :: forall x. Rep Sporadicity x -> Sporadicity
Generic, Sporadicity -> ()
(Sporadicity -> ()) -> NFData Sporadicity
forall a. (a -> ()) -> NFData a
$crnf :: Sporadicity -> ()
rnf :: Sporadicity -> ()
NFData)

-- | Flags which can be enabled, disabled or altered on a 'Rule' to
-- change how it is applied.
data Flags = Flags
  { Flags -> Bool
highlightChanges :: Bool
  , Flags -> Direction
applyDirection   :: Direction
  , Flags -> Bool
applyOnceOnly    :: Bool
  , Flags -> Sporadicity
sporadic         :: Sporadicity
  } deriving (Int -> Flags -> ShowS
[Flags] -> ShowS
Flags -> [Char]
(Int -> Flags -> ShowS)
-> (Flags -> [Char]) -> ([Flags] -> ShowS) -> Show Flags
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Flags -> ShowS
showsPrec :: Int -> Flags -> ShowS
$cshow :: Flags -> [Char]
show :: Flags -> [Char]
$cshowList :: [Flags] -> ShowS
showList :: [Flags] -> ShowS
Show, (forall x. Flags -> Rep Flags x)
-> (forall x. Rep Flags x -> Flags) -> Generic Flags
forall x. Rep Flags x -> Flags
forall x. Flags -> Rep Flags x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Flags -> Rep Flags x
from :: forall x. Flags -> Rep Flags x
$cto :: forall x. Rep Flags x -> Flags
to :: forall x. Rep Flags x -> Flags
Generic, Flags -> ()
(Flags -> ()) -> NFData Flags
forall a. (a -> ()) -> NFData a
$crnf :: Flags -> ()
rnf :: Flags -> ()
NFData)

-- | A default selection of flags which are appropriate for most
-- rules:
--
-- @
-- 'defFlags' = 'Flags'
--     { 'highlightChanges' = 'True'
--     , 'applyDirection' = 'LTR'
--     , 'applyOnceOnly' = 'False'
--     , 'sporadic' = 'False'
--     }
-- @
--
-- That is: highlight changes, apply from left to right, apply
-- repeatedly, and don’t apply sporadically.
defFlags :: Flags
defFlags :: Flags
defFlags = Flags
    { highlightChanges :: Bool
highlightChanges = Bool
True
    , applyDirection :: Direction
applyDirection = Direction
LTR
    , applyOnceOnly :: Bool
applyOnceOnly = Bool
False
    , sporadic :: Sporadicity
sporadic = Sporadicity
ApplyAlways
    }

-- | A single sound change rule: in Brassica sound-change syntax with all elements specified,
-- @-flags target / replacement \/ environment1 | environment2 | … \/ exception@.
-- (And usually the 'plaintext' of the rule will contain a 'String' resembling that pattern.)
data Rule c = Rule
  { forall (c :: LexemeType -> *). Rule c -> [Lexeme c 'Matched]
target      :: [Lexeme c 'Matched]
  , forall (c :: LexemeType -> *). Rule c -> [Lexeme c 'Replacement]
replacement :: [Lexeme c 'Replacement]
  , forall (c :: LexemeType -> *). Rule c -> [Environment c]
environment :: [Environment c]
  , forall (c :: LexemeType -> *). Rule c -> Maybe (Environment c)
exception   :: Maybe (Environment c)
  , forall (c :: LexemeType -> *). Rule c -> Flags
flags       :: Flags
  , forall (c :: LexemeType -> *). Rule c -> [Char]
plaintext   :: String
  } deriving ((forall x. Rule c -> Rep (Rule c) x)
-> (forall x. Rep (Rule c) x -> Rule c) -> Generic (Rule c)
forall x. Rep (Rule c) x -> Rule c
forall x. Rule c -> Rep (Rule c) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (c :: LexemeType -> *) x. Rep (Rule c) x -> Rule c
forall (c :: LexemeType -> *) x. Rule c -> Rep (Rule c) x
$cfrom :: forall (c :: LexemeType -> *) x. Rule c -> Rep (Rule c) x
from :: forall x. Rule c -> Rep (Rule c) x
$cto :: forall (c :: LexemeType -> *) x. Rep (Rule c) x -> Rule c
to :: forall x. Rep (Rule c) x -> Rule c
Generic)

deriving instance (forall a. Show (c a)) => Show (Rule c)
deriving instance (forall a. NFData (c a)) => NFData (Rule c)

-- | A filter, constraining the output to not match the given elements.
-- (The 'String' is the plaintext, as with 'Rule'.)
data Filter c = Filter String [Lexeme c 'Matched]
    deriving ((forall x. Filter c -> Rep (Filter c) x)
-> (forall x. Rep (Filter c) x -> Filter c) -> Generic (Filter c)
forall x. Rep (Filter c) x -> Filter c
forall x. Filter c -> Rep (Filter c) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (c :: LexemeType -> *) x. Rep (Filter c) x -> Filter c
forall (c :: LexemeType -> *) x. Filter c -> Rep (Filter c) x
$cfrom :: forall (c :: LexemeType -> *) x. Filter c -> Rep (Filter c) x
from :: forall x. Filter c -> Rep (Filter c) x
$cto :: forall (c :: LexemeType -> *) x. Rep (Filter c) x -> Filter c
to :: forall x. Rep (Filter c) x -> Filter c
Generic)

deriving instance (forall a. Show (c a)) => Show (Filter c)
deriving instance (forall a. NFData (c a)) => NFData (Filter c)

-- | A 'Statement' can be a single sound change rule, a filter,
-- or a directive (e.g. category definition).
data Statement c decl
    = RuleS (Rule c)
    | FilterS (Filter c)
    | DirectiveS decl
    deriving ((forall x. Statement c decl -> Rep (Statement c decl) x)
-> (forall x. Rep (Statement c decl) x -> Statement c decl)
-> Generic (Statement c decl)
forall x. Rep (Statement c decl) x -> Statement c decl
forall x. Statement c decl -> Rep (Statement c decl) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (c :: LexemeType -> *) decl x.
Rep (Statement c decl) x -> Statement c decl
forall (c :: LexemeType -> *) decl x.
Statement c decl -> Rep (Statement c decl) x
$cfrom :: forall (c :: LexemeType -> *) decl x.
Statement c decl -> Rep (Statement c decl) x
from :: forall x. Statement c decl -> Rep (Statement c decl) x
$cto :: forall (c :: LexemeType -> *) decl x.
Rep (Statement c decl) x -> Statement c decl
to :: forall x. Rep (Statement c decl) x -> Statement c decl
Generic)

deriving instance (forall a. Show (c a), Show decl) => Show (Statement c decl)
deriving instance (forall a. NFData (c a), NFData decl) => NFData (Statement c decl)

-- | A simple wrapper around 'plaintext' for 'Statement's. Returns
-- @"<directive>"@ for all 'DirectiveS' inputs.
plaintext' :: Statement c decl -> String
plaintext' :: forall (c :: LexemeType -> *) decl. Statement c decl -> [Char]
plaintext' (RuleS Rule c
r) = Rule c -> [Char]
forall (c :: LexemeType -> *). Rule c -> [Char]
plaintext Rule c
r
plaintext' (FilterS (Filter [Char]
p [Lexeme c 'Matched]
_)) = [Char]
p
plaintext' (DirectiveS decl
_) = [Char]
"<directive>"

-- | A set of 'SoundChanges' is simply a list of 'Statement's.
type SoundChanges c decl = [Statement c decl]

-- | The individual operations used to construct a category in
-- Brassica sound-change syntax.
data CategoryModification = Union | Intersect | Subtract
    deriving (Int -> CategoryModification -> ShowS
[CategoryModification] -> ShowS
CategoryModification -> [Char]
(Int -> CategoryModification -> ShowS)
-> (CategoryModification -> [Char])
-> ([CategoryModification] -> ShowS)
-> Show CategoryModification
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CategoryModification -> ShowS
showsPrec :: Int -> CategoryModification -> ShowS
$cshow :: CategoryModification -> [Char]
show :: CategoryModification -> [Char]
$cshowList :: [CategoryModification] -> ShowS
showList :: [CategoryModification] -> ShowS
Show, CategoryModification -> CategoryModification -> Bool
(CategoryModification -> CategoryModification -> Bool)
-> (CategoryModification -> CategoryModification -> Bool)
-> Eq CategoryModification
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CategoryModification -> CategoryModification -> Bool
== :: CategoryModification -> CategoryModification -> Bool
$c/= :: CategoryModification -> CategoryModification -> Bool
/= :: CategoryModification -> CategoryModification -> Bool
Eq, Eq CategoryModification
Eq CategoryModification =>
(CategoryModification -> CategoryModification -> Ordering)
-> (CategoryModification -> CategoryModification -> Bool)
-> (CategoryModification -> CategoryModification -> Bool)
-> (CategoryModification -> CategoryModification -> Bool)
-> (CategoryModification -> CategoryModification -> Bool)
-> (CategoryModification
    -> CategoryModification -> CategoryModification)
-> (CategoryModification
    -> CategoryModification -> CategoryModification)
-> Ord CategoryModification
CategoryModification -> CategoryModification -> Bool
CategoryModification -> CategoryModification -> Ordering
CategoryModification
-> CategoryModification -> CategoryModification
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: CategoryModification -> CategoryModification -> Ordering
compare :: CategoryModification -> CategoryModification -> Ordering
$c< :: CategoryModification -> CategoryModification -> Bool
< :: CategoryModification -> CategoryModification -> Bool
$c<= :: CategoryModification -> CategoryModification -> Bool
<= :: CategoryModification -> CategoryModification -> Bool
$c> :: CategoryModification -> CategoryModification -> Bool
> :: CategoryModification -> CategoryModification -> Bool
$c>= :: CategoryModification -> CategoryModification -> Bool
>= :: CategoryModification -> CategoryModification -> Bool
$cmax :: CategoryModification
-> CategoryModification -> CategoryModification
max :: CategoryModification
-> CategoryModification -> CategoryModification
$cmin :: CategoryModification
-> CategoryModification -> CategoryModification
min :: CategoryModification
-> CategoryModification -> CategoryModification
Ord, (forall x. CategoryModification -> Rep CategoryModification x)
-> (forall x. Rep CategoryModification x -> CategoryModification)
-> Generic CategoryModification
forall x. Rep CategoryModification x -> CategoryModification
forall x. CategoryModification -> Rep CategoryModification x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CategoryModification -> Rep CategoryModification x
from :: forall x. CategoryModification -> Rep CategoryModification x
$cto :: forall x. Rep CategoryModification x -> CategoryModification
to :: forall x. Rep CategoryModification x -> CategoryModification
Generic, CategoryModification -> ()
(CategoryModification -> ()) -> NFData CategoryModification
forall a. (a -> ()) -> NFData a
$crnf :: CategoryModification -> ()
rnf :: CategoryModification -> ()
NFData)

-- | The specification of a category in Brassica sound-change syntax.
data CategorySpec a
    = CategorySpec [(CategoryModification, Either Grapheme [Lexeme CategorySpec a])]
    | MustInline String  -- ^ A single grapheme assumed to have been specified earlier as a category
    deriving (Int -> CategorySpec a -> ShowS
[CategorySpec a] -> ShowS
CategorySpec a -> [Char]
(Int -> CategorySpec a -> ShowS)
-> (CategorySpec a -> [Char])
-> ([CategorySpec a] -> ShowS)
-> Show (CategorySpec a)
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
forall (a :: LexemeType). Int -> CategorySpec a -> ShowS
forall (a :: LexemeType). [CategorySpec a] -> ShowS
forall (a :: LexemeType). CategorySpec a -> [Char]
$cshowsPrec :: forall (a :: LexemeType). Int -> CategorySpec a -> ShowS
showsPrec :: Int -> CategorySpec a -> ShowS
$cshow :: forall (a :: LexemeType). CategorySpec a -> [Char]
show :: CategorySpec a -> [Char]
$cshowList :: forall (a :: LexemeType). [CategorySpec a] -> ShowS
showList :: [CategorySpec a] -> ShowS
Show, CategorySpec a -> CategorySpec a -> Bool
(CategorySpec a -> CategorySpec a -> Bool)
-> (CategorySpec a -> CategorySpec a -> Bool)
-> Eq (CategorySpec a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (a :: LexemeType). CategorySpec a -> CategorySpec a -> Bool
$c== :: forall (a :: LexemeType). CategorySpec a -> CategorySpec a -> Bool
== :: CategorySpec a -> CategorySpec a -> Bool
$c/= :: forall (a :: LexemeType). CategorySpec a -> CategorySpec a -> Bool
/= :: CategorySpec a -> CategorySpec a -> Bool
Eq, Eq (CategorySpec a)
Eq (CategorySpec a) =>
(CategorySpec a -> CategorySpec a -> Ordering)
-> (CategorySpec a -> CategorySpec a -> Bool)
-> (CategorySpec a -> CategorySpec a -> Bool)
-> (CategorySpec a -> CategorySpec a -> Bool)
-> (CategorySpec a -> CategorySpec a -> Bool)
-> (CategorySpec a -> CategorySpec a -> CategorySpec a)
-> (CategorySpec a -> CategorySpec a -> CategorySpec a)
-> Ord (CategorySpec a)
CategorySpec a -> CategorySpec a -> Bool
CategorySpec a -> CategorySpec a -> Ordering
CategorySpec a -> CategorySpec a -> CategorySpec a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (x :: LexemeType). Eq (CategorySpec x)
forall (a :: LexemeType). CategorySpec a -> CategorySpec a -> Bool
forall (a :: LexemeType).
CategorySpec a -> CategorySpec a -> Ordering
forall (a :: LexemeType).
CategorySpec a -> CategorySpec a -> CategorySpec a
$ccompare :: forall (a :: LexemeType).
CategorySpec a -> CategorySpec a -> Ordering
compare :: CategorySpec a -> CategorySpec a -> Ordering
$c< :: forall (a :: LexemeType). CategorySpec a -> CategorySpec a -> Bool
< :: CategorySpec a -> CategorySpec a -> Bool
$c<= :: forall (a :: LexemeType). CategorySpec a -> CategorySpec a -> Bool
<= :: CategorySpec a -> CategorySpec a -> Bool
$c> :: forall (a :: LexemeType). CategorySpec a -> CategorySpec a -> Bool
> :: CategorySpec a -> CategorySpec a -> Bool
$c>= :: forall (a :: LexemeType). CategorySpec a -> CategorySpec a -> Bool
>= :: CategorySpec a -> CategorySpec a -> Bool
$cmax :: forall (a :: LexemeType).
CategorySpec a -> CategorySpec a -> CategorySpec a
max :: CategorySpec a -> CategorySpec a -> CategorySpec a
$cmin :: forall (a :: LexemeType).
CategorySpec a -> CategorySpec a -> CategorySpec a
min :: CategorySpec a -> CategorySpec a -> CategorySpec a
Ord, (forall x. CategorySpec a -> Rep (CategorySpec a) x)
-> (forall x. Rep (CategorySpec a) x -> CategorySpec a)
-> Generic (CategorySpec a)
forall x. Rep (CategorySpec a) x -> CategorySpec a
forall x. CategorySpec a -> Rep (CategorySpec a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (a :: LexemeType) x.
Rep (CategorySpec a) x -> CategorySpec a
forall (a :: LexemeType) x.
CategorySpec a -> Rep (CategorySpec a) x
$cfrom :: forall (a :: LexemeType) x.
CategorySpec a -> Rep (CategorySpec a) x
from :: forall x. CategorySpec a -> Rep (CategorySpec a) x
$cto :: forall (a :: LexemeType) x.
Rep (CategorySpec a) x -> CategorySpec a
to :: forall x. Rep (CategorySpec a) x -> CategorySpec a
Generic, CategorySpec a -> ()
(CategorySpec a -> ()) -> NFData (CategorySpec a)
forall a. (a -> ()) -> NFData a
forall (a :: LexemeType). CategorySpec a -> ()
$crnf :: forall (a :: LexemeType). CategorySpec a -> ()
rnf :: CategorySpec a -> ()
NFData)

-- | The specification of a suprasegmental feature in Brassica
-- sound-change syntax.
data FeatureSpec = FeatureSpec
    { FeatureSpec -> Maybe [Char]
featureBaseName :: Maybe String
    , FeatureSpec -> CategorySpec 'AnyPart
featureBaseValues :: CategorySpec 'AnyPart
    , FeatureSpec -> [([Char], CategorySpec 'AnyPart)]
featureDerived :: [(String, CategorySpec 'AnyPart)]
    }
    deriving (Int -> FeatureSpec -> ShowS
[FeatureSpec] -> ShowS
FeatureSpec -> [Char]
(Int -> FeatureSpec -> ShowS)
-> (FeatureSpec -> [Char])
-> ([FeatureSpec] -> ShowS)
-> Show FeatureSpec
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FeatureSpec -> ShowS
showsPrec :: Int -> FeatureSpec -> ShowS
$cshow :: FeatureSpec -> [Char]
show :: FeatureSpec -> [Char]
$cshowList :: [FeatureSpec] -> ShowS
showList :: [FeatureSpec] -> ShowS
Show, FeatureSpec -> FeatureSpec -> Bool
(FeatureSpec -> FeatureSpec -> Bool)
-> (FeatureSpec -> FeatureSpec -> Bool) -> Eq FeatureSpec
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FeatureSpec -> FeatureSpec -> Bool
== :: FeatureSpec -> FeatureSpec -> Bool
$c/= :: FeatureSpec -> FeatureSpec -> Bool
/= :: FeatureSpec -> FeatureSpec -> Bool
Eq, Eq FeatureSpec
Eq FeatureSpec =>
(FeatureSpec -> FeatureSpec -> Ordering)
-> (FeatureSpec -> FeatureSpec -> Bool)
-> (FeatureSpec -> FeatureSpec -> Bool)
-> (FeatureSpec -> FeatureSpec -> Bool)
-> (FeatureSpec -> FeatureSpec -> Bool)
-> (FeatureSpec -> FeatureSpec -> FeatureSpec)
-> (FeatureSpec -> FeatureSpec -> FeatureSpec)
-> Ord FeatureSpec
FeatureSpec -> FeatureSpec -> Bool
FeatureSpec -> FeatureSpec -> Ordering
FeatureSpec -> FeatureSpec -> FeatureSpec
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: FeatureSpec -> FeatureSpec -> Ordering
compare :: FeatureSpec -> FeatureSpec -> Ordering
$c< :: FeatureSpec -> FeatureSpec -> Bool
< :: FeatureSpec -> FeatureSpec -> Bool
$c<= :: FeatureSpec -> FeatureSpec -> Bool
<= :: FeatureSpec -> FeatureSpec -> Bool
$c> :: FeatureSpec -> FeatureSpec -> Bool
> :: FeatureSpec -> FeatureSpec -> Bool
$c>= :: FeatureSpec -> FeatureSpec -> Bool
>= :: FeatureSpec -> FeatureSpec -> Bool
$cmax :: FeatureSpec -> FeatureSpec -> FeatureSpec
max :: FeatureSpec -> FeatureSpec -> FeatureSpec
$cmin :: FeatureSpec -> FeatureSpec -> FeatureSpec
min :: FeatureSpec -> FeatureSpec -> FeatureSpec
Ord, (forall x. FeatureSpec -> Rep FeatureSpec x)
-> (forall x. Rep FeatureSpec x -> FeatureSpec)
-> Generic FeatureSpec
forall x. Rep FeatureSpec x -> FeatureSpec
forall x. FeatureSpec -> Rep FeatureSpec x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FeatureSpec -> Rep FeatureSpec x
from :: forall x. FeatureSpec -> Rep FeatureSpec x
$cto :: forall x. Rep FeatureSpec x -> FeatureSpec
to :: forall x. Rep FeatureSpec x -> FeatureSpec
Generic, FeatureSpec -> ()
(FeatureSpec -> ()) -> NFData FeatureSpec
forall a. (a -> ()) -> NFData a
$crnf :: FeatureSpec -> ()
rnf :: FeatureSpec -> ()
NFData)

-- | A definition of a new category, either directly or via features.
data CategoryDefinition
    = DefineCategory String (CategorySpec 'AnyPart)
    | DefineFeature FeatureSpec
    deriving (Int -> CategoryDefinition -> ShowS
[CategoryDefinition] -> ShowS
CategoryDefinition -> [Char]
(Int -> CategoryDefinition -> ShowS)
-> (CategoryDefinition -> [Char])
-> ([CategoryDefinition] -> ShowS)
-> Show CategoryDefinition
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CategoryDefinition -> ShowS
showsPrec :: Int -> CategoryDefinition -> ShowS
$cshow :: CategoryDefinition -> [Char]
show :: CategoryDefinition -> [Char]
$cshowList :: [CategoryDefinition] -> ShowS
showList :: [CategoryDefinition] -> ShowS
Show, CategoryDefinition -> CategoryDefinition -> Bool
(CategoryDefinition -> CategoryDefinition -> Bool)
-> (CategoryDefinition -> CategoryDefinition -> Bool)
-> Eq CategoryDefinition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CategoryDefinition -> CategoryDefinition -> Bool
== :: CategoryDefinition -> CategoryDefinition -> Bool
$c/= :: CategoryDefinition -> CategoryDefinition -> Bool
/= :: CategoryDefinition -> CategoryDefinition -> Bool
Eq, Eq CategoryDefinition
Eq CategoryDefinition =>
(CategoryDefinition -> CategoryDefinition -> Ordering)
-> (CategoryDefinition -> CategoryDefinition -> Bool)
-> (CategoryDefinition -> CategoryDefinition -> Bool)
-> (CategoryDefinition -> CategoryDefinition -> Bool)
-> (CategoryDefinition -> CategoryDefinition -> Bool)
-> (CategoryDefinition -> CategoryDefinition -> CategoryDefinition)
-> (CategoryDefinition -> CategoryDefinition -> CategoryDefinition)
-> Ord CategoryDefinition
CategoryDefinition -> CategoryDefinition -> Bool
CategoryDefinition -> CategoryDefinition -> Ordering
CategoryDefinition -> CategoryDefinition -> CategoryDefinition
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: CategoryDefinition -> CategoryDefinition -> Ordering
compare :: CategoryDefinition -> CategoryDefinition -> Ordering
$c< :: CategoryDefinition -> CategoryDefinition -> Bool
< :: CategoryDefinition -> CategoryDefinition -> Bool
$c<= :: CategoryDefinition -> CategoryDefinition -> Bool
<= :: CategoryDefinition -> CategoryDefinition -> Bool
$c> :: CategoryDefinition -> CategoryDefinition -> Bool
> :: CategoryDefinition -> CategoryDefinition -> Bool
$c>= :: CategoryDefinition -> CategoryDefinition -> Bool
>= :: CategoryDefinition -> CategoryDefinition -> Bool
$cmax :: CategoryDefinition -> CategoryDefinition -> CategoryDefinition
max :: CategoryDefinition -> CategoryDefinition -> CategoryDefinition
$cmin :: CategoryDefinition -> CategoryDefinition -> CategoryDefinition
min :: CategoryDefinition -> CategoryDefinition -> CategoryDefinition
Ord, (forall x. CategoryDefinition -> Rep CategoryDefinition x)
-> (forall x. Rep CategoryDefinition x -> CategoryDefinition)
-> Generic CategoryDefinition
forall x. Rep CategoryDefinition x -> CategoryDefinition
forall x. CategoryDefinition -> Rep CategoryDefinition x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CategoryDefinition -> Rep CategoryDefinition x
from :: forall x. CategoryDefinition -> Rep CategoryDefinition x
$cto :: forall x. Rep CategoryDefinition x -> CategoryDefinition
to :: forall x. Rep CategoryDefinition x -> CategoryDefinition
Generic, CategoryDefinition -> ()
(CategoryDefinition -> ()) -> NFData CategoryDefinition
forall a. (a -> ()) -> NFData a
$crnf :: CategoryDefinition -> ()
rnf :: CategoryDefinition -> ()
NFData)

-- | A directive used in Brassica sound-change syntax: anything which
-- is not actually a sound change

data Directive
    = Categories Bool Bool [CategoryDefinition]
      -- ^ @categories … end@: first 'Bool' for @new@,
      -- second for @noreplace@
    | ExtraGraphemes [String]
      -- ^ @extra …@
    deriving (Int -> Directive -> ShowS
[Directive] -> ShowS
Directive -> [Char]
(Int -> Directive -> ShowS)
-> (Directive -> [Char])
-> ([Directive] -> ShowS)
-> Show Directive
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Directive -> ShowS
showsPrec :: Int -> Directive -> ShowS
$cshow :: Directive -> [Char]
show :: Directive -> [Char]
$cshowList :: [Directive] -> ShowS
showList :: [Directive] -> ShowS
Show, Directive -> Directive -> Bool
(Directive -> Directive -> Bool)
-> (Directive -> Directive -> Bool) -> Eq Directive
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Directive -> Directive -> Bool
== :: Directive -> Directive -> Bool
$c/= :: Directive -> Directive -> Bool
/= :: Directive -> Directive -> Bool
Eq, Eq Directive
Eq Directive =>
(Directive -> Directive -> Ordering)
-> (Directive -> Directive -> Bool)
-> (Directive -> Directive -> Bool)
-> (Directive -> Directive -> Bool)
-> (Directive -> Directive -> Bool)
-> (Directive -> Directive -> Directive)
-> (Directive -> Directive -> Directive)
-> Ord Directive
Directive -> Directive -> Bool
Directive -> Directive -> Ordering
Directive -> Directive -> Directive
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Directive -> Directive -> Ordering
compare :: Directive -> Directive -> Ordering
$c< :: Directive -> Directive -> Bool
< :: Directive -> Directive -> Bool
$c<= :: Directive -> Directive -> Bool
<= :: Directive -> Directive -> Bool
$c> :: Directive -> Directive -> Bool
> :: Directive -> Directive -> Bool
$c>= :: Directive -> Directive -> Bool
>= :: Directive -> Directive -> Bool
$cmax :: Directive -> Directive -> Directive
max :: Directive -> Directive -> Directive
$cmin :: Directive -> Directive -> Directive
min :: Directive -> Directive -> Directive
Ord, (forall x. Directive -> Rep Directive x)
-> (forall x. Rep Directive x -> Directive) -> Generic Directive
forall x. Rep Directive x -> Directive
forall x. Directive -> Rep Directive x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Directive -> Rep Directive x
from :: forall x. Directive -> Rep Directive x
$cto :: forall x. Rep Directive x -> Directive
to :: forall x. Rep Directive x -> Directive
Generic, Directive -> ()
(Directive -> ()) -> NFData Directive
forall a. (a -> ()) -> NFData a
$crnf :: Directive -> ()
rnf :: Directive -> ()
NFData)