Idris2/tests/prelude/unpack/unpack.idr
Stefan Höck e73ca067ed
[ performance ] a faster implementation of unpack (#3281)
* [ performance ] a much faster implementation of unpack

* [ fix ] partiality error in Hangman test

* [ test ] add some documentation to the unpack test case

* [ test ] fix expected output of unpack test
2024-05-24 08:06:56 +01:00

40 lines
1.2 KiB
Idris

||| This tests that issue #3280 has been fixed. With the former
||| implementation of `unpack`, the compiler would not produce
||| an error in a reasonable amount of time.
import Data.String
%default total
data Format = FInt Format -- %d
| FString Format -- %s
| FOther Char Format
| FEnd
format : List Char -> Format
format Nil = FEnd
format ('%' :: 'd' :: xs) = FInt (format xs)
format ('%' :: 's' :: xs) = FString (format xs)
format (x :: xs) = FOther x (format xs)
0 InterpFormat : Format -> Type
InterpFormat (FInt f) = Int -> InterpFormat f
InterpFormat (FString f) = String -> InterpFormat f
InterpFormat (FOther _ f) = InterpFormat f
InterpFormat FEnd = String
formatString : String -> Format
formatString s = format (unpack s)
toFunction : (fmt : Format) -> String -> InterpFormat fmt
toFunction (FInt x) str = \y => toFunction x (str ++ show y)
toFunction (FString x) str = \y => toFunction x (str ++ y)
toFunction (FOther c x) str = toFunction x (str ++ singleton c)
toFunction FEnd str = str
printf : (s : String) -> InterpFormat (formatString s)
printf s = toFunction (formatString s) ""
message : String
message = printf "My name is %s and I am %d years old"