Skip to content

Commit 0ab06df

Browse files
committed
Add generated Scala modules. We have Haskell/Scala parity w.r.t. code generation
1 parent 77a3f6c commit 0ab06df

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package hydra.adapters.utils
2+
3+
import hydra.basics.*
4+
5+
import hydra.core.*
6+
7+
import hydra.lib.literals
8+
9+
import hydra.lib.strings
10+
11+
def describeFloatType(t: FloatType): String = strings.cat(Seq(describePrecision(floatTypePrecision(t)), " floating-point numbers"))
12+
13+
def describeIntegerType(t: IntegerType): String = strings.cat(Seq(describePrecision(integerTypePrecision(t)), " integers"))
14+
15+
def describeLiteralType(v: LiteralType): String = v match
16+
case LiteralType.binary() => "binary strings"
17+
case LiteralType.boolean() => "boolean values"
18+
case LiteralType.float(y) => describeFloatType(y)
19+
case LiteralType.integer(y) => describeIntegerType(y)
20+
case LiteralType.string() => "character strings"
21+
22+
def describePrecision(v: Precision): String = v match
23+
case Precision.arbitrary() => "arbitrary-precision"
24+
case Precision.bits(y) => strings.cat(Seq(literals.showInt32(y), "-bit"))
25+
26+
def describeType(v: Type): String = v match
27+
case Type.literal(y) => describeLiteralType(y)
28+
case Type.element(y) => strings.cat(Seq("elements containing ", describeType(y)))
29+
case Type.function(y) => strings.cat(Seq(strings.cat(Seq(strings.cat(Seq("functions from ", describeType(y.domain))), " to ")), describeType(y.codomain)))
30+
case Type.list(y) => strings.cat(Seq("lists of ", describeType(y)))
31+
case Type.map(y) => strings.cat(Seq(strings.cat(Seq(strings.cat(Seq("maps from ", describeType(y.keys))), " to ")), describeType(y.values)))
32+
case Type.nominal(y) => strings.cat(Seq("alias for", y))
33+
case Type.optional(y) => strings.cat(Seq("optional ", describeType(y)))
34+
case Type.record(y) => "records of a particular set of fields"
35+
case Type.set(y) => strings.cat(Seq("sets of ", describeType(y)))
36+
case Type.union(y) => "unions of a particular set of fields"
37+
case Type.universal(y) => "polymorphic terms"
38+
case Type.variable(y) => "unspecified/parametric terms"
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package hydra.basics
2+
3+
import hydra.core.*
4+
5+
import hydra.lib.lists
6+
7+
import hydra.lib.strings
8+
9+
def floatTypePrecision(v: FloatType): Precision = v match
10+
case FloatType.bigfloat() => Precision.arbitrary()
11+
case FloatType.float32() => Precision.bits(32)
12+
case FloatType.float64() => Precision.bits(64)
13+
14+
val floatTypes = Seq(FloatType.bigfloat(), FloatType.float32(), FloatType.float64())
15+
16+
def floatValueType(v: FloatValue): FloatType = v match
17+
case FloatValue.bigfloat(y) => FloatType.bigfloat()
18+
case FloatValue.float32(y) => FloatType.float32()
19+
case FloatValue.float64(y) => FloatType.float64()
20+
21+
def functionVariant[a](v: hydra.core.Function[a]): FunctionVariant = v match
22+
case hydra.core.Function.cases(y) => FunctionVariant.cases()
23+
case hydra.core.Function.compareTo(y) => FunctionVariant.compareTo()
24+
case hydra.core.Function.data() => FunctionVariant.data()
25+
case hydra.core.Function.lambda(y) => FunctionVariant.lambda()
26+
case hydra.core.Function.optionalCases(y) => FunctionVariant.optionalCases()
27+
case hydra.core.Function.primitive(y) => FunctionVariant.primitive()
28+
case hydra.core.Function.projection(y) => FunctionVariant.projection()
29+
30+
val functionVariants = Seq(FunctionVariant.cases(), FunctionVariant.compareTo(), FunctionVariant.data(), FunctionVariant.lambda(), FunctionVariant.optionalCases(), FunctionVariant.primitive(), FunctionVariant.projection())
31+
32+
def integerTypeIsSigned(v: IntegerType): Boolean = v match
33+
case IntegerType.bigint() => true
34+
case IntegerType.int8() => true
35+
case IntegerType.int16() => true
36+
case IntegerType.int32() => true
37+
case IntegerType.int64() => true
38+
case IntegerType.uint8() => false
39+
case IntegerType.uint16() => false
40+
case IntegerType.uint32() => false
41+
case IntegerType.uint64() => false
42+
43+
def integerTypePrecision(v: IntegerType): Precision = v match
44+
case IntegerType.bigint() => Precision.arbitrary()
45+
case IntegerType.int8() => Precision.bits(8)
46+
case IntegerType.int16() => Precision.bits(16)
47+
case IntegerType.int32() => Precision.bits(32)
48+
case IntegerType.int64() => Precision.bits(64)
49+
case IntegerType.uint8() => Precision.bits(8)
50+
case IntegerType.uint16() => Precision.bits(16)
51+
case IntegerType.uint32() => Precision.bits(32)
52+
case IntegerType.uint64() => Precision.bits(64)
53+
54+
val integerTypes = Seq(IntegerType.bigint(), IntegerType.int8(), IntegerType.int16(), IntegerType.int32(), IntegerType.int64(), IntegerType.uint8(), IntegerType.uint16(), IntegerType.uint32(), IntegerType.uint64())
55+
56+
def integerValueType(v: IntegerValue): IntegerType = v match
57+
case IntegerValue.bigint(y) => IntegerType.bigint()
58+
case IntegerValue.int8(y) => IntegerType.int8()
59+
case IntegerValue.int16(y) => IntegerType.int16()
60+
case IntegerValue.int32(y) => IntegerType.int32()
61+
case IntegerValue.int64(y) => IntegerType.int64()
62+
case IntegerValue.uint8(y) => IntegerType.uint8()
63+
case IntegerValue.uint16(y) => IntegerType.uint16()
64+
case IntegerValue.uint32(y) => IntegerType.uint32()
65+
case IntegerValue.uint64(y) => IntegerType.uint64()
66+
67+
def literalType(v: Literal): LiteralType = v match
68+
case Literal.binary(y) => LiteralType.binary()
69+
case Literal.boolean(y) => LiteralType.boolean()
70+
case Literal.float(y) => LiteralType.float(floatValueType(y))
71+
case Literal.integer(y) => LiteralType.integer(integerValueType(y))
72+
case Literal.string(y) => LiteralType.string()
73+
74+
def literalTypeVariant(v: LiteralType): LiteralVariant = v match
75+
case LiteralType.binary() => LiteralVariant.binary()
76+
case LiteralType.boolean() => LiteralVariant.boolean()
77+
case LiteralType.float(y) => LiteralVariant.float()
78+
case LiteralType.integer(y) => LiteralVariant.integer()
79+
case LiteralType.string() => LiteralVariant.string()
80+
81+
def literalVariant(x: Literal): LiteralVariant = literalTypeVariant(literalType(x))
82+
83+
val literalVariants = Seq(LiteralVariant.binary(), LiteralVariant.boolean(), LiteralVariant.float(), LiteralVariant.integer(), LiteralVariant.string())
84+
85+
def qname(ns: String): (String => Name) = (name: String) => strings.cat(Seq(ns, ".", name))
86+
87+
def termVariant[a](term: Term[a]): TermVariant = term.data match
88+
case Expression.application(y) => TermVariant.application()
89+
case Expression.element(y) => TermVariant.element()
90+
case Expression.function(y) => TermVariant.function()
91+
case Expression.list(y) => TermVariant.list()
92+
case Expression.literal(y) => TermVariant.literal()
93+
case Expression.map(y) => TermVariant.map()
94+
case Expression.nominal(y) => TermVariant.nominal()
95+
case Expression.optional(y) => TermVariant.optional()
96+
case Expression.record(y) => TermVariant.record()
97+
case Expression.set(y) => TermVariant.set()
98+
case Expression.typeAbstraction(y) => TermVariant.typeAbstraction()
99+
case Expression.typeApplication(y) => TermVariant.typeApplication()
100+
case Expression.union(y) => TermVariant.union()
101+
case Expression.variable(y) => TermVariant.variable()
102+
103+
val termVariants = Seq(TermVariant.application(), TermVariant.literal(), TermVariant.element(), TermVariant.function(), TermVariant.list(), TermVariant.map(), TermVariant.nominal(), TermVariant.optional(), TermVariant.record(), TermVariant.set(), TermVariant.union(), TermVariant.variable())
104+
105+
def testLists[a](els: Seq[Seq[a]]): Int = lists.length(lists.concat(els))
106+
107+
def typeVariant(v: Type): TypeVariant = v match
108+
case Type.element(y) => TypeVariant.element()
109+
case Type.function(y) => TypeVariant.function()
110+
case Type.list(y) => TypeVariant.list()
111+
case Type.literal(y) => TypeVariant.literal()
112+
case Type.map(y) => TypeVariant.map()
113+
case Type.nominal(y) => TypeVariant.nominal()
114+
case Type.optional(y) => TypeVariant.optional()
115+
case Type.record(y) => TypeVariant.record()
116+
case Type.set(y) => TypeVariant.set()
117+
case Type.union(y) => TypeVariant.union()
118+
case Type.universal(y) => TypeVariant.universal()
119+
case Type.variable(y) => TypeVariant.variable()
120+
121+
val typeVariants = Seq(TypeVariant.literal(), TypeVariant.element(), TypeVariant.function(), TypeVariant.list(), TypeVariant.map(), TypeVariant.nominal(), TypeVariant.optional(), TypeVariant.record(), TypeVariant.set(), TypeVariant.union(), TypeVariant.universal(), TypeVariant.variable())

0 commit comments

Comments
 (0)