\begin{code}
module CN where
e : ( X : Set )-> Set
e X = (X -> X)
data G : Set where
zer : G
suc : G → G
{-# BUILTIN NATURAL G #-}
id : { X : Set}-> e X
id {X} x = x
const : { X : Set}-> {Y : Set}-> X -> Y -> X
const {X} {Y} x _ = x
_∘_ : {X : Set} {Y : Set} {Z : Set} -> (Y -> Z) -> (X -> Y) -> X -> Z
_∘_ {X} {Y} {Z} f g x = f (g x)
flip : {Z : Set}-> {B : Set} -> {A : Set}->
(A -> B -> Z) -> B -> A -> Z
flip f b a = f a b
cS : {X : Set}-> {Y : Set}-> {Z : Set}->
(X -> Y -> Z) -> (X -> Y) -> X -> Z
cS z y x = z x (y x)
_▷_ = cS
_◁_ : {X : Set}-> {Y : Set}-> {Z : Set}->
(X -> Y) -> (X -> Y -> Z) -> X -> Z
_◁_ {X} {Y} {Z} = flip {X -> Z} {X -> Y} {X -> Y -> Z} (cS {X} {Y} {Z})
zero_gen : {X : Set}-> {Y : Set}-> Y -> e X
zero_gen {X} {Y} = const id
CN : Set₁
CN = {X : Set}-> e (e X)
zero : CN
zero = zero_gen
one : CN
one = id
two : CN
two {X} f = f ∘ f
infixr 2 _+_
infixr 3 _*_
infixr 4 _^_
_+_ _*_ _^_ : CN -> CN -> CN
(a + b) s z = b s (a s z)
(a * b) s = b (a s)
(a ^ b) {X} = b {e X} (a {X})
O I : CN
O _ z = z
I s = s
sg sgbar : CN -> CN
sgbar b {X} s z = b {X} (const {X} {X} z) (s z)
sg b {X} s z = b {X} (const {X} {X} (s z)) z
pd : CN -> CN
pd b {X} f z = let it : e (e X)
it = b (λ m g _ → g (m f z)) (zero {X})
in it id z
pd' : {X : Set}-> e (e (e (e X))) -> e (e X)
pd' {X} n f z = n (λ m g _ → g (m f z)) (zero {X}) id z
cons : {X : Set} -> e (e X) -> e (e (e X)) -> e (e (e (e X))) -> e (e X)
cons {X} a f n s z
= let postponent : e (e X) -> X
postponent = (λ x -> x (a s z)) ∘ (λ x -> x id)
iteration : (n : e (e (e X)) → e (e (e X))) → e (e X)
iteration n = n (λ m g _ → g (f m s z)) zero
in postponent (iteration n)
module laws (a : CN) (b : CN) (c : CN)
(X : Set) (s : X -> X) (z : X)
(X' : X -> Set) where
infix 0 _~~_
_~~_ : CN -> CN -> Set
a ~~ b = X' (a s z) -> X' (b s z)
assoc+ : (a + b) + c ~~ a + (b + c)
assoc+ = id
+unit : a + O ~~ a
+unit = id
unit+ : O + a ~~ a
unit+ = id
assoc* : (a * b) * c ~~ a * (b * c)
assoc* = id
*unit : a * I ~~ a
*unit = id
unit* : I * a ~~ a
unit* = id
dist*+ : a * (b + c) ~~ (a * b) + (a * c)
dist*+ = id
dist*O : a * O ~~ O
dist*O = id
dist^+ : a ^ (b + c) ~~ (a ^ b) * (a ^ c)
dist^+ = id
dist^+O : a ^ O ~~ I
dist^+O = id
dist^B : a ^ (b * c) ~~ (a ^ b) ^ c
dist^B = id
dist^BI : a ^ I ~~ a
dist^BI = id
Set^ : Set -> Set₁
Set^ A = A -> Set
CNN : CN -> Set₁
CNN a
= {X : Set} -> {X' : Set^ X} ->
(s : X -> X) -> (s' : (x : X)-> X' x -> X' (s x))->
(z : X) -> (z' : X' z)->
X' (a {X} s z)
lemma+ : (a : CN)-> CNN a ->
(b : CN)-> CNN b -> CNN(a + b)
lemma+ a a' b b' s s' z z'
= b' s s' (a s z) (a' s s' z z')
lemmaO : CNN O
lemmaO _ _ _ z' = z'
lemma* : (a : CN)-> CNN a ->
(b : CN)-> CNN b -> CNN(a * b)
lemma* a a' b b' s s'
= b' (a s) (a' s s')
lemmaI : CNN I
lemmaI _ s' = s'
lemma^ : (a : CN)-> (a' : CNN a)->
(b : CN)-> (b' : CNN b)-> CNN(a ^ b)
lemma^ a a' b b' {X} {X'}
= b' {e X} {Cl} (a {X}) aX'
where Cl : Set^ (X → X)
Cl f = (x : X) → X' x → X' (f x)
aX' : (f : X → X) → Cl f → Cl (a {X} f)
aX' = a' {X} {X'}
data Peano (X : Set) : Set where
S : X -> Peano X
Z : Peano X
Alg-fun : Set -> Set
Alg-fun X = Peano X -> X
Alg-fun-mk : {X : Set}-> (X -> X) -> X -> Alg-fun X
Alg-fun-mk _ z Z = z
Alg-fun-mk s _ (S x) = s x
Alg-fun-s : {X : Set} -> Alg-fun X -> X -> X
Alg-fun-s alg x = alg (S x)
Alg-fun-z : {X : Set} -> Alg-fun X -> X
Alg-fun-z alg = alg Z
data Alg-data (X : Set) : Set where
mkAlg-data : (X -> X) -> X -> Alg-data X
Alg-data-s : {X : Set} -> Alg-data X -> X -> X
Alg-data-s (mkAlg-data s _) = s
Alg-data-z : {X : Set} -> Alg-data X -> X
Alg-data-z (mkAlg-data _ z) = z
record Alg (X : Set) : Set where
constructor mk
field
s : X -> X
z : X
open Alg public
record Prog (X : Set) (a : Alg X) (X' : Set^ X) : Set where
constructor mkProg
field
ss : (x : X)-> X' x -> X' (a .s x)
zz : X' (a .z)
CN# : Set₁
CN# = {X : Set}-> Alg X -> X
CNN# : CN# -> Set₁
CNN# a
= {X : Set} ->
(alg : Alg X) ->
{X' : Set^ X} ->
((x : X)-> X' x -> X' (alg .s x)) ->
X' (alg .z) ->
X' (a {X} alg)
record Lens : Set₁ where
constructor Lens_
field
L : Set -> Set
U : (X : Set)-> Alg X -> Alg (L X)
D : (X : Set)-> Alg X -> L X -> X
[_] : Lens -> CN -> CN
[ l ] a {X} s z
= let alg : Alg X
alg = mk s z
X' : Set
X' = l .Lens.L X
alg' : Alg X'
alg' = l .Lens.U X alg
s' : X' -> X'
s' = alg' .Alg.s
z' : X'
z' = alg' .Alg.z
d : X' -> X
d = l .Lens.D X alg
in
d (a s' z')
[_]# : Lens -> CN# -> CN#
[ l ]# a {X} alg
= let X' : Set
X' = Lens.L l X
alg' : Alg X'
alg' = Lens.U l X alg
in
Lens.D l X alg (a {X'} alg')
2^lens : Lens
2^lens = Lens_ e
(λ X a → mk two (a .s) )
(λ X a f → f (a .z) )
2*lens : Lens
2*lens = Lens_ (λ X → X)
(λ X a → mk (two (a .s)) (a .z) )
(λ X _ x → x )
2+lens : Lens
2+lens = Lens_ (λ X → X)
(λ X a → mk (a .s) (two (a .s) (a .z)))
(λ X _ x → x )
0^lens : Lens
0^lens = Lens_ e
(λ X a → mk zero (a .s))
(λ X a f → f (a .z))
sglens : Lens
sglens = Lens_ (λ X → X)
(λ X a → mk (const (a .z)) ((a .s) (a .z)))
(λ X a → id)
_^lens : CN -> Lens
b ^lens = Lens_ e
(λ X a → mk b (a .s))
(λ X a f → f (a .z))
_*lens : CN -> Lens
b *lens = Lens_ (λ X → X)
(λ X a → mk (b (a .s)) (a .z))
(λ X _ x → x)
_+lens : CN -> Lens
b +lens = Lens_ (λ X → X)
(λ X a → mk (a .s) (b (a .s) (a .z)))
(λ X _ x → x)
_^'_ _*'_ _+'_ : CN -> CN -> CN
_^'_ a = [ a ^lens ]
_*'_ a = [ a *lens ]
_+'_ a = [ a +lens ]
lemma+' : (a : CN)-> CNN a ->
(b : CN)-> CNN b -> CNN(a +' b)
lemma+' a a' b b' {X} {X'} s s' z z'
= b' {X} {X'} s s' (a {X} s z) (a' {X} {X'} s s' z z')
lemma*' : (a : CN)-> CNN a ->
(b : CN)-> CNN b -> CNN(a *' b)
lemma*' a a' b b' {X} {X'} s s'
= b' {X} {X'} (a {X} s) (a' {X} {X'} s s')
lemma^' : (a : CN)-> CNN a ->
(b : CN)-> CNN b -> CNN(a ^' b)
lemma^' a a' b b' {X} {X'}
= b' {e X} {Cl} (a {X}) aX'
where Cl : Set^ (X → X)
Cl f = (x : X) → X' x → X' (f x)
aX' : (f : X → X) → Cl f → Cl (a {X} f)
aX' = a' {X} {X'}
module Lemma:coincidences (a : CN) (b : CN) (X : Set)
(s : X -> X) (z : X) (X' : X -> Set) where
infix 1 _≅_
_≅_ : CN -> CN -> Set
a ≅ b = X' (a s z) -> X' (b s z)
wonder+ : a + b ≅ a +' b
wonder+ x = x
wonder* : a * b ≅ a *' b
wonder* x = x
wonder^ : a ^ b ≅ a ^' b
wonder^ x = x
comp : Lens -> Lens -> Lens
comp φ ψ = let F : Set -> Set
F X = Lens.L ψ (Lens.L φ X)
U : (X : Set)-> Alg X -> Alg (F X)
U X alg = Lens.U ψ (Lens.L φ X) (Lens.U φ X alg)
D : (X : Set)-> Alg X -> F X -> X
D X alg fx = Lens.D φ X alg
(Lens.D ψ (Lens.L φ X) (Lens.U φ X alg) fx)
in Lens_ F U D
Theorem:LensComposition : (φ : Lens)-> (ψ : Lens)-> (a : CN)->
(X : Set)-> (s : X -> X)-> (z : X) -> (X' : Set^ X)->
X' ([ comp φ ψ ] a {X} s z) -> X' ([ φ ] ([ ψ ] a) {X} s z)
Theorem:LensComposition φ ψ a X s z X' x' = x'
conjecture# : (φ : Lens)-> (ψ : Lens)-> (a : CN#)->
(X : Set)-> (alg : Alg X)-> (X' : Set^ X)->
X' ([ comp φ ψ ]# a {X} alg) -> X' ([ φ ]# ([ ψ ]# a) {X} alg)
conjecture# φ ψ a X alg X' x' = x'
^lens# *lens# +lens# : CN# -> Lens
^lens# a = Lens_ (λ X → X → X)
(λ X alg → mk
(λ f x → a {X} (mk f x))
(alg .s))
(λ X alg f → f (alg .z) )
*lens# a = Lens_ (λ X → X)
(λ X alg → mk
(λ x → a {X} (mk (alg .s) x) )
(alg .z) )
(λ X alg x → x)
+lens# a = Lens_ (λ X → X)
(λ X alg → mk
(alg .s)
(a {X} alg) )
(λ X alg x → x)
_^#_ _*#_ _+#_ : CN# -> CN# -> CN#
_^#_ a = [ ^lens# a ]#
_*#_ a = [ *lens# a ]#
_+#_ a = [ +lens# a ]#
lemma^# : (a : CN#)-> CNN# a ->
(b : CN#)-> CNN# b -> CNN# (a ^# b)
lemma^# a a' b b' {X} alg {X'} sp
= let aiter : (X -> X) -> X -> X
aiter f x = a {X} (mk f x)
alg' : Alg (X → X)
alg' = mk aiter (alg .s)
Cl : Set^ (X -> X)
Cl f = ( x : X )-> X' x -> X' (f x)
fact : (f : X -> X)-> Cl f -> Cl (aiter f)
fact f clf x = a' {X} (mk f x) {X'} clf
in b' {X -> X} alg' {Cl} fact sp (alg .z)
lemma*# : (a : CN#)-> CNN# a ->
(b : CN#)-> CNN# b -> CNN# (a *# b)
lemma*# a a' b b' {X} alg {X'} sp
= let pa : X -> X
pa x = a {X} (mk (alg .s) x)
alg' : Alg X
alg' = mk pa (alg .z)
fact : (x : X) → X' x → X' (pa x)
fact x = a' {X} (mk (alg .s) x) {X'} sp
in b' {X} alg' {X'} fact
lemma*+ : (a : CN#)-> CNN# a ->
(b : CN#)-> CNN# b -> CNN# (a +# b)
lemma*+ a a' b b' {X} alg {X'} sp zp
= let alg' : Alg X
alg' = Lens.U (+lens# a) X alg
in b' {X} alg' {X'} sp (a' {X} alg {X'} sp zp)
exp2 : CN -> CN
exp2 n {X} = n {e X} (two {X})
t tt ttt tttt : CN
t = two
tt {X} = t {e X} (t {X})
ttt {X} = (tt {e X}) (t {X})
tttt {X} = (ttt {e X}) (t {X})
mutual
data U {X : Set} : Set where
g : U {X}
e' : U {X} -> U {X}
T : {X : Set} -> U {X} -> Set
T {X} g = X
T (e' u) = e (T u)
mutual
data N : Set where
ze : N
su : N -> N
H : N -> Set
H ze = G
H (su n) = e (H n)
scn : Set
scn = (n : N)-> H (su (su n))
squidge : scn -> scn
squidge t = λ n → t (su n) two
stwo : scn
stwo = λ n → two { H n }
tetr : (n : CN) -> e (e G)
tetr n = let f : (n : CN)-> (n₁ : N) → e (e (H n₁))
f n = n {scn} squidge stwo
in f n ze
test : CN -> G
test n = tetr n suc zer
at : CN -> CN -> CN
at a b {X} =
let ax : e (e X)
ax = a {X}
bx : e (e (e X))
bx = b {e X}
in bx ax