|
| 1 | +{-# LANGUAGE AllowAmbiguousTypes #-} |
1 | 2 | {-# LANGUAGE DataKinds #-}
|
2 | 3 | {-# LANGUAGE FlexibleInstances #-}
|
3 | 4 | {-# LANGUAGE MultiParamTypeClasses #-}
|
4 | 5 | {-# LANGUAGE PolyKinds #-}
|
| 6 | +{-# LANGUAGE QuantifiedConstraints #-} |
5 | 7 | {-# LANGUAGE RankNTypes #-}
|
6 | 8 | {-# LANGUAGE StandaloneKindSignatures #-}
|
| 9 | +{-# LANGUAGE TypeOperators #-} |
| 10 | +{-# LANGUAGE UndecidableInstances #-} |
7 | 11 | {-# LANGUAGE UndecidableSuperClasses #-}
|
8 | 12 | {-# OPTIONS_GHC -Wno-orphans #-}
|
9 | 13 | -- | BOILERPLATE needed to support Hasochism.
|
10 | 14 | -- See <https://homepages.inf.ed.ac.uk/slindley/papers/hasochism.pdf>
|
11 | 15 | module AnyProgram.With where
|
12 | 16 |
|
| 17 | +import PlutusCore qualified as PLC |
13 | 18 | import PlutusCore.Data qualified as PLC
|
14 | 19 | import PlutusCore.Pretty as PLC
|
| 20 | +import PlutusIR qualified as PIR |
15 | 21 | import UntypedPlutusCore qualified as UPLC
|
16 | 22 |
|
17 | 23 | import Data.Kind (Constraint)
|
18 |
| -import Flat |
19 | 24 | import Types
|
20 | 25 |
|
21 | 26 | -- for: (typeclass `compose` type)
|
22 | 27 | type ComposeC :: forall a b. (b -> Constraint) -> (a -> b) -> a -> Constraint
|
23 | 28 | class constr (f x) => ComposeC constr f x
|
24 | 29 | instance constr (f x) => ComposeC constr f x
|
25 | 30 |
|
| 31 | +type UnitC :: forall a. a -> Constraint |
| 32 | +class UnitC x |
| 33 | +instance UnitC x |
| 34 | + |
| 35 | +type AndC :: forall a. (a -> Constraint) -> (a -> Constraint) -> a -> Constraint |
| 36 | +class (constr1 x, constr2 x) => AndC constr1 constr2 x |
| 37 | +instance (constr1 x, constr2 x) => AndC constr1 constr2 x |
| 38 | + |
26 | 39 | withN :: forall constr s r
|
27 | 40 | . ( constr (FromName 'Name)
|
28 | 41 | , constr (FromName 'DeBruijn)
|
@@ -52,34 +65,71 @@ withA s r = case s of
|
52 | 65 | SUnit -> r
|
53 | 66 | STxSrcSpans -> r
|
54 | 67 |
|
55 |
| -withFlatL :: forall s r. SLang s -> (Flat (FromLang s) => r) -> r |
56 |
| -withFlatL s r = case s of |
57 |
| - SPir sname sann -> withN @Flat sname $ withNT @Flat sname $ withA @Flat sann r |
58 |
| - SPlc sname sann -> withN @Flat sname $ withNT @Flat sname $ withA @Flat sann r |
59 |
| - SUplc sname sann -> withN @Flat sname $ withN @(ComposeC Flat UPLC.Binder) sname $ |
60 |
| - withA @Flat sann r |
61 |
| - SData -> error "Flat is not available for Data" |
| 68 | +withLangGeneral |
| 69 | + :: forall constrTyName constrBinder constrName constrAnn constr s r. |
| 70 | + ( constrTyName (FromNameTy 'Name) |
| 71 | + , constrTyName (FromNameTy 'DeBruijn) |
| 72 | + , constrTyName (FromNameTy 'NamedDeBruijn) |
| 73 | + , constrBinder (UPLC.Binder UPLC.Name) |
| 74 | + , constrBinder (UPLC.Binder UPLC.DeBruijn) |
| 75 | + , constrBinder (UPLC.Binder UPLC.NamedDeBruijn) |
| 76 | + , constrName (FromName 'Name) |
| 77 | + , constrName (FromName 'DeBruijn) |
| 78 | + , constrName (FromName 'NamedDeBruijn) |
| 79 | + , constrAnn (FromAnn 'Unit) |
| 80 | + , constrAnn (FromAnn 'TxSrcSpans) |
| 81 | + , (forall tyname name ann. (constrTyName tyname, constrName name, constrAnn ann) => |
| 82 | + constr (PIR.Program tyname name UPLC.DefaultUni UPLC.DefaultFun ann)) |
| 83 | + , (forall tyname name ann. (constrTyName tyname, constrName name, constrAnn ann) => |
| 84 | + constr (PLC.Program tyname name UPLC.DefaultUni UPLC.DefaultFun ann)) |
| 85 | + , (forall name ann. (constrBinder (UPLC.Binder name), constrName name, constrAnn ann) => |
| 86 | + constr (UPLC.UnrestrictedProgram name UPLC.DefaultUni UPLC.DefaultFun ann)) |
| 87 | + ) |
| 88 | + => SLang s -> (constr (FromLang s) => r) -> r |
| 89 | +withLangGeneral s r = case s of |
| 90 | + SPir sname sann -> |
| 91 | + withNT @constrTyName sname |
| 92 | + $ withN @constrName sname |
| 93 | + $ withA @constrAnn sann r |
| 94 | + SPlc sname sann -> |
| 95 | + withNT @constrTyName sname |
| 96 | + $ withN @constrName sname |
| 97 | + $ withA @constrAnn sann r |
| 98 | + SUplc sname sann -> |
| 99 | + withN @(ComposeC constrBinder UPLC.Binder) sname |
| 100 | + $ withN @constrName sname |
| 101 | + $ withA @constrAnn sann r |
| 102 | + SData -> error "not implemented yet" |
62 | 103 |
|
63 |
| -withShowL :: forall s r. SLang s -> (Show (FromLang s) => r) -> r |
64 |
| -withShowL s r = case s of |
65 |
| - SPir sname sann -> withN @Show sname $ withNT @Show sname $ withA @Show sann r |
66 |
| - SPlc sname sann -> withN @Show sname $ withNT @Show sname $ withA @Show sann r |
67 |
| - SUplc sname sann -> withN @Show sname $ withN @(ComposeC Show UPLC.Binder) sname $ |
68 |
| - withA @Show sann r |
69 |
| - SData -> r |
| 104 | +withLang |
| 105 | + :: forall constr s r. |
| 106 | + ( constr (FromNameTy 'Name) |
| 107 | + , constr (FromNameTy 'DeBruijn) |
| 108 | + , constr (FromNameTy 'NamedDeBruijn) |
| 109 | + , constr (UPLC.Binder UPLC.Name) |
| 110 | + , constr (UPLC.Binder UPLC.DeBruijn) |
| 111 | + , constr (UPLC.Binder UPLC.NamedDeBruijn) |
| 112 | + , constr (FromName 'Name) |
| 113 | + , constr (FromName 'DeBruijn) |
| 114 | + , constr (FromName 'NamedDeBruijn) |
| 115 | + , constr (FromAnn 'Unit) |
| 116 | + , constr (FromAnn 'TxSrcSpans) |
| 117 | + , (forall tyname name ann. (constr tyname, constr name, constr ann) => |
| 118 | + constr (PIR.Program tyname name UPLC.DefaultUni UPLC.DefaultFun ann)) |
| 119 | + , (forall tyname name ann. (constr tyname, constr name, constr ann) => |
| 120 | + constr (PLC.Program tyname name UPLC.DefaultUni UPLC.DefaultFun ann)) |
| 121 | + , (forall name ann. (constr (UPLC.Binder name), constr name, constr ann) => |
| 122 | + constr (UPLC.UnrestrictedProgram name UPLC.DefaultUni UPLC.DefaultFun ann)) |
| 123 | + ) |
| 124 | + => SLang s -> (constr (FromLang s) => r) -> r |
| 125 | +withLang = withLangGeneral @constr @constr @constr @constr @constr |
70 | 126 |
|
71 |
| -withPrettyPlcL :: forall s r. SLang s -> (PrettyBy PrettyConfigPlc (FromLang s) => r) -> r |
72 |
| -withPrettyPlcL s r = case s of |
73 |
| - SPir sname sann -> withN @PrettyClassic sname $ withN @PrettyReadable sname $ |
74 |
| - withNT @PrettyClassic sname $ withNT @PrettyReadable sname $ |
75 |
| - withA @Pretty sann r |
76 |
| - SPlc sname sann -> withN @PrettyClassic sname $ withN @PrettyReadable sname $ |
77 |
| - withNT @PrettyClassic sname $ withNT @PrettyReadable sname $ |
78 |
| - withA @Pretty sann r |
79 |
| - SUplc sname sann -> withN @PrettyClassic sname $ withN @PrettyReadable sname $ |
80 |
| - withA @Pretty sann r |
81 |
| - SData -> r |
| 127 | +withPrettyPlcL :: forall s r. SLang s -> (PrettyPlc (FromLang s) => r) -> r |
| 128 | +withPrettyPlcL = withLangGeneral |
| 129 | + @(PrettyClassic `AndC` PrettyReadable) |
| 130 | + @UnitC |
| 131 | + @(PrettyClassic `AndC` PrettyReadable) |
| 132 | + @Pretty |
| 133 | + @PrettyPlc |
82 | 134 |
|
83 |
| --- a dummy to make `withPrettyPlcL` work also with `Data` |
84 |
| -instance PrettyBy PrettyConfigPlc PLC.Data where |
85 |
| - prettyBy _ = pretty |
| 135 | +instance PrettyBy PrettyConfigPlc PLC.Data |
0 commit comments