Copyright | (c) Grant Weyburne 2019 |
---|---|
License | BSD-3 |
Maintainer | gbwey9@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
Prepackaged proxies for use with Refined3
Synopsis
- type Ccip = Map (ReadP Int) (Remove "-" Id >> Ones)
- type Ccop (n :: Nat) = Guard ('(n, Len) >> Printf2 "expected %d digits but found %d") (Len >> Same n) >> Luhn
- type Ccfmt (ns :: [Nat]) = ConcatMap ShowP Id >> (SplitAts ns Id >> Concat (Intercalate '["-"] Id))
- type Ccn (ns :: [Nat]) = '(Ccip, Ccop (SumT ns), Ccfmt ns, String)
- type CC11 = Ccn '[4, 4, 3]
- ccn :: forall (ns :: [Nat]). (KnownNat (SumT ns), P ns String, PP ns String ~ [Integer]) => Proxy (Ccn ns)
- cc11 :: Proxy (Ccn '[4, 4, 3])
- type DateTime1 (t :: Type) = '(Dtip1 t, Dtop1, Dtfmt1, String)
- type Dtip1 t = ParseTimeP t "%F %T" Id
- type Dtop1 = Map (ReadP Int) (FormatTimeP "%H %M %S" Id >> Resplit "\\s+" Id) >> (Guards '['(Printf2 "guard %d invalid hours %d", Between 0 23), '(Printf2 "guard %d invalid minutes %d", Between 0 59), '(Printf2 "guard %d invalid seconds %d", Between 0 59)] >> True)
- type Dtfmt1 = FormatTimeP "%F %T" Id
- ssn :: Proxy Ssn
- type Ssn = '(Ssnip, Ssnop, Ssnfmt, String)
- type Ssnip = Map (ReadP Int) (Rescan "^(\\d{3})-(\\d{2})-(\\d{4})$" Id >> (OneP >> Snd))
- type Ssnop = GuardsQuick (Printf2 "number for group %d invalid: found %d") '[Between 1 899 && (Id /= 666), Between 1 99, Between 1 9999] >> True
- type Ssnop' = Guards '['(Printf2 "guard %d invalid: found %d", Between 1 899 && (Id /= 666)), '(Printf2 "group %d invalid: found %d", Between 1 99), '(Printf2 "group %d invalid: found %d", Between 1 9999)] >> True
- type Ssnfmt = Printfnt 3 "%03d-%02d-%04d"
- hms :: Proxy Hms
- type Hms = '(Hmsip, Hmsop >> True, Hmsfmt, String)
- type Hmsip = Map (ReadP Int) (Resplit ":" Id)
- type Hmsop = Guard (Printf "expected len 3 but found %d" Len) (Len >> Same 3) >> Guards '['(Printf2 "guard(%d) %d hours is out of range", Between 0 23), '(Printf2 "guard(%d) %d mins is out of range", Between 0 59), '(Printf2 "guard(%d) %d secs is out of range", Between 0 59)]
- type Hmsfmt = Printfnt 3 "%02d:%02d:%02d"
- type Ip = '(Ipip, Ipop, Ipfmt, String)
- type Ip1 = '(Ipip, Ipop', Ipfmt, String)
- ip :: Proxy Ip
- ip1 :: Proxy Ip1
- type Ipip = Map (ReadP Int) (Rescan "^(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})$" Id >> (OneP >> Snd))
- type Ipop = GuardsQuick (Printf2 "guard(%d) octet out of range 0-255 found %d") (RepeatT 4 (Between 0 255)) >> True
- type Ipop' = Guards '['(Printf2 "octet %d out of range 0-255 found %d", Between 0 255), '(Printf2 "octet %d out of range 0-255 found %d", Between 0 255), '(Printf2 "octet %d out of range 0-255 found %d", Between 0 255), '(Printf2 "octet %d out of range 0-255 found %d", Between 0 255)] >> True
- type Ipfmt = Printfnt 4 "%03d.%03d.%03d.%03d"
- type HmsRE = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"
- type Hmsconv = Do '[Rescan HmsRE Id, Head, Snd, Map (ReadBaseInt 10) Id]
- type Hmsval = GuardsQuick (Printf2 "guard(%d) %d is out of range") '[Between 0 23, Between 0 59, Between 0 59]
- type Hms4 = '(Hmsconv, Hmsval >> True, Hmsfmt, String)
- hms4 :: Proxy Hms4
- type OctetRE = "(25[0-5]|2[0..4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])"
- type Ip4strictRE = ("^" `AppendSymbol` IntersperseT "\\." (RepeatT 4 OctetRE)) `AppendSymbol` "$"
- type DateFmts = '["%Y-%m-%d", "%m/%d/%y", "%B %d %Y"]
- type DateN = '(ParseTimes Day DateFmts Id, True, FormatTimeP "%Y-%m-%d" Id, String)
- type DateTimeFmts = '["%Y-%m-%d %H:%M:%S", "%m/%d/%y %H:%M:%S", "%B %d %Y %H:%M:%S", "%Y-%m-%dT%H:%M:%S"]
- type DateTimeN = '(ParseTimes UTCTime DateTimeFmts Id, True, FormatTimeP "%Y-%m-%d %H:%M:%S" Id, String)
- type BaseN (n :: Nat) = '(ReadBase Integer n, True, ShowBase n, String)
- base16 :: Proxy (BaseN 16)
- daten :: Proxy DateN
- datetimen :: Proxy DateTimeN
- type BetweenR m n = Refined3 Id (Between m n) Id Int
- type LuhnR (n :: Nat) = MakeR3 (LuhnY n)
- type LuhnR' (n :: Nat) = MakeR3 (LuhnX n)
- type LuhnY (n :: Nat) = '(Map (ReadP Int) Ones, Guard (Printfn "incorrect number of digits found %d but expected %d in [%s]" (TupleI '[Len, W n, ShowP])) (Len >> Same n) >> (Guard "luhn check failed" Luhn >> True), ConcatMap ShowP Id, String)
- type LuhnX (n :: Nat) = '(Map (ReadP Int) Ones, Luhn'' n >> True, ConcatMap ShowP Id, String)
- type Luhn'' (n :: Nat) = Guard (Printfn "incorrect number of digits found %d but expected %d in [%s]" (TupleI '[Len, W n, ShowP])) (Len >> Same n) >> (Do '[Reverse, Ziplc [1, 2] Id, Map ((Fst * Snd) >> If (Id >= 10) (Id - 9) Id) Id, FoldMap (Sum Int) Id] >> Guard (Printfn "expected %d mod 10 = 0 but found %d" (TupleI '[Id, Id `Mod` 10])) (Mod Id 10 >> Same 0))
- type Luhn' (n :: Nat) = Msg "Luhn'" (Do '[Guard (Printfn "incorrect number of digits found %d but expected %d in [%s]" (TupleI '[Len, W n, Id])) (Len >> Same n), Do '[Ones, Map (ReadP Int) Id, Reverse, Ziplc [1, 2] Id, Map ((Fst * Snd) >> If (Id >= 10) (Id - 9) Id) Id, FoldMap (Sum Int) Id], Guard (Printfn "expected %d mod 10 = 0 but found %d" (TupleI '[Id, Id `Mod` 10])) (Mod Id 10 >> Same 0)])
- type Ok (t :: Type) = '(Id, True, Id, t)
- type OkR (t :: Type) = MakeR3 (Ok t)
- type OkNot (t :: Type) = '(Id, False, Id, t)
- type OkNotR (t :: Type) = MakeR3 (OkNot t)
- type BaseIJ (i :: Nat) (j :: Nat) = BaseIJ' i j True
- type BaseIJ' (i :: Nat) (j :: Nat) p = '(ReadBase Int i >> ShowBase j, p, ReadBase Int j >> ShowBase i, String)
- type ReadShow (t :: Type) = '(ReadP t, True, ShowP, String)
- type ReadShowR (t :: Type) = MakeR3 (ReadShow t)
- type ReadShow' (t :: Type) p = '(ReadP t, p, ShowP, String)
- type ReadShowR' (t :: Type) p = MakeR3 (ReadShow' t p)
Documentation
type Ccip = Map (ReadP Int) (Remove "-" Id >> Ones) Source #
credit card with luhn algorithm
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
prtEval3P cc11 ol "1234-5678-901"
Left Step 2. False Boolean Check(op) | FalseP
>>>
prtEval3P cc11 ol "1234-5678-903"
Right (Refined3 {r3In = [1,2,3,4,5,6,7,8,9,0,3], r3Out = "1234-5678-903"})
type Ccop (n :: Nat) = Guard ('(n, Len) >> Printf2 "expected %d digits but found %d") (Len >> Same n) >> Luhn Source #
type Ccfmt (ns :: [Nat]) = ConcatMap ShowP Id >> (SplitAts ns Id >> Concat (Intercalate '["-"] Id)) Source #
ccn :: forall (ns :: [Nat]). (KnownNat (SumT ns), P ns String, PP ns String ~ [Integer]) => Proxy (Ccn ns) Source #
type DateTime1 (t :: Type) = '(Dtip1 t, Dtop1, Dtfmt1, String) Source #
read in a datetime
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
prtEval3P (Proxy @(DateTime1 UTCTime)) ol "2018-09-14 02:57:04"
Right (Refined3 {r3In = 2018-09-14 02:57:04 UTC, r3Out = "2018-09-14 02:57:04"})
type Dtip1 t = ParseTimeP t "%F %T" Id Source #
type Dtop1 = Map (ReadP Int) (FormatTimeP "%H %M %S" Id >> Resplit "\\s+" Id) >> (Guards '['(Printf2 "guard %d invalid hours %d", Between 0 23), '(Printf2 "guard %d invalid minutes %d", Between 0 59), '(Printf2 "guard %d invalid seconds %d", Between 0 59)] >> True) Source #
type Dtfmt1 = FormatTimeP "%F %T" Id Source #
type Ssn = '(Ssnip, Ssnop, Ssnfmt, String) Source #
read in an ssn
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
prtEval3P ssn ol "134-01-2211"
Right (Refined3 {r3In = [134,1,2211], r3Out = "134-01-2211"})
>>>
prtEval3P ssn ol "666-01-2211"
Left Step 2. Failed Boolean Check(op) | number for group 0 invalid: found 666
>>>
prtEval3P ssn ol "666-01-2211"
Left Step 2. Failed Boolean Check(op) | number for group 0 invalid: found 666
>>>
prtEval3P ssn ol "667-00-2211"
Left Step 2. Failed Boolean Check(op) | number for group 1 invalid: found 0
>>>
prtEval3P ssn ol "666-01-2211"
Left Step 2. Failed Boolean Check(op) | number for group 0 invalid: found 666
>>>
prtEval3P ssn ol "991-22-9999"
Left Step 2. Failed Boolean Check(op) | number for group 0 invalid: found 991
type Ssnop = GuardsQuick (Printf2 "number for group %d invalid: found %d") '[Between 1 899 && (Id /= 666), Between 1 99, Between 1 9999] >> True Source #
type Ssnop' = Guards '['(Printf2 "guard %d invalid: found %d", Between 1 899 && (Id /= 666)), '(Printf2 "group %d invalid: found %d", Between 1 99), '(Printf2 "group %d invalid: found %d", Between 1 9999)] >> True Source #
read in a time and validate it
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
prtEval3P hms ol "23:13:59"
Right (Refined3 {r3In = [23,13,59], r3Out = "23:13:59"})
>>>
prtEval3P hms ol "23:13:60"
Left Step 2. Failed Boolean Check(op) | guard(2) 60 secs is out of range
>>>
prtEval3P hms ol "26:13:59"
Left Step 2. Failed Boolean Check(op) | guard(0) 26 hours is out of range
type Hmsop = Guard (Printf "expected len 3 but found %d" Len) (Len >> Same 3) >> Guards '['(Printf2 "guard(%d) %d hours is out of range", Between 0 23), '(Printf2 "guard(%d) %d mins is out of range", Between 0 59), '(Printf2 "guard(%d) %d secs is out of range", Between 0 59)] Source #
type Ip = '(Ipip, Ipop, Ipfmt, String) Source #
read in an ipv4 address and validate it
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
prtEval3P ip ol "001.223.14.1"
Right (Refined3 {r3In = [1,223,14,1], r3Out = "001.223.014.001"})
>>>
prtEval3P ip ol "001.223.14.999"
Left Step 2. Failed Boolean Check(op) | guard(3) octet out of range 0-255 found 999
>>>
prtEval3P ip ol "001.223.14.999.1"
Left Step 1. Initial Conversion(ip) Failed | Regex no results
>>>
prtEval3P ip ol "001.257.14.1"
Left Step 2. Failed Boolean Check(op) | guard(1) octet out of range 0-255 found 257
type Ipip = Map (ReadP Int) (Rescan "^(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})$" Id >> (OneP >> Snd)) Source #
type Ipop = GuardsQuick (Printf2 "guard(%d) octet out of range 0-255 found %d") (RepeatT 4 (Between 0 255)) >> True Source #
type Ipop' = Guards '['(Printf2 "octet %d out of range 0-255 found %d", Between 0 255), '(Printf2 "octet %d out of range 0-255 found %d", Between 0 255), '(Printf2 "octet %d out of range 0-255 found %d", Between 0 255), '(Printf2 "octet %d out of range 0-255 found %d", Between 0 255)] >> True Source #
type Hmsval = GuardsQuick (Printf2 "guard(%d) %d is out of range") '[Between 0 23, Between 0 59, Between 0 59] Source #
type Ip4strictRE = ("^" `AppendSymbol` IntersperseT "\\." (RepeatT 4 OctetRE)) `AppendSymbol` "$" Source #
type DateTimeFmts = '["%Y-%m-%d %H:%M:%S", "%m/%d/%y %H:%M:%S", "%B %d %Y %H:%M:%S", "%Y-%m-%dT%H:%M:%S"] Source #
type DateTimeN = '(ParseTimes UTCTime DateTimeFmts Id, True, FormatTimeP "%Y-%m-%d %H:%M:%S" Id, String) Source #
type BaseN (n :: Nat) = '(ReadBase Integer n, True, ShowBase n, String) Source #
convert a string from the given base 'i' but stores it internally as a string of base 'j'
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
prtEval3P (Proxy @(BaseN 16)) ol "00fe"
Right (Refined3 {r3In = 254, r3Out = "fe"})
type LuhnY (n :: Nat) = '(Map (ReadP Int) Ones, Guard (Printfn "incorrect number of digits found %d but expected %d in [%s]" (TupleI '[Len, W n, ShowP])) (Len >> Same n) >> (Guard "luhn check failed" Luhn >> True), ConcatMap ShowP Id, String) Source #
type LuhnX (n :: Nat) = '(Map (ReadP Int) Ones, Luhn'' n >> True, ConcatMap ShowP Id, String) Source #
type Luhn'' (n :: Nat) = Guard (Printfn "incorrect number of digits found %d but expected %d in [%s]" (TupleI '[Len, W n, ShowP])) (Len >> Same n) >> (Do '[Reverse, Ziplc [1, 2] Id, Map ((Fst * Snd) >> If (Id >= 10) (Id - 9) Id) Id, FoldMap (Sum Int) Id] >> Guard (Printfn "expected %d mod 10 = 0 but found %d" (TupleI '[Id, Id `Mod` 10])) (Mod Id 10 >> Same 0)) Source #
type Luhn' (n :: Nat) = Msg "Luhn'" (Do '[Guard (Printfn "incorrect number of digits found %d but expected %d in [%s]" (TupleI '[Len, W n, Id])) (Len >> Same n), Do '[Ones, Map (ReadP Int) Id, Reverse, Ziplc [1, 2] Id, Map ((Fst * Snd) >> If (Id >= 10) (Id - 9) Id) Id, FoldMap (Sum Int) Id], Guard (Printfn "expected %d mod 10 = 0 but found %d" (TupleI '[Id, Id `Mod` 10])) (Mod Id 10 >> Same 0)]) Source #
type BaseIJ (i :: Nat) (j :: Nat) = BaseIJ' i j True Source #
convert a string from the given base 'i' but stores it internally as a string of base 'j'
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
prtEval3P (Proxy @(BaseIJ 16 2)) ol "fe"
Right (Refined3 {r3In = "11111110", r3Out = "fe"})
>>>
prtEval3P (Proxy @(BaseIJ 16 2)) ol "fge"
Left Step 1. Initial Conversion(ip) Failed | invalid base 16
type BaseIJ' (i :: Nat) (j :: Nat) p = '(ReadBase Int i >> ShowBase j, p, ReadBase Int j >> ShowBase i, String) Source #
type ReadShow (t :: Type) = '(ReadP t, True, ShowP, String) Source #
take any valid Read/Show instance and turn it into a valid Refined3
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
:set -XTypeOperators
>>>
:m + Data.Ratio
>>>
prtEval3P (Proxy @(ReadShow Rational)) ol "13 % 3"
Right (Refined3 {r3In = 13 % 3, r3Out = "13 % 3"})
>>>
prtEval3P (Proxy @(ReadShow Rational)) ol "13x % 3"
Left Step 1. Initial Conversion(ip) Failed | ReadP Ratio Integer (13x % 3) failed
>>>
prtEval3P (Proxy @(ReadShow' Rational (Between (Pos 3) (Pos 5)))) ol "13 % 3"
Right (Refined3 {r3In = 13 % 3, r3Out = "13 % 3"})
>>>
prtEval3P (Proxy @(ReadShow' Rational (Between (NegR 11 2) (Neg 3)))) ol "-13 % 3"
Right (Refined3 {r3In = (-13) % 3, r3Out = "(-13) % 3"})
>>>
prtEval3P (Proxy @(ReadShow' Rational (Id > Pos 15))) ol "13 % 3"
Left Step 2. False Boolean Check(op) | FalseP
>>>
prtEval3P (Proxy @(ReadShow' Rational (Guard (Printf "invalid=%3.2f" (FromRational Double Id)) (Id > Pos 15) >> 'True))) ol "13 % 3"
Left Step 2. Failed Boolean Check(op) | invalid=4.33
>>>
prtEval3P (Proxy @(ReadShow' Rational (Id > Pos 11))) ol "13 % 3"
Left Step 2. False Boolean Check(op) | FalseP
>>>
let tmString = "2018-10-19 14:53:11.5121359 UTC"
>>>
let tm = read tmString :: UTCTime
>>>
prtEval3P (Proxy @(ReadShow UTCTime)) ol tmString
Right (Refined3 {r3In = 2018-10-19 14:53:11.5121359 UTC, r3Out = "2018-10-19 14:53:11.5121359 UTC"})
>>>
:m + Data.Aeson
>>>
prtEval3P (Proxy @(ReadShow Value)) ol "String \"jsonstring\""
Right (Refined3 {r3In = String "jsonstring", r3Out = "String \"jsonstring\""})
>>>
prtEval3P (Proxy @(ReadShow Value)) ol "Number 123.4"
Right (Refined3 {r3In = Number 123.4, r3Out = "Number 123.4"})