4

I have a type, say

Inductive Tt := a | b | c.

What's the easiest and/or best way to define a subtype of it? Suppose I want the subtype to contain only constructors a and b. A way would be to parametrize on a two-element type, e.g. bool:

Definition filt (x:bool): Tt := match x with
  | true => a
  | false => b end.
Check filt true: Tt.

This works but is very awkward if your expression has several (possibly interdependent) subtypes defined this way. Besides, it works only half way, as no subtype is defined. For this I must additionally define e.g.

Notation _Tt := ltac: (let T := type of (forall {x:bool}, filt x) in exact T).
Fail Check a: _Tt. (*The term "filt x" has type "Tt" which should be Set, Prop or Type.*)

which in this case also doesn't work. Another way would be to use type classes, e.g.

Class s_Tt: Type := s: Tt.
Instance a':s_Tt := a.
Instance b':s_Tt := b.
Check a: s_Tt.
Check b': Tt.
Check c: s_Tt.

As you see, this doesn't work: c is still in s_Tt (even if type inference should work better w/ instances). Finally, a coercion

Parameter c0:> bool -> Tt.
Notation a_ := true.
Notation b_ := false.
Notation Tt_ := bool.
Check a_: Tt_.
Check b_: Tt.
Fail Check a: Tt_.

works but, of course, a and b cannot be used as terms of the defined subtype (which would be always convenient and sometimes necessary)

I suppose subset types shouldn't be an answer to this question (a term of a subset type is never that of its (proper) superset). Maybe there's a better way of using type classes for this purpose?

jaam
  • 802
  • 4
  • 18
  • 5
    I think it's generally done with what is actually called subset types (cf. http://adam.chlipala.net/cpdt/html/Subset.html ). It's still unwieldy to use sometimes, but I'll let someone answer more thoroughly. Here you could have `{ x : Tt | match x with c => False | _ => True }` – Ptival Sep 02 '17 at 22:43
  • 4
    You are actually right, defining a subtype needs way more machinery beyond what sigma types provide. I recommend you use the ssreflect's `subType` machinery [documented in the math-comp book]. That, together with math-comp's theory of decidable predicate should help you a long way. – ejgallego Sep 03 '17 at 17:55

0 Answers0