Skip to content

Commit

Permalink
moved traces to reduce circular deps in future
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Jul 16, 2024
1 parent f94549a commit a7ad8ea
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 40 deletions.
35 changes: 0 additions & 35 deletions packages/onchain/src/pluts/lib/builtins/ptrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,4 @@ export function ptrace<ReturnT extends TermType>( returnT: ReturnT )
_dbn => IRNative.trace
)
);
}

export const ptraceIfTrue = phoist(
pfn([
delayed( str ),
bool,
], bool)
(( msg, boolean ) =>
pif( bool ).$( boolean )
.then( ptrace( bool ).$( pforce( msg ) ).$( pBool( true ) ) )
.else( pBool( false ) )
)
);

export const ptraceIfFalse = phoist(
pfn([
delayed( str ),
bool,
], bool)
(( msg, boolean ) =>
pif( bool ).$( boolean )
.then( pBool( true ) )
.else( ptrace( bool ).$( pforce( msg ) ).$( pBool( false ) ) )
)
);

export function ptraceError<T extends TermType>( t: T, somemsg?: string )
: TermFn<[ PString ], ToPType<T>>
{
return phoist(
plam( str, t )
( msg => pforce(
ptrace( delayed( t ) ).$( msg ).$( pdelay( perror( t, somemsg ) ) )
) as any )
) as any;
}
3 changes: 2 additions & 1 deletion packages/onchain/src/pluts/lib/std/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from "./stdEq";
export * from "./PMaybe";
export * from "./combinators";
export * from "./UtilityTerms";
export * from "./passert";
export * from "./passert";
export * from "./traces";
28 changes: 24 additions & 4 deletions packages/onchain/src/pluts/lib/std/passert.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { bool, str, unit } from "../../type_system/types";
import { pif } from "../builtins/bool";
import { ptraceError } from "../builtins/ptrace";
import { PBool } from "../../PTypes/PBool";
import { PDelayed } from "../../PTypes/PDelayed";
import { TermFn } from "../../PTypes/PFn/PFn";
import { bool, delayed, str, TermType, unit } from "../../type_system/types";
import { pif, pstrictIf } from "../builtins/bool";
import { pdelay } from "../pdelay";
import { perror } from "../perror";
import { pfn } from "../pfn";
import { pforce } from "../pforce";
import { phoist } from "../phoist";
import { ptraceError } from "./traces";
import { pmakeUnit } from "./unit/pmakeUnit";
import { ToPType } from "../../type_system/ts-pluts-conversion";

export const passert = phoist(
pfn([ bool ], unit)
Expand All @@ -18,4 +24,18 @@ export const passertOrTrace = phoist(
.then( pmakeUnit() )
.else( ptraceError( unit ).$( msg ) )
)
);
);

export const passertAndContinue = <T extends TermType>(
resultType: T
): TermFn<[ PBool, PDelayed<ToPType<T>>], ToPType<T>> => phoist(
pfn([ bool, delayed( resultType ) ], resultType )
(( condition, continuation ) =>
pforce(
pstrictIf( delayed( resultType ) )
.$( condition )
.$( continuation )
.$( pdelay( perror( resultType ) ) )
)
) as any
)
48 changes: 48 additions & 0 deletions packages/onchain/src/pluts/lib/std/traces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { bool, delayed, str, TermType } from "../../type_system/types";
import { pdelay } from "../pdelay";
import { perror } from "../perror";
import { pfn } from "../pfn";
import { plam } from "../plam";
import { pforce } from "../pforce";
import { phoist } from "../phoist";
import { pBool } from "./bool";
import { pif } from "../builtins/bool";
import { ptrace } from "../builtins/ptrace";
import { TermFn } from "../../PTypes/PFn/PFn";
import { PString } from "../../PTypes/PString";
import { ToPType } from "../../type_system/ts-pluts-conversion";

export const ptraceIfTrue = phoist(
pfn([
delayed( str ),
bool,
], bool)
(( msg, boolean ) =>
pif( bool ).$( boolean )
.then( ptrace( bool ).$( pforce( msg ) ).$( pBool( true ) ) )
.else( pBool( false ) )
)
);

export const ptraceIfFalse = phoist(
pfn([
delayed( str ),
bool,
], bool)
(( msg, boolean ) =>
pif( bool ).$( boolean )
.then( pBool( true ) )
.else( ptrace( bool ).$( pforce( msg ) ).$( pBool( false ) ) )
)
);

export function ptraceError<T extends TermType>( t: T, somemsg?: string )
: TermFn<[ PString ], ToPType<T>>
{
return phoist(
plam( str, t )
( msg => pforce(
ptrace( delayed( t ) ).$( msg ).$( pdelay( perror( t, somemsg ) ) )
) as any )
) as any;
}

0 comments on commit a7ad8ea

Please sign in to comment.