Marc-Aurel P.
Marc-Aurel P.

Reputation: 21

Haskell - Change variable during runtime

How can i change a variable during runtime?

I got this:

data Ausdruck = K Wahrheitswert                 -- Logical constant
                | V Variable                    -- Logical Variable
                | Nicht Ausdruck                -- Logical negation
                | Und Ausdruck Ausdruck         -- Logical and
                | Oder Ausdruck Ausdruck        -- Logical or
                | Impl Ausdruck Ausdruck        -- Logical implied
                | Esgibt Variable Ausdruck      -- "exist"
                | Fueralle Variable Ausdruck    -- "all"
                                    deriving (Eq,Show)



type Variables = Variable -> Bool

    variables1 :: Variable -> Bool
    variables1 (Var N1) = True
    variables1 (Var N2) = False
    variables1 (Var N3) = True
    variables1 (Var N4) = True
    variables1 (Var N5) = False




evaluate :: Prop -> Variables -> Bool
evaluate (K bool) belegung = bool
evaluate (V var) belegung = belegung var
evaluate (Nicht ausdruck) belegung = not (evaluate ausdruck belegung)
evaluate (Und ausdruck ausdruck2) belegung = (evaluate ausdruck belegung) && (evaluate ausdruck2 belegung)
evaluate (Oder ausdruck ausdruck2) belegung = (evaluate ausdruck belegung) || (evaluate ausdruck2 belegung)

Now i want to add the quantifier "all". So i want to check an Propositional calculus if its still true, when i change N1 to False. But how can i change a variable during runtime?

Best regards Marc

Upvotes: 0

Views: 454

Answers (1)

pat
pat

Reputation: 12749

You can create a new environment based on an existing environment with a variable bound to a new value:

erweiternBelegung :: Variable -> Bool -> Variables -> Variables
erweiternBelegung v b vs = \v' -> if v == v' then b else vs v'

and use it to make sure that, in the Fueralle case, ausdruck holds when var is both True and False:

...
evaluate (Fueralle var ausdruk) belegung =
  evaluate ausdruk (erweiternBelegung var True belegung) &&
  evaluate ausdruk (erweiternBelegung var False belegung)

The existential quantifier Esgibt will be the same, except the && will be replaced with ||.

Upvotes: 1

Related Questions