@harmoniclabs/pebble 0.1.0-dev6 → 0.1.0-dev8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/IR/IRNodes/IRConst.js +1 -1
  2. package/dist/IR/IRNodes/IRNative/IRNativeTag.d.ts +6 -3
  3. package/dist/IR/IRNodes/IRNative/IRNativeTag.js +12 -4
  4. package/dist/IR/IRNodes/IRNative/index.d.ts +5 -2
  5. package/dist/IR/IRNodes/IRNative/index.js +6 -2
  6. package/dist/IR/toUPLC/CompilerOptions.d.ts +6 -1
  7. package/dist/IR/toUPLC/CompilerOptions.js +11 -1
  8. package/dist/IR/toUPLC/compileIRToUPLC.js +34 -35
  9. package/dist/IR/toUPLC/ctx/ToUplcCtx.js +2 -10
  10. package/dist/IR/toUPLC/subRoutines/handleLetted/groupByScope.js +0 -2
  11. package/dist/IR/toUPLC/subRoutines/handleLetted/index.js +69 -3
  12. package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.d.ts +3 -1
  13. package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.js +42 -10
  14. package/dist/IR/toUPLC/subRoutines/rewriteNativesAppliedToConstantsAndReturnRoot.js +292 -17
  15. package/dist/IR/utils/isClosedIRTerm.js +1 -1
  16. package/dist/IR/utils/positiveIntAsBytes.js +1 -5
  17. package/dist/compiler/AstCompiler/AstCompiler.d.ts +3 -0
  18. package/dist/compiler/AstCompiler/AstCompiler.js +66 -3
  19. package/dist/compiler/AstCompiler/internal/_deriveContractBody/_deriveContractBody.js +1 -1
  20. package/dist/compiler/AstCompiler/internal/exprs/_compileExpr.js +4 -0
  21. package/dist/compiler/AstCompiler/internal/exprs/_compileFuncExpr.js +13 -4
  22. package/dist/compiler/AstCompiler/internal/exprs/_compileTypeConversionExpr.js +0 -1
  23. package/dist/compiler/AstCompiler/internal/exprs/_compileVarAccessExpr.js +3 -1
  24. package/dist/compiler/AstCompiler/internal/statements/_compileAssignmentStmt.js +7 -7
  25. package/dist/compiler/AstCompiler/internal/statements/_compileVarStmt.d.ts +2 -1
  26. package/dist/compiler/AstCompiler/internal/statements/_compileVarStmt.js +2 -1
  27. package/dist/compiler/AstCompiler/scope/AstScope.d.ts +1 -0
  28. package/dist/compiler/AstCompiler/scope/AstScope.js +5 -0
  29. package/dist/compiler/Compiler.d.ts +8 -1
  30. package/dist/compiler/Compiler.js +38 -1
  31. package/dist/compiler/TirCompiler/compileTirProgram.js +2 -1
  32. package/dist/compiler/TirCompiler/expressify/ExpressifyCtx.d.ts +4 -2
  33. package/dist/compiler/TirCompiler/expressify/ExpressifyCtx.js +42 -16
  34. package/dist/compiler/TirCompiler/expressify/determineReassignedVariablesAndReturn.js +2 -0
  35. package/dist/compiler/TirCompiler/expressify/expressify.js +26 -9
  36. package/dist/compiler/TirCompiler/expressify/expressifyForStmt.js +23 -7
  37. package/dist/compiler/TirCompiler/expressify/expressifyVarAssignmentStmt.d.ts +1 -2
  38. package/dist/compiler/TirCompiler/expressify/expressifyVarAssignmentStmt.js +15 -8
  39. package/dist/compiler/TirCompiler/expressify/expressifyVarDecl.js +6 -9
  40. package/dist/compiler/TirCompiler/expressify/expressifyVars.js +51 -16
  41. package/dist/compiler/TirCompiler/expressify/flattenSopNamedDeconstructInplace_addTopDestructToCtx_getNestedDeconstruct.js +1 -0
  42. package/dist/compiler/internalVar.d.ts +1 -1
  43. package/dist/compiler/internalVar.js +31 -24
  44. package/dist/compiler/io/CompilerIoApi.d.ts +1 -1
  45. package/dist/compiler/tir/expressions/TirCallExpr.d.ts +1 -1
  46. package/dist/compiler/tir/expressions/TirCallExpr.js +1 -1
  47. package/dist/compiler/tir/expressions/TirCaseExpr.js +2 -0
  48. package/dist/compiler/tir/expressions/TirFromDataExpr.js +2 -2
  49. package/dist/compiler/tir/expressions/TirFuncExpr.d.ts +1 -1
  50. package/dist/compiler/tir/expressions/TirFuncExpr.js +1 -1
  51. package/dist/compiler/tir/expressions/TirLettedExpr.d.ts +2 -2
  52. package/dist/compiler/tir/expressions/TirLettedExpr.js +4 -6
  53. package/dist/compiler/tir/expressions/TirNativeFunc.d.ts +3 -2
  54. package/dist/compiler/tir/expressions/TirNativeFunc.js +33 -12
  55. package/dist/compiler/tir/expressions/TirPropAccessExpr.js +1 -2
  56. package/dist/compiler/tir/expressions/TirVariableAccessExpr.d.ts +0 -1
  57. package/dist/compiler/tir/expressions/TirVariableAccessExpr.js +9 -5
  58. package/dist/compiler/tir/expressions/ToIRTermCtx.d.ts +3 -0
  59. package/dist/compiler/tir/expressions/ToIRTermCtx.js +8 -1
  60. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.d.ts +1 -0
  61. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.js +6 -1
  62. package/dist/compiler/tir/program/TypedProgram.d.ts +2 -2
  63. package/dist/compiler/tir/program/stdScope/stdScope.js +9 -23
  64. package/dist/compiler/tir/statements/TirIfStmt.d.ts +0 -1
  65. package/dist/compiler/tir/statements/TirIfStmt.js +0 -3
  66. package/dist/parser/Parser.js +18 -2
  67. package/dist/parser/Precedence.js +1 -0
  68. package/dist/utils/BitUtils/index.d.ts +1 -1
  69. package/dist/utils/BitUtils/index.js +1 -1
  70. package/dist/utils/IsSingleKey.d.ts +1 -1
  71. package/dist/utils/UPLCFlatUtils/index.d.ts +1 -1
  72. package/dist/utils/UPLCFlatUtils/index.js +2 -2
  73. package/dist/utils/array/keepSortedArrInplace.js +1 -0
  74. package/package.json +7 -6
  75. package/dist/compiler/TirCompiler/internal/_compileHoistedDeps.d.ts +0 -5
  76. package/dist/compiler/TirCompiler/internal/_compileHoistedDeps.js +0 -39
@@ -2,7 +2,6 @@ import { Pair } from "@harmoniclabs/pair";
2
2
  import { isData, dataToCbor } from "@harmoniclabs/plutus-data";
3
3
  import { fromUtf8, toHex } from "@harmoniclabs/uint8array-utils";
4
4
  import { BasePlutsError } from "../../utils/BasePlutsError.js";
5
- import UPLCFlatUtils from "../../utils/UPLCFlatUtils/index.js";
6
5
  import { concatUint8Arr } from "../utils/concatUint8Arr.js";
7
6
  import { positiveBigIntAsBytes } from "../utils/positiveIntAsBytes.js";
8
7
  import { isIRParentTerm } from "../utils/isIRParentTerm.js";
@@ -27,6 +26,7 @@ import { TirStringT } from "../../compiler/tir/types/TirNativeType/native/string
27
26
  import { TirVoidT } from "../../compiler/tir/types/TirNativeType/native/void.js";
28
27
  import { hashIrData, isIRHash } from "../IRHash.js";
29
28
  import { ByteString } from "@harmoniclabs/bytestring";
29
+ import { UPLCFlatUtils } from "../../utils/UPLCFlatUtils/index.js";
30
30
  export function isIRConstPair(value) {
31
31
  return (value instanceof Pair &&
32
32
  isIRConstValue(value.fst) &&
@@ -96,11 +96,10 @@ export declare enum IRNativeTag {
96
96
  _dropList = -4,
97
97
  _foldr = -6,
98
98
  _foldl = -7,
99
- _mkFindDataOptional = -8,
100
99
  _length = -9,
101
100
  _some = -10,
102
101
  _every = -11,
103
- _mkFilter = -12,
102
+ _filter = -12,
104
103
  _id = -15,
105
104
  _not = -16,
106
105
  _strictAnd = -17,
@@ -131,6 +130,10 @@ export declare enum IRNativeTag {
131
130
  _exponentiateInteger = -40,
132
131
  _amountOfValue = -41,
133
132
  _isZero = -42,
134
- _sortedValueLovelaces = -43
133
+ _sortedValueLovelaces = -43,
134
+ _getCredentialsHash = -44,
135
+ _findSopOptional = -45,
136
+ _increment = -46,
137
+ _decrement = -47
135
138
  }
136
139
  export declare function nativeTagToString(nativeTag: IRNativeTag): string;
@@ -116,11 +116,11 @@ export var IRNativeTag;
116
116
  IRNativeTag[IRNativeTag["_dropList"] = -4] = "_dropList";
117
117
  IRNativeTag[IRNativeTag["_foldr"] = -6] = "_foldr";
118
118
  IRNativeTag[IRNativeTag["_foldl"] = -7] = "_foldl";
119
- IRNativeTag[IRNativeTag["_mkFindDataOptional"] = -8] = "_mkFindDataOptional";
119
+ // _mkFindDataOptional = -8,
120
120
  IRNativeTag[IRNativeTag["_length"] = -9] = "_length";
121
121
  IRNativeTag[IRNativeTag["_some"] = -10] = "_some";
122
122
  IRNativeTag[IRNativeTag["_every"] = -11] = "_every";
123
- IRNativeTag[IRNativeTag["_mkFilter"] = -12] = "_mkFilter";
123
+ IRNativeTag[IRNativeTag["_filter"] = -12] = "_filter";
124
124
  // _fstPair = -13,
125
125
  // _sndPair = -14,
126
126
  IRNativeTag[IRNativeTag["_id"] = -15] = "_id";
@@ -154,6 +154,10 @@ export var IRNativeTag;
154
154
  IRNativeTag[IRNativeTag["_amountOfValue"] = -41] = "_amountOfValue";
155
155
  IRNativeTag[IRNativeTag["_isZero"] = -42] = "_isZero";
156
156
  IRNativeTag[IRNativeTag["_sortedValueLovelaces"] = -43] = "_sortedValueLovelaces";
157
+ IRNativeTag[IRNativeTag["_getCredentialsHash"] = -44] = "_getCredentialsHash";
158
+ IRNativeTag[IRNativeTag["_findSopOptional"] = -45] = "_findSopOptional";
159
+ IRNativeTag[IRNativeTag["_increment"] = -46] = "_increment";
160
+ IRNativeTag[IRNativeTag["_decrement"] = -47] = "_decrement";
157
161
  })(IRNativeTag || (IRNativeTag = {}));
158
162
  Object.freeze(IRNativeTag);
159
163
  export function nativeTagToString(nativeTag) {
@@ -163,11 +167,12 @@ export function nativeTagToString(nativeTag) {
163
167
  case IRNativeTag._dropList: return "dropList";
164
168
  case IRNativeTag._foldr: return "foldr";
165
169
  case IRNativeTag._foldl: return "foldl";
166
- case IRNativeTag._mkFindDataOptional: return "mkFind";
170
+ // case IRNativeTag._mkFindDataOptional : return "mkFind";
171
+ case IRNativeTag._findSopOptional: return "findSopOptional";
167
172
  case IRNativeTag._length: return "length";
168
173
  case IRNativeTag._some: return "some";
169
174
  case IRNativeTag._every: return "every";
170
- case IRNativeTag._mkFilter: return "mkFilter";
175
+ case IRNativeTag._filter: return "mkFilter";
171
176
  // case IRNativeTag._fstPair : return "fstPair";
172
177
  // case IRNativeTag._sndPair : return "sndPair";
173
178
  case IRNativeTag._id: return "id";
@@ -195,6 +200,9 @@ export function nativeTagToString(nativeTag) {
195
200
  case IRNativeTag._amountOfValue: return "amountOfValue";
196
201
  case IRNativeTag._isZero: return "isZero";
197
202
  case IRNativeTag._sortedValueLovelaces: return "sortedValueLovelaces";
203
+ case IRNativeTag._getCredentialsHash: return "getCredentialHash";
204
+ case IRNativeTag._increment: return "increment";
205
+ case IRNativeTag._decrement: return "decrement";
198
206
  default: return "";
199
207
  }
200
208
  }
@@ -131,11 +131,10 @@ export declare class IRNative implements IIRTerm, Cloneable<IRNative>, IIRParent
131
131
  static get _dropList(): IRNative;
132
132
  static get _foldr(): IRNative;
133
133
  static get _foldl(): IRNative;
134
- static get _mkFindDataOptional(): IRNative;
135
134
  static get _length(): IRNative;
136
135
  static get _some(): IRNative;
137
136
  static get _every(): IRNative;
138
- static get _mkFilter(): IRNative;
137
+ static get _filter(): IRNative;
139
138
  static get _id(): IRNative;
140
139
  static get _not(): IRNative;
141
140
  static get _strictAnd(): IRNative;
@@ -161,6 +160,10 @@ export declare class IRNative implements IIRTerm, Cloneable<IRNative>, IIRParent
161
160
  static get _exponentiateInteger(): IRNative;
162
161
  static get _amountOfValue(): IRNative;
163
162
  static get _sortedValueLovelaces(): IRNative;
163
+ static get _getCredentialsHash(): IRNative;
164
+ static get _increment(): IRNative;
165
+ static get _decrement(): IRNative;
166
+ static get _isZero(): IRNative;
164
167
  static equals(type: TirType): IRTerm;
165
168
  static equalListOf(type: TirType): IRHoisted;
166
169
  }
@@ -184,11 +184,11 @@ export class IRNative {
184
184
  static get _dropList() { return new IRNative(IRNativeTag._dropList); }
185
185
  static get _foldr() { return new IRNative(IRNativeTag._foldr); }
186
186
  static get _foldl() { return new IRNative(IRNativeTag._foldl); }
187
- static get _mkFindDataOptional() { return new IRNative(IRNativeTag._mkFindDataOptional); }
187
+ // static get _mkFindDataOptional() { return new IRNative( IRNativeTag._mkFindDataOptional ); }
188
188
  static get _length() { return new IRNative(IRNativeTag._length); }
189
189
  static get _some() { return new IRNative(IRNativeTag._some); }
190
190
  static get _every() { return new IRNative(IRNativeTag._every); }
191
- static get _mkFilter() { return new IRNative(IRNativeTag._mkFilter); }
191
+ static get _filter() { return new IRNative(IRNativeTag._filter); }
192
192
  // static get _fstPair() { return new IRNative( IRNativeTag._fstPair ); }
193
193
  // static get _sndPair() { return new IRNative( IRNativeTag._sndPair ); }
194
194
  static get _id() { return new IRNative(IRNativeTag._id); }
@@ -216,6 +216,10 @@ export class IRNative {
216
216
  static get _exponentiateInteger() { return new IRNative(IRNativeTag._exponentiateInteger); }
217
217
  static get _amountOfValue() { return new IRNative(IRNativeTag._amountOfValue); }
218
218
  static get _sortedValueLovelaces() { return new IRNative(IRNativeTag._sortedValueLovelaces); }
219
+ static get _getCredentialsHash() { return new IRNative(IRNativeTag._getCredentialsHash); }
220
+ static get _increment() { return new IRNative(IRNativeTag._increment); }
221
+ static get _decrement() { return new IRNative(IRNativeTag._decrement); }
222
+ static get _isZero() { return new IRNative(IRNativeTag._isZero); }
219
223
  static equals(type) {
220
224
  type = getUnaliased(type);
221
225
  if (type instanceof TirAliasType)
@@ -24,7 +24,7 @@ export interface CompilerOptions {
24
24
  /**
25
25
  * path to the entry file
26
26
  */
27
- readonly entry: string;
27
+ entry: string;
28
28
  /**
29
29
  * path of the root of the project
30
30
  */
@@ -33,6 +33,10 @@ export interface CompilerOptions {
33
33
  * path to the output directory
34
34
  */
35
35
  readonly outDir: string;
36
+ /**
37
+ * if `true` silences all compiler output
38
+ */
39
+ readonly silent: boolean;
36
40
  /**
37
41
  * uplc version (encoded in the script)
38
42
  */
@@ -76,5 +80,6 @@ export declare const extremeOptions: CompilerOptions;
76
80
  export declare const productionOptions: CompilerOptions;
77
81
  export declare const debugOptions: CompilerOptions;
78
82
  export declare const defaultOptions: CompilerOptions;
83
+ export declare const testOptions: CompilerOptions;
79
84
  export declare const defulatCompilerOptions: CompilerOptions;
80
85
  export declare function completeCompilerOptions(options: Partial<CompilerOptions>, complete?: CompilerOptions): CompilerOptions;
@@ -36,6 +36,7 @@ export const extremeOptions = Object.freeze({
36
36
  entry: "./src/index.pebble",
37
37
  root: ".",
38
38
  outDir: "./out",
39
+ silent: false,
39
40
  targetUplcVersion: defaultUplcVersion,
40
41
  removeTraces: true,
41
42
  delayHoists: true,
@@ -46,6 +47,7 @@ export const productionOptions = Object.freeze({
46
47
  entry: "./src/index.pebble",
47
48
  root: ".",
48
49
  outDir: "./out",
50
+ silent: false,
49
51
  targetUplcVersion: defaultUplcVersion,
50
52
  removeTraces: true,
51
53
  delayHoists: true,
@@ -56,13 +58,21 @@ export const debugOptions = Object.freeze({
56
58
  entry: "./src/index.pebble",
57
59
  root: ".",
58
60
  outDir: "./out",
61
+ silent: false,
59
62
  targetUplcVersion: defaultUplcVersion,
60
63
  removeTraces: false,
61
64
  delayHoists: false,
62
65
  uplcOptimizations: debugUplcOptimizations,
63
66
  addMarker: false
64
67
  });
65
- export const defaultOptions = productionOptions;
68
+ export const defaultOptions = Object.freeze({
69
+ ...productionOptions,
70
+ });
71
+ export const testOptions = Object.freeze({
72
+ // ...debugOptions,
73
+ ...productionOptions,
74
+ silent: true
75
+ });
66
76
  export const defulatCompilerOptions = defaultOptions;
67
77
  export function completeCompilerOptions(options, complete = defaultOptions) {
68
78
  let targetUplcVersion = options.targetUplcVersion instanceof UPLCVersion ? complete.targetUplcVersion : defaultUplcVersion;
@@ -31,7 +31,7 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
31
31
  // ------------------------------------------------------------------------- //
32
32
  ///////////////////////////////////////////////////////////////////////////////
33
33
  const options = completeCompilerOptions(paritalOptions);
34
- const debugAsserts = options.debugAsserts ?? false;
34
+ // const debugAsserts = (options as any).debugAsserts ?? false;
35
35
  // unwrap top level letted and hoisted;
36
36
  while (term instanceof IRLetted || term instanceof IRHoisted) {
37
37
  // replace with value
@@ -39,33 +39,24 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
39
39
  // forget the parent; this is the new root
40
40
  term.parent = undefined;
41
41
  }
42
- debugAsserts && _debug_assertions(term);
42
+ // debugAsserts && _debug_assertions( term );
43
+ ///////////////////////////////////////////////////////////////////////////////
44
+ // ------------------------------------------------------------------------- //
45
+ // ----------------------------- optimizations ----------------------------- //
46
+ // ------------------------------------------------------------------------- //
47
+ ///////////////////////////////////////////////////////////////////////////////
43
48
  // term = preEvaluateDefinedTermsAndReturnRoot( term );
44
49
  term = rewriteNativesAppliedToConstantsAndReturnRoot(term);
45
- debugAsserts && _debug_assertions(term);
50
+ // debugAsserts && _debug_assertions( term );
46
51
  // removing unused variables BEFORE going into the rest of the compilation
47
52
  // helps letted terms to find a better spot (and possibly be inlined instead of hoisted)
48
53
  term = removeUnusedVarsAndReturnRoot(term);
49
- debugAsserts && _debug_assertions(term);
54
+ // debugAsserts && _debug_assertions( term );
50
55
  _makeAllNegativeNativesHoisted(term);
51
- ///////////////////////////////////////////////////////////////////////////////
52
- // ------------------------------------------------------------------------- //
53
- // ----------------------------- optimizations ----------------------------- //
54
- // ------------------------------------------------------------------------- //
55
- ///////////////////////////////////////////////////////////////////////////////
56
- // --------------------- optimize recursive functions --------------------- //
57
- // avoid passing whole structs
58
- // take letted terms outside
59
- // ----------------------- optimize normal functions ----------------------- //
60
- // avoid passing whole structs
61
- // reorganize function arguments to take advantage of partial applicaiton
62
- ///////////////////////////////////////////////////////////////////////////////
63
- // ------------------------------------------------------------------------- //
64
- // ------------------------------ final steps ------------------------------ //
65
- // ------------------------------------------------------------------------- //
66
- ///////////////////////////////////////////////////////////////////////////////
67
56
  term = replaceNativesAndReturnRoot(term);
68
- debugAsserts && _debug_assertions(term);
57
+ // re-call rewrite to optimize introduced hoisted
58
+ term = rewriteNativesAppliedToConstantsAndReturnRoot(term);
59
+ // debugAsserts && _debug_assertions( term );
69
60
  // unwrap top level letted and hoisted;
70
61
  // some natives may be converted to hoisted;
71
62
  // this is really just an edge case
@@ -79,31 +70,39 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
79
70
  term instanceof IRConst // while we are at it
80
71
  )
81
72
  return term.toUPLC();
73
+ ///////////////////////////////////////////////////////////////////////////////
74
+ // ------------------------------------------------------------------------- //
75
+ // ------------------------------- hoisting -------------------------------- //
76
+ // ------------------------------------------------------------------------- //
77
+ ///////////////////////////////////////////////////////////////////////////////
78
+ // hoist `(force (builtin ifThenElse))` or `(force (force (builtin fstPair)))` etc
82
79
  replaceForcedNativesWithHoisted(term);
83
- debugAsserts && _debug_assertions(term);
80
+ // debugAsserts && _debug_assertions( term );
84
81
  if (options.delayHoists)
85
82
  replaceHoistedWithLetted(term);
86
83
  else
87
84
  replaceClosedLettedWithHoisted(term);
88
- debugAsserts && _debug_assertions(term);
89
- if (debugAsserts
90
- && options.delayHoists
91
- && includesNode(term, node => node instanceof IRHoisted)) {
92
- throw new Error("debug assertion failed: hoisted nodes found while delayHoists is true");
93
- }
85
+ // debugAsserts && _debug_assertions( term );
86
+ // if(
87
+ // // debugAsserts
88
+ // && options.delayHoists
89
+ // && includesNode( term, node => node instanceof IRHoisted )
90
+ // ) {
91
+ // throw new Error("debug assertion failed: hoisted nodes found while delayHoists is true");
92
+ // }
94
93
  // handle letted before hoisted because the tree is smaller
95
94
  // and we also have less letted dependecies to handle
96
95
  term = handleLettedAndReturnRoot(term);
97
- debugAsserts && _debug_assertions(term);
96
+ // debugAsserts && _debug_assertions( term );
98
97
  term = handleHoistedAndReturnRoot(term);
99
- debugAsserts && _debug_assertions(term);
98
+ // debugAsserts && _debug_assertions( term );
100
99
  // replaced hoisted terms might include new letted terms
101
100
  while (includesNode(term, node => node instanceof IRLetted
102
101
  || node instanceof IRHoisted)) {
103
102
  term = handleLettedAndReturnRoot(term);
104
103
  term = handleHoistedAndReturnRoot(term);
105
104
  }
106
- debugAsserts && _debug_assertions(term);
105
+ // debugAsserts && _debug_assertions( term );
107
106
  ///////////////////////////////////////////////////////////////////////////////
108
107
  // ------------------------------------------------------------------------- //
109
108
  // --------------------------- translate to UPLC --------------------------- //
@@ -117,13 +116,13 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
117
116
  // if( options.delayHoists ) replaceHoistedWithLetted( term );
118
117
  // handle new hoisted terms
119
118
  term = handleHoistedAndReturnRoot(term);
120
- debugAsserts && _debug_assertions(term);
119
+ // debugAsserts && _debug_assertions( term );
121
120
  // strictly necessary to check the options
122
121
  // otherwise forced natives where already hoisted
123
122
  // will be re-hosited; causeing uselsess evaluations
124
123
  if (!options.delayHoists)
125
124
  term = hoistForcedNatives(term);
126
- debugAsserts && _debug_assertions(term);
125
+ // debugAsserts && _debug_assertions( term );
127
126
  // at this point we expect the IR to be translable 1:1 to UPLC
128
127
  // The loop is needed because after inlining some params,
129
128
  // new params in outer (or sibling) functions can become
@@ -161,11 +160,11 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
161
160
  // irJson.text,
162
161
  // JSON.stringify( onlyHoistedAndLetted( irJson ) )
163
162
  // );
164
- debugAsserts && _debug_assertions(term);
163
+ // debugAsserts && _debug_assertions( term );
165
164
  // const srcmap = {};
166
165
  const uplc = term.toUPLC(ToUplcCtx.root());
167
166
  if (!isClosedTerm(uplc)) {
168
- console.log(prettyUPLC(uplc));
167
+ console.error(prettyUPLC(uplc));
169
168
  throw new Error("compileIRToUPLC: final UPLC term is not closed:\n" +
170
169
  "This is a compiler internal error; please open an issue on github so we can fix this.");
171
170
  }
@@ -26,18 +26,10 @@ export class ToUplcCtx {
26
26
  const ctx = this.ctxMap.get(sym);
27
27
  const idx = ctx?._variables.indexOf(sym) ?? -1;
28
28
  if (idx <= -1) {
29
- console.log(sym, ctx?.allVars());
29
+ console.error(sym, ctx?.allVars());
30
30
  throw new Error("Variable not found in its defining context");
31
31
  }
32
- const declDbn = ctx._parentDbn + idx + 1;
33
- if (declDbn === 5
34
- && sym.description === "tailList") {
35
- console.log({
36
- ctxDbn: ctx.dbn,
37
- idx
38
- });
39
- }
40
- return declDbn;
32
+ return ctx._parentDbn + idx + 1;
41
33
  }
42
34
  getVarAccessDbn(sym) {
43
35
  const declDbn = this.getVarDeclDbn(sym);
@@ -65,8 +65,6 @@ export function getUnboundedIRVars(term) {
65
65
  }
66
66
  stack.push(...t.children());
67
67
  }
68
- console.log("accessedVars", [...new Set(accessedVars.keys())]);
69
- console.log("boundedVars", [...boundedVars]);
70
68
  for (const v of boundedVars)
71
69
  accessedVars.delete(v);
72
70
  return [...accessedVars.values()].flat();
@@ -4,7 +4,7 @@ import { getSortedLettedSet, getLettedTerms, IRLetted } from "../../../IRNodes/I
4
4
  import { IRVar } from "../../../IRNodes/IRVar.js";
5
5
  import { _modifyChildFromTo } from "../../_internal/_modifyChildFromTo.js";
6
6
  import { findAllNoHoisted } from "../../_internal/findAll.js";
7
- import { getMaxScope } from "./groupByScope.js";
7
+ import { getMaxScope, getUnboundedVars } from "./groupByScope.js";
8
8
  import { IRDelayed } from "../../../IRNodes/IRDelayed.js";
9
9
  import { lowestCommonAncestor } from "../../_internal/lowestCommonAncestor.js";
10
10
  import { isIRTerm } from "../../../utils/isIRTerm.js";
@@ -47,9 +47,22 @@ export function handleLettedAndReturnRoot(term) {
47
47
  //
48
48
  // hence why `pop` (and not `shift`)
49
49
  const { letted, nReferences } = sortedLettedSet.pop();
50
+ // const shouldLog = (
51
+ // letted.value instanceof IRApp
52
+ // && letted.value.fn instanceof IRNative
53
+ // && letted.value.fn.tag === IRNativeTag.addInteger
54
+ // && letted.value.arg instanceof IRConst
55
+ // && letted.value.arg.value === -1n
56
+ // )
50
57
  // shouldLog && // console.log("nReferences", nReferences);
51
58
  // console.log(` ------------------ working with ${lettedToStr(letted)} ------------------ `);
52
- if (nReferences === 1) {
59
+ // console.log(` ------------------ working with ${lettedToStr(letted)} ------------------ `);
60
+ // if( shouldLog ) {
61
+ // console.log( prettyIRInline( letted ) );
62
+ // }
63
+ const wasSingleReferenceButRecursive = nReferences <= 1;
64
+ if (wasSingleReferenceButRecursive // nReferences <= 1
65
+ && !someParentIsRecursive(letted)) {
53
66
  // console.log("inlining letted (single reference) with value", prettyIRText( letted.value ) )
54
67
  _modifyChildFromTo(letted.parent, letted, letted.value);
55
68
  continue;
@@ -81,7 +94,9 @@ export function handleLettedAndReturnRoot(term) {
81
94
  continue;
82
95
  }
83
96
  // just in case
84
- if (sameLettedRefs.length === 1 && !minScope) {
97
+ if (sameLettedRefs.length === 1
98
+ && !minScope
99
+ && !wasSingleReferenceButRecursive) {
85
100
  // console.log("inlining letted (single reference pedantic) with value", prettyIRText( letted.value ) )
86
101
  _modifyChildFromTo(letted.parent, letted, letted.value);
87
102
  continue;
@@ -108,6 +123,47 @@ export function handleLettedAndReturnRoot(term) {
108
123
  }
109
124
  ;
110
125
  }
126
+ if (wasSingleReferenceButRecursive) {
127
+ // OPTIMIZATION:
128
+ // TODO:
129
+ // see the general case below
130
+ const unbounded = getUnboundedVars(letted.value);
131
+ if (unbounded.size === 0) {
132
+ // if closed
133
+ // handle as hoisted
134
+ for (const ref of sameLettedRefs) {
135
+ _modifyChildFromTo(ref.parent, ref, new IRVar(ref.name));
136
+ }
137
+ term = new IRApp(new IRFunc([letted.name], term), letted.value);
138
+ continue;
139
+ }
140
+ // else find highest common ancestor where all unbounded vars are defined
141
+ // OPTIMIZATION:
142
+ // TODO:
143
+ // only hoist outside the highest, but fully defined, IRRecursive
144
+ // and NOT the highest overall
145
+ // otherwise we risk paying for introducing stuff we don't use
146
+ let tmp = lca;
147
+ // let lowestOutsideRecursive: IRTerm = lca;
148
+ while (tmp = tmp.parent) {
149
+ if (tmp instanceof IRDelayed) {
150
+ lca = tmp;
151
+ // lowestOutsideRecursive = tmp;
152
+ continue;
153
+ }
154
+ if (tmp instanceof IRFunc || tmp instanceof IRRecursive) {
155
+ if (tmp.params.some(p => unbounded.has(p))) {
156
+ // some parameter is defined here
157
+ // so we stop
158
+ break;
159
+ }
160
+ else
161
+ lca = tmp;
162
+ // else lowestOutsideRecursive = tmp;
163
+ }
164
+ }
165
+ // lca = lowestOutsideRecursive;
166
+ }
111
167
  if (!isIRTerm(lca)) {
112
168
  throw new Error("letting nodes with hash " + irHashToHex(lettedHash) + " from different trees");
113
169
  }
@@ -176,3 +232,13 @@ function handleLettedAsHoistedAndReturnRoot(letted, lca, sameLettedRefs, current
176
232
  }
177
233
  return currentRoot;
178
234
  }
235
+ function someParentIsRecursive(term) {
236
+ let parent;
237
+ ;
238
+ while (parent = term.parent) {
239
+ if (parent instanceof IRRecursive)
240
+ return true;
241
+ term = parent;
242
+ }
243
+ return false;
244
+ }
@@ -22,12 +22,13 @@ export declare const hoisted_sub4: IRHoisted;
22
22
  export declare const hoisted_length: IRHoisted;
23
23
  export declare const hoisted_dropList: IRHoisted;
24
24
  export declare const hoisted_sizeofPositiveInt: IRHoisted;
25
+ export declare const hoisted_findSopOptional: IRHoisted;
25
26
  export declare const hoisted_mkFindDataOptional: IRHoisted;
26
27
  export declare const hoisted_strictAnd: IRHoisted;
27
28
  export declare const hoisted_strictOr: IRHoisted;
28
29
  export declare const hoisted_some: IRHoisted;
29
30
  export declare const hoisted_every: IRHoisted;
30
- export declare const hoisted_mkFilter: IRHoisted;
31
+ export declare const hoisted_filter: IRHoisted;
31
32
  export declare const hoisted_gtBS: IRHoisted;
32
33
  export declare const hoisted_gtEqBS: IRHoisted;
33
34
  export declare const hoisted_gtInt: IRHoisted;
@@ -42,5 +43,6 @@ export declare const hoisted_exponentiateInteger: IRHoisted;
42
43
  export declare function nativeToIR(native: IRNative): IRTerm;
43
44
  export declare const hoisted_mkEqualsList: IRHoisted;
44
45
  export declare const hoisted_mkMapList: IRHoisted;
46
+ export declare const hoisted_getCredentialsHash: IRHoisted;
45
47
  export declare const hoisted_sortedValueLovelaces: IRHoisted;
46
48
  export declare const hoisted_amountOfValue: IRHoisted;
@@ -12,10 +12,11 @@ import { IRRecursive } from "../../../IRNodes/IRRecursive.js";
12
12
  import { IRSelfCall } from "../../../IRNodes/IRSelfCall.js";
13
13
  import { IRError } from "../../../IRNodes/IRError.js";
14
14
  import { _ir_apps } from "../../../IRNodes/IRApp.js";
15
- import { _ir_let } from "../../../tree_utils/_ir_let.js";
15
+ import { _ir_let, _ir_let_sym } from "../../../tree_utils/_ir_let.js";
16
16
  import { _ir_lazyChooseList } from "../../../tree_utils/_ir_lazyChooseList.js";
17
17
  import { _ir_lazyIfThenElse } from "../../../tree_utils/_ir_lazyIfThenElse.js";
18
18
  import { hoisted_drop4, hoisted_drop2, hoisted_drop3 } from "../_comptimeDropN.js";
19
+ import { IRConstr } from "../../../IRNodes/index.js";
19
20
  function _ir_strictAnd(left, right) {
20
21
  return _ir_apps(IRNative.strictIfThenElse, left, right, IRConst.bool(false));
21
22
  }
@@ -106,6 +107,22 @@ const foldl_head = Symbol("head");
106
107
  const foldl_tail = Symbol("tail");
107
108
  const hoiseted_foldl = new IRHoisted(new IRFunc([foldl_reduce], new IRRecursive(foldl_self, new IRFunc([foldl_acc], _ir_apps(hoisted_matchList.clone(), new IRDelayed(new IRVar(foldl_acc)), new IRFunc([foldl_head, foldl_tail], _ir_apps(new IRSelfCall(foldl_self), _ir_apps(new IRVar(foldl_reduce), new IRVar(foldl_acc), new IRVar(foldl_head)), new IRVar(foldl_tail))))))));
108
109
  hoiseted_foldl.hash;
110
+ // hoisted _findSopOptional
111
+ // (predicate: (a -> Bool)) -> (list: [a]) -> Optional<a>
112
+ const findSop_predicate = Symbol("predicate");
113
+ const findSop_self = Symbol("findOpt_self");
114
+ const findSop_list = Symbol("list");
115
+ const findSop_head = Symbol("head");
116
+ export const hoisted_findSopOptional = new IRHoisted(new IRFunc([findSop_predicate], new IRRecursive(findSop_self, new IRFunc([findSop_list], _ir_lazyChooseList(new IRVar(findSop_list),
117
+ // case nil
118
+ new IRConstr(1, []), // None
119
+ // case cons
120
+ new IRFunc([findSop_head], _ir_lazyIfThenElse(_ir_apps(new IRVar(findSop_predicate), new IRVar(findSop_head)),
121
+ // then => Some(head)
122
+ new IRConstr(1, [new IRVar(findSop_head)]), // Some{ head }
123
+ // else => self(tail)
124
+ _ir_apps(new IRSelfCall(findSop_self), _ir_apps(IRNative.tailList, new IRVar(findSop_list))))))))));
125
+ hoisted_findSopOptional.hash;
109
126
  // hoisted _mkFindDataOptional
110
127
  const mkFind_elemToData = Symbol("elemToData");
111
128
  const mkFind_pred = Symbol("predicate");
@@ -139,13 +156,21 @@ const every_head = Symbol("head");
139
156
  const every_tail = Symbol("tail");
140
157
  export const hoisted_every = new IRHoisted(new IRFunc([every_pred], _ir_apps(hoisted_recursiveList.clone(), new IRFunc([every_dummy], new IRDelayed(IRConst.bool(true))), new IRFunc([every_self, every_head, every_tail], new IRForced(_ir_apps(hoisted_strictAnd.clone(), new IRDelayed(new IRApp(new IRVar(every_pred), new IRVar(every_head))), new IRDelayed(new IRApp(new IRVar(every_self), new IRVar(every_tail)))))))));
141
158
  hoisted_every.hash;
142
- // hoisted _mkFilter
143
- const filt_pnil = Symbol("pnilOfType");
159
+ // hoisted _filter
144
160
  const filt_pred = Symbol("predicate");
161
+ const filt_self = Symbol("filter_self");
162
+ const filt_list = Symbol("list");
145
163
  const filt_elem = Symbol("elem");
146
- const filt_acc = Symbol("accum");
147
- export const hoisted_mkFilter = new IRHoisted(new IRFunc([filt_pnil, filt_pred], _ir_apps(hoisted_foldr.clone(), new IRFunc([filt_elem, filt_acc], new IRForced(_ir_apps(IRNative.strictIfThenElse, new IRApp(new IRVar(filt_pred), new IRVar(filt_elem)), new IRDelayed(_ir_apps(IRNative.mkCons, new IRVar(filt_elem), new IRVar(filt_acc))), new IRDelayed(new IRVar(filt_acc))))), new IRVar(filt_pnil))));
148
- hoisted_mkFilter.hash;
164
+ export const hoisted_filter = new IRHoisted(new IRFunc([filt_pred], new IRRecursive(filt_self, new IRFunc([filt_list], _ir_lazyChooseList(new IRVar(filt_list),
165
+ // case nil
166
+ new IRVar(filt_list), // nil
167
+ // case cons
168
+ _ir_let_sym(filt_elem, _ir_apps(IRNative.headList, new IRVar(filt_list)), _ir_lazyIfThenElse(_ir_apps(new IRVar(filt_pred), new IRVar(filt_elem)),
169
+ // then => cons(elem, self(tail))
170
+ _ir_apps(IRNative.mkCons, new IRVar(filt_elem), _ir_apps(new IRSelfCall(filt_self), _ir_apps(IRNative.tailList, new IRVar(filt_list)))),
171
+ // else => self(tail)
172
+ _ir_apps(new IRSelfCall(filt_self), _ir_apps(IRNative.tailList, new IRVar(filt_list))))))))));
173
+ hoisted_filter.hash;
149
174
  // comparison & conversion hoisted (previously inline)
150
175
  const gtbs_a = Symbol("a"), gtbs_b = Symbol("b");
151
176
  export const hoisted_gtBS = new IRHoisted(new IRFunc([gtbs_a, gtbs_b], _ir_apps(IRNative.lessThanByteString, new IRVar(gtbs_b), new IRVar(gtbs_a))));
@@ -194,11 +219,12 @@ export function nativeToIR(native) {
194
219
  switch (native.tag) {
195
220
  case IRNativeTag._foldr: return hoisted_foldr.clone();
196
221
  case IRNativeTag._foldl: return hoiseted_foldl.clone();
197
- case IRNativeTag._mkFindDataOptional: return hoisted_mkFindDataOptional.clone();
222
+ // case IRNativeTag._mkFindDataOptional: return hoisted_mkFindDataOptional.clone();
223
+ case IRNativeTag._findSopOptional: return hoisted_findSopOptional.clone();
198
224
  case IRNativeTag._length: return hoisted_length.clone();
199
225
  case IRNativeTag._some: return hoisted_some.clone();
200
226
  case IRNativeTag._every: return hoisted_every.clone();
201
- case IRNativeTag._mkFilter: return hoisted_mkFilter.clone();
227
+ case IRNativeTag._filter: return hoisted_filter.clone();
202
228
  case IRNativeTag._id: return hoisted_id.clone();
203
229
  case IRNativeTag._not: return hoisted_not.clone();
204
230
  case IRNativeTag._strictAnd: return hoisted_strictAnd.clone();
@@ -217,9 +243,12 @@ export function nativeToIR(native) {
217
243
  case IRNativeTag._exponentiateInteger: return hoisted_exponentiateInteger.clone();
218
244
  case IRNativeTag._amountOfValue: return hoisted_amountOfValue.clone();
219
245
  case IRNativeTag._isZero: return hoisted_isZero.clone();
220
- case IRNativeTag._sortedValueLovelaces: return hoisted_sortedValueLovelaces.clone?.() ?? (() => { throw new Error("_sortedValueLovelaces hoisted const missing"); })();
246
+ case IRNativeTag._sortedValueLovelaces: return hoisted_sortedValueLovelaces.clone();
247
+ case IRNativeTag._getCredentialsHash: return hoisted_getCredentialsHash.clone();
221
248
  case IRNativeTag._dropList: return hoisted_dropList.clone();
222
249
  case IRNativeTag._mkMapList: return hoisted_mkMapList.clone();
250
+ case IRNativeTag._increment: return hoisted_addOne.clone();
251
+ case IRNativeTag._decrement: return hoisted_subOne.clone();
223
252
  // case IRNativeTag._mkEqualsList: return hoisted_mkEqualsList.clone();
224
253
  default:
225
254
  throw new Error("unknown (negative) native calling 'nativeToIR'; " +
@@ -241,7 +270,7 @@ IRConst.bool(false),
241
270
  // both lists are cons
242
271
  _ir_strictAnd(_ir_apps(new IRVar(eqList_eqFunc), new IRApp(IRNative.headList, new IRVar(eqList_listA)), new IRApp(IRNative.headList, new IRVar(eqList_listB))), _ir_apps(new IRSelfCall(eqList_self), new IRApp(IRNative.tailList, new IRVar(eqList_listA)), new IRApp(IRNative.tailList, new IRVar(eqList_listB))))))))));
243
272
  hoisted_mkEqualsList.hash;
244
- // (nil of type) => ( a => b ) => [a] => [b]
273
+ // (nil of type) [b] => ( a => b ) => [a] => [b]
245
274
  const mkMap_nil = Symbol("nilOfType");
246
275
  const mkMap_mapFunc = Symbol("mapFunc");
247
276
  const mkMap_map = Symbol("map_self");
@@ -256,6 +285,9 @@ _ir_apps(new IRVar(mkMap_mapFunc), new IRApp(IRNative.headList, new IRVar(mkMap_
256
285
  // recurse tail
257
286
  _ir_apps(new IRSelfCall(mkMap_map), new IRApp(IRNative.tailList, new IRVar(mkMap_list)))))))));
258
287
  hoisted_mkMapList.hash;
288
+ const credHash_credentials = Symbol("credentials");
289
+ export const hoisted_getCredentialsHash = new IRHoisted(new IRFunc([credHash_credentials], _ir_apps(IRNative.unBData, _ir_apps(IRNative.headList, _ir_apps(IRNative.sndPair, _ir_apps(IRNative.unConstrData, new IRVar(credHash_credentials)))))));
290
+ hoisted_getCredentialsHash.hash;
259
291
  // If _sortedValueLovelaces was previously inline, hoist it:
260
292
  const sorted_value = Symbol("value");
261
293
  export const hoisted_sortedValueLovelaces = new IRHoisted(new IRFunc([sorted_value], _ir_apps(IRNative.unIData, _ir_apps(IRNative.sndPair, _ir_apps(IRNative.headList, _ir_apps(IRNative.unMapData, _ir_apps(IRNative.sndPair, _ir_apps(IRNative.headList, new IRVar(sorted_value)))))))));