Skip to content

Commit

Permalink
isClosedIRTerm case constr
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed May 23, 2024
1 parent 63b24d6 commit 2aadb6f
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 99 deletions.
8 changes: 4 additions & 4 deletions packages/onchain/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/onchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@harmoniclabs/cbor": "^1.3.0",
"@harmoniclabs/plutus-data": "^1.2.4",
"@harmoniclabs/cardano-costmodels-ts": "^1.1.0",
"@harmoniclabs/plutus-machine": "^2.0.0-dev2",
"@harmoniclabs/plutus-machine": "^2.0.0-dev3",
"@harmoniclabs/uplc": "^1.2.4",
"@harmoniclabs/cardano-ledger-ts": "^0.2.0-dev6"
},
Expand Down
20 changes: 20 additions & 0 deletions packages/onchain/src/IR/IRNodes/IRHoisted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { IRLetted } from "./IRLetted";
import { handleLetted } from "../toUPLC/subRoutines/handleLetted";
import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { IRConstr } from "./IRConstr";
import { IRCase } from "./IRCase";


export type HoistedSetEntry = {
Expand Down Expand Up @@ -329,6 +331,24 @@ export function getHoistedTerms( irTerm: IRTerm ): HoistedSetEntry[]
return;
}

if( term instanceof IRConstr )
{
for( let i = 0; i < term.fields.length; i++ )
{
searchIn( term.fields[i] );
}
return;
}
if( term instanceof IRCase )
{
searchIn( term.constrTerm );
for( let i = 0; i < term.continuations.length; i++ )
{
searchIn( term.continuations[i] );
}
return;
}

if( term instanceof IRFunc )
{
searchIn( term.body );
Expand Down
24 changes: 23 additions & 1 deletion packages/onchain/src/IR/IRNodes/IRNative/IRNativeTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,29 @@ export const enum IRNativeTag {
serialiseData = 51,
verifyEcdsaSecp256k1Signature = 52,
verifySchnorrSecp256k1Signature = 53,

// Plutus V3
bls12_381_G1_add = 54,
bls12_381_G1_neg = 55,
bls12_381_G1_scalarMul = 56,
bls12_381_G1_equal = 57,
bls12_381_G1_hashToGroup = 58,
bls12_381_G1_compress = 59,
bls12_381_G1_uncompress = 60,
bls12_381_G2_add = 61,
bls12_381_G2_neg = 62,
bls12_381_G2_scalarMul = 63,
bls12_381_G2_equal = 64,
bls12_381_G2_hashToGroup = 65,
bls12_381_G2_compress = 66,
bls12_381_G2_uncompress = 67,
bls12_381_millerLoop = 68,
bls12_381_mulMlResult = 69,
bls12_381_finalVerify = 70,
keccak_256 = 71,
blake2b_224 = 72,
// bitwise
integerToByteString = 73,
byteStringToInteger = 74,

////////////////////////////////////////////////////////////////////////////////
// -------------------------------------------------------------------------- //
Expand Down
191 changes: 106 additions & 85 deletions packages/onchain/src/IR/IRNodes/IRNative/index.ts

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions packages/onchain/src/IR/toUPLC/_internal/_irToUplc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ export function _irToUplc(
}
if( ir instanceof IRConstr )
{
if( ir.fields.length === 0 )
{
return {
term: new Constr( ir.index, [] ),
max_idx: node_index + 1
};
}

const fields = Array.from( ir.fields )
.slice( 1 )
.reduce(
Expand Down
12 changes: 12 additions & 0 deletions packages/onchain/src/IR/utils/isClosedIRTerm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { IRLetted } from "../IRNodes/IRLetted";
import { IRVar } from "../IRNodes/IRVar";
import { IRForced } from "../IRNodes/IRForced";
import { IRDelayed } from "../IRNodes/IRDelayed";
import { IRConstr } from "../IRNodes/IRConstr";
import { IRCase } from "../IRNodes/IRCase";

function _isClosedIRTerm( term: IRTerm, dbn: number, parent?: IRTerm ): boolean
{
Expand Down Expand Up @@ -37,6 +39,16 @@ function _isClosedIRTerm( term: IRTerm, dbn: number, parent?: IRTerm ): boolean
if( term instanceof IRForced ) return _isClosedIRTerm( term.forced, dbn, term );
if( term instanceof IRDelayed ) return _isClosedIRTerm( term.delayed, dbn, term );

if( term instanceof IRConstr )
{
return Array.from( term.fields ).every( f => _isClosedIRTerm( f, dbn, term ) );
}
if( term instanceof IRCase )
{
return _isClosedIRTerm( term.constrTerm, dbn, term ) &&
Array.from( term.continuations ).every( cont => _isClosedIRTerm( cont, dbn, term ) );
}

// not even an IRTerm
console.log( parent )
throw new Error(
Expand Down
4 changes: 1 addition & 3 deletions packages/onchain/src/pluts/PTypes/PSoP/psop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ export function psop<
return addUtilityForType(thisSopType)(
new Term(
thisSopType,
_dbn => new IRHoisted(
new IRConstr( 0, [] )
)
_dbn => new IRConstr( 0, [] )
)
) as any;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Machine } from "@harmoniclabs/plutus-machine"
import { pif, pstrictIf } from "../bool"
import { int } from "../../../type_system"
import { pBool, pInt, perror, pmatch, psop } from "../../.."
import { defaultV3Costs } from "@harmoniclabs/cardano-costmodels-ts";
import { UPLCDecoder, UPLCEncoder, UPLCProgram, showUPLC } from "@harmoniclabs/uplc";

const SopBool = psop({
True: {},
False: {}
});

describe("builtin bench", () => {

test("ifThenElse", () => {

const machine = new Machine( defaultV3Costs );

const _ifTerm = (
pif( int ).$( pBool( true ) )
.$( 1 )
.$( 2 )
);
const ifTerm = UPLCDecoder.parse(
UPLCEncoder.compile(
new UPLCProgram(
[ 1, 1, 0 ],
_ifTerm.toUPLC( 0 ),
)
).toBuffer().buffer
).body;
const a = machine.eval( ifTerm );

const sopIfTerm = (
pmatch( SopBool.True({}) )
.onTrue( _ => pInt( 1 ) )
.onFalse( _ => pInt( 2 ) )
)
const b = machine.eval( sopIfTerm );

// console.log( showUPLC( ifTerm ) );
// console.log( showUPLC( sopIfTerm.toUPLC() ) );

// console.log(
// a.budgetSpent.toJson(),
// b.budgetSpent.toJson(),
// );
})

})
54 changes: 49 additions & 5 deletions packages/onchain/src/pluts/lib/pmatch/__tests__/pmatch.sop.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Machine } from "@harmoniclabs/plutus-machine";
import { CEKConst, Machine } from "@harmoniclabs/plutus-machine";
import { psop } from "../../../PTypes/PSoP/psop";
import { bs, int } from "../../../type_system";
import { pBs } from "../../std";
Expand All @@ -21,20 +21,64 @@ describe("pmatch( sop )",() => {

describe("AB", () => {

test("get n", () => {
test("n + b.length", () => {

const term = pmatch( AB.A({ n: pInt( 1 ), b: pBs("abcd") }) )
.onA(({ n, b }) => {
return n.add( b.length );
})
.onB(({ b }) => b.length );

console.log( showIR( term.toIR( 0 ) ) );
const result = Machine.evalSimple( term );

expect( result )
.toEqual(
CEKConst.int( 3 )
);

});

test("b.length", () => {

const term = pmatch( AB.B({ b: pBs("abcd") }) )
.onA(({ n, b }) => {
return n.add( b.length );
})
.onB(({ b }) => b.length );

const result = Machine.evalSimple( term );

console.log( result );
})
expect( result )
.toEqual(
CEKConst.int( 2 )
);

});

});

test("3 fields", () => {

const Fs = psop({
Fs: {
a: int,
b: int,
c: int
}
});

const term = pmatch( Fs.Fs({ a: pInt( 1 ), b: pInt( 2 ), c: pInt( 3 ) }) )
.onFs(({ a, b, c }) => {
return a.add( b ).mult( c );
})

const result = Machine.evalSimple( term );

expect( result )
.toEqual(
CEKConst.int( 9 )
);

})

})

0 comments on commit 2aadb6f

Please sign in to comment.