[added printf example mail@stefanwehr.de**20050528085605] { hunk ./PhracPrelude.phc 12 +-- maybe the most inefficient implementation of converting an Int to a string... +showInt :: Int -> List Char; +showInt i = if i == -9 then "-9" else ( + if i == -8 then "-8" else ( + if i == -7 then "-7" else ( + if i == -6 then "-6" else ( + if i == -5 then "-5" else ( + if i == -4 then "-4" else ( + if i == -3 then "-3" else ( + if i == -2 then "-2" else ( + if i == -1 then "-1" else ( + if i == 0 then "0" else ( + if i == 1 then "1" else ( + if i == 2 then "2" else ( + if i == 3 then "3" else ( + if i == 4 then "4" else ( + if i == 5 then "5" else ( + if i == 6 then "6" else ( + if i == 7 then "7" else ( + if i == 8 then "8" else ( + if i == 9 then "9" else ( + let x = i % 10; + y = i / 10; + in showInt y ++ showInt x + ))))))))))))))))))); + +class Show __a where { + show :: __a -> List Char; +} + +instance Show Int where { + show i = showInt i; +} + +instance Show Char where { + show c = Cons c Nil; +} + addfile ./tests/examples/Printf.out hunk ./tests/examples/Printf.out 1 +sprintf :: forall fmt[1] . Format fmt[1] => fmt[1] -> Sprintf fmt[1] +formatSpec :: S (I (S (C (List Char)))) addfile ./tests/examples/Printf.phc hunk ./tests/examples/Printf.phc 1 +data I f = I f; +data C f = C f; +data S f = S (List Char) f; + +class Format fmt where { + type Sprintf fmt; + sprintf' :: List Char -> fmt -> Sprintf fmt; +} + +instance Format (List Char) where { + type Sprintf (List Char) = List Char; + sprintf' prefix str = prefix ++ str; +} + +instance Format a => Format (I a) where { + type Sprintf (I a) = Int -> Sprintf a; + sprintf' prefix i = case i of I a -> (\j -> sprintf' (prefix ++ show j) a);; +} + +instance Format a => Format (C a) where { + type Sprintf (C a) = Char -> Sprintf a; + sprintf' prefix c = case c of C a -> (\d -> sprintf' (prefix ++ show d) a);; +} + +instance Format a => Format (S a) where { + type Sprintf (S a) = Sprintf a; + sprintf' prefix s = case s of S str a -> sprintf' (prefix ++ str) a;; +} + +sprintf :: Format fmt => fmt -> Sprintf fmt; +sprintf = sprintf' ""; + +formatSpec = S "Int: " (I (S ", Char: " (C "."))); }