Skip to content

Commit

Permalink
Move Presentation into its own module Instance.Presentation. #148
Browse files Browse the repository at this point in the history
  • Loading branch information
epost committed Aug 21, 2019
1 parent 9a113fe commit 5b02e16
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 41 deletions.
43 changes: 2 additions & 41 deletions src/Language/CQL/Instance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ import qualified Data.Set as Set
import Data.Typeable hiding (typeOf)
import Data.Void
import Language.CQL.Common (elem', intercalate, fromListAccum, mapl, section, sepTup, toMapSafely, Deps(..), Err, Kind(INSTANCE), MultiTyMap, TyMap, type (+))
import Language.CQL.Collage (Collage(..), assembleGens, attsFrom, fksFrom, typeOf, typeOfCol)
import Language.CQL.Collage (Collage(..), assembleGens, attsFrom, fksFrom, typeOf)
import Language.CQL.Instance.Presentation (Presentation(..), presToCol, typecheckPresentation, eqs0)
import Language.CQL.Mapping as Mapping
import Language.CQL.Options
import Language.CQL.Prover
Expand Down Expand Up @@ -180,46 +181,6 @@ aSk alg g = nf'' alg $ Sk g

-------------------------------------------------------------------------------------------------------------------

-- | A presentation of an instance.
data Presentation var ty sym en fk att gen sk
= Presentation
{ gens :: Map gen en
, sks :: Map sk ty
, eqs :: Set (EQ Void ty sym en fk att gen sk)
}

instance (NFData ty, NFData sym, NFData en, NFData fk, NFData att, NFData gen, NFData sk)
=> NFData (Presentation var ty sym en fk att gen sk) where
rnf (Presentation g s e) = deepseq g $ deepseq s $ rnf e

-- | Checks that an instance presentation is a well-formed theory.
typecheckPresentation
:: (MultiTyMap '[Show, Ord, NFData] '[var, ty, sym, en, fk, att, gen, sk])
=> Schema var ty sym en fk att
-> Presentation var ty sym en fk att gen sk
-> Err ()
typecheckPresentation sch p = typeOfCol $ presToCol sch p

--created as an alias because of name clashes
eqs0
:: Presentation var ty sym en fk att gen sk
-> Set (EQ Void ty sym en fk att gen sk)
eqs0 (Presentation _ _ x) = x

-- | Converts a presentation to a collage.
presToCol
:: (MultiTyMap '[Show, Ord, NFData] '[var, ty, sym, en, fk, att, gen, sk])
=> Schema var ty sym en fk att
-> Presentation var ty sym en fk att gen sk
-> Collage (()+var) ty sym en fk att gen sk
presToCol sch (Presentation gens' sks' eqs') =
Collage (Set.union e1 e2) (ctys schcol)
(cens schcol) (csyms schcol) (cfks schcol) (catts schcol) gens' sks'
where
schcol = schToCol sch
e1 = Set.map (\( EQ (l,r)) -> (Map.empty, EQ (upp l, upp r))) eqs'
e2 = Set.map (\(g, EQ (l,r)) -> (g, EQ (upp l, upp r))) $ ceqs schcol

-- | A database instance on a schema. Contains a presentation, an algebra, and a decision procedure.
data Instance var ty sym en fk att gen sk x y
= Instance
Expand Down
100 changes: 100 additions & 0 deletions src/Language/CQL/Instance/Presentation.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{-
SPDX-License-Identifier: AGPL-3.0-only
This file is part of `statebox/cql`, the categorical query language.
Copyright (C) 2019 Stichting Statebox <https://statebox.nl>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LiberalTypeSynonyms #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE UndecidableInstances #-}

module Language.CQL.Instance.Presentation where

import Control.DeepSeq (deepseq, NFData(..))
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe ()
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Void
import Language.CQL.Collage (Collage(..), typeOfCol)
import Language.CQL.Common (Err, MultiTyMap, TyMap, type (+), section, sepTup, intercalate)
import Language.CQL.Schema (Schema, schToCol)
import Language.CQL.Term as Term
import Prelude hiding (EQ)

-- | A presentation of an @Instance@.
data Presentation var ty sym en fk att gen sk
= Presentation
{ gens :: Map gen en
, sks :: Map sk ty
, eqs :: Set (EQ Void ty sym en fk att gen sk)
}

instance TyMap Show '[var, ty, sym, en, fk, att, gen, sk]
=> Show (Presentation var ty sym en fk att gen sk) where
show (Presentation ens' _ eqs') =
unlines
[ section "generators" $ intercalate "\n" $ sepTup " : " <$> Map.toList ens'
, section "equations" $ intercalate "\n" $ Set.map show eqs'
]

instance (NFData ty, NFData sym, NFData en, NFData fk, NFData att, NFData gen, NFData sk)
=> NFData (Presentation var ty sym en fk att gen sk) where
rnf (Presentation g s e) = deepseq g $ deepseq s $ rnf e

-- | Checks that an instance presentation is a well-formed theory.
typecheckPresentation
:: (MultiTyMap '[Show, Ord, NFData] '[var, ty, sym, en, fk, att, gen, sk])
=> Schema var ty sym en fk att
-> Presentation var ty sym en fk att gen sk
-> Err ()
typecheckPresentation sch p = typeOfCol $ presToCol sch p

--created as an alias because of name clashes
eqs0
:: Presentation var ty sym en fk att gen sk
-> Set (EQ Void ty sym en fk att gen sk)
eqs0 (Presentation _ _ x) = x

-- | Converts a presentation to a collage.
presToCol
:: (MultiTyMap '[Show, Ord, NFData] '[var, ty, sym, en, fk, att, gen, sk])
=> Schema var ty sym en fk att
-> Presentation var ty sym en fk att gen sk
-> Collage (()+var) ty sym en fk att gen sk
presToCol sch (Presentation gens' sks' eqs') =
Collage (Set.union e1 e2) (ctys schcol)
(cens schcol) (csyms schcol) (cfks schcol) (catts schcol) gens' sks'
where
schcol = schToCol sch
e1 = Set.map (\( EQ (l,r)) -> (Map.empty, EQ (upp l, upp r))) eqs'
e2 = Set.map (\(g, EQ (l,r)) -> (g, EQ (upp l, upp r))) $ ceqs schcol

0 comments on commit 5b02e16

Please sign in to comment.