{-# LANGUAGE EmptyDataDecls, QuasiQuotes, TypeFamilies, UndecidableInstances,
  TemplateHaskell, DataKinds #-}

{- |

Module      :  Type.Ord
Copyright   :  (c) The University of Kansas 2011
License     :  BSD3

Maintainer  :  nicolas.frisby@gmail.com
Stability   :  experimental
Portability :  see LANGUAGE pragmas (... GHC)

Type-level comparison operator, its result \"kind\", and that kind's case
expression. Plus instances for @()@-terminated @type-digits@ numerals.

-}
module Type.Ord (module Type.Ord.Base) where

import Type.Digits (digit, radix, digitTypes)
import Type.Digits.Stage0 (Digit(DigitStop))

import Type.Ord.Base

import Language.Haskell.TH



type instance Compare 'DigitStop 'DigitStop = 'EQ
fmap concat $ sequence [ do
  let dL = return pureTy
      [l, r] = map (varT . mkName) ["l", "r"]
      f x y z = tySynInstD ''Compare [x, y] z
  d <- f [t| $dL $l |] [t| $dL $r |] [t| Compare $l $r |]
  ((d:) . concat) `fmap` sequence [
    let dR = promotedT $ digit m in
    sequence [f [t| $dL $l |] [t| $dR $r |] [t| 'LT |],
              f [t| $dR $l |] [t| $dL $r |] [t| 'GT |]]
    | m <- [n + 1..radix - 1] ]
  | (n, pureTy) <- zip [0..] digitTypes ]