How do I write a forall
constraint, for example for some type families F
and G
:
forall x y. G (F x y) ~ (x, y)
Is it possible using Edward A. Kmett Constraints
package? And if so could a small example be provided? I presume I need to use Forall
.
Note: Please be aware that the ForAll function comes with a few limitations: Certain functions can not be used within a ForAll function, the table ForAll is using can ot be modified and ForAll can not be delegated. (More details here) Syntax ForAll(Table, Formula)
The ForAll function evaluates a formula for all the records in a table. The formula can calculate a value and/or perform actions, such as modifying data or working with a connection. Use the With function to evaluate the formula for a single record. Use the Sequence function with the ForAll function to iterate based on a count.
ForAll is not a classic for loop Iike in JavaScript, C# or Java ForAll is a function with a return value like every function ForAll function is not executed sequentially and therefore it is prohibited to keep any kind of state What I mean by this will be explained in the article.
The FORALL statement runs one DML statement multiple times, with different values in the VALUES and WHERE clauses. The different values come from existing, populated collections or host arrays.
Yes, this is possible using constraints
. Be careful, however! The equality you claim is unlikely to hold with enough generality for constraints
if the type families are non-trivial. Consider, in particular, whether the type families successfully reduce when x
and y
are stuck type families
type family X where {}
type family Y where {}
Also, I see that your particular desired constraint has no free variables whatsoever. Hopefully, it's just an example; an actual closed constraint like that is unlikely to be useful.
The fundamental type family in Data.Constraint.Forall
is Forall
. This particular example might be handled a bit more conveniently using ForallT
, but it's most important to understand how to use Forall
.
Generally, Forall p
means forall x . p x
. That doesn't sound very general, but it actually is, if you build up your p
step by step. You seek
forall x y. G (F x y) ~ (x, y)
Start by defining a class expressing the relationship you seek.
class G (F x y) ~ (x, y) => C x y
instance G (F x y) ~ (x, y) => C x y
Now you can go step by step, defining
class Forall (C x) => D x
instance Forall (C x) => D x
(which you can read as D x = forall y . C x y
)
and then using Forall D
(i.e., forall x . D x
) to express your constraint. You'll need to use inst
to get the Dict (D x)
and again to get the Dict (C x y)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With