Tutorial 1.1: Defining Types and Functions in a Domain-Specific Language#
[1]:
from tabulate import tabulate
[2]:
from concepts.dsl.dsl_types import ValueType, ConstantType, BOOL, FLOAT32, VectorValueType
[3]:
t_item = ValueType('item')
t_item_set = ValueType('item_set')
t_concept_name = ConstantType('concept_name')
t_shape = ValueType('shape')
t_color = ValueType('color')
t_size = VectorValueType(FLOAT32, 3, alias='size')
print(tabulate([
(type(x), x.typename, x.alias)
for x in [t_item, t_item_set, t_concept_name, t_shape, t_color, t_size]
], headers=['type', 'typename', 'alias']))
type typename alias
------------------------------------------------ -------------------------------------------- -------
<class 'concepts.dsl.dsl_types.ValueType'> item
<class 'concepts.dsl.dsl_types.ValueType'> item_set
<class 'concepts.dsl.dsl_types.ConstantType'> concept_name
<class 'concepts.dsl.dsl_types.ValueType'> shape
<class 'concepts.dsl.dsl_types.ValueType'> color
<class 'concepts.dsl.dsl_types.VectorValueType'> vector[float32, dim=3, choices=0, factors=1] size
[4]:
from concepts.dsl.dsl_functions import FunctionType, Function, FunctionTyping
[5]:
ft_color_of = FunctionTyping[t_color](t_item)
print(repr(ft_color_of))
print(repr(ft_color_of.argument_types[0]))
print(repr(ft_color_of.argument_names[0]))
print(ft_color_of.arguments)
print(ft_color_of.arguments_dict)
print(repr(ft_color_of.return_type))
FunctionType<(#0: item) -> color>
ValueType<item>
'#0'
(Variable<#0: item>,)
{'#0': ValueType<item>}
ValueType<color>
[6]:
f_color_of = Function('color_of', ft_color_of)
print(f_color_of)
print(f_color_of.name)
print(f_color_of.ftype)
color_of(#0: item) -> color
color_of
(#0: item) -> color
[7]:
from concepts.dsl.function_domain import FunctionDomain
[8]:
domain = FunctionDomain()
domain.define_type(t_item)
domain.define_type(t_item_set)
domain.define_type(t_concept_name)
domain.define_type(t_color)
domain.define_type(t_shape)
domain.define_type(t_size)
domain.define_function(Function('scene', FunctionTyping[t_item_set]()))
domain.define_function(Function('filter_color', FunctionTyping[t_item_set](t_item_set, t_concept_name)))
domain.define_function(Function('filter_shape', FunctionTyping[t_item_set](t_item_set, t_concept_name)))
domain.define_function(Function('unique', FunctionTyping[t_item](t_item_set)))
domain.define_function(Function('color_of', FunctionTyping[t_color](t_item)))
domain.define_function(Function('shape_of', FunctionTyping[t_shape](t_item)))
domain.define_function(Function('size_of', FunctionTyping[t_size](t_item)))
domain.define_function(Function('same_color', FunctionTyping[BOOL](t_color, t_color)))
domain.define_function(Function('same_shape', FunctionTyping[BOOL](t_shape, t_shape)))
domain.define_function(Function('same_size', FunctionTyping[BOOL](t_size, t_size)))
[8]:
Function<same_size(#0: size, #1: size) -> bool>
[9]:
domain.print_summary()
TypeSystem: FunctionDomain
Types:
item
item_set
concept_name
color
shape
size
Constants:
Functions:
scene() -> item_set
filter_color(#0: item_set, #1: concept_name) -> item_set
filter_shape(#0: item_set, #1: concept_name) -> item_set
unique(#0: item_set) -> item
color_of(#0: item) -> color
shape_of(#0: item) -> shape
size_of(#0: item) -> size
same_color(#0: color, #1: color) -> bool
same_shape(#0: shape, #1: shape) -> bool
same_size(#0: size, #1: size) -> bool