{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE PartialTypeSignatures #-}

module Language.Haskell.Liquid.Bare.Plugged 
  ( makePluggedSig
  , makePluggedDataCon
  ) where

import Prelude hiding (error)
import Data.Generics.Aliases (mkT)
import Data.Generics.Schemes (everywhere)

import           Text.PrettyPrint.HughesPJ
import qualified Control.Exception                 as Ex
import qualified Data.HashMap.Strict               as M
import qualified Data.HashSet                      as S
import qualified Data.Maybe                        as Mb 
import qualified Data.List                         as L 
import qualified Language.Fixpoint.Types           as F
import qualified Language.Fixpoint.Types.Visitor   as F
import qualified Language.Haskell.Liquid.GHC.Misc  as GM 
import qualified Language.Haskell.Liquid.GHC.API   as Ghc 
import           Language.Haskell.Liquid.GHC.Types (StableName, mkStableName)
import           Language.Haskell.Liquid.Types.RefType ()
import           Language.Haskell.Liquid.Types
import qualified Language.Haskell.Liquid.Misc       as Misc 
import qualified Language.Haskell.Liquid.Bare.Types as Bare
import qualified Language.Haskell.Liquid.Bare.Misc  as Bare

---------------------------------------------------------------------------------------
-- [NOTE: Plug-Holes-TyVars] We have _two_ versions of `plugHoles:
-- * `HsTyVars` ensures that the returned signature uses the GHC type variables;
--   We need this as these tyvars can appear in the SOURCE (as type annotations, or
--   as the types of lambdas) and renaming them will cause problems; 
-- * `LqTyVars` ensures that the returned signatuer uses the LIQUID type variables; 
--   We need this e.g. for class specifications where we cannot change the tyvars 
--   used inside method signatures as that messes up the type for the data-constructor 
--   for the dictionary (as we need to use the same tyvars as are "bound" in the class 
--   definition).
-- In short, use `HsTyVars` when we also have to analyze the binder's SOURCE; and 
-- otherwise, use `LqTyVars`.
---------------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- | NOTE: Be *very* careful with the use functions from RType -> GHC.Type,
--   e.g. toType, in this module as they cannot handle LH type holes. Since
--   this module is responsible for plugging the holes we obviously cannot
--   assume, as in e.g. L.H.L.Constraint.* that they do not appear.
--------------------------------------------------------------------------------
makePluggedSig :: ModName -> F.TCEmb Ghc.TyCon -> TyConMap -> S.HashSet StableName
               -> Bare.PlugTV Ghc.Var -> LocSpecType
               -> LocSpecType

makePluggedSig :: ModName
-> TCEmb TyCon
-> TyConMap
-> HashSet StableName
-> PlugTV Var
-> LocSpecType
-> LocSpecType
makePluggedSig ModName
name TCEmb TyCon
embs TyConMap
tyi HashSet StableName
exports PlugTV Var
kx LocSpecType
t 
  | Just Var
x <- Maybe Var
kxv = Var -> LocSpecType
mkPlug Var
x 
  | Bool
otherwise     = LocSpecType
t
  where 
    kxv :: Maybe Var
kxv           = PlugTV Var -> Maybe Var
forall v. PlugTV v -> Maybe v
Bare.plugSrc PlugTV Var
kx
    mkPlug :: Var -> LocSpecType
mkPlug Var
x      = PlugTV Var
-> TCEmb TyCon
-> TyConMap
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
forall a.
(NamedThing a, PPrint a, Show a) =>
PlugTV a
-> TCEmb TyCon
-> TyConMap
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
plugHoles PlugTV Var
kx TCEmb TyCon
embs TyConMap
tyi  SpecType -> RReft -> RReft
r Type
τ LocSpecType
t
      where
        τ :: Type
τ         = Type -> Type
Ghc.expandTypeSynonyms (Var -> Type
Ghc.varType Var
x)
        r :: SpecType -> RReft -> RReft
r         = Var -> ModName -> HashSet StableName -> SpecType -> RReft -> RReft
forall a.
NamedThing a =>
a -> ModName -> HashSet StableName -> SpecType -> RReft -> RReft
maybeTrue Var
x ModName
name HashSet StableName
exports
    -- x = case kx of { Bare.HsTV x -> x ; Bare.LqTV x -> x }


-- makePluggedDataCon = makePluggedDataCon_old 
-- plugHoles          = plugHoles_old 
-- makePluggedDataCon = makePluggedDataCon_new 

-- plugHoles _         = plugHoles_old

plugHoles :: (Ghc.NamedThing a, PPrint a, Show a) 
          => Bare.PlugTV a
          -> F.TCEmb Ghc.TyCon
          -> Bare.TyConMap
          -> (SpecType -> RReft -> RReft)
          -> Ghc.Type
          -> LocSpecType
          -> LocSpecType
plugHoles :: PlugTV a
-> TCEmb TyCon
-> TyConMap
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
plugHoles (Bare.HsTV a
x) TCEmb TyCon
a TyConMap
b = TCEmb TyCon
-> TyConMap
-> a
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
forall a.
(NamedThing a, PPrint a, Show a) =>
TCEmb TyCon
-> TyConMap
-> a
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
plugHoles_old TCEmb TyCon
a TyConMap
b a
x 
plugHoles (Bare.LqTV a
x) TCEmb TyCon
a TyConMap
b = TCEmb TyCon
-> TyConMap
-> a
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
forall a.
(NamedThing a, PPrint a, Show a) =>
TCEmb TyCon
-> TyConMap
-> a
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
plugHoles_new TCEmb TyCon
a TyConMap
b a
x 
plugHoles PlugTV a
_             TCEmb TyCon
_ TyConMap
_ = \SpecType -> RReft -> RReft
_ Type
_ LocSpecType
t -> LocSpecType
t


makePluggedDataCon :: F.TCEmb Ghc.TyCon -> Bare.TyConMap -> Located DataConP -> Located DataConP
makePluggedDataCon :: TCEmb TyCon -> TyConMap -> Located DataConP -> Located DataConP
makePluggedDataCon TCEmb TyCon
embs TyConMap
tyi Located DataConP
ldcp 
  | Bool
mismatchFlds      = UserError -> Located DataConP
forall a e. Exception e => e -> a
Ex.throw (Doc -> UserError
err Doc
"fields")
  | Bool
mismatchTyVars    = UserError -> Located DataConP
forall a e. Exception e => e -> a
Ex.throw (Doc -> UserError
err Doc
"type variables")
  | Bool
otherwise         = Located DataConP -> DataConP -> Located DataConP
forall l b. Loc l => l -> b -> Located b
F.atLoc Located DataConP
ldcp (DataConP -> Located DataConP) -> DataConP -> Located DataConP
forall a b. (a -> b) -> a -> b
$ String -> DataConP -> DataConP
forall a. PPrint a => String -> a -> a
F.notracepp String
"makePluggedDataCon" (DataConP -> DataConP) -> DataConP -> DataConP
forall a b. (a -> b) -> a -> b
$ DataConP
dcp 
                          { dcpFreeTyVars :: [RTyVar]
dcpFreeTyVars = [RTyVar]
dcVars
                          , dcpTyArgs :: [(Symbol, SpecType)]
dcpTyArgs     = [(Symbol, SpecType)] -> [(Symbol, SpecType)]
forall a. [a] -> [a]
reverse [(Symbol, SpecType)]
tArgs 
                          , dcpTyRes :: SpecType
dcpTyRes      = SpecType
tRes 
                          }
  where 
    ([(Symbol, SpecType)]
tArgs, SpecType
tRes)     = TCEmb TyCon
-> TyConMap
-> Located DataConP
-> ([Var], [Type], Type)
-> ([RTyVar], [(Symbol, SpecType)], SpecType)
-> ([(Symbol, SpecType)], SpecType)
plugMany       TCEmb TyCon
embs TyConMap
tyi Located DataConP
ldcp ([Var]
das, [Type]
dts, Type
dt) ([RTyVar]
dcVars, [(Symbol, SpecType)]
dcArgs, DataConP -> SpecType
dcpTyRes DataConP
dcp)
    ([Var]
das, [Type]
_, [Type]
dts, Type
dt) = {- F.notracepp ("makePluggedDC: " ++ F.showpp dc) $ -} DataCon -> ([Var], [Type], [Type], Type)
Ghc.dataConSig DataCon
dc
    dcArgs :: [(Symbol, SpecType)]
dcArgs            = ((Symbol, SpecType) -> Bool)
-> [(Symbol, SpecType)] -> [(Symbol, SpecType)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ((Symbol, SpecType) -> Bool) -> (Symbol, SpecType) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SpecType -> Bool
forall c t t1. TyConable c => RType c t t1 -> Bool
isClassType (SpecType -> Bool)
-> ((Symbol, SpecType) -> SpecType) -> (Symbol, SpecType) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Symbol, SpecType) -> SpecType
forall a b. (a, b) -> b
snd) ([(Symbol, SpecType)] -> [(Symbol, SpecType)])
-> [(Symbol, SpecType)] -> [(Symbol, SpecType)]
forall a b. (a -> b) -> a -> b
$ [(Symbol, SpecType)] -> [(Symbol, SpecType)]
forall a. [a] -> [a]
reverse (DataConP -> [(Symbol, SpecType)]
dcpTyArgs DataConP
dcp)
    dcVars :: [RTyVar]
dcVars            = if Bool
isGADT 
                          then [RTyVar] -> [RTyVar]
padGADVars ([RTyVar] -> [RTyVar]) -> [RTyVar] -> [RTyVar]
forall a b. (a -> b) -> a -> b
$ [RTyVar] -> [RTyVar]
forall a. Eq a => [a] -> [a]
L.nub (DataConP -> [RTyVar]
dcpFreeTyVars DataConP
dcp [RTyVar] -> [RTyVar] -> [RTyVar]
forall a. [a] -> [a] -> [a]
++ ((SpecType -> [RTyVar]) -> [SpecType] -> [RTyVar]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((RTVar RTyVar (RType RTyCon RTyVar ()) -> RTyVar)
-> [RTVar RTyVar (RType RTyCon RTyVar ())] -> [RTyVar]
forall a b. (a -> b) -> [a] -> [b]
map RTVar RTyVar (RType RTyCon RTyVar ()) -> RTyVar
forall tv s. RTVar tv s -> tv
ty_var_value ([RTVar RTyVar (RType RTyCon RTyVar ())] -> [RTyVar])
-> (SpecType -> [RTVar RTyVar (RType RTyCon RTyVar ())])
-> SpecType
-> [RTyVar]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SpecType -> [RTVar RTyVar (RType RTyCon RTyVar ())]
forall tv c r. Eq tv => RType c tv r -> [RTVar tv (RType c tv ())]
freeTyVars) (DataConP -> SpecType
dcpTyRes DataConP
dcpSpecType -> [SpecType] -> [SpecType]
forall a. a -> [a] -> [a]
:((Symbol, SpecType) -> SpecType
forall a b. (a, b) -> b
snd ((Symbol, SpecType) -> SpecType)
-> [(Symbol, SpecType)] -> [SpecType]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Symbol, SpecType)]
dcArgs))))
                          else DataConP -> [RTyVar]
dcpFreeTyVars DataConP
dcp 
    dc :: DataCon
dc                = DataConP -> DataCon
dcpCon        DataConP
dcp
    dcp :: DataConP
dcp               = Located DataConP -> DataConP
forall a. Located a -> a
val           Located DataConP
ldcp 

    isGADT :: Bool
isGADT            = TyCon -> Bool
Ghc.isGadtSyntaxTyCon (TyCon -> Bool) -> TyCon -> Bool
forall a b. (a -> b) -> a -> b
$ DataCon -> TyCon
Ghc.dataConTyCon DataCon
dc

    -- hack to match LH and GHC GADT vars, since it is unclear how ghc generates free vars 
    padGADVars :: [RTyVar] -> [RTyVar]
padGADVars [RTyVar]
vs = (Var -> RTyVar
RTV (Var -> RTyVar) -> [Var] -> [RTyVar]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> [Var] -> [Var]
forall a. Int -> [a] -> [a]
take ([Var] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Var]
das Int -> Int -> Int
forall a. Num a => a -> a -> a
- [RTyVar] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [RTyVar]
vs) [Var]
das) [RTyVar] -> [RTyVar] -> [RTyVar]
forall a. [a] -> [a] -> [a]
++ [RTyVar]
vs

    mismatchFlds :: Bool
mismatchFlds      = [Type] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Type]
dts Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= [(Symbol, SpecType)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Symbol, SpecType)]
dcArgs 
    mismatchTyVars :: Bool
mismatchTyVars    = [Var] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Var]
das Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= [RTyVar] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [RTyVar]
dcVars 
    err :: Doc -> UserError
err Doc
things        = SrcSpan -> Doc -> Doc -> UserError
forall t. SrcSpan -> Doc -> Doc -> TError t
ErrBadData (DataConP -> SrcSpan
forall a. Loc a => a -> SrcSpan
GM.fSrcSpan DataConP
dcp) (DataCon -> Doc
forall a. PPrint a => a -> Doc
pprint DataCon
dc) (Doc
"GHC and Liquid specifications have different numbers of" Doc -> Doc -> Doc
<+> Doc
things) :: UserError


-- | @plugMany@ is used to "simultaneously" plug several different types, 
--   e.g. as arise in the fields of a data constructor. To do so we create 
--   a single "function type" that is then passed into @plugHoles@. 
--   We also pass in the type parameters as dummy arguments, because, e.g. 
--   we want @plugMany@ on the two types
--  
--      forall a. a -> a -> Bool 
--      forall b. _ -> _ -> _ 
-- 
--   to return something like 
-- 
--      forall b. b -> b -> Bool
-- 
--   and not, forall b. a -> a -> Bool.

plugMany :: F.TCEmb Ghc.TyCon -> Bare.TyConMap 
         -> Located DataConP            
         -> ([Ghc.Var], [Ghc.Type],             Ghc.Type)   -- ^ hs type 
         -> ([RTyVar] , [(F.Symbol, SpecType)], SpecType)   -- ^ lq type 
         -> ([(F.Symbol, SpecType)], SpecType)              -- ^ plugged lq type
plugMany :: TCEmb TyCon
-> TyConMap
-> Located DataConP
-> ([Var], [Type], Type)
-> ([RTyVar], [(Symbol, SpecType)], SpecType)
-> ([(Symbol, SpecType)], SpecType)
plugMany TCEmb TyCon
embs TyConMap
tyi Located DataConP
ldcp ([Var]
hsAs, [Type]
hsArgs, Type
hsRes) ([RTyVar]
lqAs, [(Symbol, SpecType)]
lqArgs, SpecType
lqRes) 
                     = String
-> ([(Symbol, SpecType)], SpecType)
-> ([(Symbol, SpecType)], SpecType)
forall a. PPrint a => String -> a -> a
F.notracepp String
msg (Int -> [(Symbol, SpecType)] -> [(Symbol, SpecType)]
forall a. Int -> [a] -> [a]
drop Int
nTyVars ([Symbol] -> [SpecType] -> [(Symbol, SpecType)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Symbol]
xs [SpecType]
ts), SpecType
t) 
  where 
    (([Symbol], [SpecType], [RReft])
_,([Symbol]
xs,[SpecType]
ts,[RReft]
_), SpecType
t) = SpecType
-> (([Symbol], [SpecType], [RReft]),
    ([Symbol], [SpecType], [RReft]), SpecType)
forall t t1 a.
RType t t1 a
-> (([Symbol], [RType t t1 a], [a]),
    ([Symbol], [RType t t1 a], [a]), RType t t1 a)
bkArrow (LocSpecType -> SpecType
forall a. Located a -> a
val LocSpecType
pT) 
    pT :: LocSpecType
pT               = PlugTV Name
-> TCEmb TyCon
-> TyConMap
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
forall a.
(NamedThing a, PPrint a, Show a) =>
PlugTV a
-> TCEmb TyCon
-> TyConMap
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
plugHoles (Name -> PlugTV Name
forall v. v -> PlugTV v
Bare.LqTV Name
dcName) TCEmb TyCon
embs TyConMap
tyi ((RReft -> RReft) -> SpecType -> RReft -> RReft
forall a b. a -> b -> a
const RReft -> RReft
killHoles) Type
hsT (Located DataConP -> SpecType -> LocSpecType
forall l b. Loc l => l -> b -> Located b
F.atLoc Located DataConP
ldcp SpecType
lqT)
    hsT :: Type
hsT              = (Type -> Type -> Type) -> Type -> [Type] -> Type
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (AnonArgFlag -> Type -> Type -> Type
Ghc.mkFunTy AnonArgFlag
Ghc.VisArg) Type
hsRes [Type]
hsArgs' 
    lqT :: SpecType
lqT              = ((Symbol, SpecType) -> SpecType -> SpecType)
-> SpecType -> [(Symbol, SpecType)] -> SpecType
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((Symbol -> SpecType -> SpecType -> SpecType)
-> (Symbol, SpecType) -> SpecType -> SpecType
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Symbol -> SpecType -> SpecType -> SpecType
forall r c tv.
Monoid r =>
Symbol -> RType c tv r -> RType c tv r -> RType c tv r
rFun) SpecType
lqRes [(Symbol, SpecType)]
lqArgs' 
    hsArgs' :: [Type]
hsArgs'          = [ Var -> Type
Ghc.mkTyVarTy Var
a               | Var
a <- [Var]
hsAs] [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
hsArgs 
    lqArgs' :: [(Symbol, SpecType)]
lqArgs'          = [(Symbol
F.dummySymbol, RTyVar -> RReft -> SpecType
forall c tv r. tv -> r -> RType c tv r
RVar RTyVar
a RReft
forall a. Monoid a => a
mempty) | RTyVar
a <- [RTyVar]
lqAs] [(Symbol, SpecType)]
-> [(Symbol, SpecType)] -> [(Symbol, SpecType)]
forall a. [a] -> [a] -> [a]
++ [(Symbol, SpecType)]
lqArgs 
    nTyVars :: Int
nTyVars          = [Var] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Var]
hsAs -- == length lqAs
    dcName :: Name
dcName           = DataCon -> Name
Ghc.dataConName (DataCon -> Name)
-> (Located DataConP -> DataCon) -> Located DataConP -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataConP -> DataCon
dcpCon (DataConP -> DataCon)
-> (Located DataConP -> DataConP) -> Located DataConP -> DataCon
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Located DataConP -> DataConP
forall a. Located a -> a
val (Located DataConP -> Name) -> Located DataConP -> Name
forall a b. (a -> b) -> a -> b
$ Located DataConP
ldcp
    msg :: String
msg              = String
"plugMany: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Name, Type, SpecType) -> String
forall a. PPrint a => a -> String
F.showpp (Name
dcName, Type
hsT, SpecType
lqT)

plugHoles_old, plugHoles_new 
  :: (Ghc.NamedThing a, PPrint a, Show a)
  => F.TCEmb Ghc.TyCon
  -> Bare.TyConMap 
  -> a
  -> (SpecType -> RReft -> RReft)
  -> Ghc.Type
  -> LocSpecType
  -> LocSpecType
  
-- NOTE: this use of toType is safe as rt' is derived from t.
plugHoles_old :: TCEmb TyCon
-> TyConMap
-> a
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
plugHoles_old TCEmb TyCon
tce TyConMap
tyi a
x SpecType -> RReft -> RReft
f Type
t0 zz :: LocSpecType
zz@(Loc SourcePos
l SourcePos
l' SpecType
st0) 
    = SourcePos -> SourcePos -> SpecType -> LocSpecType
forall a. SourcePos -> SourcePos -> a -> Located a
Loc SourcePos
l SourcePos
l' 
    (SpecType -> LocSpecType)
-> (SpecType -> SpecType) -> SpecType -> LocSpecType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
-> [PVar (RType RTyCon RTyVar ())]
-> [(Symbol, SpecType, RReft)]
-> [(Symbol, SpecType, RReft)]
-> SpecType
-> SpecType
forall tv c r.
[(RTVar tv (RType c tv ()), r)]
-> [PVar (RType c tv ())]
-> [(Symbol, RType c tv r, r)]
-> [(Symbol, RType c tv r, r)]
-> RType c tv r
-> RType c tv r
mkArrow ([RTVar RTyVar (RType RTyCon RTyVar ())]
-> [RReft] -> [(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
forall a b. [a] -> [b] -> [(a, b)]
zip (RTVar RTyVar (RType RTyCon RTyVar ())
-> RTVar RTyVar (RType RTyCon RTyVar ())
forall r i.
Monoid r =>
RTVar RTyVar i -> RTVar RTyVar (RType RTyCon RTyVar r)
updateRTVar (RTVar RTyVar (RType RTyCon RTyVar ())
 -> RTVar RTyVar (RType RTyCon RTyVar ()))
-> [RTVar RTyVar (RType RTyCon RTyVar ())]
-> [RTVar RTyVar (RType RTyCon RTyVar ())]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [RTVar RTyVar (RType RTyCon RTyVar ())]
αs') [RReft]
rs) [PVar (RType RTyCon RTyVar ())]
ps' [] [] 
    (SpecType -> SpecType)
-> (SpecType -> SpecType) -> SpecType -> SpecType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Symbol, SpecType)] -> SpecType -> SpecType
forall (t :: * -> *) r c tv.
(Foldable t, Monoid r) =>
t (Symbol, RType c tv r) -> RType c tv r -> RType c tv r
makeCls [(Symbol, SpecType)]
cs' 
    (SpecType -> SpecType)
-> (SpecType -> SpecType) -> SpecType -> SpecType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TCEmb TyCon
-> TyConMap
-> (Doc -> Doc -> Error)
-> (SpecType -> RReft -> RReft)
-> SpecType
-> SpecType
-> SpecType
goPlug TCEmb TyCon
tce TyConMap
tyi Doc -> Doc -> Error
forall t. Doc -> Doc -> TError t
err SpecType -> RReft -> RReft
f ([(RTyVar, RTyVar)] -> SpecType -> SpecType
forall tv ty c. SubsTy tv ty c => [(tv, ty)] -> c -> c
subts [(RTyVar, RTyVar)]
su SpecType
rt) 
    (SpecType -> SpecType)
-> (SpecType -> SpecType) -> SpecType -> SpecType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Symbol -> Expr -> Expr) -> SpecType -> SpecType
forall c tv.
(Symbol -> Expr -> Expr) -> RType c tv RReft -> RType c tv RReft
mapExprReft (\Symbol
_ -> CoSub -> Expr -> Expr
F.applyCoSub CoSub
coSub) 
    (SpecType -> SpecType)
-> (SpecType -> SpecType) -> SpecType -> SpecType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(RTyVar, RTyVar)] -> SpecType -> SpecType
forall tv ty c. SubsTy tv ty c => [(tv, ty)] -> c -> c
subts [(RTyVar, RTyVar)]
su 
    (SpecType -> LocSpecType) -> SpecType -> LocSpecType
forall a b. (a -> b) -> a -> b
$ SpecType
st 
  where 
    tyvsmap :: [(Var, RTyVar)]
tyvsmap      = case Type
-> SpecType -> (Doc -> Doc -> Error) -> Either Error MapTyVarST
Bare.runMapTyVars (SpecType -> Type
forall r. ToTypeable r => RRType r -> Type
toType SpecType
rt) SpecType
st Doc -> Doc -> Error
forall t. Doc -> Doc -> TError t
err of
                          Left Error
e  -> Error -> [(Var, RTyVar)]
forall a e. Exception e => e -> a
Ex.throw Error
e 
                          Right MapTyVarST
s -> MapTyVarST -> [(Var, RTyVar)]
Bare.vmap MapTyVarST
s
    su :: [(RTyVar, RTyVar)]
su           = [(RTyVar
y, Var -> RTyVar
rTyVar Var
x)           | (Var
x, RTyVar
y) <- [(Var, RTyVar)]
tyvsmap]
    su' :: [(RTyVar, RType RTyCon RTyVar ())]
su'          = [(RTyVar
y, RTyVar -> () -> RType RTyCon RTyVar ()
forall c tv r. tv -> r -> RType c tv r
RVar (Var -> RTyVar
rTyVar Var
x) ()) | (Var
x, RTyVar
y) <- [(Var, RTyVar)]
tyvsmap] :: [(RTyVar, RSort)]
    coSub :: CoSub
coSub        = [(Symbol, Sort)] -> CoSub
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList [(RTyVar -> Symbol
forall a. Symbolic a => a -> Symbol
F.symbol RTyVar
y, Symbol -> Sort
F.FObj (RTyVar -> Symbol
forall a. Symbolic a => a -> Symbol
F.symbol RTyVar
x)) | (RTyVar
y, RTyVar
x) <- [(RTyVar, RTyVar)]
su]
    ps' :: [PVar (RType RTyCon RTyVar ())]
ps'          = (RType RTyCon RTyVar () -> RType RTyCon RTyVar ())
-> PVar (RType RTyCon RTyVar ()) -> PVar (RType RTyCon RTyVar ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([(RTyVar, RType RTyCon RTyVar ())]
-> RType RTyCon RTyVar () -> RType RTyCon RTyVar ()
forall tv ty c. SubsTy tv ty c => [(tv, ty)] -> c -> c
subts [(RTyVar, RType RTyCon RTyVar ())]
su') (PVar (RType RTyCon RTyVar ()) -> PVar (RType RTyCon RTyVar ()))
-> [PVar (RType RTyCon RTyVar ())]
-> [PVar (RType RTyCon RTyVar ())]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [PVar (RType RTyCon RTyVar ())]
ps
    cs' :: [(Symbol, SpecType)]
cs'          = [(Symbol
F.dummySymbol, RTyCon
-> [SpecType] -> [RTProp RTyCon RTyVar RReft] -> RReft -> SpecType
forall c tv r.
c -> [RType c tv r] -> [RTProp c tv r] -> r -> RType c tv r
RApp RTyCon
c [SpecType]
ts [] RReft
forall a. Monoid a => a
mempty) | (RTyCon
c, [SpecType]
ts) <- [(RTyCon, [SpecType])]
cs ] 
    ([RTVar RTyVar (RType RTyCon RTyVar ())]
αs', [RReft]
rs)    = [(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
-> ([RTVar RTyVar (RType RTyCon RTyVar ())], [RReft])
forall a b. [(a, b)] -> ([a], [b])
unzip [(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
αs
    ([(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
αs,[PVar (RType RTyCon RTyVar ())]
_,[(RTyCon, [SpecType])]
cs,SpecType
rt) = SpecType
-> ([(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)],
    [PVar (RType RTyCon RTyVar ())], [(RTyCon, [SpecType])], SpecType)
bkUnivClass (String -> SpecType -> SpecType
forall a. PPrint a => String -> a -> a
F.notracepp String
"hs-spec" (SpecType -> SpecType) -> SpecType -> SpecType
forall a b. (a -> b) -> a -> b
$ Type -> SpecType
forall r. Monoid r => Type -> RRType r
ofType (Type -> Type
Ghc.expandTypeSynonyms Type
t0) :: SpecType)
    ([(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
_,[PVar (RType RTyCon RTyVar ())]
ps,[(RTyCon, [SpecType])]
_ ,SpecType
st) = SpecType
-> ([(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)],
    [PVar (RType RTyCon RTyVar ())], [(RTyCon, [SpecType])], SpecType)
bkUnivClass (String -> SpecType -> SpecType
forall a. PPrint a => String -> a -> a
F.notracepp String
"lq-spec" SpecType
st0)

    makeCls :: t (Symbol, RType c tv r) -> RType c tv r -> RType c tv r
makeCls t (Symbol, RType c tv r)
cs RType c tv r
t = ((Symbol, RType c tv r) -> RType c tv r -> RType c tv r)
-> RType c tv r -> t (Symbol, RType c tv r) -> RType c tv r
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((Symbol -> RType c tv r -> RType c tv r -> RType c tv r)
-> (Symbol, RType c tv r) -> RType c tv r -> RType c tv r
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Symbol -> RType c tv r -> RType c tv r -> RType c tv r
forall r c tv.
Monoid r =>
Symbol -> RType c tv r -> RType c tv r -> RType c tv r
rFun) RType c tv r
t t (Symbol, RType c tv r)
cs
    err :: Doc -> Doc -> TError t
err Doc
hsT Doc
lqT  = SrcSpan
-> Doc
-> Doc
-> Doc
-> Doc
-> Maybe (Doc, Doc)
-> SrcSpan
-> TError t
forall t.
SrcSpan
-> Doc
-> Doc
-> Doc
-> Doc
-> Maybe (Doc, Doc)
-> SrcSpan
-> TError t
ErrMismatch (LocSpecType -> SrcSpan
forall a. Loc a => a -> SrcSpan
GM.fSrcSpan LocSpecType
zz) (a -> Doc
forall a. PPrint a => a -> Doc
pprint a
x) 
                          (String -> Doc
text String
"Plugged Init types old")
                          (Type -> Doc
forall a. PPrint a => a -> Doc
pprint (Type -> Doc) -> Type -> Doc
forall a b. (a -> b) -> a -> b
$ Type -> Type
Ghc.expandTypeSynonyms Type
t0)
                          (RType RTyCon RTyVar () -> Doc
forall a. PPrint a => a -> Doc
pprint (RType RTyCon RTyVar () -> Doc) -> RType RTyCon RTyVar () -> Doc
forall a b. (a -> b) -> a -> b
$ SpecType -> RType RTyCon RTyVar ()
forall c tv r. RType c tv r -> RType c tv ()
toRSort SpecType
st0)
                          ((Doc, Doc) -> Maybe (Doc, Doc)
forall a. a -> Maybe a
Just (Doc
hsT, Doc
lqT))
                          (a -> SrcSpan
forall a. NamedThing a => a -> SrcSpan
Ghc.getSrcSpan a
x) 


plugHoles_new :: TCEmb TyCon
-> TyConMap
-> a
-> (SpecType -> RReft -> RReft)
-> Type
-> LocSpecType
-> LocSpecType
plugHoles_new TCEmb TyCon
tce TyConMap
tyi a
x SpecType -> RReft -> RReft
f Type
t0 zz :: LocSpecType
zz@(Loc SourcePos
l SourcePos
l' SpecType
st0) 
    = SourcePos -> SourcePos -> SpecType -> LocSpecType
forall a. SourcePos -> SourcePos -> a -> Located a
Loc SourcePos
l SourcePos
l' 
    (SpecType -> LocSpecType)
-> (SpecType -> SpecType) -> SpecType -> LocSpecType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
-> [PVar (RType RTyCon RTyVar ())]
-> [(Symbol, SpecType, RReft)]
-> [(Symbol, SpecType, RReft)]
-> SpecType
-> SpecType
forall tv c r.
[(RTVar tv (RType c tv ()), r)]
-> [PVar (RType c tv ())]
-> [(Symbol, RType c tv r, r)]
-> [(Symbol, RType c tv r, r)]
-> RType c tv r
-> RType c tv r
mkArrow ([RTVar RTyVar (RType RTyCon RTyVar ())]
-> [RReft] -> [(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
forall a b. [a] -> [b] -> [(a, b)]
zip (RTVar RTyVar (RType RTyCon RTyVar ())
-> RTVar RTyVar (RType RTyCon RTyVar ())
forall r i.
Monoid r =>
RTVar RTyVar i -> RTVar RTyVar (RType RTyCon RTyVar r)
updateRTVar (RTVar RTyVar (RType RTyCon RTyVar ())
 -> RTVar RTyVar (RType RTyCon RTyVar ()))
-> [RTVar RTyVar (RType RTyCon RTyVar ())]
-> [RTVar RTyVar (RType RTyCon RTyVar ())]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [RTVar RTyVar (RType RTyCon RTyVar ())]
as'') [RReft]
rs) [PVar (RType RTyCon RTyVar ())]
ps [] [] 
    (SpecType -> SpecType)
-> (SpecType -> SpecType) -> SpecType -> SpecType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Symbol, SpecType)] -> SpecType -> SpecType
forall (t :: * -> *) r c tv.
(Foldable t, Monoid r) =>
t (Symbol, RType c tv r) -> RType c tv r -> RType c tv r
makeCls [(Symbol, SpecType)]
cs' 
    (SpecType -> SpecType)
-> (SpecType -> SpecType) -> SpecType -> SpecType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TCEmb TyCon
-> TyConMap
-> (Doc -> Doc -> Error)
-> (SpecType -> RReft -> RReft)
-> SpecType
-> SpecType
-> SpecType
goPlug TCEmb TyCon
tce TyConMap
tyi Doc -> Doc -> Error
forall t. Doc -> Doc -> TError t
err SpecType -> RReft -> RReft
f SpecType
rt' 
    (SpecType -> LocSpecType) -> SpecType -> LocSpecType
forall a b. (a -> b) -> a -> b
$ SpecType
st 
  where 
    rt' :: SpecType
rt'          = SpecType -> SpecType
tx SpecType
rt
    as'' :: [RTVar RTyVar (RType RTyCon RTyVar ())]
as''         = [(RTyVar, RTyVar)]
-> RTVar RTyVar (RType RTyCon RTyVar ())
-> RTVar RTyVar (RType RTyCon RTyVar ())
subRTVar [(RTyVar, RTyVar)]
su (RTVar RTyVar (RType RTyCon RTyVar ())
 -> RTVar RTyVar (RType RTyCon RTyVar ()))
-> [RTVar RTyVar (RType RTyCon RTyVar ())]
-> [RTVar RTyVar (RType RTyCon RTyVar ())]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [RTVar RTyVar (RType RTyCon RTyVar ())]
as'
    ([RTVar RTyVar (RType RTyCon RTyVar ())]
as',[RReft]
rs)     = [(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
-> ([RTVar RTyVar (RType RTyCon RTyVar ())], [RReft])
forall a b. [(a, b)] -> ([a], [b])
unzip [(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
as 
    cs' :: [(Symbol, SpecType)]
cs'          = [ (Symbol
F.dummySymbol, SpecType
ct) | (RTyCon
c, [SpecType]
t) <- [(RTyCon, [SpecType])]
cs, let ct :: SpecType
ct = SpecType -> SpecType
tx (RTyCon
-> [SpecType] -> [RTProp RTyCon RTyVar RReft] -> RReft -> SpecType
forall c tv r.
c -> [RType c tv r] -> [RTProp c tv r] -> r -> RType c tv r
RApp RTyCon
c [SpecType]
t [] RReft
forall a. Monoid a => a
mempty) ]
    tx :: SpecType -> SpecType
tx           = [(RTyVar, RTyVar)] -> SpecType -> SpecType
forall tv ty c. SubsTy tv ty c => [(tv, ty)] -> c -> c
subts [(RTyVar, RTyVar)]
su
    su :: [(RTyVar, RTyVar)]
su           = case Type
-> SpecType -> (Doc -> Doc -> Error) -> Either Error MapTyVarST
Bare.runMapTyVars (SpecType -> Type
forall r. ToTypeable r => RRType r -> Type
toType SpecType
rt) SpecType
st Doc -> Doc -> Error
forall t. Doc -> Doc -> TError t
err of
                          Left Error
e  -> Error -> [(RTyVar, RTyVar)]
forall a e. Exception e => e -> a
Ex.throw Error
e 
                          Right MapTyVarST
s -> [ (Var -> RTyVar
rTyVar Var
x, RTyVar
y) | (Var
x, RTyVar
y) <- MapTyVarST -> [(Var, RTyVar)]
Bare.vmap MapTyVarST
s]
    ([(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
as,[PVar (RType RTyCon RTyVar ())]
_,[(RTyCon, [SpecType])]
cs,SpecType
rt) = SpecType
-> ([(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)],
    [PVar (RType RTyCon RTyVar ())], [(RTyCon, [SpecType])], SpecType)
bkUnivClass (Type -> SpecType
forall r. Monoid r => Type -> RRType r
ofType (Type -> Type
Ghc.expandTypeSynonyms Type
t0) :: SpecType)
    ([(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)]
_,[PVar (RType RTyCon RTyVar ())]
ps,[(RTyCon, [SpecType])]
_ ,SpecType
st) = SpecType
-> ([(RTVar RTyVar (RType RTyCon RTyVar ()), RReft)],
    [PVar (RType RTyCon RTyVar ())], [(RTyCon, [SpecType])], SpecType)
bkUnivClass SpecType
st0

    makeCls :: t (Symbol, RType c tv r) -> RType c tv r -> RType c tv r
makeCls t (Symbol, RType c tv r)
cs RType c tv r
t = ((Symbol, RType c tv r) -> RType c tv r -> RType c tv r)
-> RType c tv r -> t (Symbol, RType c tv r) -> RType c tv r
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((Symbol -> RType c tv r -> RType c tv r -> RType c tv r)
-> (Symbol, RType c tv r) -> RType c tv r -> RType c tv r
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Symbol -> RType c tv r -> RType c tv r -> RType c tv r
forall r c tv.
Monoid r =>
Symbol -> RType c tv r -> RType c tv r -> RType c tv r
rFun) RType c tv r
t t (Symbol, RType c tv r)
cs
    err :: Doc -> Doc -> TError t
err Doc
hsT Doc
lqT  = SrcSpan
-> Doc
-> Doc
-> Doc
-> Doc
-> Maybe (Doc, Doc)
-> SrcSpan
-> TError t
forall t.
SrcSpan
-> Doc
-> Doc
-> Doc
-> Doc
-> Maybe (Doc, Doc)
-> SrcSpan
-> TError t
ErrMismatch (LocSpecType -> SrcSpan
forall a. Loc a => a -> SrcSpan
GM.fSrcSpan LocSpecType
zz) (a -> Doc
forall a. PPrint a => a -> Doc
pprint a
x) 
                          (String -> Doc
text String
"Plugged Init types new")
                          (Type -> Doc
forall a. PPrint a => a -> Doc
pprint (Type -> Doc) -> Type -> Doc
forall a b. (a -> b) -> a -> b
$ Type -> Type
Ghc.expandTypeSynonyms Type
t0)
                          (RType RTyCon RTyVar () -> Doc
forall a. PPrint a => a -> Doc
pprint (RType RTyCon RTyVar () -> Doc) -> RType RTyCon RTyVar () -> Doc
forall a b. (a -> b) -> a -> b
$ SpecType -> RType RTyCon RTyVar ()
forall c tv r. RType c tv r -> RType c tv ()
toRSort SpecType
st0)
                          ((Doc, Doc) -> Maybe (Doc, Doc)
forall a. a -> Maybe a
Just (Doc
hsT, Doc
lqT))
                          (a -> SrcSpan
forall a. NamedThing a => a -> SrcSpan
Ghc.getSrcSpan a
x) 

subRTVar :: [(RTyVar, RTyVar)] -> SpecRTVar -> SpecRTVar 
subRTVar :: [(RTyVar, RTyVar)]
-> RTVar RTyVar (RType RTyCon RTyVar ())
-> RTVar RTyVar (RType RTyCon RTyVar ())
subRTVar [(RTyVar, RTyVar)]
su a :: RTVar RTyVar (RType RTyCon RTyVar ())
a@(RTVar RTyVar
v RTVInfo (RType RTyCon RTyVar ())
i) = RTVar RTyVar (RType RTyCon RTyVar ())
-> (RTyVar -> RTVar RTyVar (RType RTyCon RTyVar ()))
-> Maybe RTyVar
-> RTVar RTyVar (RType RTyCon RTyVar ())
forall b a. b -> (a -> b) -> Maybe a -> b
Mb.maybe RTVar RTyVar (RType RTyCon RTyVar ())
a (RTyVar
-> RTVInfo (RType RTyCon RTyVar ())
-> RTVar RTyVar (RType RTyCon RTyVar ())
forall tv s. tv -> RTVInfo s -> RTVar tv s
`RTVar` RTVInfo (RType RTyCon RTyVar ())
i) (RTyVar -> [(RTyVar, RTyVar)] -> Maybe RTyVar
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup RTyVar
v [(RTyVar, RTyVar)]
su)

goPlug :: F.TCEmb Ghc.TyCon -> Bare.TyConMap -> (Doc -> Doc -> Error) -> (SpecType -> RReft -> RReft) -> SpecType -> SpecType
       -> SpecType
goPlug :: TCEmb TyCon
-> TyConMap
-> (Doc -> Doc -> Error)
-> (SpecType -> RReft -> RReft)
-> SpecType
-> SpecType
-> SpecType
goPlug TCEmb TyCon
tce TyConMap
tyi Doc -> Doc -> Error
err SpecType -> RReft -> RReft
f = SpecType -> SpecType -> SpecType
go 
  where
    go :: SpecType -> SpecType -> SpecType
go SpecType
t (RHole RReft
r) = (SpecType -> SpecType
addHoles SpecType
t') { rt_reft :: RReft
rt_reft = SpecType -> RReft -> RReft
f SpecType
t RReft
r }
      where
        t' :: SpecType
t'         = (forall a. Data a => a -> a) -> SpecType -> SpecType
(forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere ((SpecType -> SpecType) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT ((SpecType -> SpecType) -> a -> a)
-> (SpecType -> SpecType) -> a -> a
forall a b. (a -> b) -> a -> b
$ TCEmb TyCon -> TyConMap -> SpecType -> SpecType
addRefs TCEmb TyCon
tce TyConMap
tyi) SpecType
t
        addHoles :: SpecType -> SpecType
addHoles   = (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere ((SpecType -> SpecType) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT ((SpecType -> SpecType) -> a -> a)
-> (SpecType -> SpecType) -> a -> a
forall a b. (a -> b) -> a -> b
$ SpecType -> SpecType
addHole)
        -- NOTE: make sure we only add holes to RVar and RApp (NOT RFun)
        addHole :: SpecType -> SpecType
        addHole :: SpecType -> SpecType
addHole t :: SpecType
t@(RVar RTyVar
v RReft
_)       = RTyVar -> RReft -> SpecType
forall c tv r. tv -> r -> RType c tv r
RVar RTyVar
v (SpecType -> RReft -> RReft
f SpecType
t ((Symbol, Expr) -> RReft
uReft (Symbol
"v", Expr
hole)))
        addHole t :: SpecType
t@(RApp RTyCon
c [SpecType]
ts [RTProp RTyCon RTyVar RReft]
ps RReft
_) = RTyCon
-> [SpecType] -> [RTProp RTyCon RTyVar RReft] -> RReft -> SpecType
forall c tv r.
c -> [RType c tv r] -> [RTProp c tv r] -> r -> RType c tv r
RApp RTyCon
c [SpecType]
ts [RTProp RTyCon RTyVar RReft]
ps (SpecType -> RReft -> RReft
f SpecType
t ((Symbol, Expr) -> RReft
uReft (Symbol
"v", Expr
hole)))
        addHole SpecType
t                  = SpecType
t

    go (RVar RTyVar
_ RReft
_)       v :: SpecType
v@(RVar RTyVar
_ RReft
_)       = SpecType
v
    go SpecType
t'               (RImpF Symbol
x SpecType
i SpecType
o RReft
r)    = Symbol -> SpecType -> SpecType -> RReft -> SpecType
forall c tv r.
Symbol -> RType c tv r -> RType c tv r -> r -> RType c tv r
RImpF Symbol
x SpecType
i  (SpecType -> SpecType -> SpecType
go SpecType
t' SpecType
o)             RReft
r
    go (RFun Symbol
_ SpecType
i SpecType
o RReft
_)   (RFun Symbol
x SpecType
i' SpecType
o' RReft
r)   = Symbol -> SpecType -> SpecType -> RReft -> SpecType
forall c tv r.
Symbol -> RType c tv r -> RType c tv r -> r -> RType c tv r
RFun Symbol
x     (SpecType -> SpecType -> SpecType
go SpecType
i SpecType
i')   (SpecType -> SpecType -> SpecType
go SpecType
o SpecType
o') RReft
r
    go (RAllT RTVar RTyVar (RType RTyCon RTyVar ())
_ SpecType
t RReft
_)    (RAllT RTVar RTyVar (RType RTyCon RTyVar ())
a SpecType
t' RReft
r)     = RTVar RTyVar (RType RTyCon RTyVar ())
-> SpecType -> RReft -> SpecType
forall c tv r. RTVU c tv -> RType c tv r -> r -> RType c tv r
RAllT RTVar RTyVar (RType RTyCon RTyVar ())
a    (SpecType -> SpecType -> SpecType
go SpecType
t SpecType
t') RReft
r
    go (RAllT RTVar RTyVar (RType RTyCon RTyVar ())
a SpecType
t RReft
r)    SpecType
t'                 = RTVar RTyVar (RType RTyCon RTyVar ())
-> SpecType -> RReft -> SpecType
forall c tv r. RTVU c tv -> RType c tv r -> r -> RType c tv r
RAllT RTVar RTyVar (RType RTyCon RTyVar ())
a    (SpecType -> SpecType -> SpecType
go SpecType
t SpecType
t') RReft
r
    go SpecType
t                (RAllP PVar (RType RTyCon RTyVar ())
p SpecType
t')       = PVar (RType RTyCon RTyVar ()) -> SpecType -> SpecType
forall c tv r. PVU c tv -> RType c tv r -> RType c tv r
RAllP PVar (RType RTyCon RTyVar ())
p    (SpecType -> SpecType -> SpecType
go SpecType
t SpecType
t')
    go SpecType
t                (RAllE Symbol
b SpecType
a SpecType
t')     = Symbol -> SpecType -> SpecType -> SpecType
forall c tv r.
Symbol -> RType c tv r -> RType c tv r -> RType c tv r
RAllE Symbol
b SpecType
a  (SpecType -> SpecType -> SpecType
go SpecType
t SpecType
t')
    go SpecType
t                (REx Symbol
b SpecType
x SpecType
t')       = Symbol -> SpecType -> SpecType -> SpecType
forall c tv r.
Symbol -> RType c tv r -> RType c tv r -> RType c tv r
REx Symbol
b SpecType
x    (SpecType -> SpecType -> SpecType
go SpecType
t SpecType
t')
    go SpecType
t                (RRTy [(Symbol, SpecType)]
e RReft
r Oblig
o SpecType
t')    = [(Symbol, SpecType)] -> RReft -> Oblig -> SpecType -> SpecType
forall c tv r.
[(Symbol, RType c tv r)]
-> r -> Oblig -> RType c tv r -> RType c tv r
RRTy [(Symbol, SpecType)]
e RReft
r Oblig
o (SpecType -> SpecType -> SpecType
go SpecType
t SpecType
t')
    go (RAppTy SpecType
t1 SpecType
t2 RReft
_) (RAppTy SpecType
t1' SpecType
t2' RReft
r) = SpecType -> SpecType -> RReft -> SpecType
forall c tv r. RType c tv r -> RType c tv r -> r -> RType c tv r
RAppTy     (SpecType -> SpecType -> SpecType
go SpecType
t1 SpecType
t1') (SpecType -> SpecType -> SpecType
go SpecType
t2 SpecType
t2') RReft
r
    -- zipWithDefM: if ts and ts' have different length then the liquid and haskell types are different.
    -- keep different types for now, as a pretty error message will be created at Bare.Check
    go (RApp RTyCon
_ [SpecType]
ts [RTProp RTyCon RTyVar RReft]
_ RReft
_)  (RApp RTyCon
c [SpecType]
ts' [RTProp RTyCon RTyVar RReft]
p RReft
r)   
      | [SpecType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [SpecType]
ts Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [SpecType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [SpecType]
ts'            = RTyCon
-> [SpecType] -> [RTProp RTyCon RTyVar RReft] -> RReft -> SpecType
forall c tv r.
c -> [RType c tv r] -> [RTProp c tv r] -> r -> RType c tv r
RApp RTyCon
c     ((SpecType -> SpecType -> SpecType)
-> [SpecType] -> [SpecType] -> [SpecType]
forall a. (a -> a -> a) -> [a] -> [a] -> [a]
Misc.zipWithDef SpecType -> SpecType -> SpecType
go [SpecType]
ts ([SpecType] -> [SpecType]) -> [SpecType] -> [SpecType]
forall a b. (a -> b) -> a -> b
$ [SpecType] -> [SpecType] -> [SpecType]
Bare.matchKindArgs [SpecType]
ts [SpecType]
ts') [RTProp RTyCon RTyVar RReft]
p RReft
r
    go SpecType
hsT SpecType
lqT                             = Error -> SpecType
forall a e. Exception e => e -> a
Ex.throw (Doc -> Doc -> Error
err (SpecType -> Doc
forall a. PPrint a => a -> Doc
F.pprint SpecType
hsT) (SpecType -> Doc
forall a. PPrint a => a -> Doc
F.pprint SpecType
lqT))

    -- otherwise                          = Ex.throw err 
    -- If we reach the default case, there's probably an error, but we defer
    -- throwing it as checkGhcSpec does a much better job of reporting the
    -- problem to the user.
    -- go st               _                 = st

addRefs :: F.TCEmb Ghc.TyCon -> TyConMap -> SpecType -> SpecType
addRefs :: TCEmb TyCon -> TyConMap -> SpecType -> SpecType
addRefs TCEmb TyCon
tce TyConMap
tyi (RApp RTyCon
c [SpecType]
ts [RTProp RTyCon RTyVar RReft]
_ RReft
r) = RTyCon
-> [SpecType] -> [RTProp RTyCon RTyVar RReft] -> RReft -> SpecType
forall c tv r.
c -> [RType c tv r] -> [RTProp c tv r] -> r -> RType c tv r
RApp RTyCon
c' [SpecType]
ts [RTProp RTyCon RTyVar RReft]
ps RReft
r
  where
    RApp RTyCon
c' [SpecType]
_ [RTProp RTyCon RTyVar RReft]
ps RReft
_ = TCEmb TyCon -> TyConMap -> SpecType -> SpecType
forall r.
(PPrint r, Reftable r, SubsTy RTyVar (RType RTyCon RTyVar ()) r,
 Reftable (RTProp RTyCon RTyVar r)) =>
TCEmb TyCon -> TyConMap -> RRType r -> RRType r
addTyConInfo TCEmb TyCon
tce TyConMap
tyi (RTyCon
-> [SpecType] -> [RTProp RTyCon RTyVar RReft] -> RReft -> SpecType
forall c tv r.
c -> [RType c tv r] -> [RTProp c tv r] -> r -> RType c tv r
RApp RTyCon
c [SpecType]
ts [] RReft
r)
addRefs TCEmb TyCon
_ TyConMap
_ SpecType
t  = SpecType
t

maybeTrue :: Ghc.NamedThing a => a -> ModName -> S.HashSet StableName -> SpecType -> RReft -> RReft
maybeTrue :: a -> ModName -> HashSet StableName -> SpecType -> RReft -> RReft
maybeTrue a
x ModName
target HashSet StableName
exports SpecType
t RReft
r
  | Bool -> Bool
not (SpecType -> Bool
forall t t1 t2. RType t t1 t2 -> Bool
isFunTy SpecType
t) Bool -> Bool -> Bool
&& (Name -> Bool
Ghc.isInternalName Name
name Bool -> Bool -> Bool
|| Bool
inTarget Bool -> Bool -> Bool
&& Bool
notExported)
  = RReft
r
  | Bool
otherwise
  = RReft -> RReft
killHoles RReft
r
  where
    inTarget :: Bool
inTarget    = Module -> ModuleName
Ghc.moduleName (HasDebugCallStack => Name -> Module
Name -> Module
Ghc.nameModule Name
name) ModuleName -> ModuleName -> Bool
forall a. Eq a => a -> a -> Bool
== ModName -> ModuleName
getModName ModName
target
    name :: Name
name        = a -> Name
forall a. NamedThing a => a -> Name
Ghc.getName a
x
    notExported :: Bool
notExported = Bool -> Bool
not (Name -> StableName
mkStableName (a -> Name
forall a. NamedThing a => a -> Name
Ghc.getName a
x) StableName -> HashSet StableName -> Bool
forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
`S.member` HashSet StableName
exports)

-- killHoles r@(U (Reft (v, rs)) _ _) = r { ur_reft = Reft (v, filter (not . isHole) rs) }

killHoles :: RReft -> RReft
killHoles :: RReft -> RReft
killHoles RReft
ur = RReft
ur { ur_reft :: Reft
ur_reft = Reft -> Reft
tx (Reft -> Reft) -> Reft -> Reft
forall a b. (a -> b) -> a -> b
$ RReft -> Reft
forall r. UReft r -> r
ur_reft RReft
ur }
  where
    tx :: Reft -> Reft
tx Reft
r = {- traceFix ("killholes: r = " ++ showFix r) $ -} (Expr -> Expr) -> Reft -> Reft
F.mapPredReft Expr -> Expr
dropHoles Reft
r
    dropHoles :: Expr -> Expr
dropHoles    = ListNE Expr -> Expr
F.pAnd (ListNE Expr -> Expr) -> (Expr -> ListNE Expr) -> Expr -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Expr -> Bool) -> ListNE Expr -> ListNE Expr
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Expr -> Bool) -> Expr -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr -> Bool
isHole) (ListNE Expr -> ListNE Expr)
-> (Expr -> ListNE Expr) -> Expr -> ListNE Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr -> ListNE Expr
F.conjuncts