packages feed

imp-ppl-0.1.0.0: src/Imp/Examples/MontyHall.hs

{-# LANGUAGE QualifiedDo, RebindableSyntax #-}
-- | Monty Hall problem with imprecise host behavior.
module Imp.Examples.MontyHall
  ( Door(..)
  , montyHall
  ) where

import Imp

-- | The three doors.
data Door = Door1 | Door2 | Door3
  deriving stock (Eq, Ord, Show)

-- | The Monty Hall encoding.
montyHall :: Imp '["host_bias"] Bool
montyHall = Imp.do
  c1       <- flip (1/3)
  c2       <- flip (1/2)
  hostBias <- knight @"host_bias"
  let car  = if c1 then Door1 else if c2 then Door2 else Door3
      host = case car of
               Door1 -> if hostBias then Door2 else Door3
               Door2 -> Door3
               Door3 -> Door2
      switchTo = case host of
                   Door2 -> Door3
                   Door3 -> Door2
                   Door1 -> error "Impossible..."
  Imp.return (switchTo == car)