1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! # Avatar Modal Logic
//!
//! This avatar modal logic builds upon the `hooo` module using Exponential Propositions.
//!
//! What makes Avatar Modal Logic different from normal Modal Logic,
//! is that there is a difference between "safe" and "unsafe" proofs:
//!
//! - `false^(false^p) => ◇p` is safe (classical in normal Modal Logic)
//! - `◇p => false^(false^p)` is unsafe (constructive in normal Modal Logic)
//!
//! The idea is to reverse decidability, but this reversed decidability should not be mixed
//! with the normal distinction between classical and constructive proofs.
//! Instead, one uses safe vs unsafe proofs.
//!
//! ### 1-Avatar and Unsafe code
//!
//! Without distinction of safe vs unsafe,
//! the usual semantics of "necessary" and "possibly" is collapsed,
//! where is it possible to prove `¬◇p == ◇¬p` and `¬□p == □¬p`.
//!
//! According [Avatar Semantics](https://advancedresearch.github.io/avatar-extensions/summary.html#avatar-semantics),
//! this is not a problem, because one can reconstruct hypercube topologies in Avatar Graphs
//! from the smallest Möbius topologies possible, by identifying the diagonals using
//! highest N-avatars. This is done by introducing a 1-avatar that covers products in
//! Avatar Algebra.
//!
//! This 1-avatar is a new-type `Pos` (possibly) that protects the content from being readable.
//! The content is accessed under special circumstances where integration of information
//! is checked by a higher N-avatar. This means, safe invariants using unsafe code.
//!
//! By constructing the semantics of "necessary" and "possibly" on top of this 1-avatar,
//! one can still prove theorems using HOOO Exponential Propositions safely.
//!
//! Safe proofs are preferrable, but one can use unsafe code in edge cases by being careful.

use crate::*;
use hooo::*;

pub use protection::*;

mod protection {
    use super::*;

    /// `◇a`.
    #[derive(Clone)]
    pub struct Pos<A>(Para<Para<A>>);
    /// `□a`.
    pub type Nec<A> = Not<Pos<Not<A>>>;

    impl<A> Pos<A> {
        /// Create a new possibly proposition.
        pub fn new(x: Para<Para<A>>) -> Self {Pos(x)}
    }

    /// `◇a => false^(false^a)`.
    ///
    /// # Safety
    ///
    /// This function is declared unsafe because it uses the "taboo" knowledge in Avatar Modal Logic.
    pub unsafe fn pos_to_para_para<A: Prop>(Pos(x): Pos<A>) -> Para<Para<A>> {x}
}

impl<A: DProp> Decidable for Pos<A> {
    fn decide() -> ExcM<Pos<A>> {
        match para_decide() {
            Left(x) => Left(Pos::new(x)),
            Right(y) => Right(imply::in_left(y, |x| unsafe {pos_to_para_para(x)})),
        }
    }
}

/// `□a => a^true`.
pub fn nec_to_tauto<A: DProp>(nec_a: Nec<A>) -> Tauto<A> {
    match tauto_decide() {
        Left(tauto_a) => tauto_a,
        Right(ntauto_a) => {
            let para_a = tauto_not_to_para(hooo_rev_not(ntauto_a));
            let x: Para<Not<A>> = npos_to_para(nec_a);
            imply::absurd()(para_rev_not(x)(para_a))
        }
    }
}

/// `a^true => □a`.
///
/// # Safety
///
/// This function is declared unsafe because it uses the "taboo" knowledge in Avatar Modal Logic.
pub unsafe fn tauto_to_nec<A: DProp>(tauto_a: Tauto<A>) -> Nec<A> {
    Rc::new(move |pos_na| {
        match para_decide() {
            Left(para_na) => pos_to_para_para(pos_na)(para_na),
            Right(npara_na) => {
                let para_a = para_not_rev_double(pow_not(npara_na));
                para_a(tauto_a(True))
            }
        }
    })
}

/// `¬◇a => false^a`.
pub fn npos_to_para<A: DProp>(npos: Not<Pos<A>>) -> Para<A> {
    match para_decide() {
        Left(para_a) => para_a,
        Right(npara_a) => imply::absurd()(pos_not(npos)(npara_a)),
    }
}

/// `false^a => ¬◇a`.
///
/// # Safety
///
/// This function is declared unsafe because it uses the "taboo" knowledge in Avatar Modal Logic.
pub unsafe fn para_to_npos<A: Prop>(para_a: Para<A>) -> Not<Pos<A>> {
    Rc::new(move |pos_a| pos_to_para_para(pos_a)(para_a))
}

/// `¬¬a => ◇a`.
pub fn not_not_to_pos<A: DProp>(nna: Not<Not<A>>) -> Pos<A> {
    Pos::new(hooo::not_not_to_para_para(nna))
}

/// `◇a => ¬¬a`.
///
/// # Safety
///
/// This function is declared unsafe because it uses the "taboo" knowledge in Avatar Modal Logic.
pub unsafe fn pos_to_not_not<A: DProp>(pos: Pos<A>) -> Not<Not<A>> {
    hooo::para_para_to_not_not(pos_to_para_para(pos))
}

/// `¬□¬a <=> ◇a`.
pub fn eq_nnecn_pos<A: DProp>() -> Eq<Not<Nec<Not<A>>>, Pos<A>> {
    fn f<A: DProp>(_: True) -> Eq<Not<Para<A>>, Not<Para<Not<Not<A>>>>> {
        (
            Rc::new(move |x| para_rev_not(para_not_triple(pow_not(x)))),
            Rc::new(move |x| para_rev_not(para_not_rev_triple(pow_not(x))))
        )
    }
    fn g<A: DProp>(_: True) -> Eq<Not<Para<A>>, Not<Para<Not<Not<A>>>>> {
        (
            Rc::new(move |x| para_rev_not(para_not_triple(pow_not(x)))),
            Rc::new(move |x| para_rev_not(para_not_rev_double(pow_not(x))))
        )
    }
    (
        Rc::new(move |nnec_na| {
            match Pos::<A>::decide() {
                Left(pos_a) => pos_a,
                Right(npos_a) => {
                    let x = pos_not(npos_a);
                    let x = para_in_arg(x, f);
                    let x = para_rev_not(x);
                    let x = imply::in_left(x, |y| unsafe {pos_to_para_para(y)});
                    imply::absurd()(nnec_na(x))
                }
            }
        }),
        Rc::new(move |x| {
            let x = unsafe {pos_to_para_para(x)};
            let x = not::double(x);
            let x = imply::in_left(x, |y| para_rev_not(y));
            let x = imply::in_left(x, |y| para_in_arg(y, tauto_eq_symmetry(g)));
            let x: Not<Not<Para<Para<Not<Not<A>>>>>> = imply::in_left(x, pow_not);
            nnpara_para_to_nnpos(x)
        })
    )
}

/// `¬◇¬a <=> □a`.
pub fn eq_nposn_nec<A: Prop>() -> Eq<Not<Pos<Not<A>>>, Nec<A>> {
    eq::refl()
}

/// `¬□a == ◇¬a`.
pub fn eq_nnec_posn<A: DProp>() -> Eq<Not<Nec<A>>, Pos<Not<A>>> {
    fn g<A: DProp>(_: True) -> Eq<Not<Not<Para<Not<A>>>>, Para<Not<A>>> {
        fn f<A: DProp>(_: True) -> Eq<Not<Not<Not<A>>>, Not<A>> {
            (Rc::new(move |x| not::rev_triple(x)), Rc::new(move |x| not::double(x)))
        }
        (
            Rc::new(move |x| {
                let x = imply::in_left(x, |y| para_rev_not(y));
                para_in_arg(pow_not(x), f)
            }),
            Rc::new(move |x| {
                let x = para_in_arg(x, tauto_eq_symmetry(f));
                let x: Not<Para<Not<Not<A>>>> = para_rev_not(x);
                imply::in_left(x, pow_not)
            })
        )
    }
    (
        Rc::new(move |nnec_a: Not<Not<Pos<Not<A>>>>| {
            let x = unsafe {nnpos_to_nnpara_para(nnec_a)};
            let x = imply::in_left(x, |y| para_rev_not(y));
            let x = pow_not(x);
            Pos::new(para_in_arg(x, g))
        }),
        Rc::new(move |pos_na| {
            let x = unsafe {pos_to_para_para(pos_na)};
            let x: Para<Not<Not<Para<Not<A>>>>> = para_in_arg(x, tauto_eq_symmetry(g));
            let x = para_rev_not(x);
            let x: Not<Not<Para<Para<Not<A>>>>> = imply::in_left(x, pow_not);
            imply::in_left(x, |y| imply::in_left(y, Pos::new))
        })
    )
}

/// `¬◇a == □¬a`.
pub fn eq_posn_nnec<A: DProp>() -> Eq<Not<Pos<A>>, Nec<Not<A>>> {
    fn f<A: DProp>(_: True) -> Eq<Not<Para<A>>, Not<Para<Not<Not<A>>>>> {
        fn g<A: Prop>(_: True) -> Eq<Not<Not<Not<A>>>, Not<A>> {
            (Rc::new(move |x| not::rev_triple(x)), Rc::new(move |x| not::double(x)))
        }
        (
            Rc::new(move |x| {
                let x = pow_not(x);
                let x = para_in_arg(x, tauto_eq_symmetry(g));
                para_rev_not(x)
            }),
            Rc::new(move |x| {
                let x = pow_not(x);
                let x = para_in_arg(x, g);
                para_rev_not(x)
            })
        )
    }
    (
        Rc::new(move |x| {
            let x = pos_not(x);
            let x = para_in_arg(x, f);
            imply::in_left(para_rev_not(x), |y| unsafe {pos_to_para_para(y)})
        }),
        Rc::new(move |x| {
            let x = pos_not(x);
            let x = para_in_arg(x, tauto_eq_symmetry(f));
            imply::in_left(para_rev_not(x), |y| unsafe {pos_to_para_para(y)})
        })
    )
}

/// `¬¬◇a => ¬¬(false^(false^a))`.
///
/// # Safety
///
/// This function is declared unsafe because it uses the "taboo" knowledge of Avatar Modal Logic.
pub unsafe fn nnpos_to_nnpara_para<A: Prop>(x: Not<Not<Pos<A>>>) -> Not<Not<Para<Para<A>>>> {
    imply::in_left(x, |y| imply::in_left(y, |x| pos_to_para_para(x)))
}

/// `¬¬◇a => ¬¬(false^(false^a))`.
pub fn nnpara_para_to_nnpos<A: Prop>(x: Not<Not<Para<Para<A>>>>) -> Not<Not<Pos<A>>> {
    imply::in_left(x, |y| imply::in_left(y, |x| Pos::new(x)))
}

/// `(false^(false^a) => ◇a)^true) => (false^(false^a) == ◇a)^true)`.
pub fn to_pos_tauto_eq<A: Prop>(
    y: Tauto<Imply<Para<Para<A>>, Pos<A>>>
) -> Tauto<Eq<Para<Para<A>>, Pos<A>>> {
    fn f<A: Prop>(_: True) -> Imply<Pos<A>, Para<Para<A>>> {
        Rc::new(move |pos_a| unsafe {pos_to_para_para(pos_a)})
    }
    hooo_rev_and((y, f::<A>))
}

/// `¬◇a => false^(¬(false^a))`.
pub fn pos_not<A: DProp>(x: Not<Pos<A>>) -> Para<Not<Para<A>>> {
    pow_not(imply::in_left(x, |y| Pos::new(y)))
}