-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
181 additions
and
150 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
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 |
---|---|---|
@@ -1,100 +1,80 @@ | ||
module Agda.Core.TCM where | ||
|
||
|
||
open import Haskell.Prelude hiding ( All; m ) | ||
open import Scope | ||
open import Agda.Core.GlobalScope | ||
open import Agda.Core.Utils | ||
|
||
import Agda.Core.Syntax as Syntax | ||
|
||
module Agda.Core.TCM | ||
{@0 name : Set} | ||
(@0 globals : Globals name) | ||
where | ||
|
||
open import Agda.Core.Reduce globals | ||
TCError = String | ||
{-# COMPILE AGDA2HS TCError #-} | ||
|
||
module Pair where | ||
open import Agda.Primitive | ||
private variable | ||
ℓ ℓ′ : Level | ||
|
||
record Pair (a : Set ℓ) (p : (@0 _ : a) → Set ℓ′) : Set (ℓ ⊔ ℓ′) where | ||
constructor MkPair | ||
field | ||
pfst : a | ||
psnd : p pfst | ||
open Pair public | ||
{-# COMPILE AGDA2HS Pair #-} | ||
record TCM (a : Set) : Set where | ||
constructor MkTCM | ||
field runTCM : Fuel → Either TCError a | ||
open TCM public | ||
{-# COMPILE AGDA2HS TCM #-} | ||
|
||
open Pair | ||
tcmFuel : TCM Fuel | ||
tcmFuel = MkTCM Right | ||
{-# COMPILE AGDA2HS tcmFuel #-} | ||
|
||
pattern _⟨_⟩ a b = MkPair a b | ||
tcError : TCError -> TCM a | ||
tcError = MkTCM ∘ const ∘ Left | ||
{-# COMPILE AGDA2HS tcError #-} | ||
|
||
infix 4 ∃ | ||
∃ : (a : Set) (p : @0 a → Set) → Set | ||
∃ a p = Pair a p | ||
{-# COMPILE AGDA2HS ∃ inline #-} | ||
private | ||
fmapTCM : (a → b) → TCM a → TCM b | ||
fmapTCM f = MkTCM ∘ fmap (fmap f) ∘ runTCM | ||
{-# COMPILE AGDA2HS fmapTCM #-} | ||
|
||
TCError = String | ||
|
||
record TCM (a : Set) : Set where | ||
constructor mkTCM | ||
field | ||
runTCM : Fuel → Either TCError a | ||
pureTCM : a → TCM a | ||
pureTCM = MkTCM ∘ const ∘ Right | ||
{-# COMPILE AGDA2HS pureTCM #-} | ||
|
||
liftA2TCM : (a → b → c) → TCM a → TCM b → TCM c | ||
liftA2TCM g ta tb = MkTCM λ f → g <$> runTCM ta f <*> runTCM tb f | ||
{-# COMPILE AGDA2HS liftA2TCM #-} | ||
|
||
tcmFuel : TCM Fuel | ||
tcmFuel = mkTCM (λ f → Right f) | ||
|
||
fmapTCM : (a → b) → TCM a → TCM b | ||
fmapTCM f (mkTCM runTCM) = mkTCM (fmap (fmap f) runTCM) | ||
|
||
pureTCM : a → TCM a | ||
pureTCM a = mkTCM (pure (pure a)) | ||
|
||
liftA2TCM : (a → b → c) → TCM a → TCM b → TCM c | ||
liftA2TCM f (mkTCM ta) (mkTCM tb) = mkTCM (liftA2Fuel (liftA2Either f) ta tb) | ||
where | ||
liftA2Fuel : {a b c f : Set} → (a → b → c) → (f → a) → (f → b) → (f → c) | ||
liftA2Fuel f a b = f <$> a <*> b | ||
liftA2Either : {a b c e : Set} → (a → b → c) → Either e a → Either e b → Either e c | ||
liftA2Either f a b = f <$> a <*> b | ||
|
||
bindTCM : TCM a → (a → TCM b) → TCM b | ||
bindTCM ma mf = mkTCM (bindTCM' ma mf) | ||
where | ||
bindTCM' : TCM a → (a → TCM b) → Fuel → Either TCError b | ||
bindTCM' (mkTCM ma) mf f with (ma f) | ||
... | Left e = Left e | ||
... | Right v = TCM.runTCM (mf v) f | ||
bindTCM : TCM a → (a → TCM b) → TCM b | ||
bindTCM ma mf = MkTCM λ f → do v ← runTCM ma f ; runTCM (mf v) f | ||
{-# COMPILE AGDA2HS bindTCM #-} | ||
|
||
instance | ||
iFunctorTCM : Functor TCM | ||
Functor.fmap iFunctorTCM = fmapTCM | ||
Functor._<$>_ iFunctorTCM = fmapTCM | ||
Functor._<&>_ iFunctorTCM = λ x f → fmapTCM f x | ||
Functor._<$_ iFunctorTCM = λ x m → fmapTCM (λ b → x {{b}}) m | ||
Functor._$>_ iFunctorTCM = λ m x → fmapTCM (λ b → x {{b}}) m | ||
Functor.void iFunctorTCM = fmapTCM (const tt) | ||
iFunctorTCM .fmap = fmapTCM | ||
iFunctorTCM ._<$>_ = fmapTCM | ||
iFunctorTCM ._<&>_ = λ x f → fmapTCM f x | ||
iFunctorTCM ._<$_ = λ x m → fmapTCM (λ b → x {{b}}) m | ||
iFunctorTCM ._$>_ = λ m x → fmapTCM (λ b → x {{b}}) m | ||
iFunctorTCM .void = fmapTCM (const tt) | ||
{-# COMPILE AGDA2HS iFunctorTCM #-} | ||
|
||
instance | ||
iApplicativeTCM : Applicative TCM | ||
Applicative.pure iApplicativeTCM = pureTCM | ||
Applicative._<*>_ iApplicativeTCM = liftA2TCM id | ||
Applicative.super iApplicativeTCM = iFunctorTCM | ||
Applicative._<*_ iApplicativeTCM = liftA2TCM (λ z _ → z) | ||
Applicative._*>_ iApplicativeTCM = liftA2TCM (λ _ z → z) | ||
iApplicativeTCM .pure = pureTCM | ||
iApplicativeTCM ._<*>_ = liftA2TCM id | ||
iApplicativeTCM ._<*_ = liftA2TCM (λ z _ → z) | ||
iApplicativeTCM ._*>_ = liftA2TCM (λ _ z → z) | ||
{-# COMPILE AGDA2HS iApplicativeTCM #-} | ||
|
||
instance | ||
iMonadTCM : Monad TCM | ||
Monad._>>=_ iMonadTCM = bindTCM | ||
Monad.super iMonadTCM = iApplicativeTCM | ||
Monad.return iMonadTCM = pureTCM | ||
Monad._>>_ iMonadTCM = λ m m₁ → bindTCM m (λ x → m₁ {{x}}) | ||
Monad._=<<_ iMonadTCM = flip bindTCM | ||
iMonadTCM ._>>=_ = bindTCM | ||
iMonadTCM .return = pureTCM | ||
iMonadTCM ._>>_ = λ x y → bindTCM x (λ z → y {{z}}) | ||
iMonadTCM ._=<<_ = flip bindTCM | ||
{-# COMPILE AGDA2HS iMonadTCM #-} | ||
|
||
liftEither : Either TCError a → TCM a | ||
liftEither (Left e) = mkTCM λ f → Left e | ||
liftEither (Right v) = mkTCM λ f → Right v | ||
liftEither (Left e) = MkTCM λ f → Left e | ||
liftEither (Right v) = MkTCM λ f → Right v | ||
|
||
{-# COMPILE AGDA2HS liftEither #-} | ||
|
||
liftMaybe : Maybe a → TCError → TCM a | ||
liftMaybe Nothing e = mkTCM λ f → Left e | ||
liftMaybe (Just x) e = mkTCM λ f → Right x | ||
liftMaybe Nothing e = MkTCM λ f → Left e | ||
liftMaybe (Just x) e = MkTCM λ f → Right x | ||
|
||
tcError : TCError -> TCM a | ||
tcError e = mkTCM λ f → Left e | ||
{-# COMPILE AGDA2HS liftMaybe #-} |
Oops, something went wrong.