Created
May 1, 2022 05:39
-
-
Save evincarofautumn/8c96ee4f806a54725647c2c97bc12f8f to your computer and use it in GitHub Desktop.
Overcomplicated STLC
This file contains hidden or 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
| {-# Language | |
| BlockArguments, | |
| DataKinds, | |
| DerivingStrategies, | |
| GADTs, | |
| InstanceSigs, | |
| LambdaCase, | |
| PatternSynonyms, | |
| PolyKinds, | |
| RankNTypes, | |
| ScopedTypeVariables, | |
| StandaloneDeriving, | |
| TypeOperators, | |
| UnicodeSyntax | |
| #-} | |
| {-# Options_GHC | |
| -Wall | |
| -Wno-unticked-promoted-constructors | |
| #-} | |
| import Control.Category (Category(..)) | |
| import Data.Kind (Type) | |
| import Prelude hiding ((.), id) | |
| main β· IO () | |
| main = pure () | |
| -------------------------------------------------------------------------------- | |
| -- ASCII Aliases | |
| -------------------------------------------------------------------------------- | |
| type Ctx0 = Ξβ -- \Gamma_0 \x0393\x2080 | |
| type Ctx1 c = Ξβ c -- \Gamma_1 \x0393\x2081 | |
| type SubCtx c1 c2 = c1 β c2 -- \subseteq \x2286 | |
| type T0 = Tβ -- \Tau \x03A4\x2080 | |
| type T1 t = Tβ(t) -- \Tau_ \x03A4\x2081 | |
| type Term c t = c β’ t -- \vdash \x22A2 | |
| type Value c t = c β¨ t -- \vDash \x22A8 | |
| type c `Ni` t = c β t -- \ni \x220B | |
| type t `In` c = t β c -- \in \x2208 | |
| type c2 ~> c1 = c2 β c1 -- \leadsto \x219D | |
| type c1 <~ c2 = c1 β c2 -- \leadsfrom \x219C | |
| pattern (:<~) β· (Ξ³β β¨ Ο) β (Ξ³β β Ξ³β) β (Ξ³β & Ο β Ξ³β) | |
| pattern c1 :<~ c2 = c1 :β c2 | |
| pattern Del0 β· '[] β Ξ³β | |
| pattern Del0 = Ξβ | |
| pattern Lam0 β· Tβ β E β E | |
| pattern Lam0 t e = Ξβ t e | |
| pattern Lam1 β· Tβ(Ξ±) β (Ξ³ & Ξ± β’ Ξ²) β (Ξ³ β’ Ξ± :β Ξ²) | |
| pattern Lam1 t e = Ξβ t e | |
| pattern (:->) β· Tβ β Tβ β Tβ | |
| pattern a :-> b = a :β b | |
| pattern N0 β· Tβ | |
| pattern N0 = TNβ | |
| pattern T0 β· Tβ | |
| pattern T0 = TTβ | |
| pattern K0 β· Tβ | |
| pattern K0 = TKβ | |
| pattern (:=>) β· Tβ(Ξ±) β Tβ(Ξ²) β Tβ(Ξ± :β Ξ²) | |
| pattern a :=> b = a :β b | |
| pattern N1 β· Tβ(TNβ) | |
| pattern N1 = TNβ | |
| pattern T1 β· Tβ(TTβ) | |
| pattern T1 = TTβ | |
| pattern K1 β· Tβ(TKβ) | |
| pattern K1 = TKβ | |
| pattern CtxEqZ β· (Ξ³ β Ξ³) | |
| pattern CtxEqZ = ΞEqZ | |
| pattern CtxLt β· (Ξ³β β Ξ³β) β (Ξ³β β Ξ³β & Ο) | |
| pattern CtxLt p = ΞLt p | |
| pattern CtxEqS β· (Ξ³β β Ξ³β) β (Ξ³β & Ο β Ξ³β & Ο) | |
| pattern CtxEqS p = ΞEqS p | |
| wmap β· (Affine p) β (Ξ³β β Ξ³β) β (p Ξ³β Ξ±) β (p Ξ³β Ξ±) | |
| wmap = (βͺ) | |
| -------------------------------------------------------------------------------- | |
| -- Fixities | |
| -------------------------------------------------------------------------------- | |
| infix 1 β, <~ | |
| infix 1 β, ~> | |
| infix 1 β’ | |
| infix 1 β¨ | |
| infix 2 β | |
| infix 2 β | |
| infix 2 β | |
| infixr 4 :β, :-> | |
| infixr 4 :β, :=> | |
| infixr 5 :β, :<~ | |
| infixl 5 & | |
| infixl 5 :& | |
| infixr 6 βͺ | |
| -------------------------------------------------------------------------------- | |
| -- Types | |
| -------------------------------------------------------------------------------- | |
| data Tβ where -- untyped types | |
| (:β) β· Tβ β Tβ β Tβ -- function type | |
| TNβ β· {} β Tβ -- natural type | |
| TTβ β· {} β Tβ -- value kind | |
| TKβ β· {} β Tβ -- type kind | |
| data Tβ(Ο β· Tβ) where -- and their typed kin | |
| (:β) β· Tβ(Ξ±) β Tβ(Ξ²) β Tβ(Ξ± :β Ξ²) | |
| TNβ β· {} β Tβ(TNβ) | |
| TTβ β· {} β Tβ(TTβ) | |
| TKβ β· {} β Tβ(TKβ) | |
| type Ξβ = [Tβ] -- untyped typing context | |
| type (Ξ³ β· Ξβ) & (Ο β· Tβ) = Ο ': Ξ³ | |
| data Ξβ(Ξ³ β· Ξβ) where -- typed typing context | |
| Ξβ β· {} β Ξβ('[]) -- nil | |
| (:&) β· Ξβ(Ξ³) β Tβ(Ο) β Ξβ(Ξ³ & Ο) -- cons | |
| data (Ξ³β β· Ξβ) β (Ξ³β β· Ξβ) where -- context inclusion proof | |
| ΞEqZ -- base case: contexts are equal (reflexivity) | |
| β· {} | |
| -- βββββββββββββ | |
| β Ξ³ β Ξ³ | |
| ΞLt -- one context is a subcontext of another | |
| β· Ξ³β β Ξ³β | |
| -- βββββββββββββ | |
| β Ξ³β β Ξ³β & Ο | |
| ΞEqS -- inductive case: contexts have a common supercontext | |
| β· Ξ³β β Ξ³β | |
| -- βββββββββββββββ | |
| β Ξ³β & Ο β Ξ³β & Ο | |
| type Ο β Ξ³ -- in | |
| = Ξ³ β Ο -- ni | |
| data (Ξ³ β· [ΞΊ]) β (Ξ± β· ΞΊ) where -- context containment proof | |
| -- made of a fancy index | |
| SZ β· {} -- zero, head of context | |
| -- βββββββββ | |
| β Ξ± β Ξ³ & Ξ± | |
| SS β· Ξ± β Ξ³ -- the variable is in another castle | |
| -- βββββββββ | |
| β Ξ± β Ξ³ & Ξ² | |
| data E where -- an untyped term | |
| Lβ β· Aβ β E -- literal | |
| Vβ β· Int β E -- variable | |
| Ξβ β· Tβ β E β E -- abstraction | |
| Aβ β· E β E β E -- application | |
| -- typed term, per a context | |
| data (Ξ³ β· Ξβ) β’ (Ο β· Tβ) where | |
| Lβ -- typed literal | |
| β· Aβ(Ο) | |
| -- βββββ [lit] | |
| β Ξ³ β’ Ο | |
| Vβ -- typed variable | |
| β· Ο β Ξ³ | |
| -- βββββ [var] | |
| β Ξ³ β’ Ο | |
| Ξβ -- typed abstraction | |
| β· Tβ(Ξ±) | |
| β Ξ³ & Ξ± β’ Ξ² | |
| -- ββββββββββ [abs] | |
| β Ξ³ β’ Ξ± :β Ξ² | |
| Aβ -- typed application | |
| β· Ξ³ β’ Ξ± :β Ξ² | |
| β Ξ³ β’ Ξ± | |
| -- ββββββββββ | |
| β Ξ³ β’ Ξ² | |
| data Aβ where -- atomic elements | |
| Zeroβ β· Aβ -- natural zero | |
| Succβ β· Aβ -- natural successor | |
| Natβ β· Aβ -- type of naturals | |
| Starβ β· Aβ -- kind of types inhabited by values | |
| Boxβ β· Aβ -- kind of types inhabited by types | |
| Arrβ β· Aβ -- function type constructor | |
| -- | |
| data Aβ(Ο β· Tβ) where -- and their typed kin | |
| Zero β· Aβ(TNβ) | |
| Succ β· Aβ(TNβ :β TNβ) | |
| Nat β· Aβ(TTβ) | |
| Star β· Aβ(TTβ) | |
| Box β· Aβ(TKβ) | |
| Arr β· Aβ(TTβ :β TTβ :β TTβ) | |
| data (Ξ³ β· Ξβ) β¨ (Ο β· Tβ) where -- valuation of a term in a model | |
| Constant β· | |
| { inconstant | |
| β· Aβ(Ο) -- from a typed atom | |
| -- βββββ -- obtain | |
| } β Ξ³ β¨ Ο -- a constant value of that type | |
| Natural β· | |
| { unnatural | |
| β· Ξ³ β’ TNβ -- from a natural term | |
| -- ββββββ -- obtain | |
| } β Ξ³ β¨ TNβ -- a natural value | |
| Neutral -- a neutral application | |
| β· Ξ³ β¨ Ξ± :β Ξ² -- comprises a function | |
| β Ξ³ β¨ Ξ± -- and argument | |
| -- ββββββββββ -- | |
| β Ξ³ β¨ Ξ² -- yet to reduce | |
| Close β· | |
| { open | |
| β· βΞ³β -- for any | |
| . Ξβ(Ξ³β) -- valid context | |
| β Ξ³β β Ξ³β -- in which it finds itself | |
| β Ξ³β β¨ Ξ± -- therein applied to an argument | |
| β Ξ³β β¨ Ξ² -- therein gives its result | |
| -- βββββββββββ -- | |
| } β Ξ³β β¨ Ξ± :β Ξ² -- a closure | |
| type Ξ³β β Ξ³β -- thence hither | |
| = Ξ³β β Ξ³β -- hither thence | |
| data (Ξ³β β· Ξβ) β (Ξ³β β· Ξβ) where -- an environment | |
| Ξβ | |
| β· {} | |
| -- ββββββββ | |
| β '[] β Ξ³β -- empty | |
| (:β) | |
| β· Ξ³β β¨ Ο -- if there is a thing | |
| β Ξ³β β Ξ³β -- and where it is may be here | |
| -- βββββββββββ | |
| β Ξ³β & Ο β Ξ³β -- then the thing may be here | |
| -------------------------------------------------------------------------------- | |
| -- Classes & Instances | |
| -------------------------------------------------------------------------------- | |
| class Affine (p β· Ξβ β ΞΊ β Type) where -- context-indexed constructors | |
| (βͺ) -- whose demands can be weakened | |
| β· Ξ³β β Ξ³β | |
| β p Ξ³β Ξ± | |
| -- βββββββ | |
| β p Ξ³β Ξ± | |
| instance Affine (β) where | |
| (βͺ) | |
| β· Ξ³β β Ξ³β | |
| β Ο β Ξ³β | |
| -- βββββββ | |
| β Ο β Ξ³β | |
| (βͺ) = \ case | |
| ΞEqZ β id | |
| ΞLt p β \ x β SS (p βͺ x) | |
| ΞEqS p β \ case | |
| SZ β SZ | |
| SS x β SS (p βͺ x) | |
| instance Affine (β’) where | |
| (βͺ) | |
| β· Ξ³β β Ξ³β | |
| β Ξ³β β’ Ξ± | |
| β Ξ³β β’ Ξ± | |
| (βͺ) p = \ case | |
| Lβ a β Lβ a | |
| Vβ x β Vβ (p βͺ x) | |
| Ξβ Ξ± e β Ξβ Ξ± (ΞEqS p βͺ e) | |
| Aβ eβ eβ β Aβ (p βͺ eβ) (p βͺ eβ) | |
| instance Show (Ξ³ β¨ Ο) where | |
| showsPrec p = showParen (p > 10) . \ case | |
| Constant a β showString "Constant " . showsPrec 10 a | |
| Natural n β showString "Natural " . showsPrec 10 n | |
| Neutral f x β showsPrec 11 f . showString " " . showsPrec 10 x | |
| Close _f β showString "Close (error \"β¨closureβ©\")" | |
| instance Affine (β¨) where | |
| (βͺ) | |
| β· Ξ³β β Ξ³β | |
| β Ξ³β β¨ Ο | |
| -- βββββββ | |
| β Ξ³β β¨ Ο | |
| (βͺ) p = \ case | |
| Constant a β Constant a | |
| Natural n β Natural (p βͺ n) | |
| Neutral eβ eβ β Neutral (p βͺ eβ) (p βͺ eβ) | |
| Close f β Close \ Ξ³ q β f Ξ³ (q . p) | |
| instance Category (β) where | |
| id β· Ξ³ β Ξ³ | |
| id = ΞEqZ | |
| (.) | |
| β· Ξ³β β Ξ³β | |
| β Ξ³β β Ξ³β | |
| β Ξ³β β Ξ³β | |
| ΞEqZ . f = f | |
| g . ΞEqZ = g | |
| ΞLt p . f = ΞLt (p . f) | |
| ΞEqS p . ΞLt q = ΞLt (p . q) | |
| ΞEqS p . ΞEqS q = ΞEqS (p . q) | |
| instance Affine (β) where | |
| (βͺ) | |
| β· Ξ³β β Ξ³β | |
| β Ξ³β β Ξ³β | |
| -- βββββββ | |
| β Ξ³β β Ξ³β | |
| (βͺ) p = \ case | |
| Ξβ β Ξβ | |
| v :β e β (p βͺ v) :β (p βͺ e) | |
| deriving stock instance Show (Aβ(Ο)) | |
| deriving stock instance Show (Tβ(Ο)) | |
| deriving stock instance Show (Ξ³ β Ξ±) | |
| deriving stock instance Show (Ξ³ β’ Ο) | |
| deriving stock instance Show (Ξ³β β Ξ³β) | |
| deriving stock instance Show Aβ | |
| deriving stock instance Show E | |
| deriving stock instance Show Tβ | |
| -------------------------------------------------------------------------------- | |
| -- Evaluation | |
| -------------------------------------------------------------------------------- | |
| (!) | |
| β· Ξ³β β Ξ³β | |
| β Ο β Ξ³β | |
| -- βββββββ | |
| β Ξ³β β¨ Ο | |
| (v :β _Ξ΄) ! SZ = v | |
| (_v :β Ξ΄) ! SS x = Ξ΄ ! x | |
| Ξβ ! _ = error "impossible" | |
| reify | |
| β· Ξβ(Ξ³) | |
| β Tβ(Ο) | |
| β Ξ³ β¨ Ο | |
| -- βββββ | |
| β Ξ³ β’ Ο | |
| reify Ξ³ = \ case | |
| TNβ β unnatural | |
| Ξ± :β Ξ² β \ v β Ξβ Ξ± (reify Ξ³' Ξ² (open v Ξ³' (ΞLt id) (reflect Ξ³' Ξ± (Vβ SZ)))) | |
| where | |
| Ξ³' = Ξ³ :& Ξ± | |
| TTβ β \ (Constant Star) β Lβ Star | |
| TKβ β \ (Constant Box) β Lβ Box | |
| reflect | |
| β· Ξβ(Ξ³) | |
| β Tβ(Ο) | |
| β Ξ³ β’ Ο | |
| -- βββββ | |
| β Ξ³ β¨ Ο | |
| reflect _Ξ³ = \ case | |
| TNβ β Natural | |
| Ξ± :β Ξ² β \ e β Close \ Ξ³' p β reflect Ξ³' Ξ² . Aβ (p βͺ e) . reify Ξ³' Ξ± | |
| TTβ β \ (Lβ Star) β Constant Star | |
| TKβ β \ (Lβ Box) β Constant Box | |
| eval | |
| β· βΞ³β Ξ³β Ο | |
| . Ξβ(Ξ³β) | |
| β Ξ³β β Ξ³β | |
| β Ξ³β β’ Ο | |
| -- βββββββ | |
| β Ξ³β β¨ Ο | |
| eval Ξ³ Ξ΄ = eval' | |
| where | |
| eval' | |
| β· βΟ' | |
| . Ξ³β β’ Ο' | |
| -- βββββββ | |
| β Ξ³β β¨ Ο' | |
| eval' = \ case | |
| Lβ a β Constant a | |
| Vβ x β Ξ΄ ! x | |
| Ξβ _Ξ± e β Close \ Ξ³' p v β let Ξ΄' = v :β p βͺ Ξ΄ in eval Ξ³' Ξ΄' e | |
| Aβ eβ eβ β let | |
| vβ = eval' eβ | |
| in case eval' eβ of | |
| Constant Succ β Natural $ Aβ (Lβ Succ) (unnatural vβ) | |
| Close f β f Ξ³ id vβ | |
| vβ β Neutral vβ vβ | |
| normalize | |
| β· βΞ³β Ξ³β Ο | |
| . Ξβ(Ξ³β) | |
| β Tβ(Ο) | |
| β Ξ³β β Ξ³β | |
| β Ξ³β β’ Ο | |
| β Ξ³β β’ Ο | |
| normalize Ξ³ Ο Ξ΄ = reify Ξ³ Ο . eval Ξ³ Ξ΄ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment