Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions primer/src/Primer/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import Control.Monad.Fix (MonadFix)
import Control.Monad.Trans (MonadTrans)
import Control.Monad.Writer (MonadWriter)
import Control.Monad.Zip (MonadZip)
import Data.Aeson (ToJSON)
import Data.Map qualified as Map
import Data.Text qualified as T
import ListT qualified (toList)
Expand Down Expand Up @@ -139,6 +138,11 @@ import Primer.Database qualified as Database (
Success
),
)
import Primer.JSON (
CustomJSON (..),
PrimerJSON,
ToJSON,
)
import Primer.Module (moduleDefsQualified, moduleName, moduleTypesQualified)
import Primer.Name (Name, unName)
import StmContainers.Map qualified as StmMap
Expand Down Expand Up @@ -383,8 +387,7 @@ data Tree = Tree
-- ^ a special subtree to be rendered to the right, rather than below - useful for `case` branches
}
deriving (Show, Eq, Generic)

instance ToJSON Tree
deriving (ToJSON) via PrimerJSON Tree

-- | The contents of a node.
data NodeBody
Expand All @@ -395,8 +398,7 @@ data NodeBody
| -- | Some simple nodes, like function application, have no body.
NoBody
deriving (Show, Eq, Generic)

instance ToJSON NodeBody
deriving (ToJSON) via PrimerJSON NodeBody

-- | An indication of the meaning of a node, which frontend may use for labelling, colour etc.
-- These mostly correspond to constructors of `Expr'` or `Type'`.
Expand Down Expand Up @@ -425,17 +427,15 @@ data NodeFlavor
| FlavorTForall
| FlavorPattern
deriving (Show, Eq, Generic)

instance ToJSON NodeFlavor
deriving (ToJSON) via PrimerJSON NodeFlavor

-- | This type is the API's view of a 'App.Prog'
-- (this is expected to evolve as we flesh out the API)
newtype Prog = Prog
{ modules :: [Module]
}
deriving (Generic)

instance ToJSON Prog
deriving (ToJSON) via PrimerJSON Prog

-- | This type is the API's view of a 'Module.Module'
-- (this is expected to evolve as we flesh out the API)
Expand All @@ -450,8 +450,7 @@ data Module = Module
defs :: [Def]
}
deriving (Generic)

instance ToJSON Module
deriving (ToJSON) via PrimerJSON Module

-- | This type is the api's view of a 'Primer.Core.Def'
-- (this is expected to evolve as we flesh out the API)
Expand All @@ -462,8 +461,7 @@ data Def = Def
-- ^ definitions with no associated tree are primitives
}
deriving (Generic)

instance ToJSON Def
deriving (ToJSON) via PrimerJSON Def

viewProg :: App.Prog -> Prog
viewProg p =
Expand Down
27 changes: 5 additions & 22 deletions primer/src/Primer/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ module Primer.App (
import Foreword hiding (mod)

import Control.Monad.Fresh (MonadFresh (..))
import Data.Aeson (
ToJSON (toEncoding),
defaultOptions,
genericToEncoding,
)
import Data.Bitraversable (bimapM)
import Data.Data (Data)
import Data.Generics.Uniplate.Operations (descendM, transform, transformM)
Expand Down Expand Up @@ -354,7 +349,7 @@ data Selection = Selection
, selectedNode :: Maybe NodeSelection
}
deriving (Eq, Show, Generic, Data)
deriving (ToJSON, FromJSON) via PrimerJSON Selection
deriving (FromJSON, ToJSON) via PrimerJSON Selection

-- | A selected node, in the body or type signature of some definition.
-- We have the following invariant: @nodeType = SigNode ==> isRight meta@
Expand All @@ -364,11 +359,11 @@ data NodeSelection = NodeSelection
, meta :: Either ExprMeta TypeMeta
}
deriving (Eq, Show, Generic, Data)
deriving (ToJSON, FromJSON) via PrimerJSON NodeSelection
deriving (FromJSON, ToJSON) via PrimerJSON NodeSelection

data NodeType = BodyNode | SigNode
deriving (Eq, Show, Generic, Data)
deriving (ToJSON, FromJSON) via PrimerJSON NodeType
deriving (FromJSON, ToJSON) via PrimerJSON NodeType

-- | The type of requests which can mutate the application state.
data MutationRequest
Expand Down Expand Up @@ -1041,20 +1036,12 @@ runQueryAppM (QueryAppM m) appState = case runExcept (runReaderT m appState) of
--
-- Building an 'App' can be tricky, so we don't export the
-- constructor. See 'mkApp' and 'mkAppSafe'.
--
-- Note that the 'ToJSON' and 'FromJSON' instances for this type are
-- not used in the frontend, and therefore we can use "Data.Aeson"s
-- generic instances for them.
data App = App
{ currentState :: AppState
, initialState :: AppState
}
deriving (Eq, Show, Generic)

instance ToJSON App where
toEncoding = genericToEncoding defaultOptions

instance FromJSON App
deriving (FromJSON, ToJSON) via PrimerJSON App

-- Internal app state. Note that this type is not exported, as we want
-- to guarantee that the counters are kept in sync with the 'Prog',
Expand All @@ -1066,11 +1053,7 @@ data AppState = AppState
, prog :: Prog
}
deriving (Eq, Show, Generic)

instance ToJSON AppState where
toEncoding = genericToEncoding defaultOptions

instance FromJSON AppState
deriving (FromJSON, ToJSON) via PrimerJSON AppState

-- | Construct an 'App' from an 'ID' and a 'Prog'.
--
Expand Down
4 changes: 1 addition & 3 deletions primer/src/Primer/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,7 @@ data GlobalName (k :: GlobalNameKind) = GlobalName
, baseName :: Name
}
deriving (Eq, Ord, Generic, Data, Show)

instance FromJSON (GlobalName k)
instance ToJSON (GlobalName k)
deriving (FromJSON, ToJSON) via PrimerJSON (GlobalName k)

-- | Construct a name from a Text. This is called unsafe because there are no
-- guarantees about whether the name refers to anything that is in scope.
Expand Down
3 changes: 2 additions & 1 deletion primer/src/Primer/Name.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ unsafeMkName :: Text -> Name
unsafeMkName = Name

newtype NameCounter = NC Natural
deriving (Eq, Enum, Show, ToJSON, FromJSON)
deriving (Eq, Enum, Show, Generic, Data)
deriving newtype (FromJSON, ToJSON)

-- | Generate a new automatic name (distinct from all other names generated by
-- this function, from the MonadFresh class), and avoiding the given set of
Expand Down
2 changes: 1 addition & 1 deletion primer/src/Primer/Typecheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ assert b s = unless b $ throwError' (InternalError s)

data SmartHoles = SmartHoles | NoSmartHoles
deriving (Eq, Show, Generic)
deriving (ToJSON, FromJSON) via PrimerJSON SmartHoles
deriving (FromJSON, ToJSON) via PrimerJSON SmartHoles

data KindOrType = K Kind | T Type
deriving (Show, Eq)
Expand Down