{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}

-- | C manifest data structure and serialisation to JSON.
--
-- A manifest contains machine-readable information about the API of
-- the compiled Futhark program.  Specifically which entry points are
-- available, which types are exposed, and what their C names are.
-- This module documentation is not intended as a full description of
-- the Futhark C API - you will need to consult the Futhark User's
-- Guide to understand most of the information here.
--
-- The type aliases are purely informative and do not actually enforce
-- correct usage.  They are present only because most of the
-- information here is ultimately just text.
module Futhark.Manifest
  ( -- * Type aliases
    CFuncName,
    CTypeName,
    TypeName,

    -- * Manifest
    Manifest (..),
    Input (..),
    Output (..),
    EntryPoint (..),
    Type (..),
    ArrayOps (..),
    RecordField (..),
    RecordOps (..),
    OpaqueOps (..),
    manifestToJSON,
    manifestFromJSON,
  )
where

import Control.Applicative
import Control.Monad (guard)
import Data.Aeson (ToJSON (..), object, (.!=), (.:), (.:?))
import qualified Data.Aeson as JSON
import qualified Data.Aeson.Key as JSON
import Data.Aeson.Text (encodeToLazyText)
import Data.Bifunctor (bimap)
import Data.ByteString.Builder (toLazyByteString)
import qualified Data.Map as M
import Data.Maybe (maybeToList)
import qualified Data.Text as T
import Data.Text.Encoding (encodeUtf8Builder)
import Data.Text.Lazy (toStrict)

-- | The name of a C function.
type CFuncName = T.Text

-- | The name of a C type (often of the form @"struct foo*"@).
type CTypeName = T.Text

-- | The name of a Futhark-level type.  This may be an array type
-- (without sizes, just empty brackets), a primitive type, or another
-- string denoting an opaque type.  The latter must have a
-- corresponding entry in 'manifestTypes'.
type TypeName = T.Text

-- | Manifest info for an entry point parameter.
data Input = Input
  { Input -> Text
inputName :: T.Text,
    Input -> Text
inputType :: TypeName,
    Input -> Bool
inputUnique :: Bool
  }
  deriving (Input -> Input -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Input -> Input -> Bool
$c/= :: Input -> Input -> Bool
== :: Input -> Input -> Bool
$c== :: Input -> Input -> Bool
Eq, Eq Input
Input -> Input -> Bool
Input -> Input -> Ordering
Input -> Input -> Input
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Input -> Input -> Input
$cmin :: Input -> Input -> Input
max :: Input -> Input -> Input
$cmax :: Input -> Input -> Input
>= :: Input -> Input -> Bool
$c>= :: Input -> Input -> Bool
> :: Input -> Input -> Bool
$c> :: Input -> Input -> Bool
<= :: Input -> Input -> Bool
$c<= :: Input -> Input -> Bool
< :: Input -> Input -> Bool
$c< :: Input -> Input -> Bool
compare :: Input -> Input -> Ordering
$ccompare :: Input -> Input -> Ordering
Ord, Int -> Input -> ShowS
[Input] -> ShowS
Input -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Input] -> ShowS
$cshowList :: [Input] -> ShowS
show :: Input -> String
$cshow :: Input -> String
showsPrec :: Int -> Input -> ShowS
$cshowsPrec :: Int -> Input -> ShowS
Show)

-- | Manifest info for an entry point return value.
data Output = Output
  { Output -> Text
outputType :: TypeName,
    Output -> Bool
outputUnique :: Bool
  }
  deriving (Output -> Output -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Output -> Output -> Bool
$c/= :: Output -> Output -> Bool
== :: Output -> Output -> Bool
$c== :: Output -> Output -> Bool
Eq, Eq Output
Output -> Output -> Bool
Output -> Output -> Ordering
Output -> Output -> Output
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Output -> Output -> Output
$cmin :: Output -> Output -> Output
max :: Output -> Output -> Output
$cmax :: Output -> Output -> Output
>= :: Output -> Output -> Bool
$c>= :: Output -> Output -> Bool
> :: Output -> Output -> Bool
$c> :: Output -> Output -> Bool
<= :: Output -> Output -> Bool
$c<= :: Output -> Output -> Bool
< :: Output -> Output -> Bool
$c< :: Output -> Output -> Bool
compare :: Output -> Output -> Ordering
$ccompare :: Output -> Output -> Ordering
Ord, Int -> Output -> ShowS
[Output] -> ShowS
Output -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Output] -> ShowS
$cshowList :: [Output] -> ShowS
show :: Output -> String
$cshow :: Output -> String
showsPrec :: Int -> Output -> ShowS
$cshowsPrec :: Int -> Output -> ShowS
Show)

-- | Manifest info for an entry point.
data EntryPoint = EntryPoint
  { EntryPoint -> Text
entryPointCFun :: CFuncName,
    EntryPoint -> [Text]
entryPointTuningParams :: [T.Text],
    EntryPoint -> [Output]
entryPointOutputs :: [Output],
    EntryPoint -> [Input]
entryPointInputs :: [Input]
  }
  deriving (EntryPoint -> EntryPoint -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EntryPoint -> EntryPoint -> Bool
$c/= :: EntryPoint -> EntryPoint -> Bool
== :: EntryPoint -> EntryPoint -> Bool
$c== :: EntryPoint -> EntryPoint -> Bool
Eq, Eq EntryPoint
EntryPoint -> EntryPoint -> Bool
EntryPoint -> EntryPoint -> Ordering
EntryPoint -> EntryPoint -> EntryPoint
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: EntryPoint -> EntryPoint -> EntryPoint
$cmin :: EntryPoint -> EntryPoint -> EntryPoint
max :: EntryPoint -> EntryPoint -> EntryPoint
$cmax :: EntryPoint -> EntryPoint -> EntryPoint
>= :: EntryPoint -> EntryPoint -> Bool
$c>= :: EntryPoint -> EntryPoint -> Bool
> :: EntryPoint -> EntryPoint -> Bool
$c> :: EntryPoint -> EntryPoint -> Bool
<= :: EntryPoint -> EntryPoint -> Bool
$c<= :: EntryPoint -> EntryPoint -> Bool
< :: EntryPoint -> EntryPoint -> Bool
$c< :: EntryPoint -> EntryPoint -> Bool
compare :: EntryPoint -> EntryPoint -> Ordering
$ccompare :: EntryPoint -> EntryPoint -> Ordering
Ord, Int -> EntryPoint -> ShowS
[EntryPoint] -> ShowS
EntryPoint -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EntryPoint] -> ShowS
$cshowList :: [EntryPoint] -> ShowS
show :: EntryPoint -> String
$cshow :: EntryPoint -> String
showsPrec :: Int -> EntryPoint -> ShowS
$cshowsPrec :: Int -> EntryPoint -> ShowS
Show)

-- | The names of the C functions implementing the operations on some
-- array type.
data ArrayOps = ArrayOps
  { ArrayOps -> Text
arrayFree :: CFuncName,
    ArrayOps -> Text
arrayShape :: CFuncName,
    ArrayOps -> Text
arrayValues :: CFuncName,
    ArrayOps -> Text
arrayNew :: CFuncName
  }
  deriving (ArrayOps -> ArrayOps -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ArrayOps -> ArrayOps -> Bool
$c/= :: ArrayOps -> ArrayOps -> Bool
== :: ArrayOps -> ArrayOps -> Bool
$c== :: ArrayOps -> ArrayOps -> Bool
Eq, Eq ArrayOps
ArrayOps -> ArrayOps -> Bool
ArrayOps -> ArrayOps -> Ordering
ArrayOps -> ArrayOps -> ArrayOps
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ArrayOps -> ArrayOps -> ArrayOps
$cmin :: ArrayOps -> ArrayOps -> ArrayOps
max :: ArrayOps -> ArrayOps -> ArrayOps
$cmax :: ArrayOps -> ArrayOps -> ArrayOps
>= :: ArrayOps -> ArrayOps -> Bool
$c>= :: ArrayOps -> ArrayOps -> Bool
> :: ArrayOps -> ArrayOps -> Bool
$c> :: ArrayOps -> ArrayOps -> Bool
<= :: ArrayOps -> ArrayOps -> Bool
$c<= :: ArrayOps -> ArrayOps -> Bool
< :: ArrayOps -> ArrayOps -> Bool
$c< :: ArrayOps -> ArrayOps -> Bool
compare :: ArrayOps -> ArrayOps -> Ordering
$ccompare :: ArrayOps -> ArrayOps -> Ordering
Ord, Int -> ArrayOps -> ShowS
[ArrayOps] -> ShowS
ArrayOps -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ArrayOps] -> ShowS
$cshowList :: [ArrayOps] -> ShowS
show :: ArrayOps -> String
$cshow :: ArrayOps -> String
showsPrec :: Int -> ArrayOps -> ShowS
$cshowsPrec :: Int -> ArrayOps -> ShowS
Show)

-- | Information about a record field.
data RecordField = RecordField
  { -- | The original name of the field.  This may be a name that is
    -- not a valid C identifier.
    RecordField -> Text
recordFieldName :: T.Text,
    -- | The type of the field.
    RecordField -> Text
recordFieldType :: TypeName,
    -- | The name of the projection function.
    RecordField -> Text
recordFieldProject :: CFuncName
  }
  deriving (RecordField -> RecordField -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RecordField -> RecordField -> Bool
$c/= :: RecordField -> RecordField -> Bool
== :: RecordField -> RecordField -> Bool
$c== :: RecordField -> RecordField -> Bool
Eq, Eq RecordField
RecordField -> RecordField -> Bool
RecordField -> RecordField -> Ordering
RecordField -> RecordField -> RecordField
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: RecordField -> RecordField -> RecordField
$cmin :: RecordField -> RecordField -> RecordField
max :: RecordField -> RecordField -> RecordField
$cmax :: RecordField -> RecordField -> RecordField
>= :: RecordField -> RecordField -> Bool
$c>= :: RecordField -> RecordField -> Bool
> :: RecordField -> RecordField -> Bool
$c> :: RecordField -> RecordField -> Bool
<= :: RecordField -> RecordField -> Bool
$c<= :: RecordField -> RecordField -> Bool
< :: RecordField -> RecordField -> Bool
$c< :: RecordField -> RecordField -> Bool
compare :: RecordField -> RecordField -> Ordering
$ccompare :: RecordField -> RecordField -> Ordering
Ord, Int -> RecordField -> ShowS
[RecordField] -> ShowS
RecordField -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RecordField] -> ShowS
$cshowList :: [RecordField] -> ShowS
show :: RecordField -> String
$cshow :: RecordField -> String
showsPrec :: Int -> RecordField -> ShowS
$cshowsPrec :: Int -> RecordField -> ShowS
Show)

-- | Some opaque types are records, from which we can extract fields,
-- and also construct them from values for their fields.  Beyond that,
-- they support the usual opaque operations.  These record facilities
-- can be ignored if you wish, and the types treated as ordinary
-- opaque types.
data RecordOps = RecordOps
  { -- | Note that the ordering of fields here is semantically
    -- significant - it is also the order that the "new" function
    -- expects.
    RecordOps -> [RecordField]
recordFields :: [RecordField],
    RecordOps -> Text
recordNew :: CFuncName
  }
  deriving (RecordOps -> RecordOps -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RecordOps -> RecordOps -> Bool
$c/= :: RecordOps -> RecordOps -> Bool
== :: RecordOps -> RecordOps -> Bool
$c== :: RecordOps -> RecordOps -> Bool
Eq, Eq RecordOps
RecordOps -> RecordOps -> Bool
RecordOps -> RecordOps -> Ordering
RecordOps -> RecordOps -> RecordOps
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: RecordOps -> RecordOps -> RecordOps
$cmin :: RecordOps -> RecordOps -> RecordOps
max :: RecordOps -> RecordOps -> RecordOps
$cmax :: RecordOps -> RecordOps -> RecordOps
>= :: RecordOps -> RecordOps -> Bool
$c>= :: RecordOps -> RecordOps -> Bool
> :: RecordOps -> RecordOps -> Bool
$c> :: RecordOps -> RecordOps -> Bool
<= :: RecordOps -> RecordOps -> Bool
$c<= :: RecordOps -> RecordOps -> Bool
< :: RecordOps -> RecordOps -> Bool
$c< :: RecordOps -> RecordOps -> Bool
compare :: RecordOps -> RecordOps -> Ordering
$ccompare :: RecordOps -> RecordOps -> Ordering
Ord, Int -> RecordOps -> ShowS
[RecordOps] -> ShowS
RecordOps -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RecordOps] -> ShowS
$cshowList :: [RecordOps] -> ShowS
show :: RecordOps -> String
$cshow :: RecordOps -> String
showsPrec :: Int -> RecordOps -> ShowS
$cshowsPrec :: Int -> RecordOps -> ShowS
Show)

-- | The names of the C functions implementing the operations on some
-- opaque type.
data OpaqueOps = OpaqueOps
  { OpaqueOps -> Text
opaqueFree :: CFuncName,
    OpaqueOps -> Text
opaqueStore :: CFuncName,
    OpaqueOps -> Text
opaqueRestore :: CFuncName
  }
  deriving (OpaqueOps -> OpaqueOps -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OpaqueOps -> OpaqueOps -> Bool
$c/= :: OpaqueOps -> OpaqueOps -> Bool
== :: OpaqueOps -> OpaqueOps -> Bool
$c== :: OpaqueOps -> OpaqueOps -> Bool
Eq, Eq OpaqueOps
OpaqueOps -> OpaqueOps -> Bool
OpaqueOps -> OpaqueOps -> Ordering
OpaqueOps -> OpaqueOps -> OpaqueOps
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: OpaqueOps -> OpaqueOps -> OpaqueOps
$cmin :: OpaqueOps -> OpaqueOps -> OpaqueOps
max :: OpaqueOps -> OpaqueOps -> OpaqueOps
$cmax :: OpaqueOps -> OpaqueOps -> OpaqueOps
>= :: OpaqueOps -> OpaqueOps -> Bool
$c>= :: OpaqueOps -> OpaqueOps -> Bool
> :: OpaqueOps -> OpaqueOps -> Bool
$c> :: OpaqueOps -> OpaqueOps -> Bool
<= :: OpaqueOps -> OpaqueOps -> Bool
$c<= :: OpaqueOps -> OpaqueOps -> Bool
< :: OpaqueOps -> OpaqueOps -> Bool
$c< :: OpaqueOps -> OpaqueOps -> Bool
compare :: OpaqueOps -> OpaqueOps -> Ordering
$ccompare :: OpaqueOps -> OpaqueOps -> Ordering
Ord, Int -> OpaqueOps -> ShowS
[OpaqueOps] -> ShowS
OpaqueOps -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OpaqueOps] -> ShowS
$cshowList :: [OpaqueOps] -> ShowS
show :: OpaqueOps -> String
$cshow :: OpaqueOps -> String
showsPrec :: Int -> OpaqueOps -> ShowS
$cshowsPrec :: Int -> OpaqueOps -> ShowS
Show)

-- | Manifest info for a non-scalar type.  Scalar types are not part
-- of the manifest for a program.
data Type
  = -- | ctype, Futhark elemtype, rank.
    TypeArray CTypeName TypeName Int ArrayOps
  | TypeOpaque CTypeName OpaqueOps (Maybe RecordOps)
  deriving (Type -> Type -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Type -> Type -> Bool
$c/= :: Type -> Type -> Bool
== :: Type -> Type -> Bool
$c== :: Type -> Type -> Bool
Eq, Eq Type
Type -> Type -> Bool
Type -> Type -> Ordering
Type -> Type -> Type
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Type -> Type -> Type
$cmin :: Type -> Type -> Type
max :: Type -> Type -> Type
$cmax :: Type -> Type -> Type
>= :: Type -> Type -> Bool
$c>= :: Type -> Type -> Bool
> :: Type -> Type -> Bool
$c> :: Type -> Type -> Bool
<= :: Type -> Type -> Bool
$c<= :: Type -> Type -> Bool
< :: Type -> Type -> Bool
$c< :: Type -> Type -> Bool
compare :: Type -> Type -> Ordering
$ccompare :: Type -> Type -> Ordering
Ord, Int -> Type -> ShowS
[Type] -> ShowS
Type -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Type] -> ShowS
$cshowList :: [Type] -> ShowS
show :: Type -> String
$cshow :: Type -> String
showsPrec :: Int -> Type -> ShowS
$cshowsPrec :: Int -> Type -> ShowS
Show)

-- | A manifest for a compiled program.
data Manifest = Manifest
  { -- | A mapping from Futhark entry points to how they are
    -- represented in C.
    Manifest -> Map Text EntryPoint
manifestEntryPoints :: M.Map T.Text EntryPoint,
    -- | A mapping from Futhark type name to how they are represented
    -- at the C level.  Should not contain any of the primitive scalar
    -- types.  For array types, these have empty dimensions,
    -- e.g. @[]i32@.
    Manifest -> Map Text Type
manifestTypes :: M.Map TypeName Type,
    -- | The compiler backend used to
    -- compile the program, e.g. @c@.
    Manifest -> Text
manifestBackend :: T.Text,
    -- | The version of the compiler used to compile the program.
    Manifest -> Text
manifestVersion :: T.Text
  }
  deriving (Manifest -> Manifest -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Manifest -> Manifest -> Bool
$c/= :: Manifest -> Manifest -> Bool
== :: Manifest -> Manifest -> Bool
$c== :: Manifest -> Manifest -> Bool
Eq, Eq Manifest
Manifest -> Manifest -> Bool
Manifest -> Manifest -> Ordering
Manifest -> Manifest -> Manifest
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Manifest -> Manifest -> Manifest
$cmin :: Manifest -> Manifest -> Manifest
max :: Manifest -> Manifest -> Manifest
$cmax :: Manifest -> Manifest -> Manifest
>= :: Manifest -> Manifest -> Bool
$c>= :: Manifest -> Manifest -> Bool
> :: Manifest -> Manifest -> Bool
$c> :: Manifest -> Manifest -> Bool
<= :: Manifest -> Manifest -> Bool
$c<= :: Manifest -> Manifest -> Bool
< :: Manifest -> Manifest -> Bool
$c< :: Manifest -> Manifest -> Bool
compare :: Manifest -> Manifest -> Ordering
$ccompare :: Manifest -> Manifest -> Ordering
Ord, Int -> Manifest -> ShowS
[Manifest] -> ShowS
Manifest -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Manifest] -> ShowS
$cshowList :: [Manifest] -> ShowS
show :: Manifest -> String
$cshow :: Manifest -> String
showsPrec :: Int -> Manifest -> ShowS
$cshowsPrec :: Int -> Manifest -> ShowS
Show)

instance JSON.ToJSON ArrayOps where
  toJSON :: ArrayOps -> Value
toJSON (ArrayOps Text
free Text
shape Text
values Text
new) =
    [Pair] -> Value
object
      [ (Key
"free", forall a. ToJSON a => a -> Value
toJSON Text
free),
        (Key
"shape", forall a. ToJSON a => a -> Value
toJSON Text
shape),
        (Key
"values", forall a. ToJSON a => a -> Value
toJSON Text
values),
        (Key
"new", forall a. ToJSON a => a -> Value
toJSON Text
new)
      ]

instance JSON.ToJSON RecordField where
  toJSON :: RecordField -> Value
toJSON (RecordField Text
name Text
typename Text
project) =
    [Pair] -> Value
object
      [ (Key
"name", forall a. ToJSON a => a -> Value
toJSON Text
name),
        (Key
"type", forall a. ToJSON a => a -> Value
toJSON Text
typename),
        (Key
"project", forall a. ToJSON a => a -> Value
toJSON Text
project)
      ]

instance JSON.ToJSON RecordOps where
  toJSON :: RecordOps -> Value
toJSON (RecordOps [RecordField]
fields Text
new) =
    [Pair] -> Value
object
      [ (Key
"fields", forall a. ToJSON a => a -> Value
toJSON [RecordField]
fields),
        (Key
"new", forall a. ToJSON a => a -> Value
toJSON Text
new)
      ]

instance JSON.ToJSON OpaqueOps where
  toJSON :: OpaqueOps -> Value
toJSON (OpaqueOps Text
free Text
store Text
restore) =
    [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
      [ (Key
"free", forall a. ToJSON a => a -> Value
toJSON Text
free),
        (Key
"store", forall a. ToJSON a => a -> Value
toJSON Text
store),
        (Key
"restore", forall a. ToJSON a => a -> Value
toJSON Text
restore)
      ]

instance JSON.ToJSON Manifest where
  toJSON :: Manifest -> Value
toJSON (Manifest Map Text EntryPoint
entry_points Map Text Type
types Text
backend Text
version) =
    [Pair] -> Value
object
      [ (Key
"backend", forall a. ToJSON a => a -> Value
toJSON Text
backend),
        (Key
"version", forall a. ToJSON a => a -> Value
toJSON Text
version),
        ( Key
"entry_points",
          [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap Text -> Key
JSON.fromText EntryPoint -> Value
onEntryPoint) forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
M.toList Map Text EntryPoint
entry_points
        ),
        ( Key
"types",
          [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap Text -> Key
JSON.fromText Type -> Value
onType) forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
M.toList Map Text Type
types
        )
      ]
    where
      onEntryPoint :: EntryPoint -> Value
onEntryPoint (EntryPoint Text
cfun [Text]
tuning_params [Output]
outputs [Input]
inputs) =
        [Pair] -> Value
object
          [ (Key
"cfun", forall a. ToJSON a => a -> Value
toJSON Text
cfun),
            (Key
"tuning_params", forall a. ToJSON a => a -> Value
toJSON [Text]
tuning_params),
            (Key
"outputs", forall a. ToJSON a => a -> Value
toJSON forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Output -> Value
onOutput [Output]
outputs),
            (Key
"inputs", forall a. ToJSON a => a -> Value
toJSON forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Input -> Value
onInput [Input]
inputs)
          ]

      onOutput :: Output -> Value
onOutput (Output Text
t Bool
u) =
        [Pair] -> Value
object
          [ (Key
"type", forall a. ToJSON a => a -> Value
toJSON Text
t),
            (Key
"unique", forall a. ToJSON a => a -> Value
toJSON Bool
u)
          ]

      onInput :: Input -> Value
onInput (Input Text
p Text
t Bool
u) =
        [Pair] -> Value
object
          [ (Key
"name", forall a. ToJSON a => a -> Value
toJSON Text
p),
            (Key
"type", forall a. ToJSON a => a -> Value
toJSON Text
t),
            (Key
"unique", forall a. ToJSON a => a -> Value
toJSON Bool
u)
          ]

      onType :: Type -> Value
onType (TypeArray Text
t Text
et Int
rank ArrayOps
ops) =
        [Pair] -> Value
object
          [ (Key
"kind", Value
"array"),
            (Key
"ctype", forall a. ToJSON a => a -> Value
toJSON Text
t),
            (Key
"rank", forall a. ToJSON a => a -> Value
toJSON Int
rank),
            (Key
"elemtype", forall a. ToJSON a => a -> Value
toJSON Text
et),
            (Key
"ops", forall a. ToJSON a => a -> Value
toJSON ArrayOps
ops)
          ]
      onType (TypeOpaque Text
t OpaqueOps
ops Maybe RecordOps
record) =
        [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
          [ (Key
"kind", Value
"opaque"),
            (Key
"ctype", forall a. ToJSON a => a -> Value
toJSON Text
t),
            (Key
"ops", forall a. ToJSON a => a -> Value
toJSON OpaqueOps
ops)
          ]
            forall a. [a] -> [a] -> [a]
++ forall a. Maybe a -> [a]
maybeToList ((Key
"record",) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> Value
toJSON forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe RecordOps
record)

instance JSON.FromJSON ArrayOps where
  parseJSON :: Value -> Parser ArrayOps
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"ArrayOps" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Text -> Text -> Text -> Text -> ArrayOps
ArrayOps forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"free" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"shape" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"values" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"new"

instance JSON.FromJSON RecordField where
  parseJSON :: Value -> Parser RecordField
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"RecordField" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Text -> Text -> Text -> RecordField
RecordField forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"project"

instance JSON.FromJSON RecordOps where
  parseJSON :: Value -> Parser RecordOps
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"RecordOps" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    [RecordField] -> Text -> RecordOps
RecordOps forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"fields" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"new"

instance JSON.FromJSON OpaqueOps where
  parseJSON :: Value -> Parser OpaqueOps
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"OpaqueOps" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Text -> Text -> Text -> OpaqueOps
OpaqueOps forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"free" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"store" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"restore"

instance JSON.FromJSON EntryPoint where
  parseJSON :: Value -> Parser EntryPoint
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"EntryPoint" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Text -> [Text] -> [Output] -> [Input] -> EntryPoint
EntryPoint
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"cfun"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"tuning_params"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"outputs"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"inputs"

instance JSON.FromJSON Output where
  parseJSON :: Value -> Parser Output
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"Output" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Text -> Bool -> Output
Output forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"unique"

instance JSON.FromJSON Input where
  parseJSON :: Value -> Parser Input
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"Input" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Text -> Text -> Bool -> Input
Input forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"unique"

instance JSON.FromJSON Type where
  parseJSON :: Value -> Parser Type
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"Type" forall a b. (a -> b) -> a -> b
$ \Object
ty -> Object -> Parser Type
pArray Object
ty forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Object -> Parser Type
pOpaque Object
ty
    where
      pArray :: Object -> Parser Type
pArray Object
ty = do
        forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Eq a => a -> a -> Bool
== (Text
"array" :: T.Text)) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (Object
ty forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"kind")
        Text -> Text -> Int -> ArrayOps -> Type
TypeArray
          forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
ty forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ctype"
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
ty forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"elemtype"
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
ty forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"rank"
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
ty forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ops"
      pOpaque :: Object -> Parser Type
pOpaque Object
ty = do
        forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Eq a => a -> a -> Bool
== (Text
"opaque" :: T.Text)) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (Object
ty forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"kind")
        Text -> OpaqueOps -> Maybe RecordOps -> Type
TypeOpaque forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
ty forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ctype" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
ty forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ops" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
ty forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"record"

instance JSON.FromJSON Manifest where
  parseJSON :: Value -> Parser Manifest
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"Manifest" forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Map Text EntryPoint -> Map Text Type -> Text -> Text -> Manifest
Manifest
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"entry_points"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"types"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"backend"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"version" forall a. Parser (Maybe a) -> a -> Parser a
.!= Text
"" -- Temporary workaround for older manifests.

-- | Serialise a manifest to JSON.
manifestToJSON :: Manifest -> T.Text
manifestToJSON :: Manifest -> Text
manifestToJSON = Text -> Text
toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> Text
encodeToLazyText

-- | Read a manifest from JSON.  Returns 'Nothing' if the text does
-- not describe a 'Manifest'.
manifestFromJSON :: T.Text -> Maybe Manifest
manifestFromJSON :: Text -> Maybe Manifest
manifestFromJSON = forall a. FromJSON a => ByteString -> Maybe a
JSON.decode forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
encodeUtf8Builder