{-# language DataKinds #-}
{-# language GADTs #-}
{-# language TypeFamilies #-}
{-# language TypeOperators #-}

module FlexTask.Form.TypeLevel where


import Data.Kind                        (Constraint, Type)
import Data.Tuple.Extra                 (first)
import GHC.TypeLits                     (ErrorMessage(..), TypeError)

import FlexTask.InputTypes              (MultipleChoice)


-- | A marker for type forms with exactly one input field
data OneField a


-- | A marker for type forms with multiple input fields, e.g. lists.
data ManyFields a


data TypeList xs where
  TOne :: OneDefault x -> TypeList (OneField x)
  TMany :: OneDefault x -> Maybe [OneDefault x] -> TypeList (ManyFields x)
  TCons :: InputDefault x -> TypeList xs -> TypeList (x :> xs)


-- | type-level non-empty list equivalent of "append" (++)
infixr 5 ++
type family xs ++ ys where
  OneField a ++ ys = OneField a :> ys
  ManyFields a ++ ys = ManyFields a :> ys
  (x :> xs) ++ ys = x :> (xs ++ ys)


singleFormDefaults :: Maybe a -> TypeList (OneField a)
singleFormDefaults :: forall a. Maybe a -> TypeList (OneField a)
singleFormDefaults Maybe a
x = OneDefault a -> TypeList (OneField a)
forall xs. OneDefault xs -> TypeList (OneField xs)
TOne (Maybe a -> OneDefault a
forall a. Maybe a -> OneDefault a
RequiredDefault Maybe a
x)


appendTypeList :: TypeList xs -> TypeList ys -> TypeList (xs ++ ys)
appendTypeList :: forall xs ys. TypeList xs -> TypeList ys -> TypeList (xs ++ ys)
appendTypeList (TOne OneDefault x
x) = InputDefault (OneField x)
-> TypeList ys -> TypeList (OneField x :> ys)
forall xs x. InputDefault xs -> TypeList x -> TypeList (xs :> x)
TCons (InputDefault (OneField x)
 -> TypeList ys -> TypeList (OneField x :> ys))
-> InputDefault (OneField x)
-> TypeList ys
-> TypeList (OneField x :> ys)
forall a b. (a -> b) -> a -> b
$ OneDefault x -> InputDefault (OneField x)
forall xs. OneDefault xs -> InputDefault (OneField xs)
OneInputDefault OneDefault x
x
appendTypeList (TMany OneDefault x
t Maybe [OneDefault x]
xs) = InputDefault (ManyFields x)
-> TypeList ys -> TypeList (ManyFields x :> ys)
forall xs x. InputDefault xs -> TypeList x -> TypeList (xs :> x)
TCons (InputDefault (ManyFields x)
 -> TypeList ys -> TypeList (ManyFields x :> ys))
-> InputDefault (ManyFields x)
-> TypeList ys
-> TypeList (ManyFields x :> ys)
forall a b. (a -> b) -> a -> b
$ OneDefault x -> Maybe [OneDefault x] -> InputDefault (ManyFields x)
forall xs.
OneDefault xs
-> Maybe [OneDefault xs] -> InputDefault (ManyFields xs)
ManyInputDefaults OneDefault x
t Maybe [OneDefault x]
xs
appendTypeList (TCons InputDefault x
x TypeList xs
xs) = InputDefault x -> TypeList (xs ++ ys) -> TypeList (x :> (xs ++ ys))
forall xs x. InputDefault xs -> TypeList x -> TypeList (xs :> x)
TCons InputDefault x
x (TypeList (xs ++ ys) -> TypeList (x :> (xs ++ ys)))
-> (TypeList ys -> TypeList (xs ++ ys))
-> TypeList ys
-> TypeList (x :> (xs ++ ys))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeList xs -> TypeList ys -> TypeList (xs ++ ys)
forall xs ys. TypeList xs -> TypeList ys -> TypeList (xs ++ ys)
appendTypeList TypeList xs
xs


data TypeShape xs where
  OneShape  :: TypeShape (OneField a)
  ManyShape :: TypeShape (ManyFields a)
  ConsShape :: TypeShape xs -> TypeShape (x :> xs)


splitTypeList :: TypeShape xs -> TypeList (xs ++ ys) -> (TypeList xs, TypeList ys)
splitTypeList :: forall xs ys.
TypeShape xs -> TypeList (xs ++ ys) -> (TypeList xs, TypeList ys)
splitTypeList TypeShape xs
OneShape (TCons (OneInputDefault OneDefault a
y) TypeList xs
ys) = (OneDefault a -> TypeList (OneField a)
forall xs. OneDefault xs -> TypeList (OneField xs)
TOne OneDefault a
y, TypeList ys
TypeList xs
ys)
splitTypeList TypeShape xs
ManyShape (TCons (ManyInputDefaults OneDefault a
t Maybe [OneDefault a]
y) TypeList xs
ys) = (OneDefault a -> Maybe [OneDefault a] -> TypeList (ManyFields a)
forall xs.
OneDefault xs -> Maybe [OneDefault xs] -> TypeList (ManyFields xs)
TMany OneDefault a
t Maybe [OneDefault a]
y, TypeList ys
TypeList xs
ys)
splitTypeList (ConsShape TypeShape xs
xs) (TCons InputDefault x
y TypeList xs
ys) = (TypeList xs -> TypeList xs)
-> (TypeList xs, TypeList ys) -> (TypeList xs, TypeList ys)
forall a a' b. (a -> a') -> (a, b) -> (a', b)
first (InputDefault x -> TypeList xs -> TypeList (x :> xs)
forall xs x. InputDefault xs -> TypeList x -> TypeList (xs :> x)
TCons InputDefault x
y) ((TypeList xs, TypeList ys) -> (TypeList xs, TypeList ys))
-> (TypeList xs, TypeList ys) -> (TypeList xs, TypeList ys)
forall a b. (a -> b) -> a -> b
$ TypeShape xs -> TypeList (xs ++ ys) -> (TypeList xs, TypeList ys)
forall xs ys.
TypeShape xs -> TypeList (xs ++ ys) -> (TypeList xs, TypeList ys)
splitTypeList TypeShape xs
xs TypeList xs
TypeList (xs ++ ys)
ys


appendShape :: TypeShape xs -> TypeShape ys -> TypeShape (xs ++ ys)
appendShape :: forall xs ys. TypeShape xs -> TypeShape ys -> TypeShape (xs ++ ys)
appendShape TypeShape xs
OneShape TypeShape ys
ys = TypeShape ys -> TypeShape (OneField a :> ys)
forall xs x. TypeShape xs -> TypeShape (x :> xs)
ConsShape TypeShape ys
ys
appendShape TypeShape xs
ManyShape TypeShape ys
ys = TypeShape ys -> TypeShape (ManyFields a :> ys)
forall xs x. TypeShape xs -> TypeShape (x :> xs)
ConsShape TypeShape ys
ys
appendShape (ConsShape TypeShape xs
xs) TypeShape ys
ys = TypeShape (xs ++ ys) -> TypeShape (x :> (xs ++ ys))
forall xs x. TypeShape xs -> TypeShape (x :> xs)
ConsShape (TypeShape xs -> TypeShape ys -> TypeShape (xs ++ ys)
forall xs ys. TypeShape xs -> TypeShape ys -> TypeShape (xs ++ ys)
appendShape TypeShape xs
xs TypeShape ys
ys)


-- | Type-level non-empty list equivalent of "cons" (:)
infixr 6 :>
data x :> xs


type family SingleInputType fields :: Type where
  SingleInputType (OneField a) = a

  SingleInputType fields = TypeError
    ( 'Text "This type does not correspond to exactly one input field."
    )


getSingleDefault :: TypeList fields -> OneDefault (SingleInputType fields)
getSingleDefault :: forall fields.
TypeList fields -> OneDefault (SingleInputType fields)
getSingleDefault (TOne OneDefault x
d) = OneDefault x
OneDefault (SingleInputType fields)
d
getSingleDefault TypeList fields
_ = [Char] -> OneDefault (SingleInputType fields)
forall a. HasCallStack => [Char] -> a
error [Char]
"unreachable: SingleInputType rejected this form shape"


data OneDefault a
  = RequiredDefault (Maybe a)
  | OptionalDefault (Maybe a)


data InputDefault a where
  OneInputDefault :: OneDefault a -> InputDefault (OneField a)
  ManyInputDefaults :: OneDefault a -> Maybe [OneDefault a] -> InputDefault (ManyFields a)


-- | A constraint for types that can meaningfully be made optional.
type family CanBeOptional a :: Constraint where
  CanBeOptional (MultipleChoice a) =
    TypeError
      ( 'Text "MultipleChoice cannot be optional."
        ':$$:
        'Text "No selection is represented by the empty list."
      )
  CanBeOptional [a] =
    TypeError
      ( 'Text "Lists cannot be optional as a whole."
        ':$$:
        'Text "Use type [Maybe a] to turn all fields optional instead."
      )
  CanBeOptional a = ()