From 1379ad0ce97807101241244142755375fed5faed Mon Sep 17 00:00:00 2001 From: Joel Kaasinen Date: Mon, 23 May 2022 15:59:03 +0300 Subject: [PATCH] Set 16b Ex 3: better description, more tests --- exercises/Set16b.hs | 7 ++++--- exercises/Set16bTest.hs | 10 +++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/exercises/Set16b.hs b/exercises/Set16b.hs index eefc3a33..1ca87c8d 100644 --- a/exercises/Set16b.hs +++ b/exercises/Set16b.hs @@ -36,9 +36,10 @@ composeRates rate1 rate2 = todo -- * Name Last - for last names -- * Name Full - for full names -- --- In this exercise, you should define the types First, Last, Full and --- Name. Then implement the functions fromName, toFirst and toLast. Give --- the functions the commented-out types +-- In this exercise, you should define the phantom types First, Last +-- and Full, and the parameterised type Name. Then implement the +-- functions fromName, toFirst and toLast. Give the functions the +-- commented-out types -- -- Examples: -- fromName (toFirst "bob") ==> "bob" diff --git a/exercises/Set16bTest.hs b/exercises/Set16bTest.hs index ed5c8354..18710070 100644 --- a/exercises/Set16bTest.hs +++ b/exercises/Set16bTest.hs @@ -57,6 +57,14 @@ letter = choose ('a','z') word = listOf1 letter +assertPhantom (DataType vars cs) = + conjoin [counterexample " should have no constructors (be a phantom type)" $ null cs + ,counterexample " should have no type parameters" $ null vars] + +ex3_phantom_First = $(reifyType "First") assertPhantom +ex3_phantom_Last = $(reifyType "Last") assertPhantom +ex3_phantom_Full = $(reifyType "Full") assertPhantom + ex3_v = forAll word $ \w -> conjoin [counterexample ("fromName (toFirst "++show w++")") $ fromName (toFirst w) ?== w @@ -110,7 +118,7 @@ ex5_c = counterexample "render (Money 1.0 :: Money CHF)" $ tests = [(1,"pounds",[ex1_t,ex1_v]) ,(2,"composeRates",[ex2_v, ex2_t1, ex2_t2]) - ,(3,"firstlastfull1",[ex3_v, ex3_t1, ex3_t2]) + ,(3,"firstlastfull1",[ex3_phantom_First, ex3_phantom_Last, ex3_phantom_Full, ex3_v, ex3_t1, ex3_t2]) ,(4,"firstlastfull2",[ex4_v1, ex4_t1, ex4_v2, ex4_t2]) ,(5,"Render currency",[ex5_e, ex5_u, ex5_c])]