{-# LANGUAGE UndecidableInstances #-}
module Raehik.GHC.TypeNats.Bits where
import GHC.TypeNats
import GHC.TypeError qualified as TE
type ShiftL x i = x * 2^i
type ShiftR x i = x `Div` 2^i
type Xor n m = XorLoop 1 0 n m
type family XorLoop factor acc n m where
XorLoop factor acc 0 0 = acc
XorLoop factor acc n m =
XorLoop
(factor*2)
(acc + factor * (((n `Mod` 2) `BitXor` (m `Mod` 2))))
(n `Div` 2)
(m `Div` 2)
type family BitXor n m where
BitXor 0 0 = 0
BitXor 0 1 = 1
BitXor 1 0 = 1
BitXor 1 1 = 0
BitXor n m = TE.TypeError (TE.Text "BitXor: got non-bit Naturals")