Skip to content

Commit

Permalink
fixed cyclic dependency PMaybe
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Jul 16, 2024
1 parent 161c026 commit f94549a
Show file tree
Hide file tree
Showing 19 changed files with 180 additions and 79 deletions.
74 changes: 64 additions & 10 deletions packages/onchain/src/pluts/lib/builtins/bs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { Term } from "../../../Term";
import { fn, bs, bool, int, lam } from "../../../type_system";
import { papp } from "../../papp";
import { PappArg } from "../../pappArg";
import { phoist } from "../../phoist";
import { TermBS, addPByteStringMethods } from "../../std/UtilityTerms/TermBS";
import { TermBool, addPBoolMethods } from "../../std/UtilityTerms/TermBool";
import { TermInt, addPIntMethods } from "../../std/UtilityTerms/TermInt";
import { addApplications } from "../addApplications";
import { pfn } from "../../pfn";
import { _pflipIR } from "../_pflipIR";
import { defineReadOnlyProperty } from "@harmoniclabs/obj-utils";
import { IRNative } from "../../../../IR/IRNodes/IRNative";
import { IRHoisted } from "../../../../IR/IRNodes/IRHoisted";
import { IRApp } from "../../../../IR/IRNodes/IRApp";
import { IRConst } from "../../../../IR/IRNodes/IRConst";
import { _ir_apps } from "../../../../IR/tree_utils/_ir_apps";
import { IRVar } from "../../../../IR/IRNodes/IRVar";
import { IRFunc } from "../../../../IR/IRNodes/IRFunc";


export type ByteStrBinOPToBS = Term< PLam< PByteString, PLam< PByteString, PByteString>>>
Expand Down Expand Up @@ -245,18 +246,20 @@ export const plessBs = byteStrBinOpToBool( IRNative.lessThanByteString );
export const plessEqBs = byteStrBinOpToBool( IRNative.lessThanEqualsByteString );

export const pgreaterBS: ByteStrBinOPToBool =
phoist(
pfn([ bs, bs ], bool )(
( a: Term<PByteString>, b: Term<PByteString> ): TermBool => plessBs.$( b ).$( a )
addApplications<[ PByteString, PByteString ], PBool>(
new Term(
fn([ bs, bs ], bool),
_dbn => IRNative._gtBS
)
) as any;
);

export const pgreaterEqBS: ByteStrBinOPToBool =
phoist(
pfn([ bs, bs ], bool )(
( a: Term<PByteString>, b: Term<PByteString> ): TermBool => plessEqBs.$( b ).$( a )
addApplications<[ PByteString, PByteString ], PBool>(
new Term(
fn([ bs, bs ], bool),
_dbn => IRNative._gtEqBS
)
) as any;
);

export const psha2_256: TermFn<[ PByteString ], PByteString> =
addApplications<[ PByteString ], PByteString>(
Expand Down Expand Up @@ -415,4 +418,55 @@ export const pdecodeIntLE: TermFn<[ PByteString ], PInt> =
)
)
)
);


export const subByteString =
addApplications<[ PByteString, PInt, PInt ], PByteString>(
new Term(
fn([
bs,
int,
int
], bs),
_dbn => new IRHoisted(
new IRFunc(
// (( term, fromInclusive , ofLength ): TermBS =>
3,
_ir_apps(
IRNative.sliceByteString,
new IRVar( 1 ), // fromInclusive
new IRVar( 0 ), // ofLenght
new IRVar( 2 ), // term
)
)
)
)
);

export const jsLikeSlice =
addApplications<[ PByteString, PInt, PInt ], PByteString>(
new Term(
fn([
bs,
int,
int
], bs),
_dbn => new IRHoisted(
new IRFunc(
// (( term, fromInclusive , toExclusive ): TermBS =>
3,
_ir_apps(
IRNative.sliceByteString,
new IRVar( 1 ), // fromInclusive
_ir_apps(
IRNative.subtractInteger,
new IRVar( 0 ), // toExclusive
new IRVar( 1 ), // fromInclusive
), // ofLenght
new IRVar( 2 ), // term
)
)
)
)
);
4 changes: 2 additions & 2 deletions packages/onchain/src/pluts/lib/builtins/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { papp } from "../../papp";
import { PappArg } from "../../pappArg";
import { pdelay } from "../../pdelay";
import { pforce_minimal } from "../../pforce/minimal";
import { TermBS, addPByteStringMethods } from "../../std/UtilityTerms/TermBS";
import { TermBS } from "../../std/UtilityTerms/TermBS";
import { addApplications } from "../addApplications";
import { IRNative } from "../../../../IR/IRNodes/IRNative";
import { IRHoisted } from "../../../../IR/IRNodes/IRHoisted";
Expand Down Expand Up @@ -292,7 +292,7 @@ export const punBData: Term<PLam<PData, PByteString>>
unBData,
"$",
( dataBS: Term<PData> ): TermBS =>
addPByteStringMethods( papp( unBData, dataBS ) )
papp( unBData, dataBS )
);
})()

Expand Down
7 changes: 3 additions & 4 deletions packages/onchain/src/pluts/lib/builtins/pair/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { unwrapAsData } from "../../../type_system/tyArgs";
import { UtilityTermOf } from "../../std/UtilityTerms/addUtilityForType";
import { papp } from "../../papp";
import { punsafeConvertType } from "../../punsafeConvertType";
import { fromData } from "../../std";
import { _pfromData } from "../../std/data/conversion/fromData_minimal";
import { _fromData, _pfromData } from "../../std/data/conversion/fromData_minimal";


export function pfstPair<A extends TermType, B extends TermType>( fstType: A, sndType: B )
Expand Down Expand Up @@ -36,7 +35,7 @@ export function pfstPair<A extends TermType, B extends TermType>( fstType: A, sn
)
{
return punsafeConvertType(
fromData( outT )(
_fromData( outT )(
papp(
punsafeConvertType( bnTerm, lam( pair( data, data ), data ) ),
punsafeConvertType( _pair, pair( data, data ) )
Expand Down Expand Up @@ -78,7 +77,7 @@ export function psndPair<A extends TermType, B extends TermType>( fstType: A, sn
)
{
return punsafeConvertType(
fromData( outT )(
_fromData( outT )(
papp(
punsafeConvertType( bnTerm, lam( pair( data, data ), data ) ),
punsafeConvertType( _pair, pair( data, data ) )
Expand Down
4 changes: 2 additions & 2 deletions packages/onchain/src/pluts/lib/plam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { Term } from "../Term";
import { includesDynamicPairs } from "../type_system/includesDynamicPairs";
import { ToPType } from "../type_system/ts-pluts-conversion";
import { TermType, lam } from "../type_system/types";
import { UtilityTermOf, addUtilityForType } from "./std/UtilityTerms/addUtilityForType";
import { getCallStackAt } from "../../utils/getCallStackAt";
import { PappResult, papp } from "./papp";
import { IRVar } from "../../IR/IRNodes/IRVar";
import { IRFunc } from "../../IR/IRNodes/IRFunc";
import { getCallStackAt } from "../../utils/getCallStackAt";
import { UtilityTermOf, addUtilityForType } from "./std/UtilityTerms/addUtilityForType";


export function plam<A extends TermType, B extends TermType >( inputType: A, outputType: B )
Expand Down
26 changes: 14 additions & 12 deletions packages/onchain/src/pluts/lib/std/PMaybe/PMaybe.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import { pfn } from "../../pfn";
import { PDataRepresentable } from "../../../PType/PDataRepresentable";
import { PStruct, pstruct } from "../../../PTypes/PStruct/pstruct";
import { StructT, TermType, FromPType } from "../../../type_system";
import { StructT, TermType, FromPType, ToPType } from "../../../type_system";
import { _fromData } from "../data/conversion/fromData_minimal";

/*
TODO:
need to fix circular dependencies before implementing PMaybe methods
*/
import { TermFn } from "../../../PTypes/PFn/PFn";
import { pmatch } from "../../pmatch/pmatch";

export type MaybeT<T extends TermType> = StructT<{
Just: { val: T },
Nothing: {}
}/*, {
}, {
unwrap: TermFn<[ PMaybeRawT<ToPType<T>> ], ToPType<T>>,
default: TermFn<[ PMaybeRawT<ToPType<T>>, ToPType<T> ], ToPType<T>>,
}*/>
}>

type PMaybeRawT<PTy extends PDataRepresentable> = PStruct<{
Just: { val: FromPType<PTy> },
Nothing: {}
}, {}>

export type PMaybeT<PTy extends PDataRepresentable> = PStruct<{
Just: { val: FromPType<PTy> },
Nothing: {}
}, any/*{
}, {
unwrap: TermFn<[ PMaybeRawT<PTy> ], PTy>,
default: TermFn<[ PMaybeRawT<PTy>, PTy ], PTy>,
}*/>
}>

export function PMaybe<T extends TermType>(tyArg: T)
{
return pstruct({
Just: { val: tyArg },
Nothing: {}
}/*, self_t => {
}, self_t => {

return {
unwrap: pfn([ self_t ], tyArg)
Expand All @@ -42,5 +44,5 @@ export function PMaybe<T extends TermType>(tyArg: T)
.onNothing(_ => defaultValue )
)
};
}*/);
});
}
61 changes: 61 additions & 0 deletions packages/onchain/src/pluts/lib/std/PMaybe/__tests__/PMaybe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { CEKError, Machine } from "@harmoniclabs/plutus-machine";
import { PMaybe } from "..";
import { int } from "../../../../type_system/types";
import { pDataI } from "../../data/pData";
import { pInt } from "../../int/pInt";
import { perror } from "../../../perror";

describe("PMaybe", () => {

const PMaybeInt = PMaybe( int );
const just = PMaybeInt.Just({ val: pDataI( 1 ) });
const nothing = PMaybeInt.Nothing({});

test("unwrap just", () => {

const res = Machine.evalSimple(
just.unwrap
);

const expected = Machine.evalSimple( pInt( 1 ) );

expect( res ).toEqual( expected );

});

test("unwrap nothing", () => {

const res = Machine.evalSimple(
nothing.unwrap
);

// const expected = Machine.evalSimple( perror( int ) );

expect( res instanceof CEKError ).toEqual( true );

});

test("default just", () => {

const res = Machine.evalSimple(
just.default( 0 )
);

const expected = Machine.evalSimple( pInt( 1 ) );

expect( res ).toEqual( expected );

});

test("default nothing", () => {

const res = Machine.evalSimple(
nothing.default( 0 )
);

const expected = Machine.evalSimple( pInt( 0 ) );

expect( res ).toEqual( expected );

});
})
28 changes: 1 addition & 27 deletions packages/onchain/src/pluts/lib/std/UtilityTerms/TermBS.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { definePropertyIfNotPresent, defineReadOnlyProperty } from "@harmoniclabs/obj-utils"
import { PByteString, TermFn, PInt, PBool } from "../../../PTypes"
import { Term } from "../../../Term"
import { bs, int } from "../../../type_system/types"
import { flippedCons, pappendBs, pconsBs, peqBs, pgreaterBS, pgreaterEqBS, pindexBs, plengthBs, plessBs, plessEqBs, psliceBs } from "../../builtins/bs"
import { psub } from "../../builtins/int/psub"
import { flippedCons, jsLikeSlice, pappendBs, pconsBs, peqBs, pgreaterBS, pgreaterEqBS, pindexBs, plengthBs, plessBs, plessEqBs, psliceBs, subByteString } from "../../builtins/bs"
import { pdecodeUtf8 } from "../../builtins/str"
import { PappArg } from "../../pappArg"
import { pfn } from "../../pfn"
import { phoist } from "../../phoist"
import { plet } from "../../plet"
import { TermBool } from "./TermBool"
import { TermInt } from "./TermInt"
Expand Down Expand Up @@ -57,28 +53,6 @@ export type TermBS = Term<PByteString> & {

}

const subByteString = phoist(
pfn([
bs,
int,
int
], bs)
(( term, fromInclusive , ofLength ): TermBS =>
psliceBs.$( fromInclusive ).$( ofLength ).$( term )
)
)

const jsLikeSlice = phoist(
pfn([
bs,
int,
int
], bs)
(( term, fromInclusive , toExclusive ): TermBS =>
psliceBs.$( fromInclusive ).$( psub.$( toExclusive ).$( fromInclusive ) ).$( term )
)
);

const getterOnly = {
set: () => {},
configurable: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Term } from "../../../Term";
import { FromPType, isWellFormedType, typeExtends, unwrapAlias } from "../../../type_system";
import { tyVar, pair, TermType, PrimType, PairT } from "../../../type_system/types";
import { UtilityTermOf } from "./addUtilityForType";
import { pfstPair, psndPair } from "../../builtins";
import { pfstPair, psndPair } from "../../builtins/pair";
import { plet } from "../../plet";
import { PappArg } from "../../pappArg";
import { TermBool } from "./TermBool";
Expand Down
2 changes: 1 addition & 1 deletion packages/onchain/src/pluts/lib/std/bool/pBoolToData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataConstr } from "@harmoniclabs/plutus-data";
import { asData, bool } from "../../../type_system/types";
import { pif } from "../../builtins";
import { pif } from "../../builtins/bool";
import { phoist } from "../../phoist";
import { plam } from "../../plam";
import { _punsafeConvertType } from "../../punsafeConvertType/minimal";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { phoist } from "../../../phoist";
import { plam } from "../../../plam";
import { punsafeConvertType } from "../../../punsafeConvertType";
import { pBoolFromData } from "../../bool/pBoolFromData";
import { pUnitFromData } from "../../unit";
import { pUnitFromData } from "../../unit/pUnitFromData";
import { _papp, _pcompose } from "./minimal_common";
import { getElemsT, getFstT, getSndT } from "../../../../type_system/tyArgs";
import { _pmap } from "../../list/pmap/minimal";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import { phoist } from "../../../phoist";
import { plam } from "../../../plam";
import { punsafeConvertType } from "../../../punsafeConvertType";
import { pBoolToData } from "../../bool/pBoolToData";
import { pList, pmap } from "../../list";
import { pData } from "../pData";
import { _papp, _pcompose } from "./minimal_common";
import { getElemsT, getFstT, getSndT } from "../../../../type_system/tyArgs";
import { pfstPair, psndPair } from "../../../builtins";
import { _punsafeConvertType } from "../../../punsafeConvertType/minimal";
import { IRNative } from "../../../../../IR/IRNodes/IRNative";
import { PType } from "../../../../PType";
import { DataConstr } from "@harmoniclabs/plutus-data";
import { pList } from "../../list/const";
import { _pmap } from "../../list/pmap/minimal";
import { pfstPair, psndPair } from "../../../builtins/pair";

const pIntToData =new Term<PLam<PInt, PAsData<PInt>>>(
lam( int, asData( int ) ) as any,
Expand Down Expand Up @@ -174,7 +175,7 @@ export function _toData<T extends TermType>( t: T ): ( term: Term<ToPType<T>> )
return (( term: Term<any> ) => {
const theTerm = _papp(
pMapToData( fstT, sndT ) as any,
pmap( elemsT, pair( asData( fstT ) , asData( sndT ) ) )
_pmap( elemsT, pair( asData( fstT ) , asData( sndT ) ) )
.$(
((_pair: any) => {

Expand Down Expand Up @@ -215,7 +216,7 @@ export function _toData<T extends TermType>( t: T ): ( term: Term<ToPType<T>> )
return (( term: Term<any> ) => {
const theTerm = _papp(
pListToData( asData( elemsT ) ),
pmap( elemsT, asData(elemsT) )
_pmap( elemsT, asData(elemsT) )
.$( _ptoData( elemsT ) )
.$( term )
) as any;
Expand Down
Loading

0 comments on commit f94549a

Please sign in to comment.