{- |
    Module      :  $Header$
    Description :  Conversion of kind representation
    Copyright   :  (c) 2016        Finn Teegen
    License     :  BSD-3-clause

    Maintainer  :  bjp@informatik.uni-kiel.de
    Stability   :  experimental
    Portability :  portable

   The functions 'tokind' and 'fromKind' convert Curry kind expressions into
   kinds and vice versa.

   When Curry kinds are converted with 'fromKind', kind variables are
   instantiated with the kind *.
-}

module Base.CurryKinds
  ( toKind, toKind', fromKind, fromKind', ppKind
  ) where

import Curry.Base.Pretty (Doc)
import Curry.Syntax.Pretty (ppKindExpr)
import Curry.Syntax.Type (KindExpr (..))

import Base.Kinds

toKind :: KindExpr -> Kind
toKind :: KindExpr -> Kind
toKind Star              = Kind
KindStar
toKind (ArrowKind k1 :: KindExpr
k1 k2 :: KindExpr
k2) = Kind -> Kind -> Kind
KindArrow (KindExpr -> Kind
toKind KindExpr
k1) (KindExpr -> Kind
toKind KindExpr
k2)

toKind' :: Maybe KindExpr -> Int -> Kind
toKind' :: Maybe KindExpr -> Int -> Kind
toKind' k :: Maybe KindExpr
k n :: Int
n = Kind -> (KindExpr -> Kind) -> Maybe KindExpr -> Kind
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> Kind
simpleKind Int
n) KindExpr -> Kind
toKind Maybe KindExpr
k

fromKind :: Kind -> KindExpr
fromKind :: Kind -> KindExpr
fromKind KindStar          = KindExpr
Star
fromKind (KindVariable  _) = KindExpr
Star
fromKind (KindArrow k1 :: Kind
k1 k2 :: Kind
k2) = KindExpr -> KindExpr -> KindExpr
ArrowKind (Kind -> KindExpr
fromKind Kind
k1) (Kind -> KindExpr
fromKind Kind
k2)

fromKind' :: Kind -> Int -> Maybe KindExpr
fromKind' :: Kind -> Int -> Maybe KindExpr
fromKind' k :: Kind
k n :: Int
n | Kind
k Kind -> Kind -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Kind
simpleKind Int
n = Maybe KindExpr
forall a. Maybe a
Nothing
              | Bool
otherwise         = KindExpr -> Maybe KindExpr
forall a. a -> Maybe a
Just (Kind -> KindExpr
fromKind Kind
k)

ppKind :: Kind -> Doc
ppKind :: Kind -> Doc
ppKind = Int -> KindExpr -> Doc
ppKindExpr 0 (KindExpr -> Doc) -> (Kind -> KindExpr) -> Kind -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Kind -> KindExpr
fromKind