From 0ddaf1b2c9ca66cf0ae03d2f6ad792c7885dfc32 Mon Sep 17 00:00:00 2001 From: Chloe Brown Date: Wed, 21 Jun 2023 16:05:44 +0100 Subject: Add sums, vectors and arithmetic encodings. Also define pretty printing of terms. --- src/Encoded/Arith.idr | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/Encoded/Bool.idr | 26 +++++++++++++++++ src/Encoded/Fin.idr | 48 ++++++++++++++++++++++++++++++ src/Encoded/Pair.idr | 41 ++++++++++++++++++++++++++ src/Encoded/Sum.idr | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Encoded/Union.idr | 2 ++ src/Encoded/Vect.idr | 66 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 344 insertions(+) create mode 100644 src/Encoded/Arith.idr create mode 100644 src/Encoded/Fin.idr create mode 100644 src/Encoded/Sum.idr create mode 100644 src/Encoded/Vect.idr (limited to 'src/Encoded') diff --git a/src/Encoded/Arith.idr b/src/Encoded/Arith.idr new file mode 100644 index 0000000..d2f83bc --- /dev/null +++ b/src/Encoded/Arith.idr @@ -0,0 +1,81 @@ +module Encoded.Arith + +import Encoded.Bool +import Encoded.Pair +import Term.Syntax + +export +rec : {ty : Ty} -> Term (N ~> ty ~> (N * ty ~> ty) ~> ty) ctx +rec = Abs $ Abs $ Abs $ + let n = Var $ There $ There Here in + let z = Var $ There Here in + let s = Var Here in + let + p : Term (N * ty) (ctx :< N :< ty :< (N * ty ~> ty)) + p = Rec n + (App pair [ + App pair + [ N ~> N) ctx +plus = Abs' (\n => Rec n Id (Abs' (Abs' Suc .))) + +export +pred : Term (N ~> N) ctx +-- pred = Abs' (\n => App rec [ App + (Rec n + (Const Zero) + (Abs' (\f => + Rec (App f [ Rec k (Suc Zero) (Const Zero))) + (Const $ Abs' (\k => Rec k (Suc Zero) (Abs' Suc . shift f)))))) + [ N ~> N) ctx +minus = Abs $ Abs $ + let m = Var $ There Here in + let n = Var Here in + Rec n m pred + +export +mult : Term (N ~> N ~> N) ctx +mult = Abs' (\n => + Rec n + (Const Zero) + (Abs $ Abs $ + let f = Var $ There Here in + let m = Var Here in + App plus [ N ~> B) ctx +lte = Abs' (\m => isZero . App minus [ N ~> B) ctx +equal = Abs $ Abs $ + let m = Var $ There Here in + let n = Var Here in + App and [ N ~> N * N) ctx +divmod = Abs $ Abs $ + let n = Var (There Here) in + let d = Var Here in + Rec + n + (App pair [ + App if' + [ Bool +toBool = (== 0) + export True : Term B ctx True = Lit 0 @@ -20,3 +30,19 @@ if' = Abs' (\b => Rec b (Abs $ Const $ Var Here) (Const $ Const $ Abs $ Var Here)) + +export +and : Term (B ~> B ~> B) ctx +and = Abs' (\b => App if' [ B ~> B) ctx +or = Abs' (\b => App if' [ B) ctx +not = Abs' (\b => App if' [ B) ctx +isZero = Id diff --git a/src/Encoded/Fin.idr b/src/Encoded/Fin.idr new file mode 100644 index 0000000..901c612 --- /dev/null +++ b/src/Encoded/Fin.idr @@ -0,0 +1,48 @@ +module Encoded.Fin + +import public Data.Nat + +import Data.Stream +import Encoded.Arith +import Encoded.Pair +import Term.Semantics +import Term.Syntax + +export +Fin : Nat -> Ty +Fin k = N + +oldShow : Nat -> String +oldShow = show + +export +zero : Term (Fin (S k)) ctx +zero = Zero + +export +suc : Term (Fin k ~> Fin (S k)) ctx +suc = Abs' Suc + +export +inject : Term (Fin k ~> Fin (S k)) ctx +inject = Id + +export +absurd : {ty : Ty} -> Term (Fin 0 ~> ty) ctx +absurd = Arb + +export +induct : {ty : Ty} -> Term (Fin (S k) ~> ty ~> (Fin k * ty ~> ty) ~> ty) ctx +induct = rec + +export +forget : Term (Fin k ~> N) ctx +forget = Id + +export +allSem : (k : Nat) -> List (TypeOf (Fin k)) +allSem k = take k nats + +export +divmod' : (k : Nat) -> {auto 0 ok : NonZero k} -> Term (N ~> N * Fin k) ctx +divmod' k = Abs' (\n => App divmod [ Ty -> Ty ty1 * ty2 = B ~> (ty1 <+> ty2) +export +[ShowPair] +{ty1, ty2 : Ty} -> +Show (TypeOf ty1) => +Show (TypeOf ty2) => +Show (TypeOf (ty1 * ty2)) where + show f = fastConcat + [ "(", show (sem prL [<] (f $ sem True [<])) + , ", ", show (sem prR [<] (f $ sem False [<])) + , ")"] + export pair : {ty1, ty2 : Ty} -> Term (ty1 ~> ty2 ~> (ty1 * ty2)) ctx pair = Abs $ Abs $ Abs $ @@ -23,3 +35,32 @@ fst = Abs $ App (prL . Var Here) [ Term ((ty1 * ty2) ~> ty2) ctx snd = Abs $ App (prR . Var Here) [ + Term ((ty1 ~> ty1') ~> (ty2 ~> ty2') ~> ty1 * ty2 ~> ty1' * ty2') ctx +bimap = Abs $ Abs $ Abs $ Abs $ + let f = Var (There $ There $ There Here) in + let g = Var (There $ There Here) in + let x = Var (There $ Here) in + let b = Var Here in + App if' + [ Term ((ty2 ~> ty2') ~> ty1 * ty2 ~> ty1 * ty2') ctx +mapSnd = Abs $ Abs $ + let f = Var (There Here) in + let x = Var Here in + App pair [ Term ((ty1 ~> ty2 ~> ty) ~> ty1 * ty2 ~> ty) ctx +uncurry = Abs $ Abs $ + let f = Var $ There Here in + let p = Var Here in + App f [ Ty -> Ty +ty1 + ty2 = B * (ty1 <+> ty2) + +export +{ty1, ty2 : Ty} -> Show (TypeOf ty1) => Show (TypeOf ty2) => Show (TypeOf (ty1 + ty2)) where + show p = + if toBool (sem fst [<] p) + then fastConcat ["Left (", show (sem (prL . snd) [<] p), ")"] + else fastConcat ["Right (", show (sem (prR . snd) [<] p), ")"] + +export +left : {ty1, ty2 : Ty} -> Term (ty1 ~> (ty1 + ty2)) ctx +left = Abs' (\t => App pair [ Term (ty2 ~> (ty1 + ty2)) ctx +right = Abs' (\t => App pair [ Term ((ty1 + ty2) ~> (ty1 ~> ty) ~> (ty2 ~> ty) ~> ty) ctx +case' = Abs' (\t => + App if' + [ Term ((ty1 ~> ty) ~> (ty2 ~> ty) ~> (ty1 + ty2) ~> ty) ctx +either = Abs $ Abs $ Abs $ + let f = Var $ There $ There Here in + let g = Var $ There Here in + let x = Var Here in + App case' [ NonEmpty (map f xs) +mapNonEmpty IsNonEmpty = IsNonEmpty + +export +Sum : (tys : List Ty) -> {auto 0 ok : NonEmpty tys} -> Ty +Sum = foldr1 (+) + +export +any : + {tys : List Ty} -> + {ty : Ty} -> + {auto 0 ok : NonEmpty tys} -> + All (\ty' => Term (ty' ~> ty) ctx) tys -> + Term (Sum tys ~> ty) ctx +any [f] = f +any (f :: fs@(_ :: _)) = App either [ + {ty : Ty} -> + {auto 0 ok : NonEmpty tys} -> + Elem ty tys -> + Term (ty ~> Sum tys) ctx +tag {tys = [_]} Here = Id +tag {tys = _ :: _ :: _} Here = left +tag {tys = _ :: _ :: _} (There i) = right . tag i diff --git a/src/Encoded/Union.idr b/src/Encoded/Union.idr index 5c3b95c..00b07e7 100644 --- a/src/Encoded/Union.idr +++ b/src/Encoded/Union.idr @@ -2,6 +2,8 @@ module Encoded.Union import Term.Syntax +-- Binary Union ---------------------------------------------------------------- + export (<+>) : Ty -> Ty -> Ty N <+> N = N diff --git a/src/Encoded/Vect.idr b/src/Encoded/Vect.idr new file mode 100644 index 0000000..a427196 --- /dev/null +++ b/src/Encoded/Vect.idr @@ -0,0 +1,66 @@ +module Encoded.Vect + +import Data.String + +import Encoded.Bool +import Encoded.Pair +import Encoded.Fin + +import Term.Semantics +import Term.Syntax + +export +Vect : Nat -> Ty -> Ty +Vect k ty = Fin k ~> ty + +export +[ShowVect] +{k : Nat} -> +Show (TypeOf ty) => +Show (TypeOf (Vect k ty)) where + show f = "[" ++ joinBy ", " (map (show . f) $ allSem k) ++ "]" + +export +nil : {ty : Ty} -> Term (Vect 0 ty) ctx +nil = absurd + +export +cons : {k : Nat} -> {ty : Ty} -> Term (ty ~> Vect k ty ~> Vect (S k) ty) ctx +cons = Abs $ Abs $ Abs $ + let x = Var $ There $ There Here in + let xs = Var $ There Here in + let i = Var Here in + App induct [ ty) ~> Vect k ty) ctx +tabulate = Id + +export +dmap : + {k : Nat} -> + {ty1, ty2 : Ty} -> + Term ((Fin k ~> ty1 ~> ty2) ~> Vect k ty1 ~> Vect k ty2) ctx +dmap = + Abs $ Abs $ Abs $ + let f = Var (There $ There Here) in + let xs = Var (There Here) in + let i = Var Here in + App f [ {ty1, ty2 : Ty} -> Term ((ty1 ~> ty2) ~> Vect k ty1 ~> Vect k ty2) ctx +map = Abs' (\f => App dmap (Const f)) + +export +index : {k : Nat} -> {ty : Ty} -> Term (Vect k ty ~> Fin k ~> ty) ctx +index = Id + +export +foldr : {k : Nat} -> {ty, ty' : Ty} -> Term (ty' ~> (ty ~> ty' ~> ty') ~> Vect k ty ~> ty') ctx +foldr {k = 0} = Abs $ Const $ Const $ Var Here +foldr {k = S k} = Abs $ Abs $ Abs $ + let z = Var (There $ There Here) in + let c = Var (There Here) in + let xs = Var Here in + App c [