Skip to content

Commit

Permalink
🧩 [consolidate]: typeError helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
rolyp committed Oct 7, 2023
1 parent fcbda5a commit 7957397
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/Primitive.purs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ type ToFrom d a =
, unpack :: Val a -> d × a
}

typeError :: forall a b. Val a -> String -> b
typeError v typeName = error (typeName <> " expected; got " <> prettyP (erase v))

int :: forall a. ToFrom Int a
int =
{ pack: \(n × α) -> Int α n
, unpack
}
where
unpack (Int α n) = n × α
unpack v = error ("Int expected; got " <> prettyP (erase v))
unpack v = typeError v "Int"

number :: forall a. ToFrom Number a
number =
Expand All @@ -41,7 +44,7 @@ number =
}
where
unpack (Float α n) = n × α
unpack v = error ("Float expected; got " <> prettyP (erase v))
unpack v = typeError v "Float"

string :: forall a. ToFrom String a
string =
Expand All @@ -50,7 +53,7 @@ string =
}
where
unpack (Str α str) = str × α
unpack v = error ("Str expected; got " <> prettyP (erase v))
unpack v = typeError v "Str"

intOrNumber :: forall a. ToFrom (Int + Number) a
intOrNumber =
Expand All @@ -62,7 +65,7 @@ intOrNumber =
where
unpack (Int α n) = Left n × α
unpack (Float α n) = Right n × α
unpack v = error ("Int or Float expected; got " <> prettyP (erase v))
unpack v = typeError v "Int or Float"

intOrNumberOrString :: forall a. ToFrom (Int + Number + String) a
intOrNumberOrString =
Expand All @@ -76,7 +79,7 @@ intOrNumberOrString =
unpack (Int α n) = Left n × α
unpack (Float α n) = Right (Left n) × α
unpack (Str α str) = Right (Right str) × α
unpack v = error ("Int, Float or Str expected; got " <> prettyP (erase v))
unpack v = typeError v "Int, Float or Str"

intPair :: forall a. ToFrom ((Int × a) × (Int × a)) a
intPair =
Expand All @@ -85,7 +88,7 @@ intPair =
}
where
unpack (Constr α c (v : v' : Nil)) | c == cPair = (int.unpack v × int.unpack v') × α
unpack v = error ("Pair expected; got " <> prettyP (erase v))
unpack v = typeError v "Pair"

matrixRep :: forall a. Ann a => ToFrom (MatrixRep a) a
matrixRep =
Expand All @@ -94,7 +97,7 @@ matrixRep =
}
where
unpack (Matrix α m) = m × α
unpack v = error ("Matrix expected; got " <> prettyP v)
unpack v = typeError v "Matrix"

record :: forall a. Ann a => ToFrom (Dict (Val a)) a
record =
Expand All @@ -103,7 +106,7 @@ record =
}
where
unpack (Record α xvs) = xvs × α
unpack v = error ("Record expected; got " <> prettyP v)
unpack v = typeError v "Record"

boolean :: forall a. ToFrom Boolean a
boolean =
Expand All @@ -116,7 +119,7 @@ boolean =
unpack (Constr α c Nil)
| c == cTrue = true × α
| c == cFalse = false × α
unpack v = error ("Boolean expected; got " <> prettyP (erase v))
unpack v = typeError v "Boolean"

class IsZero a where
isZero :: a -> Boolean
Expand Down

0 comments on commit 7957397

Please sign in to comment.