transformations-0.1.0.0: examples/Datatypes.hs
module Datatypes (
Tree (..), exTree1, exTree2, exTree3, exTree4, exTree5,
List (..), toL, fromL, exLst1, exLst2,
X (..), exX1, exX2,
Zig (..), Zag (..), zigzag, zigzag2,
Expr, prog1, prog2, prog3, prog4, prog5, prog6,
module Lang
) where
import Lang
import Data.List ( unfoldr )
--------------------------------------------------------------------------------
-- Example datatypes
--------------------------------------------------------------------------------
--Trees
data Tree = Leaf Int | Bin Tree Tree deriving (Show, Eq)
exTree1, exTree2, exTree3, exTree4, exTree5 :: Tree
exTree1 = Bin (Leaf 0) (Leaf 1)
exTree2 = Bin (Leaf 1) (Leaf 0)
exTree3 = Bin (Leaf 2) (Leaf 3)
exTree4 = Bin exTree2 exTree3
exTree5 = Bin exTree3 exTree4
-- Lists
data List a = Nil | Cons a (List a) deriving (Eq, Show)
toL :: [a] -> List a
toL = foldr Cons Nil
fromL :: List a -> [a]
fromL = unfoldr f where
f Nil = Nothing
f (Cons h t) = Just (h,t)
exLst1, exLst2 :: List Int
exLst1 = Cons 1 $ Cons 2 $ Cons 3 $ Cons 4 Nil
exLst2 = Cons 4 $ Cons 2 $ Cons 3 $ Cons 1 Nil
-- Something more exotic
data X = XA X | XB X | XC X X | XD Int deriving (Show, Eq)
exX1, exX2 :: X
exX1 = XA (XA (XA (XC (XD 1) (XD 2))))
exX2 = XA (XB (XA (XA (XB (XC (XD 2) (XD 1))))))
-- Mutually recursive
data Zig = Zig1 Zig | Zig2 Zag | Zig3 deriving (Show, Eq)
data Zag = Zag1 Zag | Zag2 Zig | Zag3 deriving (Show, Eq)
zigzag :: Zig
zigzag = Zig1 (Zig2 (Zag2 Zig3))
zigzag2 :: Zig
zigzag2 = Zig2 (Zag1 (Zag2 Zig3))
-- Example from paper (imported from Lang)
type Expr = AExpr
progFragment1, progFragment2 :: String
progFragment1 = "a := 1;"
++ "b := a + 2;"
++ "if b > 3"
++ "then a := 2"
++ "else b := 1;"
progFragment2 = "a := 1;"
++ "b := a + 2;"
++ "if not b > 3"
++ "then b := 1"
++ "else a := 2;"
prog1, prog2, prog3, prog4, prog5, prog6 :: Stmt
prog1 = parseString . init . concat . replicate 1 $ progFragment1
prog2 = parseString . init . concat . replicate 1 $ progFragment2
prog3 = parseString . init . concat . replicate 4 $ progFragment1
prog4 = parseString . init . concat . replicate 4 $ progFragment2
prog5 = parseString . init . concat . replicate 5 $ progFragment1
prog6 = parseString . init . concat . replicate 5 $ progFragment2