Tom de Jong, 3 - 5 March 2020

As suggested by Martin Escardo.

Dyadic rationals (https://en.wikipedia.org/wiki/Dyadic_rational)

\begin{code}

{-# OPTIONS --safe --without-K #-}

module DyadicsInductive.Dyadics where

open import MLTT.Spartan
open import MLTT.Unit-Properties
open import UF.DiscreteAndSeparated
open import UF.Sets

\end{code}

We inductively construct dyadic rationals in (-1,1), as follows.
Start with the point 0 in the middle (represented by middle below).
Define two functions (represented by left and right below):

l : (-1,1) โ†’ (-1,1)
l x = (x-1)/2

r : (-1,1) โ†’ (-1,1)
r x = (x+1)/2

Some values (ordered) to give an impression:

                   0                  -- just 0
        -1/2                1/2       -- l 0 = -1/2 and r 0 = 1/2
   -3/4      -1/4      1/4       3/4  -- l (l 0), l (r 0), r (l 0) and r (r 0)

In this module we just define the type and prove that it has decidable
equality. The order on ๐”ป is defined in the separate module Dyadic-Order.

\begin{code}

data ๐”ป : ๐“คโ‚€ ฬ‡ where
  middle : ๐”ป
  left   : ๐”ป โ†’ ๐”ป
  right  : ๐”ป โ†’ ๐”ป

\end{code}

Using the well-known encode-decode method (cf. Section 2.13 of the HoTT book),
we can show that right and left are injective and that ๐”ป is discrete (i.e. it
has decidable equality).

By Hedberg's Theorem, ๐”ป is a set.

\begin{code}

middle-is-not-left : {x : ๐”ป} โ†’ middle โ‰  left x
middle-is-not-left p = ๐Ÿ™-is-not-๐Ÿ˜ (ap f p)
 where
  f : ๐”ป โ†’ ๐“คโ‚€ ฬ‡
  f middle    = ๐Ÿ™
  f (left _)  = ๐Ÿ˜
  f (right _) = ๐Ÿ˜

middle-is-not-right : {x : ๐”ป} โ†’ middle โ‰  right x
middle-is-not-right p = ๐Ÿ™-is-not-๐Ÿ˜ (ap f p)
 where
  f : ๐”ป โ†’ ๐“คโ‚€ ฬ‡
  f middle    = ๐Ÿ™
  f (left _)  = ๐Ÿ˜
  f (right _) = ๐Ÿ˜

left-is-not-right : {x y : ๐”ป} โ†’ left x โ‰  right y
left-is-not-right p = ๐Ÿ™-is-not-๐Ÿ˜ (ap f p)
 where
  f : ๐”ป โ†’ ๐“คโ‚€ ฬ‡
  f middle    = ๐Ÿ™
  f (left x)  = ๐Ÿ™
  f (right x) = ๐Ÿ˜

left-lc : {x y : ๐”ป} โ†’ left x ๏ผ left y โ†’ x ๏ผ y
left-lc = ap f
 where
  f : ๐”ป โ†’ ๐”ป
  f middle    = middle
  f (left x)  = x
  f (right x) = right x

right-lc : {x y : ๐”ป} โ†’ right x ๏ผ right y โ†’ x ๏ผ y
right-lc = ap f
 where
  f : ๐”ป โ†’ ๐”ป
  f middle    = middle
  f (left x)  = left x
  f (right x) = x

๐”ป-is-discrete : is-discrete ๐”ป
๐”ป-is-discrete middle    middle    = inl refl
๐”ป-is-discrete middle    (left y)  = inr middle-is-not-left
๐”ป-is-discrete middle    (right y) = inr middle-is-not-right
๐”ป-is-discrete (left x)  middle    = inr (ฮป p โ†’ middle-is-not-left (p โปยน))
๐”ป-is-discrete (left x)  (left y)  = cases a b (๐”ป-is-discrete x y)
 where
  a : x ๏ผ y โ†’ is-decidable (left x ๏ผ left y)
  a = inl โˆ˜ ap left
  b : ยฌ (x ๏ผ y) โ†’ is-decidable (left x ๏ผ left y)
  b = inr โˆ˜ contrapositive left-lc
๐”ป-is-discrete (left x)  (right y) = inr left-is-not-right
๐”ป-is-discrete (right x) middle    = inr (ฮป p โ†’ middle-is-not-right (p โปยน))
๐”ป-is-discrete (right x) (left y)  = inr (ฮป p โ†’ left-is-not-right (p โปยน))
๐”ป-is-discrete (right x) (right y) = cases a b (๐”ป-is-discrete x y)
 where
  a : x ๏ผ y โ†’ is-decidable (right x ๏ผ right y)
  a = inl โˆ˜ ap right
  b : ยฌ (x ๏ผ y) โ†’ is-decidable (right x ๏ผ right y)
  b = inr โˆ˜ contrapositive right-lc

๐”ป-is-set : is-set ๐”ป
๐”ป-is-set = discrete-types-are-sets ๐”ป-is-discrete

\end{code}