Advertisement
Guest User

j

a guest
Dec 2nd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE GADTs #-}
  2.  
  3. module PDA (PDA(..), test) where
  4.  
  5. import Data.Maybe
  6. import Safe
  7.  
  8. -- state -> top stack symbol -> sequence symbol -> list of resulting PDA states
  9. type Delta state char = state -> Maybe char -> Maybe char -> [(state, [char])]
  10.  
  11. data PDA state char where
  12.     -- PDA state (state + stack) -> accepting states -> transition function
  13.     PDA :: (Eq state) => (state, [char]) -> [state] -> Delta state char -> PDA state char
  14.  
  15. test :: PDA state char -> [char] -> Bool
  16. test (PDA start_pda_state accepting_states delta) sequence = helper sequence start_pda_state
  17.     where helper seq@(s:seqr) pda_state = or ((fmap (helper seqr) $ handle_pda_state pda_state $ Just s) ++
  18.                                               (fmap (helper seq) $ handle_pda_state pda_state Nothing))
  19.           helper [] pda_state = or $ accepted pda_state : (fmap (helper []) $ handle_pda_state pda_state Nothing)
  20.           handle_pda_state (st, stack) s = fmap (\(st, chs) -> (st, chs ++ tailSafe stack)) $ delta st (headMay stack) s
  21.           accepted (state, stack) = state `elem` accepting_states && null stack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement