Skip to content

Commit

Permalink
Merge pull request #381 from michaelpj/imp/just-a-few-more-renames
Browse files Browse the repository at this point in the history
PlutusTx: two final renames
  • Loading branch information
michaelpj committed Dec 7, 2018
2 parents f296b1b + 4c5173b commit f2b0fac
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ campaignAddress = Ledger.scriptAddress . contributionScript
contributionScript :: Campaign -> ValidatorScript
contributionScript cmp = ValidatorScript val where
val = Ledger.applyScript mkValidator (Ledger.lifted cmp)
mkValidator = Ledger.fromPlcCode $$(PlutusTx.plutus [|| (\Campaign{..} (act :: CampaignAction) (con :: CampaignActor) (p :: PendingTx ValidatorHash) ->
mkValidator = Ledger.fromCompiledCode $$(PlutusTx.compile [|| (\Campaign{..} (act :: CampaignAction) (con :: CampaignActor) (p :: PendingTx ValidatorHash) ->
let

infixr 3 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mkRedeemerScript word =
in RedeemerScript (Ledger.lifted (ClearString clearWord))

gameValidator :: ValidatorScript
gameValidator = ValidatorScript (Ledger.fromPlcCode $$(PlutusTx.plutus [||
gameValidator = ValidatorScript (Ledger.fromCompiledCode $$(PlutusTx.compile [||
\(ClearString guess) (HashedString actual) (p :: PendingTx ValidatorHash) ->

if $$(P.equalsByteString) actual ($$(P.sha2_256) guess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ validatorScriptHash =
validatorScript :: Vesting -> ValidatorScript
validatorScript v = ValidatorScript val where
val = Ledger.applyScript inner (Ledger.lifted v)
inner = Ledger.fromPlcCode $$(PlutusTx.plutus [|| \Vesting{..} () VestingData{..} (p :: PendingTx ValidatorHash) ->
inner = Ledger.fromCompiledCode $$(PlutusTx.compile [|| \Vesting{..} () VestingData{..} (p :: PendingTx ValidatorHash) ->
let

eqPk :: PubKey -> PubKey -> Bool
Expand Down
24 changes: 12 additions & 12 deletions plutus-tx-plugin/src/Language/PlutusTx/Plugin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-unused-foralls #-}
module Language.PlutusTx.Plugin (
PlcCode,
CompiledCode,
getSerializedPlc,
getSerializedPir,
getPlc,
Expand Down Expand Up @@ -59,20 +59,20 @@ import GHC.TypeLits
import System.IO.Unsafe (unsafePerformIO)

-- | A PLC program.
data PlcCode = PlcCode {
data CompiledCode = CompiledCode {
serializedPlc :: BS.ByteString
, serializedPir :: BS.ByteString
}

-- Note that we do *not* have a TypeablePlc instance, since we don't know what the type is. We could in principle store it after the plugin
-- typechecks the code, but we don't currently.
instance Lift.Lift PlcCode where
instance Lift.Lift CompiledCode where
lift (getPlc -> (PLC.Program () _ body)) = PIR.embedIntoIR <$> PLC.rename body

getSerializedPlc :: PlcCode -> BSL.ByteString
getSerializedPlc :: CompiledCode -> BSL.ByteString
getSerializedPlc = BSL.fromStrict . serializedPlc

getSerializedPir :: PlcCode -> BSL.ByteString
getSerializedPir :: CompiledCode -> BSL.ByteString
getSerializedPir = BSL.fromStrict . serializedPir

{- Note [Deserializing the AST]
Expand All @@ -85,20 +85,20 @@ instance Show ImpossibleDeserialisationFailure where
show (ImpossibleDeserialisationFailure e) = "Failed to deserialise our own program! This is a bug, please report it. Caused by: " ++ show e
instance Exception ImpossibleDeserialisationFailure

getPlc :: PlcCode -> PLC.Program PLC.TyName PLC.Name ()
getPlc :: CompiledCode -> PLC.Program PLC.TyName PLC.Name ()
getPlc wrapper = case deserialiseOrFail $ getSerializedPlc wrapper of
Left e -> throw $ ImpossibleDeserialisationFailure e
Right p -> p

getPir :: PlcCode -> PIR.Program PIR.TyName PIR.Name ()
getPir :: CompiledCode -> PIR.Program PIR.TyName PIR.Name ()
getPir wrapper = case deserialiseOrFail $ getSerializedPir wrapper of
Left e -> throw $ ImpossibleDeserialisationFailure e
Right p -> p

-- | Marks the given expression for conversion to PLC.
plc :: forall (loc::Symbol) a . a -> PlcCode
plc :: forall (loc::Symbol) a . a -> CompiledCode
-- this constructor is only really there to get rid of the unused warning
plc _ = PlcCode mustBeReplaced mustBeReplaced
plc _ = CompiledCode mustBeReplaced mustBeReplaced

data PluginOptions = PluginOptions {
poDoTypecheck :: Bool
Expand Down Expand Up @@ -128,9 +128,9 @@ pluginPass opts guts = getMarkerName >>= \case

{- Note [Hooking in the plugin]
Working out what to process and where to put it is tricky. We are going to turn the result in
to a 'PlcCode', not the Haskell expression we started with!
to a 'CompiledCode', not the Haskell expression we started with!
Currently we look for calls to the 'plc :: a -> PlcCode' function, and we replace the whole application with the
Currently we look for calls to the 'plc :: a -> CompiledCode' function, and we replace the whole application with the
generated code object, which will still be well-typed.
However, if we do this with a polymorphic expression as the argument to 'plc', we have problems
Expand Down Expand Up @@ -287,7 +287,7 @@ convertExpr opts locStr origE resType = do
bsLitPir <- makeByteStringLiteral $ BSL.toStrict $ serialise pirP
bsLitPlc <- makeByteStringLiteral $ BSL.toStrict $ serialise plcP

dcName <- thNameToGhcNameOrFail 'PlcCode
dcName <- thNameToGhcNameOrFail 'CompiledCode
dc <- GHC.lookupDataCon dcName

pure $ GHC.Var (GHC.dataConWrapId dc) `GHC.App` bsLitPlc `GHC.App` bsLitPir
18 changes: 9 additions & 9 deletions plutus-tx-plugin/test/Plugin/IllTyped.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ import Language.PlutusTx.Plugin
-- this module does lots of weird stuff deliberately
{-# ANN module "HLint: ignore" #-}

string :: PlcCode
string :: CompiledCode
string = plc @"string" "test"

listConstruct :: PlcCode
listConstruct :: CompiledCode
listConstruct = plc @"listConstruct" ([]::[Int])

listConstruct2 :: PlcCode
listConstruct2 :: CompiledCode
listConstruct2 = plc @"listConstruct2" ([1]::[Int])

-- It is very difficult to get GHC to make a non-polymorphic redex if you use
-- list literal syntax with integers. But this works.
listConstruct3 :: PlcCode
listConstruct3 :: CompiledCode
listConstruct3 = plc @"listConstruct3" ((1::Int):(2::Int):(3::Int):[])

listMatch :: PlcCode
listMatch :: CompiledCode
listMatch = plc @"listMatch" (\(l::[Int]) -> case l of { (x:_) -> x ; [] -> 0; })

data B a = One a | Two (B (a, a))

ptreeConstruct :: PlcCode
ptreeConstruct :: CompiledCode
ptreeConstruct = plc @"ptreeConstruct" (Two (Two (One ((1,2),(3,4)))) :: B Int)

-- TODO: replace this with 'first' when we have working recursive functions
ptreeMatch :: PlcCode
ptreeMatch :: CompiledCode
ptreeMatch = plc @"ptreeMatch" (\(t::B Int) -> case t of { One a -> a; Two _ -> 2; })

sumDirect :: PlcCode
sumDirect :: CompiledCode
sumDirect = plc @"sumDirect" (
let sum :: [Int] -> Int
sum [] = 0
Expand All @@ -50,7 +50,7 @@ sumDirect = plc @"sumDirect" (
-- This doesn't work: we can't handle things that aren't of plain function type, and fold
-- is a universally quantified function type
{-
sumViaFold :: PlcCode
sumViaFold :: CompiledCode
sumViaFold = plc (let fold :: (a -> b -> a) -> a -> [b] -> a
fold f base l = case l of
[] -> base
Expand Down
92 changes: 46 additions & 46 deletions plutus-tx-plugin/test/Plugin/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import GHC.Generics
-- this module does lots of weird stuff deliberately
{-# ANN module ("HLint: ignore"::String) #-}

instance GetProgram PlcCode where
instance GetProgram CompiledCode where
getProgram = catchAll . getPlc

goldenPir :: String -> PlcCode -> TestNested
goldenPir :: String -> CompiledCode -> TestNested
goldenPir name value = nestedGoldenVsDoc name $ PIR.prettyDef $ getPir value

tests :: TestNested
Expand All @@ -52,10 +52,10 @@ basic = testNested "basic" [
, goldenPlc "monoK" monoK
]

monoId :: PlcCode
monoId :: CompiledCode
monoId = plc @"monoId" (\(x :: Int) -> x)

monoK :: PlcCode
monoK :: CompiledCode
monoK = plc @"monoK" (\(i :: Int) -> \(j :: Int) -> i)

primitives :: TestNested
Expand Down Expand Up @@ -85,53 +85,53 @@ primitives = testNested "primitives" [
, goldenPir "verify" verify
]

int :: PlcCode
int :: CompiledCode
int = plc @"int" (1::Int)

int2 :: PlcCode
int2 :: CompiledCode
int2 = plc @"int2" (2::Int)

bool :: PlcCode
bool :: CompiledCode
bool = plc @"bool" True

andPlc :: PlcCode
andPlc :: CompiledCode
andPlc = plc @"andPlc" (\(x::Bool) (y::Bool) -> if x then (if y then True else False) else False)

tuple :: PlcCode
tuple :: CompiledCode
tuple = plc @"tuple" ((1::Int), (2::Int))

tupleMatch :: PlcCode
tupleMatch :: CompiledCode
tupleMatch = plc @"tupleMatch" (\(x:: (Int, Int)) -> let (a, b) = x in a)

intCompare :: PlcCode
intCompare :: CompiledCode
intCompare = plc @"intCompare" (\(x::Int) (y::Int) -> x < y)

intEq :: PlcCode
intEq :: CompiledCode
intEq = plc @"intEq" (\(x::Int) (y::Int) -> x == y)

-- Has a Void in it
void :: PlcCode
void :: CompiledCode
void = plc @"void" (\(x::Int) (y::Int) -> let a x' y' = case (x', y') of { (True, True) -> True; _ -> False; } in (x == y) `a` (y == x))

intPlus :: PlcCode
intPlus :: CompiledCode
intPlus = plc @"intPlus" (\(x::Int) (y::Int) -> x + y)

intDiv :: PlcCode
intDiv :: CompiledCode
intDiv = plc @"intDiv" (\(x::Int) (y::Int) -> x `div` y)

errorPlc :: PlcCode
errorPlc :: CompiledCode
errorPlc = plc @"errorPlc" (Builtins.error @Int)

ifThenElse :: PlcCode
ifThenElse :: CompiledCode
ifThenElse = plc @"ifThenElse" (\(x::Int) (y::Int) -> if x == y then x else y)

--blocknumPlc :: PlcCode
--blocknumPlc :: CompiledCode
--blocknumPlc = plc @"blocknumPlc" Builtins.blocknum

bytestring :: PlcCode
bytestring :: CompiledCode
bytestring = plc @"bytestring" (\(x::ByteString) -> x)

verify :: PlcCode
verify :: CompiledCode
verify = plc @"verify" (\(x::ByteString) (y::ByteString) (z::ByteString) -> Builtins.verifySignature x y z)

structure :: TestNested
Expand All @@ -140,7 +140,7 @@ structure = testNested "structure" [
]

-- GHC acutually turns this into a lambda for us, try and make one that stays a let
letFun :: PlcCode
letFun :: CompiledCode
letFun = plc @"lefFun" (\(x::Int) (y::Int) -> let f z = x == z in f y)

datat :: TestNested
Expand Down Expand Up @@ -169,44 +169,44 @@ monoData = testNested "monomorphic" [

data MyEnum = Enum1 | Enum2

basicEnum :: PlcCode
basicEnum :: CompiledCode
basicEnum = plc @"basicEnum" (Enum1)

data MyMonoData = Mono1 Int Int | Mono2 Int | Mono3 Int deriving (Generic)

monoDataType :: PlcCode
monoDataType :: CompiledCode
monoDataType = plc @"monoDataType" (\(x :: MyMonoData) -> x)

monoConstructor :: PlcCode
monoConstructor :: CompiledCode
monoConstructor = plc @"monConstructor" (Mono1)

monoConstructed :: PlcCode
monoConstructed :: CompiledCode
monoConstructed = plc @"monoConstructed" (Mono2 1)

monoCase :: PlcCode
monoCase :: CompiledCode
monoCase = plc @"monoCase" (\(x :: MyMonoData) -> case x of { Mono1 _ b -> b; Mono2 a -> a; Mono3 a -> a })

defaultCase :: PlcCode
defaultCase :: CompiledCode
defaultCase = plc @"defaultCase" (\(x :: MyMonoData) -> case x of { Mono3 a -> a ; _ -> 2; })

irrefutableMatch :: PlcCode
irrefutableMatch :: CompiledCode
irrefutableMatch = plc @"irrefutableMatch" (\(x :: MyMonoData) -> case x of { Mono2 a -> a })

atPattern :: PlcCode
atPattern :: CompiledCode
atPattern = plc @"atPattern" (\t@(x::Int, y::Int) -> let fst (a, b) = a in y + fst t)

data MyMonoRecord = MyMonoRecord { mrA :: Int , mrB :: Int} deriving Generic

monoRecord :: PlcCode
monoRecord :: CompiledCode
monoRecord = plc @"monoRecord" (\(x :: MyMonoRecord) -> x)

-- must be compiled with a lazy case
nonValueCase :: PlcCode
nonValueCase :: CompiledCode
nonValueCase = plc @"nonValueCase" (\(x :: MyEnum) -> case x of { Enum1 -> 1::Int ; Enum2 -> Builtins.error (); })

type Synonym = Int

synonym :: PlcCode
synonym :: CompiledCode
synonym = plc @"synonym" (1::Synonym)

polyData :: TestNested
Expand All @@ -218,13 +218,13 @@ polyData = testNested "polymorphic" [

data MyPolyData a b = Poly1 a b | Poly2 a

polyDataType :: PlcCode
polyDataType :: CompiledCode
polyDataType = plc @"polyDataType" (\(x:: MyPolyData Int Int) -> x)

polyConstructed :: PlcCode
polyConstructed :: CompiledCode
polyConstructed = plc @"polyConstructed" (Poly1 (1::Int) (2::Int))

defaultCasePoly :: PlcCode
defaultCasePoly :: CompiledCode
defaultCasePoly = plc @"defaultCasePoly" (\(x :: MyPolyData Int Int) -> case x of { Poly1 a _ -> a ; _ -> 2; })

newtypes :: TestNested
Expand All @@ -241,19 +241,19 @@ newtype MyNewtype = MyNewtype Int

newtype MyNewtype2 = MyNewtype2 MyNewtype

basicNewtype :: PlcCode
basicNewtype :: CompiledCode
basicNewtype = plc @"basicNewtype" (\(x::MyNewtype) -> x)

newtypeMatch :: PlcCode
newtypeMatch :: CompiledCode
newtypeMatch = plc @"newtypeMatch" (\(MyNewtype x) -> x)

newtypeCreate :: PlcCode
newtypeCreate :: CompiledCode
newtypeCreate = plc @"newtypeCreate" (\(x::Int) -> MyNewtype x)

newtypeCreate2 :: PlcCode
newtypeCreate2 :: CompiledCode
newtypeCreate2 = plc @"newtypeCreate2" (MyNewtype 1)

nestedNewtypeMatch :: PlcCode
nestedNewtypeMatch :: CompiledCode
nestedNewtypeMatch = plc @"nestedNewtypeMatch" (\(MyNewtype2 (MyNewtype x)) -> x)

recursiveTypes :: TestNested
Expand Down Expand Up @@ -283,14 +283,14 @@ recursion = testNested "recursiveFunctions" [
, goldenEval "even4" [ evenMutual, plc @"4" (4::Int) ]
]

fib :: PlcCode
fib :: CompiledCode
-- not using case to avoid literal cases
fib = plc @"fib" (
let fib :: Int -> Int
fib n = if n == 0 then 0 else if n == 1 then 1 else fib(n-1) + fib(n-2)
in fib)

evenMutual :: PlcCode
evenMutual :: CompiledCode
evenMutual = plc @"evenMutual" (
let even :: Int -> Bool
even n = if n == 0 then True else odd (n-1)
Expand All @@ -306,16 +306,16 @@ errors = testNested "errors" [
, goldenPlcCatch "recordSelector" recordSelector
]

integer :: PlcCode
integer :: CompiledCode
integer = plc @"integer" (1::Integer)

free :: PlcCode
free :: CompiledCode
free = plc @"free" (True && False)

-- It's little tricky to get something that GHC actually turns into a polymorphic computation! We use our value twice
-- at different types to prevent the obvious specialization.
valueRestriction :: PlcCode
valueRestriction :: CompiledCode
valueRestriction = plc @"valueRestriction" (let { f :: forall a . a; f = Builtins.error (); } in (f @Bool, f @Int))

recordSelector :: PlcCode
recordSelector :: CompiledCode
recordSelector = plc @"recordSelector" (\(x :: MyMonoRecord) -> mrA x)
Loading

0 comments on commit f2b0fac

Please sign in to comment.