Git Product home page Git Product logo

Comments (1)

gusbicalho avatar gusbicalho commented on September 16, 2024 1

I just adapted my example to use the current version from main (commit 21337e4), and everything seem to be working now! New code below:

module Specs.SimpleCounterSpec where

import Data.Either qualified as Either
import Data.Hashable (Hashable)
import GHC.Generics (Generic)
import Language.Spectacle (
  ActionType (ActionWF),
  Fairness (WeakFair),
  Modality (Eventually, Stays),
  Specification (..),
  Temporal,
  TemporalType (PropF, PropFG),
  modelcheck,
  plain,
  (.=),
  pattern ConF,
  pattern NilF,
  type (#),
 )
import Test.Hspec (Spec, describe, hspec, it, shouldSatisfy)

main :: IO ()
main = hspec spec

spec :: Spec
spec =
  describe "SimpleCounter spec" $ do
    describe "eventually is maxCounter" $ do
      specification <- pure $ specification @Eventually PropF
      it "passes when initialCounter < maxCounter" $ do
        result <- modelcheck (specification (1, 4))
        result `shouldSatisfy` Either.isRight
      it "passes when initialCounter = maxCounter" $ do
        result <- modelcheck (specification (4, 4))
        result `shouldSatisfy` Either.isRight
      it "fails when initialCounter > maxCounter" $ do
        result <- modelcheck (specification (4, 2))
        result `shouldSatisfy` Either.isLeft
    describe "eventually always is maxCounter" $ do
      specification <- pure $ specification @Stays PropFG
      it "passes when initialCounter < maxCounter" $ do
        result <- modelcheck (specification (1, 4))
        result `shouldSatisfy` Either.isRight
      it "passes when initialCounter = maxCounter" $ do
        result <- modelcheck (specification (4, 4))
        result `shouldSatisfy` Either.isRight
      it "fails when initialCounter > maxCounter" $ do
        result <- modelcheck (specification (4, 2))
        result `shouldSatisfy` Either.isLeft

data Counter = Counting Word | Done Word
  deriving stock (Eq, Show, Generic)
  deriving anyclass (Hashable)

type CounterSpec modality =
  Specification CounterState CounterActions (CounterProperty modality)

type CounterState =
  '[ "goal" # Word
   , "counter" # Counter
   ]

type CounterActions =
  '[ "counterInc" # 'WeakFair
   ]

type CounterProperty modality =
  '["counterEqualsMax" # modality]

specification ::
  (forall s. Temporal s Bool -> TemporalType s modality) ->
  (Word, Word) ->
  CounterSpec modality
specification toProp (initialCounter, maxCounter) =
  Specification
    { specInit =
        ConF #goal (pure maxCounter)
          . ConF #counter (pure (Counting initialCounter))
          $ NilF
    , specNext =
        ConF #counterInc (ActionWF counterInc) NilF
    , specProp =
        ConF #counterEqualsMax (toProp counterEqualsMax) NilF
    }
 where
  counterInc =
    plain #counter >>= \case
      Done _ -> pure False
      Counting counter -> do
        goal <- plain #goal
        let counter' =
              if counter < goal
                then Counting $ counter + 1
                else Done counter
        #counter .= pure counter'
        pure True
  counterEqualsMax = do
    goal <- plain #goal
    counter <- plain #counter
    pure
      case counter of
        Done c -> c == goal
        _ -> False

from spectacle.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.