doIntroduce' expr thresholdValue thresholdName runEval theRPar theSeq t
 = applyTP (once_buTP (failTP `adhocTP` inMatch) `choiceTP` failure) t
  where

    -- has to be a match: generalising already converted pattern bindings
    -- to a match
    inMatch (match@(HsMatch l name pats rhs ds)::HsMatchP)
     |  findEntity expr match
     && runEval `myElem` ds
       = do (f,d) <- hsFDNamesFromInside match
            -- error (show ((f++d), thresholdName))
            -- unless (not (thresholdName `elem` (f++d))) $ error "Error: name of threshold parameter is already used on RHS!"
            let runEval' = findPat runEval ds
            if runEval' == Nothing
               then mzero
               else do
                       let  newName = mkNewName "rpar_abs" (f++d) 1
                            newRunEval = modifyRunEval expr thresholdValue thresholdName newName theRPar theSeq (fromJust runEval')
                       -- match' <- addDecl match Nothing ([newParAbs], Nothing) False
                       match'' <- update (fromJust runEval') newRunEval match

                       return match''

    inMatch _ = mzero

    -- inModule (mod::HsModuleP)
    --  | isDeclaredIn (

    failure = idTP `adhocTP` mod
      where mod (m::HsModuleP) = error "Cannot find the activated Eval Monad! Please activate a run eval monad within scope of the selected entity."

    modifyRunEval expr thresholdValue thresholdName newName theRPar theSeq (pat@(Dec (HsPatBind l p rhs ds)))
     = Dec (HsPatBind l p (convertToSeq newName rhs) (ds++[mkParAbs newName theRPar theSeq]))   -- (Dec (HsPatBind l p (addGuards rhs) ds))
      {-   where



            addGuards (HsGuard es) = error "Cannot add threshold to an eval monad with guards already defined!"
            addGuards (HsBody e) = 	HsGuard [(loc0, theGuard, convertToSeq e), (loc0, nameToExp "otherwise", e)]
            theGuard = Exp (HsInfixApp expr (theOpp) (nameToExp thresholdName))
            theOpp   = HsVar (nameToPNT "<")
        -}

    mkParAbs name theRPar theRSeq = (Dec (HsPatBind loc0 (nameToPat name) addGuards []))
     where
      addGuards = HsGuard [(loc0, theGuard, nameToExp theRPar), (loc0, nameToExp "otherwise", nameToExp theRSeq)]
      theGuard = Exp (HsInfixApp expr (theOpp) (nameToExp thresholdName))
      theOpp   = HsVar (nameToPNT ">")

    convertToSeq :: (Term t) => String -> t -> t
    convertToSeq newName e
      = runIdentity (applyTP (full_tdTP (idTP `adhocTP` exp)) e)
       where
        exp (Exp (HsId (HsVar i))::HsExpP)
         | pNTtoName i == "rpar" = return $ nameToExp newName
        exp x = return x

    findPat :: HsPatP -> [HsDeclP] -> Maybe HsDeclP
    findPat p [] = Nothing -- error "Cannot find active Eval Monad pattern binding!"
    findPat p ((Dec (HsFunBind s m)):ds) = findPat p ds
    findPat p (a@(Dec (HsPatBind l p2 rhs _)):ds)
      | p == p2 = Just $ a
      | otherwise = findPat p ds
    findPat p (_:ds) = findPat p ds

    myElem :: HsPatP -> [HsDeclP] -> Bool
    myElem p [] = False
    myElem p ((Dec (HsFunBind s m)):ds) = myElem p ds
    myElem p ((Dec (HsPatBind l p2 rhs _)):ds)
      | p == p2 = True
      | otherwise = myElem p ds
    myElem p (_:ds) = myElem p ds
doIntroduce' expr thresholdValue thresholdName runEval theRPar theSeq t
 = applyTP (once_buTP (failTP `adhocTP` inMatch) `choiceTP` failure) t
  where

    -- has to be a match: generalising already converted pattern bindings
    -- to a match
    inMatch (match@(HsMatch l name pats rhs ds)::HsMatchP)
     |  findEntity expr match
     && runEval `myElem` ds
       = do (f,d) <- hsFDNamesFromInside match
            -- error (show ((f++d), thresholdName))
            -- unless (not (thresholdName `elem` (f++d))) $ error "Error: name of threshold parameter is already used on RHS!"
            let runEval' = findPat runEval ds
            if runEval' == Nothing
               then mzero
               else do
                       let  newName = mkNewName "rpar_abs" (f++d) 1
                            newRunEval = modifyRunEval expr thresholdValue thresholdName newName theRPar theSeq (fromJust runEval')
                       -- match' <- addDecl match Nothing ([newParAbs], Nothing) False
                       match'' <- update (fromJust runEval') newRunEval match

                       return match''

    inMatch _ = mzero

    -- inModule (mod::HsModuleP)
    --  | isDeclaredIn (

    failure = idTP `adhocTP` mod
      where mod (m::HsModuleP) = error "Cannot find the activated Eval Monad! Please activate a run eval monad within scope of the selected entity."

    modifyRunEval expr thresholdValue thresholdName newName theRPar theSeq (pat@(Dec (HsPatBind l p rhs ds)))
     = Dec (HsPatBind l p (convertToSeq newName rhs) (ds++[mkParAbs newName theRPar theSeq]))   -- (Dec (HsPatBind l p (addGuards rhs) ds))
      {-   where



            addGuards (HsGuard es) = error "Cannot add threshold to an eval monad with guards already defined!"
            addGuards (HsBody e) = 	HsGuard [(loc0, theGuard, convertToSeq e), (loc0, nameToExp "otherwise", e)]
            theGuard = Exp (HsInfixApp expr (theOpp) (nameToExp thresholdName))
            theOpp   = HsVar (nameToPNT "<")
        -}

    mkParAbs name theRPar theRSeq = (Dec (HsPatBind loc0 (nameToPat name) addGuards []))
     where
      addGuards = HsGuard [(loc0, theGuard, nameToExp theRPar), (loc0, nameToExp "otherwise", nameToExp theRSeq)]
      theGuard = Exp (HsInfixApp expr (theOpp) (nameToExp thresholdName))
      theOpp   = HsVar (nameToPNT ">")

    convertToSeq :: (Term t) => String -> t -> t
    convertToSeq newName e
      = runIdentity (applyTP (full_tdTP (idTP `adhocTP` exp)) e)
       where
        exp (Exp (HsId (HsVar i))::HsExpP)
         | pNTtoName i == "rpar" = return $ nameToExp newName
        exp x = return x

    findPat :: HsPatP -> [HsDeclP] -> Maybe HsDeclP
    findPat p [] = Nothing -- error "Cannot find active Eval Monad pattern binding!"
    findPat p ((Dec (HsFunBind s m)):ds) = findPat p ds
    findPat p (a@(Dec (HsPatBind l p2 rhs _)):ds)
      | p == p2 = Just $ a
      | otherwise = findPat p ds
    findPat p (_:ds) = findPat p ds

    myElem :: HsPatP -> [HsDeclP] -> Bool
    myElem p [] = False
    myElem p ((Dec (HsFunBind s m)):ds) = myElem p ds
    myElem p ((Dec (HsPatBind l p2 rhs _)):ds)
      | p == p2 = True
      | otherwise = myElem p ds
    myElem p (_:ds) = myElem p ds
doIntroduce' expr thresholdValue thresholdName runEval theRPar theSeq t
 = applyTP (once_buTP (failTP `adhocTP` inMatch) `choiceTP` failure) t
  where

    -- has to be a match: generalising already converted pattern bindings
    -- to a match
    inMatch (match@(HsMatch l name pats rhs ds)::HsMatchP)
     |  findEntity expr match
     && runEval `myElem` ds
       = do (f,d) <- hsFDNamesFromInside match
            -- error (show ((f++d), thresholdName))
            -- unless (not (thresholdName `elem` (f++d))) $ error "Error: name of threshold parameter is already used on RHS!"
            let runEval' = findPat runEval ds
            if runEval' == Nothing
               then mzero
               else do
                       let  newName = mkNewName "rpar_abs" (f++d) 1
                            newRunEval = modifyRunEval expr thresholdValue thresholdName newName theRPar theSeq (fromJust runEval')
                       -- match' <- addDecl match Nothing ([newParAbs], Nothing) False
                       match'' <- update (fromJust runEval') newRunEval match

                       return match''

    inMatch _ = mzero

    -- inModule (mod::HsModuleP)
    --  | isDeclaredIn (

    failure = idTP `adhocTP` mod
      where mod (m::HsModuleP) = error "Cannot find the activated Eval Monad! Please activate a run eval monad within scope of the selected entity."

    modifyRunEval expr thresholdValue thresholdName newName theRPar theSeq (pat@(Dec (HsPatBind l p rhs ds)))
     = Dec (HsPatBind l p (convertToSeq newName rhs) (ds++[mkParAbs newName theRPar theSeq]))   -- (Dec (HsPatBind l p (addGuards rhs) ds))
      {-   where



            addGuards (HsGuard es) = error "Cannot add threshold to an eval monad with guards already defined!"
            addGuards (HsBody e) = 	HsGuard [(loc0, theGuard, convertToSeq e), (loc0, nameToExp "otherwise", e)]
            theGuard = Exp (HsInfixApp expr (theOpp) (nameToExp thresholdName))
            theOpp   = HsVar (nameToPNT "<")
        -}

    mkParAbs name theRPar theRSeq = (Dec (HsPatBind loc0 (nameToPat name) addGuards []))
     where
      addGuards = HsGuard [(loc0, theGuard, nameToExp theRPar), (loc0, nameToExp "otherwise", nameToExp theRSeq)]
      theGuard = Exp (HsInfixApp expr (theOpp) (nameToExp thresholdName))
      theOpp   = HsVar (nameToPNT ">")

    convertToSeq :: (Term t) => String -> t -> t
    convertToSeq newName e
      = runIdentity (applyTP (full_tdTP (idTP `adhocTP` exp)) e)
       where
        exp (Exp (HsId (HsVar i))::HsExpP)
         | pNTtoName i == "rpar" = return $ nameToExp newName
        exp x = return x

    findPat :: HsPatP -> [HsDeclP] -> Maybe HsDeclP
    findPat p [] = Nothing -- error "Cannot find active Eval Monad pattern binding!"
    findPat p ((Dec (HsFunBind s m)):ds) = findPat p ds
    findPat p (a@(Dec (HsPatBind l p2 rhs _)):ds)
      | p == p2 = Just $ a
      | otherwise = findPat p ds
    findPat p (_:ds) = findPat p ds

    myElem :: HsPatP -> [HsDeclP] -> Bool
    myElem p [] = False
    myElem p ((Dec (HsFunBind s m)):ds) = myElem p ds
    myElem p ((Dec (HsPatBind l p2 rhs _)):ds)
      | p == p2 = True
      | otherwise = myElem p ds
    myElem p (_:ds) = myElem p ds