content: {-# LANGUAGE QuasiQuotes #-}
  
  module Conjure.Rules.Vertical.MSet.ExplicitWithRepetition where
  
  import Conjure.Rules.Import
  
  
  rule_Comprehension :: Rule
  rule_Comprehension = "mset-comprehension{ExplicitWithRepetition}" `namedRule` theRule where
      theRule (Comprehension body gensOrConds) = do
          (gocBefore, (pat, s), gocAfter) <- matchFirst gensOrConds $ \ goc -> case goc of
              Generator (GenInExpr pat@Single{} s) -> return (pat, s)
              _ -> na "rule_Comprehension"
          TypeMSet{}                  <- typeOf s
          MSet_ExplicitWithRepetition <- representationOf s
          [flag, values]              <- downX1 s
          DomainMatrix index _        <- domainOf values
          let upd val old = lambdaToFunction pat old val
          return
              ( "Vertical rule for mset-comprehension, ExplicitWithRepetition representation"
              , do
                  (jPat, j) <- quantifiedVar
                  let val = [essence| &values[&j] |]
                  return $ Comprehension (upd val body)
                          $  gocBefore
                          ++ [ Generator (GenDomainNoRepr jPat index)
                             , Condition [essence| &j <= &flag |]
                             ]
                          ++ transformBi (upd val) gocAfter
                 )
      theRule _ = na "rule_Comprehension"
  