This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

JSON Schemas

JSON schema definitions for Morphir IR format versions

Morphir IR JSON Schemas

This directory contains formal JSON schema specifications for all supported format versions of the Morphir IR (Intermediate Representation).

Schema Files

  • morphir-ir-v3.yaml: Current format version (v3)
  • morphir-ir-v2.yaml: Format version 2
  • morphir-ir-v1.yaml: Format version 1

Format Version Differences

Version 1 → Version 2

Tag Capitalization:

  • Distribution: "library""Library"
  • Access control: "public"/"private""Public"/"Private"
  • Type tags: "variable""Variable", "reference""Reference", etc.

Structure Changes:

  • Modules changed from {"name": ..., "def": ...} objects to [modulePath, accessControlled] arrays

Version 2 → Version 3

Tag Capitalization:

  • Value expression tags: "apply""Apply", "lambda""Lambda", etc.
  • Pattern tags: "as_pattern""AsPattern", "wildcard_pattern""WildcardPattern", etc.
  • Literal tags: "bool_literal""BoolLiteral", "string_literal""StringLiteral", etc.

Usage

Validation

The schemas can be used to validate Morphir IR JSON files. Note that due to the complexity and recursive nature of these schemas, validation can be slow with some validators.

Using Python jsonschema

pip install jsonschema pyyaml

python3 << 'EOF'
import json
import yaml
from jsonschema import validate

# Load schema
with open('morphir-ir-v3.yaml', 'r') as f:
    schema = yaml.safe_load(f)

# Load Morphir IR JSON
with open('morphir-ir.json', 'r') as f:
    data = json.load(f)

# Validate
validate(instance=data, schema=schema)
print("✓ Valid Morphir IR")
EOF

Using Node.js ajv

npm install -g ajv-cli ajv-formats

# Convert YAML to JSON first
python3 -c "import yaml, json; \
  json.dump(yaml.safe_load(open('morphir-ir-v3.yaml')), open('morphir-ir-v3.json', 'w'))"

# Validate
ajv validate -s morphir-ir-v3.json -d morphir-ir.json

Quick Structural Check

For a quick check without full validation, you can verify basic structure:

import json

def check_morphir_ir(filepath):
    with open(filepath) as f:
        data = json.load(f)
    
    # Check format version
    version = data.get('formatVersion')
    assert version in [1, 2, 3], f"Unknown format version: {version}"
    
    # Check distribution structure
    dist = data['distribution']
    assert isinstance(dist, list) and len(dist) == 4
    assert dist[0] in ["library", "Library"], f"Unknown distribution type: {dist[0]}"
    
    # Check package definition
    pkg_def = dist[3]
    assert 'modules' in pkg_def
    
    print(f"✓ Basic structure valid: Format v{version}, {len(pkg_def['modules'])} modules")

check_morphir_ir('morphir-ir.json')

Integration with Tools

These schemas can be used to:

  1. Generate Code: Create type definitions and parsers for various programming languages
  2. IDE Support: Provide autocomplete and validation in JSON editors
  3. Testing: Validate generated IR in test suites
  4. Documentation: Generate human-readable documentation from schema definitions

Schema Format

The schemas are written in YAML format for better readability and include:

  • Comprehensive inline documentation
  • Type constraints and patterns
  • Required vs. optional fields
  • Recursive type definitions
  • Enum values for tagged unions

Contributing

When updating the IR format:

  1. Update the appropriate schema file(s) to match the upstream schemas from the main Morphir repository
  2. Update the format version handling in the .NET codec implementation if needed
  3. Add migration logic in the codec files if needed
  4. Update this README with the changes
  5. Test the schema against example IR files

References

1 - Schema Version 3

Morphir IR JSON Schema for format version 3 (Current)

Morphir IR Schema - Version 3

Format version 3 is the current version of the Morphir IR format. It uses capitalized tags throughout for consistency and clarity.

Overview

Version 3 of the Morphir IR format standardizes on capitalized tags for all constructs. This provides a consistent naming convention across the entire IR structure.

Key Characteristics

Tag Capitalization

All tags in version 3 are capitalized:

  • Distribution: "Library"
  • Access Control: "Public" and "Private"
  • Type Tags: "Variable", "Reference", "Tuple", "Record", etc.
  • Value Tags: "Apply", "Lambda", "LetDefinition", etc.
  • Pattern Tags: "AsPattern", "WildcardPattern", "ConstructorPattern", etc.
  • Literal Tags: "BoolLiteral", "StringLiteral", "WholeNumberLiteral", etc.

Core Concepts

Naming System

The Morphir IR uses a sophisticated naming system independent of any specific naming convention.

Name

A Name represents a human-readable identifier made up of one or more words.

  • Structure: Array of lowercase word strings
  • Purpose: Atomic unit for all identifiers
  • Example: ["value", "in", "u", "s", "d"] renders as valueInUSD or value_in_USD
Name:
  type: array
  items:
    type: string
    pattern: "^[a-z][a-z0-9]*$"
  minItems: 1

Path

A Path represents a hierarchical location in the IR structure.

  • Structure: List of Names
  • Purpose: Identifies packages and modules
  • Example: [["morphir"], ["s", "d", "k"], ["string"]] for the String module
Path:
  type: array
  items:
    $ref: "#/definitions/Name"
  minItems: 1

Fully-Qualified Name (FQName)

Provides globally unique identifiers for types and values.

  • Structure: [packagePath, modulePath, localName]
  • Purpose: Unambiguous references across package boundaries
FQName:
  type: array
  minItems: 3
  maxItems: 3
  items:
    - $ref: "#/definitions/PackageName"
    - $ref: "#/definitions/ModuleName"
    - $ref: "#/definitions/Name"

Access Control

AccessControlled

Manages visibility of types and values.

  • Structure: {access, value}
  • Access levels: "Public" (visible externally) or "Private" (package-only)
  • Purpose: Controls API exposure
AccessControlled:
  type: object
  required: ["access", "value"]
  properties:
    access:
      type: string
      enum: ["Public", "Private"]
    value:
      description: "The value being access controlled."

Distribution and Package Structure

Distribution

A Distribution represents a complete, self-contained package with all dependencies.

  • Current type: Library (only supported distribution type)
  • Structure: ["Library", packageName, dependencies, packageDefinition]
  • Purpose: Output of compilation process, ready for execution or transformation
distribution:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - type: string
      const: "Library"
    - $ref: "#/definitions/PackageName"
    - $ref: "#/definitions/Dependencies"
    - $ref: "#/definitions/PackageDefinition"

Package Definition

Complete implementation of a package with all details.

  • Contains: All modules (public and private)
  • Includes: Type signatures and implementations
  • Purpose: Full package representation for processing

Package Specification

Public interface of a package.

  • Contains: Only publicly exposed modules
  • Includes: Only type signatures, no implementations
  • Purpose: Dependency interface

Module Structure

Module Definition

Complete implementation of a module.

  • Contains: All types and values (public and private) with implementations
  • Structure: Dictionary of type names to AccessControlled type definitions, and value names to AccessControlled value definitions
  • Purpose: Complete module implementation

Module Specification

Public interface of a module.

  • Contains: Only publicly exposed types and values
  • Includes: Type signatures only, no implementations
  • Purpose: Module’s public API

Type System

The type system is based on functional programming principles, supporting:

Type Expressions

Variable

Represents a type variable (generic parameter).

  • Structure: ["Variable", attributes, name]
  • Example: The a in List a
  • Purpose: Enables polymorphic types
VariableType:
  type: array
  minItems: 3
  maxItems: 3
  items:
    - const: "Variable"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/Name"

Reference

Reference to another type or type alias.

  • Structure: ["Reference", attributes, fqName, typeArgs]
  • Examples: String, List Int, Maybe a
  • Purpose: References built-in types, custom types, or type aliases
ReferenceType:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - const: "Reference"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/FQName"
    - type: array
      items:
        $ref: "#/definitions/Type"

Tuple

Composition of multiple types in fixed order.

  • Structure: ["Tuple", attributes, elementTypes]
  • Example: (Int, String, Bool)
  • Purpose: Product types with positional access

Record

Composition of named fields with types.

  • Structure: ["Record", attributes, fields]
  • Example: {firstName: String, age: Int}
  • Purpose: Product types with named field access
  • Note: All fields are required

Function

Function type representation.

  • Structure: ["Function", attributes, argType, returnType]
  • Example: Int -> String
  • Purpose: Represents function and lambda types
  • Note: Multi-argument functions use currying (nested Function types)

Type Specifications

TypeAliasSpecification

An alias for another type.

  • Structure: ["TypeAliasSpecification", typeParams, aliasedType]
  • Example: type alias UserId = String
  • Purpose: Meaningful name for type expression

CustomTypeSpecification

Tagged union type (sum type).

  • Structure: ["CustomTypeSpecification", typeParams, constructors]
  • Example: type Result e a = Ok a | Err e
  • Purpose: Choice between multiple alternatives

OpaqueTypeSpecification

Type with unknown structure.

  • Structure: ["OpaqueTypeSpecification", typeParams]
  • Characteristics: Structure hidden, no automatic serialization
  • Purpose: Encapsulates implementation details

Value System

All data and logic in Morphir are represented as value expressions.

Value Expressions

Literal

Literal constant value.

  • Structure: ["Literal", attributes, literal]
  • Types: BoolLiteral, CharLiteral, StringLiteral, WholeNumberLiteral, FloatLiteral, DecimalLiteral
  • Purpose: Represents constant data

Variable

Reference to a variable in scope.

  • Structure: ["Variable", attributes, name]
  • Example: References to function parameters or let-bound variables
  • Purpose: Accesses values bound in current scope

Reference

Reference to a defined value (function or constant).

  • Structure: ["Reference", attributes, fqName]
  • Example: Morphir.SDK.List.map, Basics.add
  • Purpose: Invokes or references defined functions

Apply

Function application.

  • Structure: ["Apply", attributes, function, argument]
  • Example: add 1 2 (nested Apply nodes for currying)
  • Purpose: Invokes functions with arguments

Lambda

Anonymous function.

  • Structure: ["Lambda", attributes, argumentPattern, body]
  • Example: \x -> x + 1
  • Purpose: Creates inline functions

LetDefinition

Let binding introducing a single value.

  • Structure: ["LetDefinition", attributes, bindingName, definition, inExpr]
  • Example: let x = 5 in x + x
  • Purpose: Introduces local bindings

IfThenElse

Conditional expression.

  • Structure: ["IfThenElse", attributes, condition, thenBranch, elseBranch]
  • Example: if x > 0 then "positive" else "non-positive"
  • Purpose: Conditional logic

PatternMatch

Pattern matching with multiple cases.

  • Structure: ["PatternMatch", attributes, valueToMatch, cases]
  • Example: case maybeValue of Just x -> x; Nothing -> 0
  • Purpose: Conditional logic based on structure

Patterns

Used for destructuring and filtering values.

WildcardPattern

Matches any value without binding.

  • Structure: ["WildcardPattern", attributes]
  • Syntax: _
  • Purpose: Ignores a value

AsPattern

Binds a name to a matched value.

  • Structure: ["AsPattern", attributes, nestedPattern, variableName]
  • Special case: Simple variable binding uses AsPattern with WildcardPattern
  • Purpose: Captures matched values

ConstructorPattern

Matches specific type constructor and arguments.

  • Structure: ["ConstructorPattern", attributes, fqName, argPatterns]
  • Example: Just x matches Just with pattern x
  • Purpose: Destructures and filters tagged unions

Literals

BoolLiteral

Boolean literal.

  • Structure: ["BoolLiteral", boolean]
  • Values: true or false
BoolLiteral:
  type: array
  minItems: 2
  maxItems: 2
  items:
    - const: "BoolLiteral"
    - type: boolean

StringLiteral

Text string literal.

  • Structure: ["StringLiteral", string]
  • Example: "hello"
StringLiteral:
  type: array
  minItems: 2
  maxItems: 2
  items:
    - const: "StringLiteral"
    - type: string

WholeNumberLiteral

Integer literal.

  • Structure: ["WholeNumberLiteral", integer]
  • Example: 42, -17

Version 3 is the recommended format for new Morphir IR files. It provides:

  • Consistency: All tags follow the same capitalization convention
  • Clarity: Capitalized tags are easier to distinguish in JSON
  • Future-proof: This format will be maintained going forward

Full Schema

For the complete schema definition, see the full schema page.

References

1.1 - What's New in Version 3

Changes and improvements in Morphir IR schema version 3

What’s New in Version 3

Version 3 of the Morphir IR schema introduces consistent capitalization across all tags, providing a uniform and predictable format.

Key Changes from Version 2

Consistent Capitalization

The primary change in version 3 is the complete capitalization of all tags throughout the schema:

Value Expression Tags

All value expression tags are now capitalized:

  • "apply""Apply"
  • "lambda""Lambda"
  • "let_definition""LetDefinition"
  • "if_then_else""IfThenElse"
  • "pattern_match""PatternMatch"
  • "literal""Literal"
  • "variable""Variable"
  • "reference""Reference"
  • "constructor""Constructor"
  • "tuple""Tuple"
  • "list""List"
  • "record""Record"
  • "field""Field"
  • "field_function""FieldFunction"
  • "let_recursion""LetRecursion"
  • "destructure""Destructure"
  • "update_record""UpdateRecord"
  • "unit""Unit"

Pattern Tags

All pattern tags are now capitalized:

  • "wildcard_pattern""WildcardPattern"
  • "as_pattern""AsPattern"
  • "tuple_pattern""TuplePattern"
  • "constructor_pattern""ConstructorPattern"
  • "empty_list_pattern""EmptyListPattern"
  • "head_tail_pattern""HeadTailPattern"
  • "literal_pattern""LiteralPattern"
  • "unit_pattern""UnitPattern"

Literal Tags

All literal tags are now capitalized:

  • "bool_literal""BoolLiteral"
  • "char_literal""CharLiteral"
  • "string_literal""StringLiteral"
  • "whole_number_literal""WholeNumberLiteral"
  • "float_literal""FloatLiteral"
  • "decimal_literal""DecimalLiteral"

Benefits

Consistency

Version 3 provides a single, uniform naming convention across the entire IR structure. This makes the schema:

  • Easier to remember: One rule applies everywhere
  • More predictable: All tags follow PascalCase capitalization
  • Cleaner to work with: No need to remember which tags use underscores or lowercase

Better Tooling Support

The consistent capitalization improves:

  • Code generation: Automated tools can rely on uniform naming
  • Serialization/Deserialization: Simplified mapping to programming language types
  • Validation: Easier to write validation rules and tests

Migration from Version 2

Migrating from version 2 to version 3 requires updating all lowercase and underscore-separated tags:

  1. Capitalize all value tags
  2. Capitalize all pattern tags
  3. Capitalize all literal tags
  4. Remove underscores and use PascalCase

Recommendation

Version 3 is the current and recommended format for all new Morphir IR files. It provides the best balance of consistency, clarity, and tooling support.

See Also

1.2 - Full Schema

Complete Morphir IR JSON Schema for format version 3

Morphir IR Schema Version 3 - Complete Schema

This page contains the complete JSON schema definition for Morphir IR format version 3 (current version).

Download

You can download the schema file directly: morphir-ir-v3.yaml

Usage

This schema can be used to validate Morphir IR JSON files in format version 3:

# Using Python jsonschema (recommended for YAML schemas)
pip install jsonschema pyyaml
python -c "import json, yaml, jsonschema; \
  schema = yaml.safe_load(open('morphir-ir-v3.yaml')); \
  data = json.load(open('your-morphir-ir.json')); \
  jsonschema.validate(data, schema); \
  print('✓ Valid Morphir IR')"

References


Appendix: Complete Schema Definition

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
# JSON Schema for Morphir IR Format Version 3
# This schema defines the structure of a Morphir IR distribution in version 3 format.
# A distribution is the output of the Morphir compilation process (e.g., morphir-elm make).

$schema: "http://json-schema.org/draft-07/schema#"
$id: "https://finos.github.io/morphir/schemas/morphir-ir-v3.yaml"
title: "Morphir IR Distribution"
description: |
  A Morphir IR distribution represents a complete, self-contained package of business logic
  with all its dependencies. It captures the semantics of functional programs in a
  language-independent, platform-agnostic format.

type: object
required:
  - formatVersion
  - distribution
properties:
  formatVersion:
    type: integer
    const: 3
    description: "The version of the IR format. Must be 3 for this schema."
  
  distribution:
    description: "The distribution data, currently only Library distributions are supported."
    type: array
    minItems: 4
    maxItems: 4
    items:
      - type: string
        const: "Library"
        description: "The type of distribution. Currently only Library is supported."
      - $ref: "#/definitions/PackageName"
      - $ref: "#/definitions/Dependencies"
      - $ref: "#/definitions/PackageDefinition"

definitions:
  # ===== Basic Building Blocks =====
  
  Name:
    type: array
    items:
      type: string
      pattern: "^[a-z][a-z0-9]*$"
    minItems: 1
    description: |
      A Name is a list of lowercase words that represents a human-readable identifier.
      Example: ["value", "in", "u", "s", "d"] can be rendered as valueInUSD or value_in_USD.
  
  Path:
    type: array
    items:
      $ref: "#/definitions/Name"
    minItems: 1
    description: |
      A Path is a list of Names representing a hierarchical location in the IR structure.
      Used for package names and module names.
  
  PackageName:
    $ref: "#/definitions/Path"
    description: "Globally unique identifier for a package."
  
  ModuleName:
    $ref: "#/definitions/Path"
    description: "Unique identifier for a module within a package."
  
  FQName:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - $ref: "#/definitions/PackageName"
      - $ref: "#/definitions/ModuleName"
      - $ref: "#/definitions/Name"
    description: |
      Fully-Qualified Name that provides a globally unique identifier for any type or value.
      Consists of [packagePath, modulePath, localName].
  
  # ===== Attributes =====
  
  Attributes:
    type: object
    description: |
      Attributes can be attached to various nodes in the IR for extensibility.
      When no additional information is needed, an empty object {} is used.
  
  # ===== Access Control =====
  
  AccessControlled:
    type: object
    required: ["access", "value"]
    properties:
      access:
        type: string
        enum: ["Public", "Private"]
        description: "Controls visibility of types and values."
      value:
        description: "The value being access controlled."
    description: "Wrapper that manages visibility of types and values."
  
  # Note: Documented is not a separate schema definition because it's encoded conditionally.
  # When documentation exists, the JSON has both "doc" and "value" fields.
  # When documentation is absent, the JSON contains only the documented element directly (no wrapper).
  # This is handled inline in the definitions that use Documented.
  
  # ===== Distribution Structure =====
  
  Dependencies:
    type: array
    items:
      type: array
      minItems: 2
      maxItems: 2
      items:
        - $ref: "#/definitions/PackageName"
        - $ref: "#/definitions/PackageSpecification"
    description: "Dictionary of package dependencies, contains only type signatures."
  
  PackageDefinition:
    type: object
    required: ["modules"]
    properties:
      modules:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/ModuleName"
            - allOf:
                - $ref: "#/definitions/AccessControlled"
                - properties:
                    value:
                      $ref: "#/definitions/ModuleDefinition"
        description: "All modules in the package (public and private)."
    description: "Complete implementation of a package with all details."
  
  PackageSpecification:
    type: object
    required: ["modules"]
    properties:
      modules:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/ModuleName"
            - $ref: "#/definitions/ModuleSpecification"
        description: "Public modules only."
    description: "Public interface of a package, contains only type signatures."
  
  # ===== Module Structure =====
  
  ModuleDefinition:
    type: object
    required: ["types", "values"]
    properties:
      types:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - allOf:
                - $ref: "#/definitions/AccessControlled"
                - properties:
                    value:
                      # Documented wrapper: can have "doc" and "value", or just the type definition directly
                      oneOf:
                        - type: object
                          required: ["doc", "value"]
                          properties:
                            doc:
                              type: string
                            value:
                              $ref: "#/definitions/TypeDefinition"
                        - $ref: "#/definitions/TypeDefinition"
        description: "All type definitions (public and private)."
      values:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - allOf:
                - $ref: "#/definitions/AccessControlled"
                - properties:
                    value:
                      # Documented wrapper: can have "doc" and "value", or just the value definition directly
                      oneOf:
                        - type: object
                          required: ["doc", "value"]
                          properties:
                            doc:
                              type: string
                            value:
                              $ref: "#/definitions/ValueDefinition"
                        - $ref: "#/definitions/ValueDefinition"
        description: "All value definitions (public and private)."
      doc:
        type: string
        description: "Optional documentation for the module."
    description: "Complete implementation of a module."
  
  ModuleSpecification:
    type: object
    required: ["types", "values"]
    properties:
      types:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - oneOf:
                - type: object
                  required: ["doc", "value"]
                  properties:
                    doc:
                      type: string
                    value:
                      $ref: "#/definitions/TypeSpecification"
                - $ref: "#/definitions/TypeSpecification"
        description: "Public type specifications only."
      values:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - oneOf:
                - type: object
                  required: ["doc", "value"]
                  properties:
                    doc:
                      type: string
                    value:
                      $ref: "#/definitions/ValueSpecification"
                - $ref: "#/definitions/ValueSpecification"
        description: "Public value specifications only."
      doc:
        type: string
        description: "Optional documentation for the module."
    description: "Public interface of a module."
  
  # ===== Type System =====
  
  Type:
    description: |
      A Type is a recursive tree structure representing type expressions.
      Each type can be one of: Variable, Reference, Tuple, Record, ExtensibleRecord, Function, or Unit.
    oneOf:
      - $ref: "#/definitions/VariableType"
      - $ref: "#/definitions/ReferenceType"
      - $ref: "#/definitions/TupleType"
      - $ref: "#/definitions/RecordType"
      - $ref: "#/definitions/ExtensibleRecordType"
      - $ref: "#/definitions/FunctionType"
      - $ref: "#/definitions/UnitType"
  
  VariableType:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Variable"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
    description: "Represents a type variable (generic parameter)."
  
  ReferenceType:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "Reference"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
      - type: array
        items:
          $ref: "#/definitions/Type"
        description: "Type arguments for generic types."
    description: "Reference to another type or type alias."
  
  TupleType:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Tuple"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Type"
        description: "Element types in order."
    description: "A composition of multiple types in a fixed order (product type)."
  
  RecordType:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Record"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Field"
        description: "List of field definitions."
    description: "A composition of named fields with their types."
  
  ExtensibleRecordType:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "ExtensibleRecord"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
      - type: array
        items:
          $ref: "#/definitions/Field"
        description: "Known fields."
    description: "A record type that can be extended with additional fields."
  
  FunctionType:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "Function"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Type"
      - $ref: "#/definitions/Type"
    description: |
      Represents a function type. Multi-argument functions are represented via currying.
      Items: [tag, attributes, argumentType, returnType]
  
  UnitType:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "Unit"
      - $ref: "#/definitions/Attributes"
    description: "The type with exactly one value (similar to void in some languages)."
  
  Field:
    type: object
    required: ["name", "tpe"]
    properties:
      name:
        $ref: "#/definitions/Name"
        description: "Field name."
      tpe:
        $ref: "#/definitions/Type"
        description: "Field type."
    description: "A field in a record type."
  
  # ===== Type Specifications =====
  
  TypeSpecification:
    description: "Defines the interface of a type without implementation details."
    oneOf:
      - $ref: "#/definitions/TypeAliasSpecification"
      - $ref: "#/definitions/OpaqueTypeSpecification"
      - $ref: "#/definitions/CustomTypeSpecification"
      - $ref: "#/definitions/DerivedTypeSpecification"
  
  TypeAliasSpecification:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "TypeAliasSpecification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - $ref: "#/definitions/Type"
    description: "An alias for another type."
  
  OpaqueTypeSpecification:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "OpaqueTypeSpecification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
    description: |
      A type with unknown structure. The implementation is hidden from consumers.
  
  CustomTypeSpecification:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "CustomTypeSpecification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - $ref: "#/definitions/Constructors"
    description: "A tagged union type (sum type)."
  
  DerivedTypeSpecification:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "DerivedTypeSpecification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - type: object
        required: ["baseType", "fromBaseType", "toBaseType"]
        properties:
          baseType:
            $ref: "#/definitions/Type"
            description: "The type used for serialization."
          fromBaseType:
            $ref: "#/definitions/FQName"
            description: "Function to convert from base type."
          toBaseType:
            $ref: "#/definitions/FQName"
            description: "Function to convert to base type."
        description: "Details for derived type."
    description: |
      A type with platform-specific representation but known serialization.
  
  # ===== Type Definitions =====
  
  TypeDefinition:
    description: "Provides the complete implementation of a type."
    oneOf:
      - $ref: "#/definitions/TypeAliasDefinition"
      - $ref: "#/definitions/CustomTypeDefinition"
  
  TypeAliasDefinition:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "TypeAliasDefinition"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - $ref: "#/definitions/Type"
    description: "Complete definition of a type alias."
  
  CustomTypeDefinition:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "CustomTypeDefinition"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - allOf:
          - $ref: "#/definitions/AccessControlled"
          - properties:
              value:
                $ref: "#/definitions/Constructors"
    description: |
      Complete definition of a custom type. If constructors are Private, 
      the specification becomes OpaqueTypeSpecification.
  
  Constructors:
    type: array
    items:
      type: array
      minItems: 2
      maxItems: 2
      items:
        - $ref: "#/definitions/Name"
        - type: array
          items:
            type: array
            minItems: 2
            maxItems: 2
            items:
              - $ref: "#/definitions/Name"
              - $ref: "#/definitions/Type"
          description: "Constructor arguments as (name, type) pairs."
    description: "Dictionary of constructor names to their typed arguments."
  
  # ===== Value System =====
  
  Value:
    description: |
      A Value is a recursive tree structure representing computations.
      All data and logic in Morphir are represented as value expressions.
    oneOf:
      - $ref: "#/definitions/LiteralValue"
      - $ref: "#/definitions/ConstructorValue"
      - $ref: "#/definitions/TupleValue"
      - $ref: "#/definitions/ListValue"
      - $ref: "#/definitions/RecordValue"
      - $ref: "#/definitions/VariableValue"
      - $ref: "#/definitions/ReferenceValue"
      - $ref: "#/definitions/FieldValue"
      - $ref: "#/definitions/FieldFunctionValue"
      - $ref: "#/definitions/ApplyValue"
      - $ref: "#/definitions/LambdaValue"
      - $ref: "#/definitions/LetDefinitionValue"
      - $ref: "#/definitions/LetRecursionValue"
      - $ref: "#/definitions/DestructureValue"
      - $ref: "#/definitions/IfThenElseValue"
      - $ref: "#/definitions/PatternMatchValue"
      - $ref: "#/definitions/UpdateRecordValue"
      - $ref: "#/definitions/UnitValue"
  
  LiteralValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Literal"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Literal"
    description: "A literal constant value."
  
  ConstructorValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Constructor"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
    description: "Reference to a custom type constructor."
  
  TupleValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Tuple"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Value"
        description: "Element values in order."
    description: "A tuple value with multiple elements."
  
  ListValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "List"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Value"
        description: "List elements."
    description: "A list of values."
  
  RecordValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Record"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Value"
        description: "Dictionary mapping field names to values."
    description: "A record value with named fields."
  
  VariableValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Variable"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
    description: "Reference to a variable in scope."
  
  ReferenceValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Reference"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
    description: "Reference to a defined value (function or constant)."
  
  FieldValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "Field"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Name"
    description: "Field access on a record. Items: [tag, attributes, recordExpr, fieldName]"
  
  FieldFunctionValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "FieldFunction"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
    description: "A function that extracts a field (e.g., .firstName)."
  
  ApplyValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "Apply"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
    description: |
      Function application. Items: [tag, attributes, function, argument].
      Multi-argument calls are represented via currying (nested Apply nodes).
  
  LambdaValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "Lambda"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Value"
    description: |
      Anonymous function (lambda abstraction).
      Items: [tag, attributes, argumentPattern, body]
  
  LetDefinitionValue:
    type: array
    minItems: 5
    maxItems: 5
    items:
      - const: "LetDefinition"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
      - $ref: "#/definitions/ValueDefinition"
      - $ref: "#/definitions/Value"
    description: |
      A let binding introducing a single value.
      Items: [tag, attributes, bindingName, definition, inExpr]
  
  LetRecursionValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "LetRecursion"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/ValueDefinition"
        description: "Multiple bindings that can reference each other."
      - $ref: "#/definitions/Value"
    description: "Mutually recursive let bindings."
  
  DestructureValue:
    type: array
    minItems: 5
    maxItems: 5
    items:
      - const: "Destructure"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
    description: |
      Pattern-based destructuring.
      Items: [tag, attributes, pattern, valueToDestructure, inExpr]
  
  IfThenElseValue:
    type: array
    minItems: 5
    maxItems: 5
    items:
      - const: "IfThenElse"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
    description: |
      Conditional expression.
      Items: [tag, attributes, condition, thenBranch, elseBranch]
  
  PatternMatchValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "PatternMatch"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Pattern"
            - $ref: "#/definitions/Value"
        description: "List of pattern-branch pairs."
    description: "Pattern matching with multiple cases."
  
  UpdateRecordValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "UpdateRecord"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Value"
        description: "Fields to update with new values."
    description: |
      Record update expression (immutable copy-on-update).
      Items: [tag, attributes, recordToUpdate, fieldsToUpdate]
  
  UnitValue:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "Unit"
      - $ref: "#/definitions/Attributes"
    description: "The unit value (the single value of the Unit type)."
  
  # ===== Literals =====
  
  Literal:
    description: "Represents literal constant values."
    oneOf:
      - $ref: "#/definitions/BoolLiteral"
      - $ref: "#/definitions/CharLiteral"
      - $ref: "#/definitions/StringLiteral"
      - $ref: "#/definitions/WholeNumberLiteral"
      - $ref: "#/definitions/FloatLiteral"
      - $ref: "#/definitions/DecimalLiteral"
  
  BoolLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "BoolLiteral"
      - type: boolean
    description: "Boolean literal (true or false)."
  
  CharLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "CharLiteral"
      - type: string
        minLength: 1
        maxLength: 1
    description: "Single character literal."
  
  StringLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "StringLiteral"
      - type: string
    description: "Text string literal."
  
  WholeNumberLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "WholeNumberLiteral"
      - type: integer
    description: "Integer literal."
  
  FloatLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "FloatLiteral"
      - type: number
    description: "Floating-point number literal."
  
  DecimalLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "DecimalLiteral"
      - type: string
        pattern: "^-?[0-9]+(\\.[0-9]+)?$"
    description: "Arbitrary-precision decimal literal (stored as string)."
  
  # ===== Patterns =====
  
  Pattern:
    description: |
      Patterns are used for destructuring and filtering values.
      They appear in lambda, let destructure, and pattern match expressions.
    oneOf:
      - $ref: "#/definitions/WildcardPattern"
      - $ref: "#/definitions/AsPattern"
      - $ref: "#/definitions/TuplePattern"
      - $ref: "#/definitions/ConstructorPattern"
      - $ref: "#/definitions/EmptyListPattern"
      - $ref: "#/definitions/HeadTailPattern"
      - $ref: "#/definitions/LiteralPattern"
      - $ref: "#/definitions/UnitPattern"
  
  WildcardPattern:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "WildcardPattern"
      - $ref: "#/definitions/Attributes"
    description: "Matches any value without binding (the _ pattern)."
  
  AsPattern:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "AsPattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Name"
    description: |
      Binds a name to a value matched by a nested pattern.
      Simple variable binding is AsPattern with WildcardPattern nested.
      Items: [tag, attributes, nestedPattern, variableName]
  
  TuplePattern:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "TuplePattern"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Pattern"
        description: "Patterns for each tuple element."
    description: "Matches a tuple by matching each element."
  
  ConstructorPattern:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "ConstructorPattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
      - type: array
        items:
          $ref: "#/definitions/Pattern"
        description: "Patterns for constructor arguments."
    description: "Matches a specific type constructor and its arguments."
  
  EmptyListPattern:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "EmptyListPattern"
      - $ref: "#/definitions/Attributes"
    description: "Matches an empty list (the [] pattern)."
  
  HeadTailPattern:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "HeadTailPattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Pattern"
    description: |
      Matches a non-empty list by head and tail (the x :: xs pattern).
      Items: [tag, attributes, headPattern, tailPattern]
  
  LiteralPattern:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "LiteralPattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Literal"
    description: "Matches an exact literal value."
  
  UnitPattern:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "UnitPattern"
      - $ref: "#/definitions/Attributes"
    description: "Matches the unit value."
  
  # ===== Value Specifications and Definitions =====
  
  ValueSpecification:
    type: object
    required: ["inputs", "output"]
    properties:
      inputs:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Type"
        description: "Function parameters as (name, type) pairs."
      output:
        $ref: "#/definitions/Type"
        description: "The return type."
    description: |
      The type signature of a value or function.
      Contains only type information, no implementation.
  
  ValueDefinition:
    type: object
    required: ["inputTypes", "outputType", "body"]
    properties:
      inputTypes:
        type: array
        items:
          type: array
          minItems: 3
          maxItems: 3
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Attributes"
            - $ref: "#/definitions/Type"
        description: "Function parameters as (name, attributes, type) tuples."
      outputType:
        $ref: "#/definitions/Type"
        description: "The return type."
      body:
        $ref: "#/definitions/Value"
        description: "The value expression implementing the logic."
    description: |
      The complete implementation of a value or function.
      Contains both type information and implementation.

2 - Schema Version 2

Morphir IR JSON Schema for format version 2

Morphir IR Schema - Version 2

Format version 2 introduced capitalized tags for distribution, access control, and types, while keeping value and pattern tags lowercase.

Overview

Version 2 of the Morphir IR format represents a transition between version 1 (all lowercase) and version 3 (all capitalized). It uses capitalized tags for distribution, access control, and types, but keeps value expressions and patterns in lowercase.

Key Characteristics

Tag Capitalization

Version 2 uses a mixed capitalization approach:

Capitalized:

  • Distribution: "Library" (capitalized)
  • Access Control: "Public" and "Private" (capitalized)
  • Type Tags: "Variable", "Reference", "Tuple", "Record", etc.

Lowercase:

  • Value Tags: "apply", "lambda", "let_definition", etc.
  • Pattern Tags: "as_pattern", "wildcard_pattern", etc.
  • Literal Tags: "bool_literal", "string_literal", etc.

Module Structure

Version 2 changed the module structure from objects to arrays:

modules:
  type: array
  items:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - $ref: "#/definitions/ModuleName"
      - allOf:
          - $ref: "#/definitions/AccessControlled"
          - properties:
              value:
                $ref: "#/definitions/ModuleDefinition"

This is a significant change from version 1’s {"name": ..., "def": ...} structure.

Core Concepts

Naming System

The Morphir IR uses a sophisticated naming system independent of any specific naming convention.

Name

A Name represents a human-readable identifier made up of one or more words.

  • Structure: Array of lowercase word strings
  • Purpose: Atomic unit for all identifiers
  • Example: ["value", "in", "u", "s", "d"] renders as valueInUSD or value_in_USD
Name:
  type: array
  items:
    type: string
    pattern: "^[a-z][a-z0-9]*$"
  minItems: 1

Path

A Path represents a hierarchical location in the IR structure.

  • Structure: List of Names
  • Purpose: Identifies packages and modules
  • Example: [["morphir"], ["s", "d", "k"], ["string"]] for the String module
Path:
  type: array
  items:
    $ref: "#/definitions/Name"
  minItems: 1

Fully-Qualified Name (FQName)

Provides globally unique identifiers for types and values.

  • Structure: [packagePath, modulePath, localName]
  • Purpose: Unambiguous references across package boundaries
FQName:
  type: array
  minItems: 3
  maxItems: 3
  items:
    - $ref: "#/definitions/PackageName"
    - $ref: "#/definitions/ModuleName"
    - $ref: "#/definitions/Name"

Access Control

AccessControlled

Manages visibility of types and values.

  • Structure: {access, value}
  • Access levels: "Public" (visible externally) or "Private" (package-only)
  • Purpose: Controls API exposure
  • Version 2 note: Capitalized access levels ("Public", "Private")
AccessControlled:
  type: object
  required: ["access", "value"]
  properties:
    access:
      type: string
      enum: ["Public", "Private"]
    value:
      description: "The value being access controlled."

Distribution and Package Structure

Distribution

A Distribution represents a complete, self-contained package with all dependencies.

  • Current type: Library (only supported distribution type)
  • Structure: ["Library", packageName, dependencies, packageDefinition]
  • Purpose: Output of compilation process, ready for execution or transformation
  • Version 2 note: Uses capitalized "Library" tag
distribution:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - type: string
      const: "Library"
    - $ref: "#/definitions/PackageName"
    - $ref: "#/definitions/Dependencies"
    - $ref: "#/definitions/PackageDefinition"

Package Definition

Complete implementation of a package with all details.

  • Contains: All modules (public and private)
  • Includes: Type signatures and implementations
  • Purpose: Full package representation for processing
  • Version 2 note: Modules stored as arrays of [name, accessControlledDefinition] pairs

Package Specification

Public interface of a package.

  • Contains: Only publicly exposed modules
  • Includes: Only type signatures, no implementations
  • Purpose: Dependency interface

Module Structure Details

Module Definition

Complete implementation of a module.

  • Contains: All types and values (public and private) with implementations
  • Structure: Dictionary of type names to AccessControlled type definitions, and value names to AccessControlled value definitions
  • Purpose: Complete module implementation

Module Specification

Public interface of a module.

  • Contains: Only publicly exposed types and values
  • Includes: Type signatures only, no implementations
  • Purpose: Module’s public API

Type System

The type system is based on functional programming principles, supporting:

Type Expressions

Version 2 note: Type tags are capitalized in version 2.

Variable

Represents a type variable (generic parameter).

  • Structure: ["Variable", attributes, name]
  • Example: The a in List a
  • Purpose: Enables polymorphic types
VariableType:
  type: array
  minItems: 3
  maxItems: 3
  items:
    - const: "Variable"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/Name"

Reference

Reference to another type or type alias.

  • Structure: ["Reference", attributes, fqName, typeArgs]
  • Examples: String, List Int, Maybe a
  • Purpose: References built-in types, custom types, or type aliases
ReferenceType:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - const: "Reference"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/FQName"
    - type: array
      items:
        $ref: "#/definitions/Type"

Tuple

Composition of multiple types in fixed order.

  • Structure: ["Tuple", attributes, elementTypes]
  • Example: (Int, String, Bool)
  • Purpose: Product types with positional access

Record

Composition of named fields with types.

  • Structure: ["Record", attributes, fields]
  • Example: {firstName: String, age: Int}
  • Purpose: Product types with named field access
  • Note: All fields are required

Function

Function type representation.

  • Structure: ["Function", attributes, argType, returnType]
  • Example: Int -> String
  • Purpose: Represents function and lambda types
  • Note: Multi-argument functions use currying (nested Function types)

Type Specifications

TypeAliasSpecification

An alias for another type.

  • Structure: ["TypeAliasSpecification", typeParams, aliasedType]
  • Example: type alias UserId = String
  • Purpose: Meaningful name for type expression

CustomTypeSpecification

Tagged union type (sum type).

  • Structure: ["CustomTypeSpecification", typeParams, constructors]
  • Example: type Result e a = Ok a | Err e
  • Purpose: Choice between multiple alternatives

OpaqueTypeSpecification

Type with unknown structure.

  • Structure: ["OpaqueTypeSpecification", typeParams]
  • Characteristics: Structure hidden, no automatic serialization
  • Purpose: Encapsulates implementation details

Value System

All data and logic in Morphir are represented as value expressions.

Version 2 note: Value tags are lowercase in version 2.

Value Expressions

literal

Literal constant value.

  • Structure: ["literal", attributes, literal]
  • Types: bool_literal, char_literal, string_literal, whole_number_literal, float_literal, decimal_literal
  • Purpose: Represents constant data

variable

Reference to a variable in scope.

  • Structure: ["variable", attributes, name]
  • Example: References to function parameters or let-bound variables
  • Purpose: Accesses values bound in current scope

reference

Reference to a defined value (function or constant).

  • Structure: ["reference", attributes, fqName]
  • Example: Morphir.SDK.List.map, Basics.add
  • Purpose: Invokes or references defined functions

apply

Function application.

  • Structure: ["apply", attributes, function, argument]
  • Example: add 1 2 (nested apply nodes for currying)
  • Purpose: Invokes functions with arguments
ApplyValue:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - const: "apply"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/Value"
    - $ref: "#/definitions/Value"

lambda

Anonymous function.

  • Structure: ["lambda", attributes, argumentPattern, body]
  • Example: \x -> x + 1
  • Purpose: Creates inline functions
LambdaValue:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - const: "lambda"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/Pattern"
    - $ref: "#/definitions/Value"

let_definition

Let binding introducing a single value.

  • Structure: ["let_definition", attributes, bindingName, definition, inExpr]
  • Example: let x = 5 in x + x
  • Purpose: Introduces local bindings

if_then_else

Conditional expression.

  • Structure: ["if_then_else", attributes, condition, thenBranch, elseBranch]
  • Example: if x > 0 then "positive" else "non-positive"
  • Purpose: Conditional logic

pattern_match

Pattern matching with multiple cases.

  • Structure: ["pattern_match", attributes, valueToMatch, cases]
  • Example: case maybeValue of Just x -> x; Nothing -> 0
  • Purpose: Conditional logic based on structure

Patterns

Used for destructuring and filtering values.

Version 2 note: Pattern tags are lowercase in version 2.

wildcard_pattern

Matches any value without binding.

  • Structure: ["wildcard_pattern", attributes]
  • Syntax: _
  • Purpose: Ignores a value

as_pattern

Binds a name to a matched value.

  • Structure: ["as_pattern", attributes, nestedPattern, variableName]
  • Special case: Simple variable binding uses as_pattern with wildcard_pattern
  • Purpose: Captures matched values

constructor_pattern

Matches specific type constructor and arguments.

  • Structure: ["constructor_pattern", attributes, fqName, argPatterns]
  • Example: Just x matches Just with pattern x
  • Purpose: Destructures and filters tagged unions

Literals

Version 2 note: Literal tags are lowercase in version 2.

bool_literal

Boolean literal.

  • Structure: ["bool_literal", boolean]
  • Values: true or false
BoolLiteral:
  type: array
  minItems: 2
  maxItems: 2
  items:
    - const: "bool_literal"
    - type: boolean

string_literal

Text string literal.

  • Structure: ["string_literal", string]
  • Example: "hello"
StringLiteral:
  type: array
  minItems: 2
  maxItems: 2
  items:
    - const: "string_literal"
    - type: string

whole_number_literal

Integer literal.

  • Structure: ["whole_number_literal", integer]
  • Example: 42, -17

Migration from Version 2

When migrating from version 2 to version 3:

  1. Capitalize value tags: "apply""Apply", "lambda""Lambda", etc.
  2. Capitalize pattern tags: "as_pattern""AsPattern", "wildcard_pattern""WildcardPattern", etc.
  3. Capitalize literal tags: "bool_literal""BoolLiteral", "string_literal""StringLiteral", etc.

Full Schema

For the complete schema definition, see the full schema page.

References

2.1 - What's New in Version 2

Changes and improvements in Morphir IR schema version 2

What’s New in Version 2

Version 2 of the Morphir IR schema introduces partial capitalization and a new module structure, representing a significant evolution from version 1.

Key Changes from Version 1

Partial Capitalization

Version 2 introduces capitalization for distribution, access control, and type-related tags:

Capitalized Tags

Distribution:

  • "library""Library"

Access Control:

  • "public""Public"
  • "private""Private"

Type Tags:

  • "variable""Variable"
  • "reference""Reference"
  • "tuple""Tuple"
  • "record""Record"
  • "extensible_record""ExtensibleRecord"
  • "function""Function"
  • "unit""Unit"

Type Specifications:

  • "type_alias_specification""TypeAliasSpecification"
  • "opaque_type_specification""OpaqueTypeSpecification"
  • "custom_type_specification""CustomTypeSpecification"
  • "derived_type_specification""DerivedTypeSpecification"

Type Definitions:

  • "type_alias_definition""TypeAliasDefinition"
  • "custom_type_definition""CustomTypeDefinition"

Unchanged (Lowercase) Tags

Value expressions remain lowercase:

  • "apply", "lambda", "let_definition", "if_then_else", etc.

Patterns remain lowercase:

  • "wildcard_pattern", "as_pattern", "constructor_pattern", etc.

Literals remain lowercase:

  • "bool_literal", "string_literal", "whole_number_literal", etc.

New Module Structure

Version 2 changes how modules are represented in packages:

Version 1 Structure

{
  "modules": [
    {
      "name": [["my"], ["module"]],
      "def": ["public", { ... }]
    }
  ]
}

Version 2 Structure

{
  "modules": [
    [
      [["my"], ["module"]],
      {
        "access": "Public",
        "value": { ... }
      }
    ]
  ]
}

Changes:

  • Modules are now represented as arrays instead of objects
  • Structure changed from {"name": ..., "def": ...} to [modulePath, accessControlled]
  • Access control uses the new AccessControlled wrapper with capitalized values

Access Control Wrapper

Version 2 introduces a structured AccessControlled wrapper:

AccessControlled:
  type: object
  required: ["access", "value"]
  properties:
    access:
      type: string
      enum: ["Public", "Private"]
    value:
      description: "The value being access controlled."

This provides a consistent way to manage visibility across types and values.

Benefits

Improved Clarity

  • Capitalized type tags stand out more clearly in JSON structures
  • Structured access control makes visibility explicit and consistent
  • Array-based module structure is more compact and follows the pattern used elsewhere in the IR

Better Type Safety

The structured AccessControlled wrapper provides:

  • Explicit access level declaration
  • Type-safe representation
  • Easier validation

Foundation for Version 3

Version 2 serves as a transition toward the fully capitalized format in version 3, making eventual migration easier.

Migration from Version 1

To migrate from version 1 to version 2:

  1. Capitalize distribution tag: "library""Library"
  2. Capitalize access control: "public""Public", "private""Private"
  3. Update module structure: Convert {"name": ..., "def": ...} to array format
  4. Capitalize all type tags: "variable""Variable", "reference""Reference", etc.
  5. Capitalize type specification and definition tags

Looking Forward

While version 2 introduces important improvements, version 3 completes the capitalization by extending it to value expressions, patterns, and literals. For new projects, consider using version 3 directly for maximum consistency.

See Also

2.2 - Full Schema

Complete Morphir IR JSON Schema for format version 2

Morphir IR Schema Version 2 - Complete Schema

This page contains the complete JSON schema definition for Morphir IR format version 2.

Download

You can download the schema file directly: morphir-ir-v2.yaml

Usage

This schema can be used to validate Morphir IR JSON files in format version 2:

# Using Python jsonschema (recommended for YAML schemas)
pip install jsonschema pyyaml
python -c "import json, yaml, jsonschema; \
  schema = yaml.safe_load(open('morphir-ir-v2.yaml')); \
  data = json.load(open('your-morphir-ir.json')); \
  jsonschema.validate(data, schema); \
  print('✓ Valid Morphir IR v2')"

References


Appendix: Complete Schema Definition

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
# JSON Schema for Morphir IR Format Version 2
# This schema defines the structure of a Morphir IR distribution in version 2 format.
# Format version 2 uses capitalized tag names (e.g., "Library", "Public", "Variable").

$schema: "http://json-schema.org/draft-07/schema#"
$id: "https://finos.github.io/morphir/schemas/morphir-ir-v2.yaml"
title: "Morphir IR Distribution (Version 2)"
description: |
  A Morphir IR distribution represents a complete, self-contained package of business logic
  with all its dependencies. It captures the semantics of functional programs in a
  language-independent, platform-agnostic format.
  
  This is format version 2, which differs from version 3 primarily in tag capitalization.

type: object
required:
  - formatVersion
  - distribution
properties:
  formatVersion:
    type: integer
    const: 2
    description: "The version of the IR format. Must be 2 for this schema."
  
  distribution:
    description: "The distribution data, currently only Library distributions are supported."
    type: array
    minItems: 4
    maxItems: 4
    items:
      - type: string
        const: "Library"
        description: "Distribution type (capitalized in v2)."
      - $ref: "#/definitions/PackageName"
      - $ref: "#/definitions/Dependencies"
      - $ref: "#/definitions/PackageDefinition"

definitions:
  # ===== Basic Building Blocks =====
  
  Name:
    type: array
    items:
      type: string
      pattern: "^[a-z][a-z0-9]*$"
    minItems: 1
    description: |
      A Name is a list of lowercase words that represents a human-readable identifier.
      Example: ["value", "in", "u", "s", "d"] can be rendered as valueInUSD or value_in_USD.
  
  Path:
    type: array
    items:
      $ref: "#/definitions/Name"
    minItems: 1
    description: |
      A Path is a list of Names representing a hierarchical location in the IR structure.
      Used for package names and module names.
  
  PackageName:
    $ref: "#/definitions/Path"
    description: "Globally unique identifier for a package."
  
  ModuleName:
    $ref: "#/definitions/Path"
    description: "Unique identifier for a module within a package."
  
  FQName:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - $ref: "#/definitions/PackageName"
      - $ref: "#/definitions/ModuleName"
      - $ref: "#/definitions/Name"
    description: |
      Fully-Qualified Name that provides a globally unique identifier for any type or value.
      Consists of [packagePath, modulePath, localName].
  
  # ===== Attributes =====
  
  Attributes:
    type: object
    description: |
      Attributes can be attached to various nodes in the IR for extensibility.
      When no additional information is needed, an empty object {} is used.
  
  # ===== Access Control =====
  
  AccessControlled:
    type: object
    required: ["access", "value"]
    properties:
      access:
        type: string
        enum: ["Public", "Private"]
        description: "Controls visibility of types and values (capitalized in v2)."
      value:
        description: "The value being access controlled."
    description: "Wrapper that manages visibility of types and values."
  
  # Note: Documented is not a separate schema definition because it's encoded conditionally.
  # When documentation exists, the JSON has both "doc" and "value" fields.
  # When documentation is absent, the JSON contains only the documented element directly (no wrapper).
  # This is handled inline in the definitions that use Documented.
  
  # ===== Distribution Structure =====
  
  Dependencies:
    type: array
    items:
      type: array
      minItems: 2
      maxItems: 2
      items:
        - $ref: "#/definitions/PackageName"
        - $ref: "#/definitions/PackageSpecification"
    description: "Dictionary of package dependencies, contains only type signatures."
  
  PackageDefinition:
    type: object
    required: ["modules"]
    properties:
      modules:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/ModuleName"
            - allOf:
                - $ref: "#/definitions/AccessControlled"
                - properties:
                    value:
                      $ref: "#/definitions/ModuleDefinition"
        description: "All modules in the package (public and private)."
    description: "Complete implementation of a package with all details."
  
  PackageSpecification:
    type: object
    required: ["modules"]
    properties:
      modules:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/ModuleName"
            - $ref: "#/definitions/ModuleSpecification"
        description: "Public modules only."
    description: "Public interface of a package, contains only type signatures."
  
  # ===== Module Structure =====
  
  ModuleDefinition:
    type: object
    required: ["types", "values"]
    properties:
      types:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - allOf:
                - $ref: "#/definitions/AccessControlled"
                - properties:
                    value:
                      # Documented wrapper: can have "doc" and "value", or just the type definition directly
                      oneOf:
                        - type: object
                          required: ["doc", "value"]
                          properties:
                            doc:
                              type: string
                            value:
                              $ref: "#/definitions/TypeDefinition"
                        - $ref: "#/definitions/TypeDefinition"
        description: "All type definitions (public and private)."
      values:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - allOf:
                - $ref: "#/definitions/AccessControlled"
                - properties:
                    value:
                      # Documented wrapper: can have "doc" and "value", or just the value definition directly
                      oneOf:
                        - type: object
                          required: ["doc", "value"]
                          properties:
                            doc:
                              type: string
                            value:
                              $ref: "#/definitions/ValueDefinition"
                        - $ref: "#/definitions/ValueDefinition"
        description: "All value definitions (public and private)."
      doc:
        type: string
        description: "Optional documentation for the module."
    description: "Complete implementation of a module."
  
  ModuleSpecification:
    type: object
    required: ["types", "values"]
    properties:
      types:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - oneOf:
                - type: object
                  required: ["doc", "value"]
                  properties:
                    doc:
                      type: string
                    value:
                      $ref: "#/definitions/TypeSpecification"
                - $ref: "#/definitions/TypeSpecification"
        description: "Public type specifications only."
      values:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - oneOf:
                - type: object
                  required: ["doc", "value"]
                  properties:
                    doc:
                      type: string
                    value:
                      $ref: "#/definitions/ValueSpecification"
                - $ref: "#/definitions/ValueSpecification"
        description: "Public value specifications only."
      doc:
        type: string
        description: "Optional documentation for the module."
    description: "Public interface of a module."
  
  # ===== Type System =====
  
  Type:
    description: |
      A Type is a recursive tree structure representing type expressions.
      Tags are capitalized in format version 2.
    oneOf:
      - $ref: "#/definitions/VariableType"
      - $ref: "#/definitions/ReferenceType"
      - $ref: "#/definitions/TupleType"
      - $ref: "#/definitions/RecordType"
      - $ref: "#/definitions/ExtensibleRecordType"
      - $ref: "#/definitions/FunctionType"
      - $ref: "#/definitions/UnitType"
  
  VariableType:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Variable"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
    description: "Represents a type variable (generic parameter)."
  
  ReferenceType:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "Reference"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
      - type: array
        items:
          $ref: "#/definitions/Type"
        description: "Type arguments for generic types."
    description: "Reference to another type or type alias."
  
  TupleType:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Tuple"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Type"
        description: "Element types in order."
    description: "A composition of multiple types in a fixed order (product type)."
  
  RecordType:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "Record"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Field"
        description: "List of field definitions."
    description: "A composition of named fields with their types."
  
  ExtensibleRecordType:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "ExtensibleRecord"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
      - type: array
        items:
          $ref: "#/definitions/Field"
        description: "Known fields."
    description: "A record type that can be extended with additional fields."
  
  FunctionType:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "Function"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Type"
      - $ref: "#/definitions/Type"
    description: |
      Represents a function type. Multi-argument functions are represented via currying.
      Items: [tag, attributes, argumentType, returnType]
  
  UnitType:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "Unit"
      - $ref: "#/definitions/Attributes"
    description: "The type with exactly one value (similar to void in some languages)."
  
  Field:
    type: object
    required: ["name", "tpe"]
    properties:
      name:
        $ref: "#/definitions/Name"
        description: "Field name."
      tpe:
        $ref: "#/definitions/Type"
        description: "Field type."
    description: "A field in a record type."
  
  # ===== Type Specifications =====
  
  TypeSpecification:
    description: "Defines the interface of a type without implementation details."
    oneOf:
      - $ref: "#/definitions/TypeAliasSpecification"
      - $ref: "#/definitions/OpaqueTypeSpecification"
      - $ref: "#/definitions/CustomTypeSpecification"
      - $ref: "#/definitions/DerivedTypeSpecification"
  
  TypeAliasSpecification:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "TypeAliasSpecification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - $ref: "#/definitions/Type"
    description: "An alias for another type."
  
  OpaqueTypeSpecification:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "OpaqueTypeSpecification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
    description: |
      A type with unknown structure. The implementation is hidden from consumers.
  
  CustomTypeSpecification:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "CustomTypeSpecification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - $ref: "#/definitions/Constructors"
    description: "A tagged union type (sum type)."
  
  DerivedTypeSpecification:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "DerivedTypeSpecification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - type: object
        required: ["baseType", "fromBaseType", "toBaseType"]
        properties:
          baseType:
            $ref: "#/definitions/Type"
            description: "The type used for serialization."
          fromBaseType:
            $ref: "#/definitions/FQName"
            description: "Function to convert from base type."
          toBaseType:
            $ref: "#/definitions/FQName"
            description: "Function to convert to base type."
        description: "Details for derived type."
    description: |
      A type with platform-specific representation but known serialization.
  
  # ===== Type Definitions =====
  
  TypeDefinition:
    description: "Provides the complete implementation of a type."
    oneOf:
      - $ref: "#/definitions/TypeAliasDefinition"
      - $ref: "#/definitions/CustomTypeDefinition"
  
  TypeAliasDefinition:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "TypeAliasDefinition"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - $ref: "#/definitions/Type"
    description: "Complete definition of a type alias."
  
  CustomTypeDefinition:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "CustomTypeDefinition"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - allOf:
          - $ref: "#/definitions/AccessControlled"
          - properties:
              value:
                $ref: "#/definitions/Constructors"
    description: |
      Complete definition of a custom type. If constructors are Private, 
      the specification becomes OpaqueTypeSpecification.
  
  Constructors:
    type: array
    items:
      type: array
      minItems: 2
      maxItems: 2
      items:
        - $ref: "#/definitions/Name"
        - type: array
          items:
            type: array
            minItems: 2
            maxItems: 2
            items:
              - $ref: "#/definitions/Name"
              - $ref: "#/definitions/Type"
          description: "Constructor arguments as (name, type) pairs."
    description: "Dictionary of constructor names to their typed arguments."
  
  # ===== Value System =====
  # Value expressions use lowercase tags in v2 (e.g., "apply", "lambda")
  
  Value:
    description: |
      A Value is a recursive tree structure representing computations.
      All data and logic in Morphir are represented as value expressions.
      Note: Value tags are lowercase in format version 2.
    oneOf:
      - $ref: "#/definitions/LiteralValue"
      - $ref: "#/definitions/ConstructorValue"
      - $ref: "#/definitions/TupleValue"
      - $ref: "#/definitions/ListValue"
      - $ref: "#/definitions/RecordValue"
      - $ref: "#/definitions/VariableValue"
      - $ref: "#/definitions/ReferenceValue"
      - $ref: "#/definitions/FieldValue"
      - $ref: "#/definitions/FieldFunctionValue"
      - $ref: "#/definitions/ApplyValue"
      - $ref: "#/definitions/LambdaValue"
      - $ref: "#/definitions/LetDefinitionValue"
      - $ref: "#/definitions/LetRecursionValue"
      - $ref: "#/definitions/DestructureValue"
      - $ref: "#/definitions/IfThenElseValue"
      - $ref: "#/definitions/PatternMatchValue"
      - $ref: "#/definitions/UpdateRecordValue"
      - $ref: "#/definitions/UnitValue"
  
  LiteralValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "literal"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Literal"
    description: "A literal constant value."
  
  ConstructorValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "constructor"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
    description: "Reference to a custom type constructor."
  
  TupleValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "tuple"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Value"
        description: "Element values in order."
    description: "A tuple value with multiple elements."
  
  ListValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "list"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Value"
        description: "List elements."
    description: "A list of values."
  
  RecordValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "record"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Value"
        description: "Dictionary mapping field names to values."
    description: "A record value with named fields."
  
  VariableValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "variable"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
    description: "Reference to a variable in scope."
  
  ReferenceValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "reference"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
    description: "Reference to a defined value (function or constant)."
  
  FieldValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "field"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Name"
    description: "Field access on a record. Items: [tag, attributes, recordExpr, fieldName]"
  
  FieldFunctionValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "field_function"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
    description: "A function that extracts a field (e.g., .firstName)."
  
  ApplyValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "apply"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
    description: |
      Function application. Items: [tag, attributes, function, argument].
      Multi-argument calls are represented via currying (nested Apply nodes).
  
  LambdaValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "lambda"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Value"
    description: |
      Anonymous function (lambda abstraction).
      Items: [tag, attributes, argumentPattern, body]
  
  LetDefinitionValue:
    type: array
    minItems: 5
    maxItems: 5
    items:
      - const: "let_definition"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
      - $ref: "#/definitions/ValueDefinition"
      - $ref: "#/definitions/Value"
    description: |
      A let binding introducing a single value.
      Items: [tag, attributes, bindingName, definition, inExpr]
  
  LetRecursionValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "let_recursion"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/ValueDefinition"
        description: "Multiple bindings that can reference each other."
      - $ref: "#/definitions/Value"
    description: "Mutually recursive let bindings."
  
  DestructureValue:
    type: array
    minItems: 5
    maxItems: 5
    items:
      - const: "destructure"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
    description: |
      Pattern-based destructuring.
      Items: [tag, attributes, pattern, valueToDestructure, inExpr]
  
  IfThenElseValue:
    type: array
    minItems: 5
    maxItems: 5
    items:
      - const: "if_then_else"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
    description: |
      Conditional expression.
      Items: [tag, attributes, condition, thenBranch, elseBranch]
  
  PatternMatchValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "pattern_match"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Pattern"
            - $ref: "#/definitions/Value"
        description: "List of pattern-branch pairs."
    description: "Pattern matching with multiple cases."
  
  UpdateRecordValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "update_record"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Value"
        description: "Fields to update with new values."
    description: |
      Record update expression (immutable copy-on-update).
      Items: [tag, attributes, recordToUpdate, fieldsToUpdate]
  
  UnitValue:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "unit"
      - $ref: "#/definitions/Attributes"
    description: "The unit value (the single value of the Unit type)."
  
  # ===== Literals =====
  
  Literal:
    description: "Represents literal constant values."
    oneOf:
      - $ref: "#/definitions/BoolLiteral"
      - $ref: "#/definitions/CharLiteral"
      - $ref: "#/definitions/StringLiteral"
      - $ref: "#/definitions/WholeNumberLiteral"
      - $ref: "#/definitions/FloatLiteral"
      - $ref: "#/definitions/DecimalLiteral"
  
  BoolLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "bool_literal"
      - type: boolean
    description: "Boolean literal (true or false)."
  
  CharLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "char_literal"
      - type: string
        minLength: 1
        maxLength: 1
    description: "Single character literal."
  
  StringLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "string_literal"
      - type: string
    description: "Text string literal."
  
  WholeNumberLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "whole_number_literal"
      - type: integer
    description: "Integer literal."
  
  FloatLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "float_literal"
      - type: number
    description: "Floating-point number literal."
  
  DecimalLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "decimal_literal"
      - type: string
        pattern: "^-?[0-9]+(\\.[0-9]+)?$"
    description: "Arbitrary-precision decimal literal (stored as string)."
  
  # ===== Patterns =====
  
  Pattern:
    description: |
      Patterns are used for destructuring and filtering values.
      They appear in lambda, let destructure, and pattern match expressions.
      Pattern tags are lowercase with underscores in format version 2.
    oneOf:
      - $ref: "#/definitions/WildcardPattern"
      - $ref: "#/definitions/AsPattern"
      - $ref: "#/definitions/TuplePattern"
      - $ref: "#/definitions/ConstructorPattern"
      - $ref: "#/definitions/EmptyListPattern"
      - $ref: "#/definitions/HeadTailPattern"
      - $ref: "#/definitions/LiteralPattern"
      - $ref: "#/definitions/UnitPattern"
  
  WildcardPattern:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "wildcard_pattern"
      - $ref: "#/definitions/Attributes"
    description: "Matches any value without binding (the _ pattern)."
  
  AsPattern:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "as_pattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Name"
    description: |
      Binds a name to a value matched by a nested pattern.
      Simple variable binding is AsPattern with WildcardPattern nested.
      Items: [tag, attributes, nestedPattern, variableName]
  
  TuplePattern:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "tuple_pattern"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Pattern"
        description: "Patterns for each tuple element."
    description: "Matches a tuple by matching each element."
  
  ConstructorPattern:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "constructor_pattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
      - type: array
        items:
          $ref: "#/definitions/Pattern"
        description: "Patterns for constructor arguments."
    description: "Matches a specific type constructor and its arguments."
  
  EmptyListPattern:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "empty_list_pattern"
      - $ref: "#/definitions/Attributes"
    description: "Matches an empty list (the [] pattern)."
  
  HeadTailPattern:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "head_tail_pattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Pattern"
    description: |
      Matches a non-empty list by head and tail (the x :: xs pattern).
      Items: [tag, attributes, headPattern, tailPattern]
  
  LiteralPattern:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "literal_pattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Literal"
    description: "Matches an exact literal value."
  
  UnitPattern:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "unit_pattern"
      - $ref: "#/definitions/Attributes"
    description: "Matches the unit value."
  
  # ===== Value Specifications and Definitions =====
  
  ValueSpecification:
    type: object
    required: ["inputs", "output"]
    properties:
      inputs:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Type"
        description: "Function parameters as (name, type) pairs."
      output:
        $ref: "#/definitions/Type"
        description: "The return type."
    description: |
      The type signature of a value or function.
      Contains only type information, no implementation.
  
  ValueDefinition:
    type: object
    required: ["inputTypes", "outputType", "body"]
    properties:
      inputTypes:
        type: array
        items:
          type: array
          minItems: 3
          maxItems: 3
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Attributes"
            - $ref: "#/definitions/Type"
        description: "Function parameters as (name, attributes, type) tuples."
      outputType:
        $ref: "#/definitions/Type"
        description: "The return type."
      body:
        $ref: "#/definitions/Value"
        description: "The value expression implementing the logic."
    description: |
      The complete implementation of a value or function.
      Contains both type information and implementation.

3 - Schema Version 1

Morphir IR JSON Schema for format version 1

Morphir IR Schema - Version 1

Format version 1 is the original Morphir IR format. It uses lowercase tag names throughout and has a different module structure compared to later versions.

Overview

Version 1 of the Morphir IR format uses lowercase tags for all constructs. This includes distribution types, access control levels, type tags, value expression tags, pattern tags, and literal tags.

Key Characteristics

Tag Capitalization

All tags in version 1 are lowercase:

  • Distribution: "library" (not "Library")
  • Access Control: "public" and "private" (not "Public" and "Private")
  • Type Tags: "variable", "reference", "tuple", "record", etc.
  • Value Tags: "apply", "lambda", "let_definition", etc.
  • Pattern Tags: "as_pattern", "wildcard_pattern", "constructor_pattern", etc.
  • Literal Tags: "bool_literal", "string_literal", "whole_number_literal", etc.

Module Structure

In version 1, modules are represented as objects with name and def fields:

ModuleEntry:
  type: object
  required: ["name", "def"]
  properties:
    name:
      $ref: "#/definitions/ModuleName"
    def:
      type: array
      minItems: 2
      maxItems: 2
      items:
        - $ref: "#/definitions/AccessLevel"
        - $ref: "#/definitions/ModuleDefinition"

This differs from version 2+, where modules are represented as arrays: [modulePath, accessControlled].

Core Concepts

Naming System

The Morphir IR uses a sophisticated naming system independent of any specific naming convention.

Name

A Name represents a human-readable identifier made up of one or more words.

  • Structure: Array of lowercase word strings
  • Purpose: Atomic unit for all identifiers
  • Example: ["value", "in", "u", "s", "d"] renders as valueInUSD or value_in_USD
Name:
  type: array
  items:
    type: string
    pattern: "^[a-z][a-z0-9]*$"
  minItems: 1
  description: |
    A Name is a list of lowercase words that represents a human-readable identifier.
    Example: ["value", "in", "u", "s", "d"] can be rendered as valueInUSD or value_in_USD.

Path

A Path represents a hierarchical location in the IR structure.

  • Structure: List of Names
  • Purpose: Identifies packages and modules
  • Example: [["morphir"], ["s", "d", "k"], ["string"]] for the String module
Path:
  type: array
  items:
    $ref: "#/definitions/Name"
  minItems: 1
  description: |
    A Path is a list of Names representing a hierarchical location in the IR structure.

Fully-Qualified Name (FQName)

Provides globally unique identifiers for types and values.

  • Structure: [packagePath, modulePath, localName]
  • Purpose: Unambiguous references across package boundaries
FQName:
  type: array
  minItems: 3
  maxItems: 3
  items:
    - $ref: "#/definitions/PackageName"
    - $ref: "#/definitions/ModuleName"
    - $ref: "#/definitions/Name"

Access Control

Access Levels

Manages visibility of types and values.

  • Levels: "public" (visible externally) or "private" (package-only)
  • Purpose: Controls API exposure
  • Version 1 note: Lowercase access levels ("public", "private")
AccessLevel:
  type: string
  enum: ["public", "private"]

Distribution and Package Structure

Distribution

A Distribution represents a complete, self-contained package with all dependencies.

  • Current type: library (only supported distribution type)
  • Structure: ["library", packageName, dependencies, packageDefinition]
  • Purpose: Output of compilation process, ready for execution or transformation
  • Version 1 note: Uses lowercase "library" tag
distribution:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - type: string
      const: "library"
    - $ref: "#/definitions/PackageName"
    - $ref: "#/definitions/Dependencies"
    - $ref: "#/definitions/PackageDefinition"

Package Definition

Complete implementation of a package with all details.

  • Contains: All modules (public and private)
  • Includes: Type signatures and implementations
  • Purpose: Full package representation for processing
  • Version 1 note: Modules stored as objects with {"name": ..., "def": [accessLevel, moduleDefinition]}

Package Specification

Public interface of a package.

  • Contains: Only publicly exposed modules
  • Includes: Only type signatures, no implementations
  • Purpose: Dependency interface

Module Structure Details

Module Entry (Version 1 specific)

In version 1, modules use an object structure with explicit name and def fields:

ModuleEntry:
  type: object
  required: ["name", "def"]
  properties:
    name:
      $ref: "#/definitions/ModuleName"
    def:
      type: array
      minItems: 2
      maxItems: 2
      items:
        - $ref: "#/definitions/AccessLevel"
        - $ref: "#/definitions/ModuleDefinition"

Module Definition

Complete implementation of a module.

  • Contains: All types and values (public and private) with implementations
  • Structure: Dictionary of type names to type definitions, and value names to value definitions
  • Purpose: Complete module implementation

Module Specification

Public interface of a module.

  • Contains: Only publicly exposed types and values
  • Includes: Type signatures only, no implementations
  • Purpose: Module’s public API

Type System

The type system is based on functional programming principles, supporting:

Version 1 note: All type tags are lowercase in version 1.

Type Expressions

variable

Represents a type variable (generic parameter).

  • Structure: ["variable", attributes, name]
  • Example: The a in List a
  • Purpose: Enables polymorphic types
VariableType:
  type: array
  minItems: 3
  maxItems: 3
  items:
    - const: "variable"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/Name"

reference

Reference to another type or type alias.

  • Structure: ["reference", attributes, fqName, typeArgs]
  • Examples: String, List Int, Maybe a
  • Purpose: References built-in types, custom types, or type aliases
ReferenceType:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - const: "reference"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/FQName"
    - type: array
      items:
        $ref: "#/definitions/Type"

tuple

Composition of multiple types in fixed order.

  • Structure: ["tuple", attributes, elementTypes]
  • Example: (Int, String, Bool)
  • Purpose: Product types with positional access

record

Composition of named fields with types.

  • Structure: ["record", attributes, fields]
  • Example: {firstName: String, age: Int}
  • Purpose: Product types with named field access
  • Note: All fields are required

function

Function type representation.

  • Structure: ["function", attributes, argType, returnType]
  • Example: Int -> String
  • Purpose: Represents function and lambda types
  • Note: Multi-argument functions use currying (nested function types)

Type Specifications

type_alias_specification

An alias for another type.

  • Structure: ["type_alias_specification", typeParams, aliasedType]
  • Example: type alias UserId = String
  • Purpose: Meaningful name for type expression

custom_type_specification

Tagged union type (sum type).

  • Structure: ["custom_type_specification", typeParams, constructors]
  • Example: type Result e a = Ok a | Err e
  • Purpose: Choice between multiple alternatives

opaque_type_specification

Type with unknown structure.

  • Structure: ["opaque_type_specification", typeParams]
  • Characteristics: Structure hidden, no automatic serialization
  • Purpose: Encapsulates implementation details

Value System

All data and logic in Morphir are represented as value expressions.

Version 1 note: All value tags are lowercase in version 1.

Value Expressions

literal

Literal constant value.

  • Structure: ["literal", attributes, literal]
  • Types: bool_literal, char_literal, string_literal, whole_number_literal, float_literal, decimal_literal
  • Purpose: Represents constant data

variable

Reference to a variable in scope.

  • Structure: ["variable", attributes, name]
  • Example: References to function parameters or let-bound variables
  • Purpose: Accesses values bound in current scope

reference

Reference to a defined value (function or constant).

  • Structure: ["reference", attributes, fqName]
  • Example: Morphir.SDK.List.map, Basics.add
  • Purpose: Invokes or references defined functions

apply

Function application.

  • Structure: ["apply", attributes, function, argument]
  • Example: add 1 2 (nested apply nodes for currying)
  • Purpose: Invokes functions with arguments
ApplyValue:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - const: "apply"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/Value"
    - $ref: "#/definitions/Value"

lambda

Anonymous function.

  • Structure: ["lambda", attributes, argumentPattern, body]
  • Example: \x -> x + 1
  • Purpose: Creates inline functions
LambdaValue:
  type: array
  minItems: 4
  maxItems: 4
  items:
    - const: "lambda"
    - $ref: "#/definitions/Attributes"
    - $ref: "#/definitions/Pattern"
    - $ref: "#/definitions/Value"

let_definition

Let binding introducing a single value.

  • Structure: ["let_definition", attributes, bindingName, definition, inExpr]
  • Example: let x = 5 in x + x
  • Purpose: Introduces local bindings

if_then_else

Conditional expression.

  • Structure: ["if_then_else", attributes, condition, thenBranch, elseBranch]
  • Example: if x > 0 then "positive" else "non-positive"
  • Purpose: Conditional logic

pattern_match

Pattern matching with multiple cases.

  • Structure: ["pattern_match", attributes, valueToMatch, cases]
  • Example: case maybeValue of Just x -> x; Nothing -> 0
  • Purpose: Conditional logic based on structure

Patterns

Used for destructuring and filtering values.

Version 1 note: All pattern tags are lowercase in version 1.

wildcard_pattern

Matches any value without binding.

  • Structure: ["wildcard_pattern", attributes]
  • Syntax: _
  • Purpose: Ignores a value

as_pattern

Binds a name to a matched value.

  • Structure: ["as_pattern", attributes, nestedPattern, variableName]
  • Special case: Simple variable binding uses as_pattern with wildcard_pattern
  • Purpose: Captures matched values

constructor_pattern

Matches specific type constructor and arguments.

  • Structure: ["constructor_pattern", attributes, fqName, argPatterns]
  • Example: Just x matches Just with pattern x
  • Purpose: Destructures and filters tagged unions

Literals

Version 1 note: All literal tags are lowercase in version 1.

bool_literal

Boolean literal.

  • Structure: ["bool_literal", boolean]
  • Values: true or false
BoolLiteral:
  type: array
  minItems: 2
  maxItems: 2
  items:
    - const: "bool_literal"
    - type: boolean

string_literal

Text string literal.

  • Structure: ["string_literal", string]
  • Example: "hello"
StringLiteral:
  type: array
  minItems: 2
  maxItems: 2
  items:
    - const: "string_literal"
    - type: string

whole_number_literal

Integer literal.

  • Structure: ["whole_number_literal", integer]
  • Example: 42, -17

Migration from Version 1

When migrating from version 1 to version 2 or 3:

  1. Capitalize distribution tag: "library""Library"
  2. Capitalize access control: "public""Public", "private""Private"
  3. Update module structure: Convert {"name": ..., "def": ...} to [modulePath, accessControlled]
  4. Capitalize type tags: "variable""Variable", etc.
  5. For version 3: Also capitalize value and pattern tags

Full Schema

For the complete schema definition, see the full schema page.

References

3.1 - Full Schema

Complete Morphir IR JSON Schema for format version 1

Morphir IR Schema Version 1 - Complete Schema

This page contains the complete JSON schema definition for Morphir IR format version 1.

Download

You can download the schema file directly: morphir-ir-v1.yaml

Usage

This schema can be used to validate Morphir IR JSON files in format version 1:

# Using Python jsonschema (recommended for YAML schemas)
pip install jsonschema pyyaml
python -c "import json, yaml, jsonschema; \
  schema = yaml.safe_load(open('morphir-ir-v1.yaml')); \
  data = json.load(open('your-morphir-ir.json')); \
  jsonschema.validate(data, schema); \
  print('✓ Valid Morphir IR v1')"

References


Appendix: Complete Schema Definition

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
# JSON Schema for Morphir IR Format Version 1
# This schema defines the structure of a Morphir IR distribution in version 1 format.
# Format version 1 uses lowercase tag names and different structure for modules.

$schema: "http://json-schema.org/draft-07/schema#"
$id: "https://finos.github.io/morphir/schemas/morphir-ir-v1.yaml"
title: "Morphir IR Distribution (Version 1)"
description: |
  A Morphir IR distribution represents a complete, self-contained package of business logic
  with all its dependencies. It captures the semantics of functional programs in a
  language-independent, platform-agnostic format.
  
  This is format version 1, which uses lowercase tags and a different module structure.

type: object
required:
  - formatVersion
  - distribution
properties:
  formatVersion:
    type: integer
    const: 1
    description: "The version of the IR format. Must be 1 for this schema."
  
  distribution:
    description: "The distribution data, currently only Library distributions are supported."
    type: array
    minItems: 4
    maxItems: 4
    items:
      - type: string
        const: "library"
        description: "Distribution type (lowercase in v1)."
      - $ref: "#/definitions/PackageName"
      - $ref: "#/definitions/Dependencies"
      - $ref: "#/definitions/PackageDefinition"

definitions:
  # ===== Basic Building Blocks =====
  
  Name:
    type: array
    items:
      type: string
      pattern: "^[a-z][a-z0-9]*$"
    minItems: 1
    description: |
      A Name is a list of lowercase words that represents a human-readable identifier.
      Example: ["value", "in", "u", "s", "d"] can be rendered as valueInUSD or value_in_USD.
  
  Path:
    type: array
    items:
      $ref: "#/definitions/Name"
    minItems: 1
    description: |
      A Path is a list of Names representing a hierarchical location in the IR structure.
      Used for package names and module names.
  
  PackageName:
    $ref: "#/definitions/Path"
    description: "Globally unique identifier for a package."
  
  ModuleName:
    $ref: "#/definitions/Path"
    description: "Unique identifier for a module within a package."
  
  FQName:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - $ref: "#/definitions/PackageName"
      - $ref: "#/definitions/ModuleName"
      - $ref: "#/definitions/Name"
    description: |
      Fully-Qualified Name that provides a globally unique identifier for any type or value.
      Consists of [packagePath, modulePath, localName].
  
  # ===== Attributes =====
  
  Attributes:
    type: object
    description: |
      Attributes can be attached to various nodes in the IR for extensibility.
      When no additional information is needed, an empty object {} is used.
  
  # ===== Access Control (V1 format) =====
  
  AccessLevel:
    type: string
    enum: ["public", "private"]
    description: "Controls visibility of types and values (lowercase in v1)."
  
  # Note: Documented is not a separate schema definition because it's encoded conditionally.
  # When documentation exists, the JSON has both "doc" and "value" fields.
  # When documentation is absent, the JSON contains only the documented element directly (no wrapper).
  # This is handled inline in the definitions that use Documented.
  
  # ===== Distribution Structure =====
  
  Dependencies:
    type: array
    items:
      type: array
      minItems: 2
      maxItems: 2
      items:
        - $ref: "#/definitions/PackageName"
        - $ref: "#/definitions/PackageSpecification"
    description: "Dictionary of package dependencies, contains only type signatures."
  
  PackageDefinition:
    type: object
    required: ["modules"]
    properties:
      modules:
        type: array
        items:
          $ref: "#/definitions/ModuleEntry"
        description: "All modules in the package (public and private)."
    description: "Complete implementation of a package with all details."
  
  ModuleEntry:
    type: object
    required: ["name", "def"]
    properties:
      name:
        $ref: "#/definitions/ModuleName"
        description: "The module name/path."
      def:
        type: array
        minItems: 2
        maxItems: 2
        items:
          - $ref: "#/definitions/AccessLevel"
          - $ref: "#/definitions/ModuleDefinition"
        description: "Access-controlled module definition [accessLevel, definition]."
    description: "Module entry with name and access-controlled definition (v1 format)."
  
  PackageSpecification:
    type: object
    required: ["modules"]
    properties:
      modules:
        type: array
        items:
          type: object
          required: ["name", "spec"]
          properties:
            name:
              $ref: "#/definitions/ModuleName"
              description: "The module name/path."
            spec:
              $ref: "#/definitions/ModuleSpecification"
              description: "The module specification."
        description: "Public modules only."
    description: "Public interface of a package, contains only type signatures."
  
  # ===== Module Structure =====
  
  ModuleDefinition:
    type: object
    required: ["types", "values"]
    properties:
      types:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - type: array
              minItems: 2
              maxItems: 2
              items:
                - $ref: "#/definitions/AccessLevel"
                - oneOf:
                    - type: object
                      required: ["doc", "value"]
                      properties:
                        doc:
                          type: string
                        value:
                          $ref: "#/definitions/TypeDefinition"
                    - $ref: "#/definitions/TypeDefinition"
        description: "All type definitions (public and private)."
      values:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - type: array
              minItems: 2
              maxItems: 2
              items:
                - $ref: "#/definitions/AccessLevel"
                - oneOf:
                    - type: object
                      required: ["doc", "value"]
                      properties:
                        doc:
                          type: string
                        value:
                          $ref: "#/definitions/ValueDefinition"
                    - $ref: "#/definitions/ValueDefinition"
        description: "All value definitions (public and private)."
      doc:
        type: string
        description: "Optional documentation for the module."
    description: "Complete implementation of a module."
  
  ModuleSpecification:
    type: object
    required: ["types", "values"]
    properties:
      types:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - oneOf:
                - type: object
                  required: ["doc", "value"]
                  properties:
                    doc:
                      type: string
                    value:
                      $ref: "#/definitions/TypeSpecification"
                - $ref: "#/definitions/TypeSpecification"
        description: "Public type specifications only."
      values:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - oneOf:
                - type: object
                  required: ["doc", "value"]
                  properties:
                    doc:
                      type: string
                    value:
                      $ref: "#/definitions/ValueSpecification"
                - $ref: "#/definitions/ValueSpecification"
        description: "Public value specifications only."
      doc:
        type: string
        description: "Optional documentation for the module."
    description: "Public interface of a module."
  
  # ===== Type System =====
  # All type tags are lowercase in v1
  
  Type:
    description: |
      A Type is a recursive tree structure representing type expressions.
      Tags are lowercase in format version 1.
    oneOf:
      - $ref: "#/definitions/VariableType"
      - $ref: "#/definitions/ReferenceType"
      - $ref: "#/definitions/TupleType"
      - $ref: "#/definitions/RecordType"
      - $ref: "#/definitions/ExtensibleRecordType"
      - $ref: "#/definitions/FunctionType"
      - $ref: "#/definitions/UnitType"
  
  VariableType:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "variable"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
    description: "Represents a type variable (generic parameter)."
  
  ReferenceType:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "reference"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
      - type: array
        items:
          $ref: "#/definitions/Type"
        description: "Type arguments for generic types."
    description: "Reference to another type or type alias."
  
  TupleType:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "tuple"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Type"
        description: "Element types in order."
    description: "A composition of multiple types in a fixed order (product type)."
  
  RecordType:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "record"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Field"
        description: "List of field definitions."
    description: "A composition of named fields with their types."
  
  ExtensibleRecordType:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "extensible_record"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
      - type: array
        items:
          $ref: "#/definitions/Field"
        description: "Known fields."
    description: "A record type that can be extended with additional fields."
  
  FunctionType:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "function"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Type"
      - $ref: "#/definitions/Type"
    description: |
      Represents a function type. Multi-argument functions are represented via currying.
      Items: [tag, attributes, argumentType, returnType]
  
  UnitType:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "unit"
      - $ref: "#/definitions/Attributes"
    description: "The type with exactly one value (similar to void in some languages)."
  
  Field:
    type: object
    required: ["name", "tpe"]
    properties:
      name:
        $ref: "#/definitions/Name"
        description: "Field name."
      tpe:
        $ref: "#/definitions/Type"
        description: "Field type."
    description: "A field in a record type."
  
  # ===== Type Specifications =====
  # All type specification tags are lowercase with underscores in v1
  
  TypeSpecification:
    description: "Defines the interface of a type without implementation details."
    oneOf:
      - $ref: "#/definitions/TypeAliasSpecification"
      - $ref: "#/definitions/OpaqueTypeSpecification"
      - $ref: "#/definitions/CustomTypeSpecification"
      - $ref: "#/definitions/DerivedTypeSpecification"
  
  TypeAliasSpecification:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "type_alias_specification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - $ref: "#/definitions/Type"
    description: "An alias for another type."
  
  OpaqueTypeSpecification:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "opaque_type_specification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
    description: |
      A type with unknown structure. The implementation is hidden from consumers.
  
  CustomTypeSpecification:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "custom_type_specification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - $ref: "#/definitions/Constructors"
    description: "A tagged union type (sum type)."
  
  DerivedTypeSpecification:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "derived_type_specification"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - type: object
        required: ["baseType", "fromBaseType", "toBaseType"]
        properties:
          baseType:
            $ref: "#/definitions/Type"
            description: "The type used for serialization."
          fromBaseType:
            $ref: "#/definitions/FQName"
            description: "Function to convert from base type."
          toBaseType:
            $ref: "#/definitions/FQName"
            description: "Function to convert to base type."
        description: "Details for derived type."
    description: |
      A type with platform-specific representation but known serialization.
  
  # ===== Type Definitions =====
  # All type definition tags are lowercase with underscores in v1
  
  TypeDefinition:
    description: "Provides the complete implementation of a type."
    oneOf:
      - $ref: "#/definitions/TypeAliasDefinition"
      - $ref: "#/definitions/CustomTypeDefinition"
  
  TypeAliasDefinition:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "type_alias_definition"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - $ref: "#/definitions/Type"
    description: "Complete definition of a type alias."
  
  CustomTypeDefinition:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "custom_type_definition"
      - type: array
        items:
          $ref: "#/definitions/Name"
        description: "Type parameters."
      - type: array
        minItems: 2
        maxItems: 2
        items:
          - $ref: "#/definitions/AccessLevel"
          - $ref: "#/definitions/Constructors"
    description: |
      Complete definition of a custom type. If constructors are private, 
      the specification becomes opaque_type_specification.
  
  Constructors:
    type: array
    items:
      type: array
      minItems: 2
      maxItems: 2
      items:
        - $ref: "#/definitions/Name"
        - type: array
          items:
            type: array
            minItems: 2
            maxItems: 2
            items:
              - $ref: "#/definitions/Name"
              - $ref: "#/definitions/Type"
          description: "Constructor arguments as (name, type) pairs."
    description: "Dictionary of constructor names to their typed arguments."
  
  # ===== Value System =====
  # Value expressions use lowercase tags with underscores in v1
  
  Value:
    description: |
      A Value is a recursive tree structure representing computations.
      All data and logic in Morphir are represented as value expressions.
      Note: Value tags are lowercase with underscores in format version 1.
    oneOf:
      - $ref: "#/definitions/LiteralValue"
      - $ref: "#/definitions/ConstructorValue"
      - $ref: "#/definitions/TupleValue"
      - $ref: "#/definitions/ListValue"
      - $ref: "#/definitions/RecordValue"
      - $ref: "#/definitions/VariableValue"
      - $ref: "#/definitions/ReferenceValue"
      - $ref: "#/definitions/FieldValue"
      - $ref: "#/definitions/FieldFunctionValue"
      - $ref: "#/definitions/ApplyValue"
      - $ref: "#/definitions/LambdaValue"
      - $ref: "#/definitions/LetDefinitionValue"
      - $ref: "#/definitions/LetRecursionValue"
      - $ref: "#/definitions/DestructureValue"
      - $ref: "#/definitions/IfThenElseValue"
      - $ref: "#/definitions/PatternMatchValue"
      - $ref: "#/definitions/UpdateRecordValue"
      - $ref: "#/definitions/UnitValue"
  
  LiteralValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "literal"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Literal"
    description: "A literal constant value."
  
  ConstructorValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "constructor"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
    description: "Reference to a custom type constructor."
  
  TupleValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "tuple"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Value"
        description: "Element values in order."
    description: "A tuple value with multiple elements."
  
  ListValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "list"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Value"
        description: "List elements."
    description: "A list of values."
  
  RecordValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "record"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Value"
        description: "Dictionary mapping field names to values."
    description: "A record value with named fields."
  
  VariableValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "variable"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
    description: "Reference to a variable in scope."
  
  ReferenceValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "reference"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
    description: "Reference to a defined value (function or constant)."
  
  FieldValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "field"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Name"
    description: "Field access on a record. Items: [tag, attributes, recordExpr, fieldName]"
  
  FieldFunctionValue:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "field_function"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
    description: "A function that extracts a field (e.g., .firstName)."
  
  ApplyValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "apply"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
    description: |
      Function application. Items: [tag, attributes, function, argument].
      Multi-argument calls are represented via currying (nested Apply nodes).
  
  LambdaValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "lambda"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Value"
    description: |
      Anonymous function (lambda abstraction).
      Items: [tag, attributes, argumentPattern, body]
  
  LetDefinitionValue:
    type: array
    minItems: 5
    maxItems: 5
    items:
      - const: "let_definition"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Name"
      - $ref: "#/definitions/ValueDefinition"
      - $ref: "#/definitions/Value"
    description: |
      A let binding introducing a single value.
      Items: [tag, attributes, bindingName, definition, inExpr]
  
  LetRecursionValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "let_recursion"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/ValueDefinition"
        description: "Multiple bindings that can reference each other."
      - $ref: "#/definitions/Value"
    description: "Mutually recursive let bindings."
  
  DestructureValue:
    type: array
    minItems: 5
    maxItems: 5
    items:
      - const: "destructure"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
    description: |
      Pattern-based destructuring.
      Items: [tag, attributes, pattern, valueToDestructure, inExpr]
  
  IfThenElseValue:
    type: array
    minItems: 5
    maxItems: 5
    items:
      - const: "if_then_else"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
      - $ref: "#/definitions/Value"
    description: |
      Conditional expression.
      Items: [tag, attributes, condition, thenBranch, elseBranch]
  
  PatternMatchValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "pattern_match"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Pattern"
            - $ref: "#/definitions/Value"
        description: "List of pattern-branch pairs."
    description: "Pattern matching with multiple cases."
  
  UpdateRecordValue:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "update_record"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Value"
      - type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Value"
        description: "Fields to update with new values."
    description: |
      Record update expression (immutable copy-on-update).
      Items: [tag, attributes, recordToUpdate, fieldsToUpdate]
  
  UnitValue:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "unit"
      - $ref: "#/definitions/Attributes"
    description: "The unit value (the single value of the Unit type)."
  
  # ===== Literals =====
  # All literal tags are lowercase with underscores in v1
  
  Literal:
    description: "Represents literal constant values."
    oneOf:
      - $ref: "#/definitions/BoolLiteral"
      - $ref: "#/definitions/CharLiteral"
      - $ref: "#/definitions/StringLiteral"
      - $ref: "#/definitions/WholeNumberLiteral"
      - $ref: "#/definitions/FloatLiteral"
      - $ref: "#/definitions/DecimalLiteral"
  
  BoolLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "bool_literal"
      - type: boolean
    description: "Boolean literal (true or false)."
  
  CharLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "char_literal"
      - type: string
        minLength: 1
        maxLength: 1
    description: "Single character literal."
  
  StringLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "string_literal"
      - type: string
    description: "Text string literal."
  
  WholeNumberLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "whole_number_literal"
      - type: integer
    description: "Integer literal."
  
  FloatLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "float_literal"
      - type: number
    description: "Floating-point number literal."
  
  DecimalLiteral:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "decimal_literal"
      - type: string
        pattern: "^-?[0-9]+(\\.[0-9]+)?$"
    description: "Arbitrary-precision decimal literal (stored as string)."
  
  # ===== Patterns =====
  # All pattern tags are lowercase with underscores in v1
  
  Pattern:
    description: |
      Patterns are used for destructuring and filtering values.
      They appear in lambda, let destructure, and pattern match expressions.
      Pattern tags are lowercase with underscores in format version 1.
    oneOf:
      - $ref: "#/definitions/WildcardPattern"
      - $ref: "#/definitions/AsPattern"
      - $ref: "#/definitions/TuplePattern"
      - $ref: "#/definitions/ConstructorPattern"
      - $ref: "#/definitions/EmptyListPattern"
      - $ref: "#/definitions/HeadTailPattern"
      - $ref: "#/definitions/LiteralPattern"
      - $ref: "#/definitions/UnitPattern"
  
  WildcardPattern:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "wildcard_pattern"
      - $ref: "#/definitions/Attributes"
    description: "Matches any value without binding (the _ pattern)."
  
  AsPattern:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "as_pattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Name"
    description: |
      Binds a name to a value matched by a nested pattern.
      Simple variable binding is AsPattern with WildcardPattern nested.
      Items: [tag, attributes, nestedPattern, variableName]
  
  TuplePattern:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "tuple_pattern"
      - $ref: "#/definitions/Attributes"
      - type: array
        items:
          $ref: "#/definitions/Pattern"
        description: "Patterns for each tuple element."
    description: "Matches a tuple by matching each element."
  
  ConstructorPattern:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "constructor_pattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/FQName"
      - type: array
        items:
          $ref: "#/definitions/Pattern"
        description: "Patterns for constructor arguments."
    description: "Matches a specific type constructor and its arguments."
  
  EmptyListPattern:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "empty_list_pattern"
      - $ref: "#/definitions/Attributes"
    description: "Matches an empty list (the [] pattern)."
  
  HeadTailPattern:
    type: array
    minItems: 4
    maxItems: 4
    items:
      - const: "head_tail_pattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Pattern"
      - $ref: "#/definitions/Pattern"
    description: |
      Matches a non-empty list by head and tail (the x :: xs pattern).
      Items: [tag, attributes, headPattern, tailPattern]
  
  LiteralPattern:
    type: array
    minItems: 3
    maxItems: 3
    items:
      - const: "literal_pattern"
      - $ref: "#/definitions/Attributes"
      - $ref: "#/definitions/Literal"
    description: "Matches an exact literal value."
  
  UnitPattern:
    type: array
    minItems: 2
    maxItems: 2
    items:
      - const: "unit_pattern"
      - $ref: "#/definitions/Attributes"
    description: "Matches the unit value."
  
  # ===== Value Specifications and Definitions =====
  
  ValueSpecification:
    type: object
    required: ["inputs", "output"]
    properties:
      inputs:
        type: array
        items:
          type: array
          minItems: 2
          maxItems: 2
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Type"
        description: "Function parameters as (name, type) pairs."
      output:
        $ref: "#/definitions/Type"
        description: "The return type."
    description: |
      The type signature of a value or function.
      Contains only type information, no implementation.
  
  ValueDefinition:
    type: object
    required: ["inputTypes", "outputType", "body"]
    properties:
      inputTypes:
        type: array
        items:
          type: array
          minItems: 3
          maxItems: 3
          items:
            - $ref: "#/definitions/Name"
            - $ref: "#/definitions/Attributes"
            - $ref: "#/definitions/Type"
        description: "Function parameters as (name, attributes, type) tuples."
      outputType:
        $ref: "#/definitions/Type"
        description: "The return type."
      body:
        $ref: "#/definitions/Value"
        description: "The value expression implementing the logic."
    description: |
      The complete implementation of a value or function.
      Contains both type information and implementation.