{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE CPP #-}
-- | Utilities for working with 'KnownChar' constraints.
--
-- This module is only available on GHC 9.2 or later.
module Data.Constraint.Char
  ( CharToNat
  , NatToChar
  , charToNat
  , natToChar
  ) where

import Data.Char
import Data.Constraint
import Data.Proxy
import GHC.TypeLits
#if MIN_VERSION_base(4,18,0)
import Data.Constraint.Unsafe
import qualified GHC.TypeNats as TN
#else
import Unsafe.Coerce
#endif

-- implementation details

#if !MIN_VERSION_base(4,18,0)
newtype Magic c = Magic (KnownChar c => Dict (KnownChar c))
#endif

magicCN :: forall c n. (Char -> Int) -> KnownChar c :- KnownNat n
#if MIN_VERSION_base(4,18,0)
magicCN f = Sub $ TN.withKnownNat (unsafeSNat @n (fromIntegral (f (charVal (Proxy @c))))) Dict
#else
magicCN :: forall (c :: Char) (n :: Nat).
(Char -> Int) -> KnownChar c :- KnownNat n
magicCN Char -> Int
f = forall (a :: Constraint) (b :: Constraint). (a => Dict b) -> a :- b
Sub forall a b. (a -> b) -> a -> b
$ forall a b. a -> b
unsafeCoerce (forall (c :: Char). (KnownChar c => Dict (KnownChar c)) -> Magic c
Magic forall (a :: Constraint). a => Dict a
Dict) (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int @Natural (Char -> Int
f (forall (n :: Char) (proxy :: Char -> *).
KnownChar n =>
proxy n -> Char
charVal (forall {k} (t :: k). Proxy t
Proxy @c))))
#endif

magicNC :: forall n c. (Int -> Char) -> KnownNat n :- KnownChar c
#if MIN_VERSION_base(4,18,0)
magicNC f = Sub $ withKnownChar (unsafeSChar @c (f (fromIntegral (natVal (Proxy @n))))) Dict
#else
magicNC :: forall (n :: Nat) (c :: Char).
(Int -> Char) -> KnownNat n :- KnownChar c
magicNC Int -> Char
f = forall (a :: Constraint) (b :: Constraint). (a => Dict b) -> a :- b
Sub forall a b. (a -> b) -> a -> b
$ forall a b. a -> b
unsafeCoerce (forall (c :: Char). (KnownChar c => Dict (KnownChar c)) -> Magic c
Magic forall (a :: Constraint). a => Dict a
Dict) (Int -> Char
f (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (forall {k} (t :: k). Proxy t
Proxy @n))))
#endif

-- operations

charToNat :: forall c. KnownChar c :- KnownNat (CharToNat c)
charToNat :: forall (c :: Char). KnownChar c :- KnownNat (CharToNat c)
charToNat = forall (c :: Char) (n :: Nat).
(Char -> Int) -> KnownChar c :- KnownNat n
magicCN Char -> Int
ord

-- NB: 0x10FFFF the maximum value for a Unicode code point. Calling `chr` on
-- anything greater will throw an exception.
natToChar :: forall n. (n <= 0x10FFFF, KnownNat n) :- KnownChar (NatToChar n)
natToChar :: forall (n :: Nat).
(n <= 1114111, KnownNat n) :- KnownChar (NatToChar n)
natToChar = forall (a :: Constraint) (b :: Constraint). (a => Dict b) -> a :- b
Sub forall a b. (a -> b) -> a -> b
$ case forall (n :: Nat) (c :: Char).
(Int -> Char) -> KnownNat n :- KnownChar c
magicNC @n @(NatToChar n) Int -> Char
chr of Sub KnownNat n => Dict (KnownChar (NatToChar n))
r -> KnownNat n => Dict (KnownChar (NatToChar n))
r