2022-02-09 23:14:29 +00:00
|
|
|
|
module Luau.Type where
|
|
|
|
|
|
2022-03-02 22:02:51 +00:00
|
|
|
|
open import FFI.Data.Maybe using (Maybe; just; nothing; just-inv)
|
|
|
|
|
open import Agda.Builtin.Equality using (_≡_; refl)
|
|
|
|
|
open import Properties.Dec using (Dec; yes; no)
|
|
|
|
|
open import Properties.Equality using (cong)
|
2022-02-11 20:38:35 +00:00
|
|
|
|
open import FFI.Data.Maybe using (Maybe; just; nothing)
|
|
|
|
|
|
2022-02-09 23:14:29 +00:00
|
|
|
|
data Type : Set where
|
|
|
|
|
nil : Type
|
|
|
|
|
_⇒_ : Type → Type → Type
|
2022-04-09 06:07:08 +01:00
|
|
|
|
never : Type
|
|
|
|
|
unknown : Type
|
2022-03-02 22:02:51 +00:00
|
|
|
|
boolean : Type
|
2022-02-18 19:09:00 +00:00
|
|
|
|
number : Type
|
2022-03-02 23:26:58 +00:00
|
|
|
|
string : Type
|
2022-02-09 23:14:29 +00:00
|
|
|
|
_∪_ : Type → Type → Type
|
|
|
|
|
_∩_ : Type → Type → Type
|
|
|
|
|
|
2022-03-23 20:02:57 +00:00
|
|
|
|
data Scalar : Type → Set where
|
|
|
|
|
|
|
|
|
|
number : Scalar number
|
|
|
|
|
boolean : Scalar boolean
|
|
|
|
|
string : Scalar string
|
|
|
|
|
nil : Scalar nil
|
|
|
|
|
|
2022-03-02 22:02:51 +00:00
|
|
|
|
lhs : Type → Type
|
|
|
|
|
lhs (T ⇒ _) = T
|
|
|
|
|
lhs (T ∪ _) = T
|
|
|
|
|
lhs (T ∩ _) = T
|
|
|
|
|
lhs nil = nil
|
2022-04-09 06:07:08 +01:00
|
|
|
|
lhs never = never
|
|
|
|
|
lhs unknown = unknown
|
2022-03-02 22:02:51 +00:00
|
|
|
|
lhs number = number
|
|
|
|
|
lhs boolean = boolean
|
2022-03-02 23:26:58 +00:00
|
|
|
|
lhs string = string
|
2022-03-02 22:02:51 +00:00
|
|
|
|
|
|
|
|
|
rhs : Type → Type
|
|
|
|
|
rhs (_ ⇒ T) = T
|
|
|
|
|
rhs (_ ∪ T) = T
|
|
|
|
|
rhs (_ ∩ T) = T
|
|
|
|
|
rhs nil = nil
|
2022-04-09 06:07:08 +01:00
|
|
|
|
rhs never = never
|
|
|
|
|
rhs unknown = unknown
|
2022-03-02 22:02:51 +00:00
|
|
|
|
rhs number = number
|
|
|
|
|
rhs boolean = boolean
|
2022-03-02 23:26:58 +00:00
|
|
|
|
rhs string = string
|
2022-03-02 22:02:51 +00:00
|
|
|
|
|
|
|
|
|
_≡ᵀ_ : ∀ (T U : Type) → Dec(T ≡ U)
|
|
|
|
|
nil ≡ᵀ nil = yes refl
|
|
|
|
|
nil ≡ᵀ (S ⇒ T) = no (λ ())
|
2022-04-09 06:07:08 +01:00
|
|
|
|
nil ≡ᵀ never = no (λ ())
|
|
|
|
|
nil ≡ᵀ unknown = no (λ ())
|
2022-03-02 22:02:51 +00:00
|
|
|
|
nil ≡ᵀ number = no (λ ())
|
|
|
|
|
nil ≡ᵀ boolean = no (λ ())
|
|
|
|
|
nil ≡ᵀ (S ∪ T) = no (λ ())
|
|
|
|
|
nil ≡ᵀ (S ∩ T) = no (λ ())
|
2022-03-02 23:26:58 +00:00
|
|
|
|
nil ≡ᵀ string = no (λ ())
|
|
|
|
|
(S ⇒ T) ≡ᵀ string = no (λ ())
|
2022-04-09 06:07:08 +01:00
|
|
|
|
never ≡ᵀ string = no (λ ())
|
|
|
|
|
unknown ≡ᵀ string = no (λ ())
|
2022-03-02 23:26:58 +00:00
|
|
|
|
boolean ≡ᵀ string = no (λ ())
|
|
|
|
|
number ≡ᵀ string = no (λ ())
|
|
|
|
|
(S ∪ T) ≡ᵀ string = no (λ ())
|
|
|
|
|
(S ∩ T) ≡ᵀ string = no (λ ())
|
2022-03-02 22:02:51 +00:00
|
|
|
|
(S ⇒ T) ≡ᵀ nil = no (λ ())
|
|
|
|
|
(S ⇒ T) ≡ᵀ (U ⇒ V) with (S ≡ᵀ U) | (T ≡ᵀ V)
|
|
|
|
|
(S ⇒ T) ≡ᵀ (S ⇒ T) | yes refl | yes refl = yes refl
|
|
|
|
|
(S ⇒ T) ≡ᵀ (U ⇒ V) | _ | no p = no (λ q → p (cong rhs q))
|
|
|
|
|
(S ⇒ T) ≡ᵀ (U ⇒ V) | no p | _ = no (λ q → p (cong lhs q))
|
2022-04-09 06:07:08 +01:00
|
|
|
|
(S ⇒ T) ≡ᵀ never = no (λ ())
|
|
|
|
|
(S ⇒ T) ≡ᵀ unknown = no (λ ())
|
2022-03-02 22:02:51 +00:00
|
|
|
|
(S ⇒ T) ≡ᵀ number = no (λ ())
|
|
|
|
|
(S ⇒ T) ≡ᵀ boolean = no (λ ())
|
|
|
|
|
(S ⇒ T) ≡ᵀ (U ∪ V) = no (λ ())
|
|
|
|
|
(S ⇒ T) ≡ᵀ (U ∩ V) = no (λ ())
|
2022-04-09 06:07:08 +01:00
|
|
|
|
never ≡ᵀ nil = no (λ ())
|
|
|
|
|
never ≡ᵀ (U ⇒ V) = no (λ ())
|
|
|
|
|
never ≡ᵀ never = yes refl
|
|
|
|
|
never ≡ᵀ unknown = no (λ ())
|
|
|
|
|
never ≡ᵀ number = no (λ ())
|
|
|
|
|
never ≡ᵀ boolean = no (λ ())
|
|
|
|
|
never ≡ᵀ (U ∪ V) = no (λ ())
|
|
|
|
|
never ≡ᵀ (U ∩ V) = no (λ ())
|
|
|
|
|
unknown ≡ᵀ nil = no (λ ())
|
|
|
|
|
unknown ≡ᵀ (U ⇒ V) = no (λ ())
|
|
|
|
|
unknown ≡ᵀ never = no (λ ())
|
|
|
|
|
unknown ≡ᵀ unknown = yes refl
|
|
|
|
|
unknown ≡ᵀ number = no (λ ())
|
|
|
|
|
unknown ≡ᵀ boolean = no (λ ())
|
|
|
|
|
unknown ≡ᵀ (U ∪ V) = no (λ ())
|
|
|
|
|
unknown ≡ᵀ (U ∩ V) = no (λ ())
|
2022-03-02 22:02:51 +00:00
|
|
|
|
number ≡ᵀ nil = no (λ ())
|
|
|
|
|
number ≡ᵀ (T ⇒ U) = no (λ ())
|
2022-04-09 06:07:08 +01:00
|
|
|
|
number ≡ᵀ never = no (λ ())
|
|
|
|
|
number ≡ᵀ unknown = no (λ ())
|
2022-03-02 22:02:51 +00:00
|
|
|
|
number ≡ᵀ number = yes refl
|
|
|
|
|
number ≡ᵀ boolean = no (λ ())
|
|
|
|
|
number ≡ᵀ (T ∪ U) = no (λ ())
|
|
|
|
|
number ≡ᵀ (T ∩ U) = no (λ ())
|
|
|
|
|
boolean ≡ᵀ nil = no (λ ())
|
|
|
|
|
boolean ≡ᵀ (T ⇒ U) = no (λ ())
|
2022-04-09 06:07:08 +01:00
|
|
|
|
boolean ≡ᵀ never = no (λ ())
|
|
|
|
|
boolean ≡ᵀ unknown = no (λ ())
|
2022-03-02 22:02:51 +00:00
|
|
|
|
boolean ≡ᵀ boolean = yes refl
|
|
|
|
|
boolean ≡ᵀ number = no (λ ())
|
|
|
|
|
boolean ≡ᵀ (T ∪ U) = no (λ ())
|
|
|
|
|
boolean ≡ᵀ (T ∩ U) = no (λ ())
|
2022-03-02 23:26:58 +00:00
|
|
|
|
string ≡ᵀ nil = no (λ ())
|
|
|
|
|
string ≡ᵀ (x ⇒ x₁) = no (λ ())
|
2022-04-09 06:07:08 +01:00
|
|
|
|
string ≡ᵀ never = no (λ ())
|
|
|
|
|
string ≡ᵀ unknown = no (λ ())
|
2022-03-02 23:26:58 +00:00
|
|
|
|
string ≡ᵀ boolean = no (λ ())
|
|
|
|
|
string ≡ᵀ number = no (λ ())
|
|
|
|
|
string ≡ᵀ string = yes refl
|
|
|
|
|
string ≡ᵀ (U ∪ V) = no (λ ())
|
|
|
|
|
string ≡ᵀ (U ∩ V) = no (λ ())
|
2022-03-02 22:02:51 +00:00
|
|
|
|
(S ∪ T) ≡ᵀ nil = no (λ ())
|
|
|
|
|
(S ∪ T) ≡ᵀ (U ⇒ V) = no (λ ())
|
2022-04-09 06:07:08 +01:00
|
|
|
|
(S ∪ T) ≡ᵀ never = no (λ ())
|
|
|
|
|
(S ∪ T) ≡ᵀ unknown = no (λ ())
|
2022-03-02 22:02:51 +00:00
|
|
|
|
(S ∪ T) ≡ᵀ number = no (λ ())
|
|
|
|
|
(S ∪ T) ≡ᵀ boolean = no (λ ())
|
|
|
|
|
(S ∪ T) ≡ᵀ (U ∪ V) with (S ≡ᵀ U) | (T ≡ᵀ V)
|
|
|
|
|
(S ∪ T) ≡ᵀ (S ∪ T) | yes refl | yes refl = yes refl
|
|
|
|
|
(S ∪ T) ≡ᵀ (U ∪ V) | _ | no p = no (λ q → p (cong rhs q))
|
|
|
|
|
(S ∪ T) ≡ᵀ (U ∪ V) | no p | _ = no (λ q → p (cong lhs q))
|
|
|
|
|
(S ∪ T) ≡ᵀ (U ∩ V) = no (λ ())
|
|
|
|
|
(S ∩ T) ≡ᵀ nil = no (λ ())
|
|
|
|
|
(S ∩ T) ≡ᵀ (U ⇒ V) = no (λ ())
|
2022-04-09 06:07:08 +01:00
|
|
|
|
(S ∩ T) ≡ᵀ never = no (λ ())
|
|
|
|
|
(S ∩ T) ≡ᵀ unknown = no (λ ())
|
2022-03-02 22:02:51 +00:00
|
|
|
|
(S ∩ T) ≡ᵀ number = no (λ ())
|
|
|
|
|
(S ∩ T) ≡ᵀ boolean = no (λ ())
|
|
|
|
|
(S ∩ T) ≡ᵀ (U ∪ V) = no (λ ())
|
|
|
|
|
(S ∩ T) ≡ᵀ (U ∩ V) with (S ≡ᵀ U) | (T ≡ᵀ V)
|
|
|
|
|
(S ∩ T) ≡ᵀ (U ∩ V) | yes refl | yes refl = yes refl
|
|
|
|
|
(S ∩ T) ≡ᵀ (U ∩ V) | _ | no p = no (λ q → p (cong rhs q))
|
|
|
|
|
(S ∩ T) ≡ᵀ (U ∩ V) | no p | _ = no (λ q → p (cong lhs q))
|
|
|
|
|
|
|
|
|
|
_≡ᴹᵀ_ : ∀ (T U : Maybe Type) → Dec(T ≡ U)
|
|
|
|
|
nothing ≡ᴹᵀ nothing = yes refl
|
|
|
|
|
nothing ≡ᴹᵀ just U = no (λ ())
|
|
|
|
|
just T ≡ᴹᵀ nothing = no (λ ())
|
|
|
|
|
just T ≡ᴹᵀ just U with T ≡ᵀ U
|
|
|
|
|
(just T ≡ᴹᵀ just T) | yes refl = yes refl
|
|
|
|
|
(just T ≡ᴹᵀ just U) | no p = no (λ q → p (just-inv q))
|
|
|
|
|
|
2022-04-16 01:19:42 +01:00
|
|
|
|
src : Type → Type
|
|
|
|
|
src nil = never
|
|
|
|
|
src number = never
|
|
|
|
|
src boolean = never
|
|
|
|
|
src string = never
|
|
|
|
|
src (S ⇒ T) = S
|
|
|
|
|
src (S ∪ T) = (src S) ∩ (src T)
|
|
|
|
|
src (S ∩ T) = (src S) ∪ (src T)
|
|
|
|
|
src never = unknown
|
|
|
|
|
src unknown = never
|
2022-02-11 20:38:35 +00:00
|
|
|
|
|
|
|
|
|
tgt : Type → Type
|
2022-04-09 06:07:08 +01:00
|
|
|
|
tgt nil = never
|
2022-02-11 20:38:35 +00:00
|
|
|
|
tgt (S ⇒ T) = T
|
2022-04-09 06:07:08 +01:00
|
|
|
|
tgt never = never
|
|
|
|
|
tgt unknown = unknown
|
|
|
|
|
tgt number = never
|
|
|
|
|
tgt boolean = never
|
|
|
|
|
tgt string = never
|
2022-02-11 20:38:35 +00:00
|
|
|
|
tgt (S ∪ T) = (tgt S) ∪ (tgt T)
|
|
|
|
|
tgt (S ∩ T) = (tgt S) ∩ (tgt T)
|
|
|
|
|
|
|
|
|
|
optional : Type → Type
|
|
|
|
|
optional nil = nil
|
|
|
|
|
optional (T ∪ nil) = (T ∪ nil)
|
|
|
|
|
optional T = (T ∪ nil)
|
|
|
|
|
|
|
|
|
|
normalizeOptional : Type → Type
|
|
|
|
|
normalizeOptional (S ∪ T) with normalizeOptional S | normalizeOptional T
|
|
|
|
|
normalizeOptional (S ∪ T) | (S′ ∪ nil) | (T′ ∪ nil) = (S′ ∪ T′) ∪ nil
|
|
|
|
|
normalizeOptional (S ∪ T) | S′ | (T′ ∪ nil) = (S′ ∪ T′) ∪ nil
|
|
|
|
|
normalizeOptional (S ∪ T) | (S′ ∪ nil) | T′ = (S′ ∪ T′) ∪ nil
|
|
|
|
|
normalizeOptional (S ∪ T) | S′ | nil = optional S′
|
|
|
|
|
normalizeOptional (S ∪ T) | nil | T′ = optional T′
|
|
|
|
|
normalizeOptional (S ∪ T) | S′ | T′ = S′ ∪ T′
|
|
|
|
|
normalizeOptional T = T
|