module Testworld.Tests where import HFABase import DFA.DFA import Test.QuickCheck printAll = sequence [ putStrLn "\n::Category, DFA Type Function::", printDFAtoIO dfaCategory , putStrLn "\n::Hard, DFA Char Int::", printDFAtoIO dfaHard , putStrLn "\n::Fancy, DFA Int Int::" ,printDFAtoIO dfaFancy , putStrLn "\n"] --------------------------------------------- --Ad Hoc Tests------------------------------- --------------------------------------------- -- -- a DFA for recognizing Strings of Type a+b+ -- -- The State machine for this Regex should look like -- -- (This is a simple state/symbol table -- -- Sym a Sym b --[St 1] 1 2 --(St 2) E 2 -- St E E E -- --that should be read as Row X on Column Y goes to State (X,Y), eg: -- In State 1, on Symbol a, goto state (1,a) = 1 -- In State 2, on Symbol a, goto state (2,a) = E -- In State E (Error), on Symbol * (Anything) goto (E,*) = E -- --(x) denotes a final state --[x] denotes a start state -- --So the transition function trASBS looks like: -- trASBS :: State Int -> Symbol Char -> State Int -- state E will be represented by state 3 trASBS (St st) (Sym inp) = case (st,inp) of (1,'a') -> (St 1) (2,'a') -> (St 3) (1,'b') -> (St 2) (2,'b') -> (St 2) (_,_) -> (St 3) -- this covers all other transitions on other symbols, including the lambda -- transition. dfaASBS :: DFA Int Char dfaASBS = Union [St 1, St 2, St 3] [Sym 'a', Sym 'b'] (trASBS) (St 1) [St 2] -- Heres a tougher, fancy DFA, one that represents (01)*1*0*(10)* -- -- The State table looks like: -- (x) = start state -- = accepting-initial state -- [x] = accept state -- {x} = error state -- 0 1 -- <0> 1 7 -- 1 7 2 -- [2] 1 3 -- [3] 4 3 -- [4] 4 5 -- 5 6 7 -- [6] 7 5 -- {7} 7 7 trFancy :: State Int -> Symbol Int -> State Int trFancy (St st) (Sym inp) = case (st,inp) of (0,0) -> (St 1) (1,0) -> (St 7) (2,0) -> (St 1) (3,0) -> (St 4) (4,0) -> (St 4) (5,0) -> (St 6) (6,0) -> (St 7) (0,1) -> (St 7) (1,1) -> (St 2) (2,1) -> (St 3) (3,1) -> (St 3) (4,1) -> (St 5) (5,1) -> (St 7) (6,1) -> (St 5) (_,_) -> (St 7) dfaFancy :: DFA Int Int dfaFancy = Union (toStates [0..7]) (toSymbols [0,1]) (trFancy) (St 0) (toStates [0,2,3,4,6]) -- -- one more DFA test, this one has a bigger alphabet, it will parse the regex -- (0+1+2+3+)0? -- -- the state table is -- -- 0 1 2 3 -- a a b E E -- b E b c E -- c E E c d -- [d] e E E d -- [e] E E E E -- E E E E E trHard :: State Char -> Symbol Int -> State Char trHard (St st) (Sym sym) = case (st, sym) of ('a',0) -> St 'a' ('a',1) -> St 'b' ('a',_) -> St 'E' ('b',1) -> St 'b' ('b',2) -> St 'c' ('b',_) -> St 'E' ('c',2) -> St 'c' ('c',3) -> St 'd' ('c',_) -> St 'E' ('d',3) -> St 'd' ('d',0) -> St 'e' (_,_) -> St 'E' dfaHard :: DFA Char Int dfaHard = Union (toStates "abcdeE") (toSymbols [0..3]) (trHard) (St 'a') [(St 'd'), (St 'e')] -- This is a simple typechecker implemented as an NFA -- -- The tough part here is the initial state, this can only be worded efficiently as a NFA, so thats what we'll do -- this bit when we have finished NFA, assume Type is a transition to all Types -- succ id ord asc not and or isZero -- InitT Type Type Type Type Type Type Type Type -- Int Int Int err Char err err err Bool -- Char err Char Int err err err err err -- Bool err Bool err err Bool Bool Bool err -- Unit err Unit err err err err err err -- err err err err err err err err err -- -- so heres the transition function data Type = InitT | IntT | CharT | BoolT | UnitT | ErrT deriving (Ord,Eq, Show) data Function = SuccF | IdF | OrdF | AscF | NotF | OrF | AndF | IsZeroF deriving (Ord,Eq, Show) -- this allows us not to have to write St on these things trCategory :: State Type -> Symbol Function -> State Type trCategory (St t) (Sym f) = case (t,f) of (IntT, SuccF) -> (St IntT) (IntT, IdF) -> (St IntT) (IntT, AscF) -> (St IntT) (IntT, IsZeroF) -> (St IntT) (CharT, IdF) -> (St CharT) (CharT, OrdF) -> (St IntT) (BoolT, IdF) -> (St BoolT) (BoolT, OrF) -> (St BoolT) (BoolT, NotF) -> (St BoolT) (BoolT, AndF) -> (St BoolT) (UnitT, IdF) -> (St UnitT) (_, _) -> (St ErrT) dfaCategory :: DFA Type Function dfaCategory = Union (toStates [IntT, CharT, BoolT, UnitT, ErrT]) (toSymbols [SuccF, IdF, OrdF, AscF, NotF, OrF, AndF, IsZeroF]) (trCategory) (St InitT) (toStates [IntT, CharT, BoolT, UnitT])