Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a typeToString function to the prototype #354

Merged
merged 3 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/prototyping.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- 'master'
- 'prototyping-*'
paths:
- '.github/workflows/**'
- 'prototyping/**'
Expand Down
2 changes: 1 addition & 1 deletion prototyping/Examples.agda
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module Examples where
import Examples.Syntax
import Examples.OpSem
import Examples.Run

import Examples.Type
21 changes: 21 additions & 0 deletions prototyping/Examples/Type.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Examples.Type where

open import Agda.Builtin.Equality using (_≡_; refl)
open import FFI.Data.String using (_++_)
open import Luau.Type using (nil; _∪_; _∩_; _⇒_)
open import Luau.Type.ToString using (typeToString)

ex1 : typeToString(nil) ≡ "nil"
ex1 = refl

ex2 : typeToString(nil ⇒ nil) ≡ "(nil) -> nil"
ex2 = refl

ex3 : typeToString(nil ⇒ (nil ⇒ nil)) ≡ "(nil) -> (nil) -> nil"
ex3 = refl

ex4 : typeToString(nil ∪ (nil ⇒ (nil ⇒ nil))) ≡ "((nil) -> (nil) -> nil)?"
ex4 = refl

ex5 : typeToString(nil ⇒ ((nil ⇒ nil) ∪ nil)) ≡ "(nil) -> ((nil) -> nil)?"
ex5 = refl
31 changes: 31 additions & 0 deletions prototyping/Luau/Type.agda
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Luau.Type where

open import FFI.Data.Maybe using (Maybe; just; nothing)

data Type : Set where
nil : Type
_⇒_ : Type → Type → Type
Expand All @@ -8,3 +10,32 @@ data Type : Set where
_∪_ : Type → Type → Type
_∩_ : Type → Type → Type

src : Type → Type
src nil = none
src (S ⇒ T) = S
src none = none
src any = any
src (S ∪ T) = (src S) ∪ (src T)
src (S ∩ T) = (src S) ∩ (src T)

tgt : Type → Type
tgt nil = none
tgt (S ⇒ T) = T
tgt none = none
tgt any = any
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′ | nil = optional S′
normalizeOptional (S ∪ T) | nil | T′ = optional T′
normalizeOptional (S ∪ T) | S′ | T′ = S′ ∪ T′
normalizeOptional T = T

28 changes: 28 additions & 0 deletions prototyping/Luau/Type/ToString.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Luau.Type.ToString where

open import FFI.Data.String using (String; _++_)
open import Luau.Type using (Type; nil; _⇒_; none; any; _∪_; _∩_; normalizeOptional)

{-# TERMINATING #-}
typeToString : Type → String
typeToStringᵁ : Type → String
typeToStringᴵ : Type → String

typeToString nil = "nil"
typeToString (S ⇒ T) = "(" ++ (typeToString S) ++ ") -> " ++ (typeToString T)
typeToString none = "none"
typeToString any = "any"
typeToString (S ∪ T) with normalizeOptional(S ∪ T)
typeToString (S ∪ T) | ((S′ ⇒ T′) ∪ nil) = "(" ++ typeToString (S′ ⇒ T′) ++ ")?"
typeToString (S ∪ T) | (S′ ∪ nil) = "(" ++ typeToString S′ ++ "?"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing ) here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, not enough unit tests here. I added some and fixed some bugs that kicked up.

typeToString (S ∪ T) | (S′ ∪ T′) = "(" ++ typeToStringᵁ (S ∪ T) ++ ")"
typeToString (S ∪ T) | T′ = typeToString T′
typeToString (S ∩ T) = "(" ++ typeToStringᴵ (S ∩ T) ++ ")"

typeToStringᵁ (S ⇒ T) = "(" ++ typeToString (S ⇒ T) ++ ")"
typeToStringᵁ (S ∪ T) = (typeToStringᵁ S) ++ " | " ++ (typeToStringᵁ T)
typeToStringᵁ T = typeToString T

typeToStringᴵ (S ⇒ T) = "(" ++ typeToString (S ⇒ T) ++ ")"
typeToStringᴵ (S ∩ T) = (typeToStringᴵ S) ++ " & " ++ (typeToStringᴵ T)
typeToStringᴵ T = typeToString T
2 changes: 2 additions & 0 deletions prototyping/Properties.agda
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module Properties where

import Properties.Dec
import Properties.Step
import Properties.Remember