-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototyping type normalizaton (#466)
* Added type normalization
- Loading branch information
1 parent
e0a6461
commit 74c8481
Showing
13 changed files
with
940 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{-# OPTIONS --rewriting #-} | ||
|
||
open import FFI.Data.Either using (Either; Left; Right) | ||
open import Luau.Type using (Type; nil; number; string; boolean; never; unknown; _⇒_; _∪_; _∩_) | ||
open import Luau.TypeNormalization using (normalize) | ||
|
||
module Luau.FunctionTypes where | ||
|
||
-- The domain of a normalized type | ||
srcⁿ : Type → Type | ||
srcⁿ (S ⇒ T) = S | ||
srcⁿ (S ∩ T) = srcⁿ S ∪ srcⁿ T | ||
srcⁿ never = unknown | ||
srcⁿ T = never | ||
|
||
-- To get the domain of a type, we normalize it first We need to do | ||
-- this, since if we try to use it on non-normalized types, we get | ||
-- | ||
-- src(number ∩ string) = src(number) ∪ src(string) = never ∪ never | ||
-- src(never) = unknown | ||
-- | ||
-- so src doesn't respect type equivalence. | ||
src : Type → Type | ||
src (S ⇒ T) = S | ||
src T = srcⁿ(normalize T) | ||
|
||
-- The codomain of a type | ||
tgt : Type → Type | ||
tgt nil = never | ||
tgt (S ⇒ T) = T | ||
tgt never = never | ||
tgt unknown = unknown | ||
tgt number = never | ||
tgt boolean = never | ||
tgt string = never | ||
tgt (S ∪ T) = (tgt S) ∪ (tgt T) | ||
tgt (S ∩ T) = (tgt S) ∩ (tgt T) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module Luau.TypeNormalization where | ||
|
||
open import Luau.Type using (Type; nil; number; string; boolean; never; unknown; _⇒_; _∪_; _∩_) | ||
|
||
-- The top non-function type | ||
¬function : Type | ||
¬function = number ∪ (string ∪ (nil ∪ boolean)) | ||
|
||
-- Unions and intersections of normalized types | ||
_∪ᶠ_ : Type → Type → Type | ||
_∪ⁿˢ_ : Type → Type → Type | ||
_∩ⁿˢ_ : Type → Type → Type | ||
_∪ⁿ_ : Type → Type → Type | ||
_∩ⁿ_ : Type → Type → Type | ||
|
||
-- Union of function types | ||
(F₁ ∩ F₂) ∪ᶠ G = (F₁ ∪ᶠ G) ∩ (F₂ ∪ᶠ G) | ||
F ∪ᶠ (G₁ ∩ G₂) = (F ∪ᶠ G₁) ∩ (F ∪ᶠ G₂) | ||
(R ⇒ S) ∪ᶠ (T ⇒ U) = (R ∩ⁿ T) ⇒ (S ∪ⁿ U) | ||
F ∪ᶠ G = F ∪ G | ||
|
||
-- Union of normalized types | ||
S ∪ⁿ (T₁ ∪ T₂) = (S ∪ⁿ T₁) ∪ T₂ | ||
S ∪ⁿ unknown = unknown | ||
S ∪ⁿ never = S | ||
unknown ∪ⁿ T = unknown | ||
never ∪ⁿ T = T | ||
(S₁ ∪ S₂) ∪ⁿ G = (S₁ ∪ⁿ G) ∪ S₂ | ||
F ∪ⁿ G = F ∪ᶠ G | ||
|
||
-- Intersection of normalized types | ||
S ∩ⁿ (T₁ ∪ T₂) = (S ∩ⁿ T₁) ∪ⁿˢ (S ∩ⁿˢ T₂) | ||
S ∩ⁿ unknown = S | ||
S ∩ⁿ never = never | ||
(S₁ ∪ S₂) ∩ⁿ G = (S₁ ∩ⁿ G) | ||
unknown ∩ⁿ G = G | ||
never ∩ⁿ G = never | ||
F ∩ⁿ G = F ∩ G | ||
|
||
-- Intersection of normalized types with a scalar | ||
(S₁ ∪ nil) ∩ⁿˢ nil = nil | ||
(S₁ ∪ boolean) ∩ⁿˢ boolean = boolean | ||
(S₁ ∪ number) ∩ⁿˢ number = number | ||
(S₁ ∪ string) ∩ⁿˢ string = string | ||
(S₁ ∪ S₂) ∩ⁿˢ T = S₁ ∩ⁿˢ T | ||
unknown ∩ⁿˢ T = T | ||
F ∩ⁿˢ T = never | ||
|
||
-- Union of normalized types with an optional scalar | ||
S ∪ⁿˢ never = S | ||
unknown ∪ⁿˢ T = unknown | ||
(S₁ ∪ nil) ∪ⁿˢ nil = S₁ ∪ nil | ||
(S₁ ∪ boolean) ∪ⁿˢ boolean = S₁ ∪ boolean | ||
(S₁ ∪ number) ∪ⁿˢ number = S₁ ∪ number | ||
(S₁ ∪ string) ∪ⁿˢ string = S₁ ∪ string | ||
(S₁ ∪ S₂) ∪ⁿˢ T = (S₁ ∪ⁿˢ T) ∪ S₂ | ||
F ∪ⁿˢ T = F ∪ T | ||
|
||
-- Normalize! | ||
normalize : Type → Type | ||
normalize nil = never ∪ nil | ||
normalize (S ⇒ T) = (normalize S ⇒ normalize T) | ||
normalize never = never | ||
normalize unknown = unknown | ||
normalize boolean = never ∪ boolean | ||
normalize number = never ∪ number | ||
normalize string = never ∪ string | ||
normalize (S ∪ T) = normalize S ∪ⁿ normalize T | ||
normalize (S ∩ T) = normalize S ∩ⁿ normalize T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{-# OPTIONS --rewriting #-} | ||
|
||
module Properties.DecSubtyping where | ||
|
||
open import Agda.Builtin.Equality using (_≡_; refl) | ||
open import FFI.Data.Either using (Either; Left; Right; mapLR; swapLR; cond) | ||
open import Luau.FunctionTypes using (src; srcⁿ; tgt) | ||
open import Luau.Subtyping using (_<:_; _≮:_; Tree; Language; ¬Language; witness; unknown; never; scalar; function; scalar-function; scalar-function-ok; scalar-function-err; scalar-scalar; function-scalar; function-ok; function-err; left; right; _,_) | ||
open import Luau.Type using (Type; Scalar; nil; number; string; boolean; never; unknown; _⇒_; _∪_; _∩_) | ||
open import Properties.Contradiction using (CONTRADICTION; ¬) | ||
open import Properties.Functions using (_∘_) | ||
open import Properties.Subtyping using (<:-refl; <:-trans; ≮:-trans-<:; <:-trans-≮:; <:-never; <:-unknown; <:-∪-left; <:-∪-right; <:-∪-lub; ≮:-∪-left; ≮:-∪-right; <:-∩-left; <:-∩-right; <:-∩-glb; ≮:-∩-left; ≮:-∩-right; dec-language; scalar-<:; <:-everything; <:-function; ≮:-function-left; ≮:-function-right) | ||
open import Properties.TypeNormalization using (FunType; Normal; never; unknown; _∩_; _∪_; _⇒_; normal; <:-normalize; normalize-<:) | ||
open import Properties.FunctionTypes using (fun-¬scalar; ¬fun-scalar; fun-function; src-unknown-≮:; tgt-never-≮:; src-tgtᶠ-<:) | ||
open import Properties.Equality using (_≢_) | ||
|
||
-- Honest this terminates, since src and tgt reduce the depth of nested arrows | ||
{-# TERMINATING #-} | ||
dec-subtypingˢⁿ : ∀ {T U} → Scalar T → Normal U → Either (T ≮: U) (T <: U) | ||
dec-subtypingᶠ : ∀ {T U} → FunType T → FunType U → Either (T ≮: U) (T <: U) | ||
dec-subtypingᶠⁿ : ∀ {T U} → FunType T → Normal U → Either (T ≮: U) (T <: U) | ||
dec-subtypingⁿ : ∀ {T U} → Normal T → Normal U → Either (T ≮: U) (T <: U) | ||
dec-subtyping : ∀ T U → Either (T ≮: U) (T <: U) | ||
|
||
dec-subtypingˢⁿ T U with dec-language _ (scalar T) | ||
dec-subtypingˢⁿ T U | Left p = Left (witness (scalar T) (scalar T) p) | ||
dec-subtypingˢⁿ T U | Right p = Right (scalar-<: T p) | ||
|
||
dec-subtypingᶠ {T = T} _ (U ⇒ V) with dec-subtypingⁿ U (normal (src T)) | dec-subtypingⁿ (normal (tgt T)) V | ||
dec-subtypingᶠ {T = T} _ (U ⇒ V) | Left p | q = Left (≮:-trans-<: (src-unknown-≮: (≮:-trans-<: p (<:-normalize (src T)))) (<:-function <:-refl <:-unknown)) | ||
dec-subtypingᶠ {T = T} _ (U ⇒ V) | Right p | Left q = Left (≮:-trans-<: (tgt-never-≮: (<:-trans-≮: (normalize-<: (tgt T)) q)) (<:-trans (<:-function <:-never <:-refl) <:-∪-right)) | ||
dec-subtypingᶠ T (U ⇒ V) | Right p | Right q = Right (src-tgtᶠ-<: T (<:-trans p (normalize-<: _)) (<:-trans (<:-normalize _) q)) | ||
|
||
dec-subtypingᶠ T (U ∩ V) with dec-subtypingᶠ T U | dec-subtypingᶠ T V | ||
dec-subtypingᶠ T (U ∩ V) | Left p | q = Left (≮:-∩-left p) | ||
dec-subtypingᶠ T (U ∩ V) | Right p | Left q = Left (≮:-∩-right q) | ||
dec-subtypingᶠ T (U ∩ V) | Right p | Right q = Right (<:-∩-glb p q) | ||
|
||
dec-subtypingᶠⁿ T never = Left (witness function (fun-function T) never) | ||
dec-subtypingᶠⁿ T unknown = Right <:-unknown | ||
dec-subtypingᶠⁿ T (U ⇒ V) = dec-subtypingᶠ T (U ⇒ V) | ||
dec-subtypingᶠⁿ T (U ∩ V) = dec-subtypingᶠ T (U ∩ V) | ||
dec-subtypingᶠⁿ T (U ∪ V) with dec-subtypingᶠⁿ T U | ||
dec-subtypingᶠⁿ T (U ∪ V) | Left (witness t p q) = Left (witness t p (q , ¬fun-scalar V T p)) | ||
dec-subtypingᶠⁿ T (U ∪ V) | Right p = Right (<:-trans p <:-∪-left) | ||
|
||
dec-subtypingⁿ never U = Right <:-never | ||
dec-subtypingⁿ unknown unknown = Right <:-refl | ||
dec-subtypingⁿ unknown U with dec-subtypingᶠⁿ (never ⇒ unknown) U | ||
dec-subtypingⁿ unknown U | Left p = Left (<:-trans-≮: <:-unknown p) | ||
dec-subtypingⁿ unknown U | Right p₁ with dec-subtypingˢⁿ number U | ||
dec-subtypingⁿ unknown U | Right p₁ | Left p = Left (<:-trans-≮: <:-unknown p) | ||
dec-subtypingⁿ unknown U | Right p₁ | Right p₂ with dec-subtypingˢⁿ string U | ||
dec-subtypingⁿ unknown U | Right p₁ | Right p₂ | Left p = Left (<:-trans-≮: <:-unknown p) | ||
dec-subtypingⁿ unknown U | Right p₁ | Right p₂ | Right p₃ with dec-subtypingˢⁿ nil U | ||
dec-subtypingⁿ unknown U | Right p₁ | Right p₂ | Right p₃ | Left p = Left (<:-trans-≮: <:-unknown p) | ||
dec-subtypingⁿ unknown U | Right p₁ | Right p₂ | Right p₃ | Right p₄ with dec-subtypingˢⁿ boolean U | ||
dec-subtypingⁿ unknown U | Right p₁ | Right p₂ | Right p₃ | Right p₄ | Left p = Left (<:-trans-≮: <:-unknown p) | ||
dec-subtypingⁿ unknown U | Right p₁ | Right p₂ | Right p₃ | Right p₄ | Right p₅ = Right (<:-trans <:-everything (<:-∪-lub p₁ (<:-∪-lub p₂ (<:-∪-lub p₃ (<:-∪-lub p₄ p₅))))) | ||
dec-subtypingⁿ (S ⇒ T) U = dec-subtypingᶠⁿ (S ⇒ T) U | ||
dec-subtypingⁿ (S ∩ T) U = dec-subtypingᶠⁿ (S ∩ T) U | ||
dec-subtypingⁿ (S ∪ T) U with dec-subtypingⁿ S U | dec-subtypingˢⁿ T U | ||
dec-subtypingⁿ (S ∪ T) U | Left p | q = Left (≮:-∪-left p) | ||
dec-subtypingⁿ (S ∪ T) U | Right p | Left q = Left (≮:-∪-right q) | ||
dec-subtypingⁿ (S ∪ T) U | Right p | Right q = Right (<:-∪-lub p q) | ||
|
||
dec-subtyping T U with dec-subtypingⁿ (normal T) (normal U) | ||
dec-subtyping T U | Left p = Left (<:-trans-≮: (normalize-<: T) (≮:-trans-<: p (<:-normalize U))) | ||
dec-subtyping T U | Right p = Right (<:-trans (<:-normalize T) (<:-trans p (normalize-<: U))) | ||
|
Oops, something went wrong.