Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forall constraints

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.

like image 645
Clinton Avatar asked Aug 14 '16 19:08

Clinton


People also ask

What are the limitations of the forall function?

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)

How do you use the forall function in SQL?

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.

What is forall in JavaScript?

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.

How many times does the forall statement run in DML?

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.


1 Answers

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).

like image 159
dfeuer Avatar answered Sep 17 '22 12:09

dfeuer