{-# LANGUAGE OverloadedStrings #-}

module Language.Haskell.Liquid.Types.Literals 
  ( literalFRefType
  , literalFReft
  , literalConst
  , mkI, mkS
  ) where

import Prelude hiding (error)
import Language.Haskell.Liquid.GHC.TypeRep ()
import Liquid.GHC.API hiding (panic)

import Language.Haskell.Liquid.Types.Types
import Language.Haskell.Liquid.Types.RefType
import Language.Haskell.Liquid.Transforms.CoreToLogic (mkLit, mkI, mkS)

import qualified Language.Fixpoint.Types as F

---------------------------------------------------------------
----------------------- Typing Literals -----------------------
---------------------------------------------------------------

makeRTypeBase :: Monoid r => Type -> r -> RType RTyCon RTyVar r
makeRTypeBase :: forall r. Monoid r => Type -> r -> RType RTyCon RTyVar r
makeRTypeBase (TyVarTy Var
α)    r
x
  = forall c tv r. tv -> r -> RType c tv r
RVar (Var -> RTyVar
rTyVar Var
α) r
x
makeRTypeBase (TyConApp TyCon
c [Type]
ts) r
x
  = forall tv r.
TyCon
-> [RType RTyCon tv r]
-> [RTProp RTyCon tv r]
-> r
-> RType RTyCon tv r
rApp TyCon
c ((forall r. Monoid r => Type -> r -> RType RTyCon RTyVar r
`makeRTypeBase` forall a. Monoid a => a
mempty) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Type]
ts) [] r
x
makeRTypeBase Type
_              r
_
  = forall a. Maybe SrcSpan -> String -> a
panic forall a. Maybe a
Nothing String
"RefType : makeRTypeBase"

literalFRefType :: Literal -> RType RTyCon RTyVar F.Reft
literalFRefType :: Literal -> RType RTyCon RTyVar Reft
literalFRefType Literal
l
  = forall r. Monoid r => Type -> r -> RType RTyCon RTyVar r
makeRTypeBase (Literal -> Type
literalType Literal
l) (Literal -> Reft
literalFReft Literal
l)

literalFReft :: Literal -> F.Reft
literalFReft :: Literal -> Reft
literalFReft Literal
l = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty Expr -> Reft
mkReft forall a b. (a -> b) -> a -> b
$ Literal -> Maybe Expr
mkLit Literal
l

mkReft :: F.Expr -> F.Reft
mkReft :: Expr -> Reft
mkReft = forall a. Expression a => a -> Reft
F.exprReft

-- | `literalConst` returns `Nothing` for unhandled lits because
--    otherwise string-literals show up as global int-constants
--    which blow up qualifier instantiation.

literalConst :: F.TCEmb TyCon -> Literal -> (F.Sort, Maybe F.Expr)
literalConst :: TCEmb TyCon -> Literal -> (Sort, Maybe Expr)
literalConst TCEmb TyCon
tce Literal
l = (Sort
t, Literal -> Maybe Expr
mkLit Literal
l)
  where
    t :: Sort
t              = TCEmb TyCon -> Type -> Sort
typeSort TCEmb TyCon
tce forall a b. (a -> b) -> a -> b
$ Literal -> Type
literalType Literal
l