{- |
    Module      : $Header$
    Description : Representation of annotated FlatCurry.
    Copyright   : (c) 2016 - 2017 Finn Teegen
                      2018        Kai-Oliver Prott
    License     : BSD-3-clause

    Maintainer  : fte@informatik.uni-kiel.de
    Stability   : experimental
    Portability : portable
 
    This library contains a version of FlatCurry's abstract syntax tree
    modified with type information

    For more information about the abstract syntax tree of `FlatCurry`,
    see the documentation of the respective module.
-}

module Curry.FlatCurry.Typed.Type
  ( module Curry.FlatCurry.Typed.Type
  , module Curry.FlatCurry.Typeable
  , module Curry.FlatCurry.Type
  ) where

import Curry.FlatCurry.Typeable
import Curry.FlatCurry.Type ( QName, VarIndex, Visibility (..), TVarIndex
                            , TypeDecl (..), OpDecl (..), Fixity (..)
                            , TypeExpr (..), ConsDecl (..)
                            , Literal (..), CombType (..), CaseType (..)
                            )

data TProg = TProg String [String] [TypeDecl] [TFuncDecl] [OpDecl]
  deriving (TProg -> TProg -> Bool
(TProg -> TProg -> Bool) -> (TProg -> TProg -> Bool) -> Eq TProg
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TProg -> TProg -> Bool
$c/= :: TProg -> TProg -> Bool
== :: TProg -> TProg -> Bool
$c== :: TProg -> TProg -> Bool
Eq, ReadPrec [TProg]
ReadPrec TProg
Int -> ReadS TProg
ReadS [TProg]
(Int -> ReadS TProg)
-> ReadS [TProg]
-> ReadPrec TProg
-> ReadPrec [TProg]
-> Read TProg
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TProg]
$creadListPrec :: ReadPrec [TProg]
readPrec :: ReadPrec TProg
$creadPrec :: ReadPrec TProg
readList :: ReadS [TProg]
$creadList :: ReadS [TProg]
readsPrec :: Int -> ReadS TProg
$creadsPrec :: Int -> ReadS TProg
Read, Int -> TProg -> ShowS
[TProg] -> ShowS
TProg -> String
(Int -> TProg -> ShowS)
-> (TProg -> String) -> ([TProg] -> ShowS) -> Show TProg
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TProg] -> ShowS
$cshowList :: [TProg] -> ShowS
show :: TProg -> String
$cshow :: TProg -> String
showsPrec :: Int -> TProg -> ShowS
$cshowsPrec :: Int -> TProg -> ShowS
Show)

data TFuncDecl = TFunc QName Int Visibility TypeExpr TRule
  deriving (TFuncDecl -> TFuncDecl -> Bool
(TFuncDecl -> TFuncDecl -> Bool)
-> (TFuncDecl -> TFuncDecl -> Bool) -> Eq TFuncDecl
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TFuncDecl -> TFuncDecl -> Bool
$c/= :: TFuncDecl -> TFuncDecl -> Bool
== :: TFuncDecl -> TFuncDecl -> Bool
$c== :: TFuncDecl -> TFuncDecl -> Bool
Eq, ReadPrec [TFuncDecl]
ReadPrec TFuncDecl
Int -> ReadS TFuncDecl
ReadS [TFuncDecl]
(Int -> ReadS TFuncDecl)
-> ReadS [TFuncDecl]
-> ReadPrec TFuncDecl
-> ReadPrec [TFuncDecl]
-> Read TFuncDecl
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TFuncDecl]
$creadListPrec :: ReadPrec [TFuncDecl]
readPrec :: ReadPrec TFuncDecl
$creadPrec :: ReadPrec TFuncDecl
readList :: ReadS [TFuncDecl]
$creadList :: ReadS [TFuncDecl]
readsPrec :: Int -> ReadS TFuncDecl
$creadsPrec :: Int -> ReadS TFuncDecl
Read, Int -> TFuncDecl -> ShowS
[TFuncDecl] -> ShowS
TFuncDecl -> String
(Int -> TFuncDecl -> ShowS)
-> (TFuncDecl -> String)
-> ([TFuncDecl] -> ShowS)
-> Show TFuncDecl
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TFuncDecl] -> ShowS
$cshowList :: [TFuncDecl] -> ShowS
show :: TFuncDecl -> String
$cshow :: TFuncDecl -> String
showsPrec :: Int -> TFuncDecl -> ShowS
$cshowsPrec :: Int -> TFuncDecl -> ShowS
Show)

data TRule
  = TRule     [(VarIndex, TypeExpr)] TExpr
  | TExternal TypeExpr String
  deriving (TRule -> TRule -> Bool
(TRule -> TRule -> Bool) -> (TRule -> TRule -> Bool) -> Eq TRule
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TRule -> TRule -> Bool
$c/= :: TRule -> TRule -> Bool
== :: TRule -> TRule -> Bool
$c== :: TRule -> TRule -> Bool
Eq, ReadPrec [TRule]
ReadPrec TRule
Int -> ReadS TRule
ReadS [TRule]
(Int -> ReadS TRule)
-> ReadS [TRule]
-> ReadPrec TRule
-> ReadPrec [TRule]
-> Read TRule
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TRule]
$creadListPrec :: ReadPrec [TRule]
readPrec :: ReadPrec TRule
$creadPrec :: ReadPrec TRule
readList :: ReadS [TRule]
$creadList :: ReadS [TRule]
readsPrec :: Int -> ReadS TRule
$creadsPrec :: Int -> ReadS TRule
Read, Int -> TRule -> ShowS
[TRule] -> ShowS
TRule -> String
(Int -> TRule -> ShowS)
-> (TRule -> String) -> ([TRule] -> ShowS) -> Show TRule
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TRule] -> ShowS
$cshowList :: [TRule] -> ShowS
show :: TRule -> String
$cshow :: TRule -> String
showsPrec :: Int -> TRule -> ShowS
$cshowsPrec :: Int -> TRule -> ShowS
Show)

data TExpr
  = TVarE  TypeExpr VarIndex -- otherwise name clash with TypeExpr's TVar
  | TLit   TypeExpr Literal
  | TComb  TypeExpr CombType QName [TExpr]
  | TLet   [((VarIndex, TypeExpr), TExpr)] TExpr
  | TFree  [(VarIndex, TypeExpr)] TExpr
  | TOr    TExpr TExpr
  | TCase  CaseType TExpr [TBranchExpr]
  | TTyped TExpr TypeExpr
  deriving (TExpr -> TExpr -> Bool
(TExpr -> TExpr -> Bool) -> (TExpr -> TExpr -> Bool) -> Eq TExpr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TExpr -> TExpr -> Bool
$c/= :: TExpr -> TExpr -> Bool
== :: TExpr -> TExpr -> Bool
$c== :: TExpr -> TExpr -> Bool
Eq, ReadPrec [TExpr]
ReadPrec TExpr
Int -> ReadS TExpr
ReadS [TExpr]
(Int -> ReadS TExpr)
-> ReadS [TExpr]
-> ReadPrec TExpr
-> ReadPrec [TExpr]
-> Read TExpr
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TExpr]
$creadListPrec :: ReadPrec [TExpr]
readPrec :: ReadPrec TExpr
$creadPrec :: ReadPrec TExpr
readList :: ReadS [TExpr]
$creadList :: ReadS [TExpr]
readsPrec :: Int -> ReadS TExpr
$creadsPrec :: Int -> ReadS TExpr
Read, Int -> TExpr -> ShowS
[TExpr] -> ShowS
TExpr -> String
(Int -> TExpr -> ShowS)
-> (TExpr -> String) -> ([TExpr] -> ShowS) -> Show TExpr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TExpr] -> ShowS
$cshowList :: [TExpr] -> ShowS
show :: TExpr -> String
$cshow :: TExpr -> String
showsPrec :: Int -> TExpr -> ShowS
$cshowsPrec :: Int -> TExpr -> ShowS
Show)

data TBranchExpr = TBranch TPattern TExpr
  deriving (TBranchExpr -> TBranchExpr -> Bool
(TBranchExpr -> TBranchExpr -> Bool)
-> (TBranchExpr -> TBranchExpr -> Bool) -> Eq TBranchExpr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TBranchExpr -> TBranchExpr -> Bool
$c/= :: TBranchExpr -> TBranchExpr -> Bool
== :: TBranchExpr -> TBranchExpr -> Bool
$c== :: TBranchExpr -> TBranchExpr -> Bool
Eq, ReadPrec [TBranchExpr]
ReadPrec TBranchExpr
Int -> ReadS TBranchExpr
ReadS [TBranchExpr]
(Int -> ReadS TBranchExpr)
-> ReadS [TBranchExpr]
-> ReadPrec TBranchExpr
-> ReadPrec [TBranchExpr]
-> Read TBranchExpr
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TBranchExpr]
$creadListPrec :: ReadPrec [TBranchExpr]
readPrec :: ReadPrec TBranchExpr
$creadPrec :: ReadPrec TBranchExpr
readList :: ReadS [TBranchExpr]
$creadList :: ReadS [TBranchExpr]
readsPrec :: Int -> ReadS TBranchExpr
$creadsPrec :: Int -> ReadS TBranchExpr
Read, Int -> TBranchExpr -> ShowS
[TBranchExpr] -> ShowS
TBranchExpr -> String
(Int -> TBranchExpr -> ShowS)
-> (TBranchExpr -> String)
-> ([TBranchExpr] -> ShowS)
-> Show TBranchExpr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TBranchExpr] -> ShowS
$cshowList :: [TBranchExpr] -> ShowS
show :: TBranchExpr -> String
$cshow :: TBranchExpr -> String
showsPrec :: Int -> TBranchExpr -> ShowS
$cshowsPrec :: Int -> TBranchExpr -> ShowS
Show)

data TPattern
  = TPattern  TypeExpr QName [(VarIndex, TypeExpr)]
  | TLPattern TypeExpr Literal
  deriving (TPattern -> TPattern -> Bool
(TPattern -> TPattern -> Bool)
-> (TPattern -> TPattern -> Bool) -> Eq TPattern
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TPattern -> TPattern -> Bool
$c/= :: TPattern -> TPattern -> Bool
== :: TPattern -> TPattern -> Bool
$c== :: TPattern -> TPattern -> Bool
Eq, ReadPrec [TPattern]
ReadPrec TPattern
Int -> ReadS TPattern
ReadS [TPattern]
(Int -> ReadS TPattern)
-> ReadS [TPattern]
-> ReadPrec TPattern
-> ReadPrec [TPattern]
-> Read TPattern
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TPattern]
$creadListPrec :: ReadPrec [TPattern]
readPrec :: ReadPrec TPattern
$creadPrec :: ReadPrec TPattern
readList :: ReadS [TPattern]
$creadList :: ReadS [TPattern]
readsPrec :: Int -> ReadS TPattern
$creadsPrec :: Int -> ReadS TPattern
Read, Int -> TPattern -> ShowS
[TPattern] -> ShowS
TPattern -> String
(Int -> TPattern -> ShowS)
-> (TPattern -> String) -> ([TPattern] -> ShowS) -> Show TPattern
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TPattern] -> ShowS
$cshowList :: [TPattern] -> ShowS
show :: TPattern -> String
$cshow :: TPattern -> String
showsPrec :: Int -> TPattern -> ShowS
$cshowsPrec :: Int -> TPattern -> ShowS
Show)

instance Typeable TRule where
  typeOf :: TRule -> TypeExpr
typeOf (TRule args :: [(Int, TypeExpr)]
args e :: TExpr
e) = ((Int, TypeExpr) -> TypeExpr -> TypeExpr)
-> TypeExpr -> [(Int, TypeExpr)] -> TypeExpr
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (TypeExpr -> TypeExpr -> TypeExpr
FuncType (TypeExpr -> TypeExpr -> TypeExpr)
-> ((Int, TypeExpr) -> TypeExpr)
-> (Int, TypeExpr)
-> TypeExpr
-> TypeExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, TypeExpr) -> TypeExpr
forall a b. (a, b) -> b
snd) (TExpr -> TypeExpr
forall a. Typeable a => a -> TypeExpr
typeOf TExpr
e) [(Int, TypeExpr)]
args
  typeOf (TExternal ty :: TypeExpr
ty _) = TypeExpr
ty

instance Typeable TExpr where
  typeOf :: TExpr -> TypeExpr
typeOf (TVarE ty :: TypeExpr
ty _) = TypeExpr
ty
  typeOf (TLit ty :: TypeExpr
ty _) = TypeExpr
ty
  typeOf (TComb  ty :: TypeExpr
ty _ _ _) = TypeExpr
ty
  typeOf (TLet _ e :: TExpr
e) = TExpr -> TypeExpr
forall a. Typeable a => a -> TypeExpr
typeOf TExpr
e
  typeOf (TFree _ e :: TExpr
e) = TExpr -> TypeExpr
forall a. Typeable a => a -> TypeExpr
typeOf TExpr
e
  typeOf (TOr e :: TExpr
e _) = TExpr -> TypeExpr
forall a. Typeable a => a -> TypeExpr
typeOf TExpr
e
  typeOf (TCase _ _ (e :: TBranchExpr
e:_)) = TBranchExpr -> TypeExpr
forall a. Typeable a => a -> TypeExpr
typeOf TBranchExpr
e
  typeOf (TTyped _ ty :: TypeExpr
ty) = TypeExpr
ty
  typeOf (TCase _ _ []) = String -> TypeExpr
forall a. HasCallStack => String -> a
error (String -> TypeExpr) -> String -> TypeExpr
forall a b. (a -> b) -> a -> b
$ "Curry.FlatCurry.Typed.Type.typeOf: " String -> ShowS
forall a. [a] -> [a] -> [a]
++
                                  "empty list in case expression"

instance Typeable TPattern where
  typeOf :: TPattern -> TypeExpr
typeOf (TPattern ty :: TypeExpr
ty _ _) = TypeExpr
ty
  typeOf (TLPattern ty :: TypeExpr
ty _) = TypeExpr
ty

instance Typeable TBranchExpr where
  typeOf :: TBranchExpr -> TypeExpr
typeOf (TBranch _ e :: TExpr
e) = TExpr -> TypeExpr
forall a. Typeable a => a -> TypeExpr
typeOf TExpr
e