0

I know I am missing something obvious, but I am lost in how to make this one work. I am trying to write the set comprehension for a kind of "Cartesian product". Given a finite set S of finite sets, I would like to build a sets of sets of pairs, where each pair is given by one of the sets in S and one value v in that set; and each set has exactly one pair for each of set sets in S.

Basically, given the (simplistic) example

definition s1 :: "nat set" where 
  "s1 = {1, 2}"
definition s2 :: "nat set" where
  "s2 = {4, 5}"
definition S :: "nat set set" where 
  "S = {s1, s2}"

value "{{(s, v) | s. s ∈ S} | v. v ∈ s}"

I would like the evaluation to become {{({1, 2}, 1), ({4, 5}, 4)}, {({1, 2}, 1), ({4, 5}, 5)}, {({1, 2}, 2), ({4, 5}, 4)}, {({1, 2}, 2), ({4, 5}, 5)}}

Instead, it does evaluate to (what it is supposed to, I realize that)

"(λu. {({1, 2}, u), ({4, 5}, u)}) ` s"
  :: "(nat set × 'a) set set"

The u variable is bound to a free variable "s", I don't know how to have different "u"s and bound each u to its "associated"

s ∈ S 

Thank you for any points or directions.

Alicia M.
  • 7
  • 3

1 Answers1

0

The simple way is to avoid the problem and express everything as image over the set:

value "(λv. (Pair v) ` S) ` S"

In general, there are two ways to do this kind of things: make the code generator work or the better way, namely adapt the definition to make them easier to fit the evaluation model.

Mathias Fleury
  • 2,221
  • 5
  • 12
  • Hi @Mathias, thank you... however, what I am trying is slightly different: the pairs have first component one of the sets, and 2nd component a value of that set. Each pair is of the form (s, v) - so I need whatever can give me a 4-element set: { {(s1, 1), (s2, 4)}, {(s1, 1), (s2, 5)}, ...}. The example provided returns { {(s1, s1), (s1, s2)}, {(s2, s1), (s2, s2)} } – Alicia M. Nov 15 '22 at 20:41
  • You want that `(λ(a,c,d). {(a)} ∪ {(c,d)}) \` ((λw. (((Pair s1 w)))) \` s1 × (λw. (s2, w)) \` s2)`? – Mathias Fleury Nov 15 '22 at 21:18
  • If yes: the reasoning behind it was: 1) lets obtain the pair separately `(s_i, k)` for k in s_i; 2) combine them to pairs that can be processed further; 3) map the pairs to the correct type. – Mathias Fleury Nov 15 '22 at 21:20
  • I also tried with `(λ(w,v). ((({w} × w), {v} × v))) ` (S× S)` but this gave too many pairs. That is when I realized that working on S instead of s1 and s2 was not a good idea. – Mathias Fleury Nov 15 '22 at 21:22
  • Yes! the a, c, d one... however... in a more generic form! it has to go over the s_i in S... Let me work on the logic of that; I already have an "unfold s" that will obtain a set of all (s_i, k) pairs, with k in s_i... I need to work on the generalization after that. Thank you, it gives me an idea where to look, for now. Much appreciated! – Alicia M. Nov 15 '22 at 21:53
  • LE: I am slowly making some progress. Here is where I stand right now, and while I understand what is happening ([link](https://stackoverflow.com/questions/69785317/evaluate-complex-set-comprehension-expression)), I can't figure the exact _why_. Assuming same `s1, s2` as above I have defined two helper functions `fun f1 :: "'a set ⇒ ('a set × 'a) set set" where "f1 s = (λa. {(a)}) ` ((λx. (Pair s x)) ` s)"` which evaluates `f1 s1` fine. – Alicia M. Nov 17 '22 at 15:53
  • The 2nd one `fun f2 :: "'a set set ⇒ ('a set × 'a) set set" where "f2 U = (if is_singleton U then (f1 (the_elem U)) else {})"` throws the famous "Wellsortedness error"... what am I doing wrong? @Mathias – Alicia M. Nov 17 '22 at 15:55
  • To explain the process: I got the same error. So I tried `value "singleton S"`. This failed with the same error... So I checked the definition and it is not executable. You need to add `lemmas [code] = is_singleton_altdef`: this replaces the bad definition with the definition `card S = 1`which is executable. – Mathias Fleury Nov 18 '22 at 12:07
  • Mathias, fantastic (sorry, it won't let me tag)! thank you, that moved it further on (I simply replaced the "is_singleton U" with "card U = 1", easier that way) – Alicia M. Nov 22 '22 at 22:01