module Infernu.Builtins.String
(stringRowType)
where
import Control.Monad (foldM, forM)
import Infernu.Types
import Infernu.InferState
import Infernu.Lib (safeLookup)
func :: Type -> Type -> Type -> Type
func this x y = Fix $ TFunc [this, x] y
string :: Type
string = Fix $ TBody TString
number :: Type
number = Fix $ TBody TNumber
ts :: t -> TScheme t
ts t = TScheme [] $ qualEmpty t
stringProps :: [(String, TypeScheme)]
stringProps =
[ ("length", ts number)
, ("charAt", ts $ func string number string)
, ("charCodeAt", ts $ func string number number)
, ("concat", ts $ func string string string)
, ("indexOf", ts $ func string string number)
, ("lastIndexOf", ts $ func string string number)
, ("localeCompare", ts $ func string string number)
]
stringRowType :: Infer (TRowList Type)
stringRowType = foldM addProp (TRowEnd Nothing) $ stringProps
where addProp rowlist (name, propTS) =
do allocNames <- forM (schemeVars propTS) $ \tvName -> (fresh >>= return . (tvName,))
let ts' = mapVarNames (safeLookup allocNames) propTS
return $ TRowProp name ts' rowlist