Commit 77af3e95 authored by Reg Huijben's avatar Reg Huijben

Added coin

parent 6a834ac7
......@@ -10,7 +10,7 @@ module serialize7start
import StdEnv, StdMaybe
class serialize a | isUnit a where
class serialize a | isUnit a where // isUnit restriction on a for the write of serialize (CONS a)
write :: a [String] -> [String]
read :: [String] -> Maybe (a,[String])
......@@ -127,7 +127,7 @@ instance serialize (EITHER a b) | serialize a & serialize b where
instance serialize (CONS a) | serialize a & isUnit a where
instance serialize (CONS a) | serialize a where
// Cheekily using the fact that a written unit is an empty list
//write (CONS s a) c = let written = write a [] in
// if (length (written) == 0)
......@@ -363,6 +363,37 @@ instance serialize (a,b) | serialize a & serialize b where
// == already defined for tuple
//
//
//
//
// Coin
//
:: Coin = Head | Tail
:: CoinG :== EITHER (CONS UNIT) (CONS UNIT)
fromCoin :: Coin -> CoinG
fromCoin Head = LEFT (CONS "Head" UNIT)
fromCoin Tail = RIGHT (CONS "Tail" UNIT)
toCoin :: CoinG -> Coin
toCoin (LEFT _) = Head
toCoin (RIGHT _) = Tail
instance == Coin where
(==) c1 c2 = (fromCoin c1) == (fromCoin c2)
instance serialize Coin where
write c cont = write (fromCoin c) cont
read r = case read r of
Just (c, rest) -> Just(toCoin c, rest)
_ -> Nothing
//
//
//
......@@ -427,6 +458,8 @@ Start =
,test 0
,test 123
,test -36
,test Tail
,test Head
,test (True,3) // Added (Bool,Int)
,test (Triple 1 True [1]) // Added (Int,Bool,[Int])
,test (Just True) // Added (Maybe Bool)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment