souffle  2.0.2-371-g6315b36
AlgebraicDataType.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020 The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file AlgebraicDataType.h
12  *
13  * Defines a node corresponding to an ast declaration of Algebraic Data Type
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/BranchDeclaration.h"
20 #include "ast/Node.h"
21 #include "ast/QualifiedName.h"
22 #include "ast/Type.h"
23 #include "parser/SrcLocation.h"
27 #include <cassert>
28 #include <iosfwd>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace souffle::ast {
35 
36 /**
37  * @class AlgebraicDataType
38  * @brief Combination of types using sums and products.
39  *
40  * ADT combines a simpler types using product types and sum types.
41  *
42  * Example:
43  * .type Nat = S {n : Nat} | Zero {}
44  *
45  * The type Nat has two branches, S which takes element of type Nat and Zero which doesn't take any
46  * arguments.
47  *
48  */
49 class AlgebraicDataType : public Type {
50 public:
51  AlgebraicDataType(QualifiedName name, VecOwn<BranchDeclaration> branches, SrcLocation loc = {})
52  : Type(std::move(name), std::move(loc)), branches(std::move(branches)) {
53  assert(!this->branches.empty());
54  };
55 
56  std::vector<BranchDeclaration*> getBranches() const {
58  }
59 
60  void print(std::ostream& os) const override {
61  os << tfm::format(".type %s = %s", getQualifiedName(), join(branches, " | "));
62  }
63 
64  AlgebraicDataType* clone() const override {
65  return new AlgebraicDataType(getQualifiedName(), souffle::clone(branches), getSrcLoc());
66  }
67 
68 protected:
69  bool equal(const Node& node) const override {
70  const auto& other = dynamic_cast<const AlgebraicDataType&>(node);
71  return getQualifiedName() == other.getQualifiedName() && branches == other.branches;
72  }
73 
74 private:
75  /** The list of branches for this sum type. */
77 };
78 
79 } // namespace souffle::ast
tinyformat::format
void format(std::ostream &out, const char *fmt)
Definition: tinyformat.h:1089
SrcLocation.h
souffle::ast::AlgebraicDataType::equal
bool equal(const Node &node) const override
Definition: AlgebraicDataType.h:75
souffle::ast::AlgebraicDataType::print
void print(std::ostream &os) const override
Definition: AlgebraicDataType.h:66
tinyformat.h
souffle::ast::AlgebraicDataType::branches
VecOwn< BranchDeclaration > branches
The list of branches for this sum type.
Definition: AlgebraicDataType.h:82
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
ContainerUtil.h
souffle::join
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
Definition: StreamUtil.h:175
souffle::ast::AlgebraicDataType::AlgebraicDataType
AlgebraicDataType(QualifiedName name, VecOwn< BranchDeclaration > branches, SrcLocation loc={})
Definition: AlgebraicDataType.h:57
Type.h
souffle::ast::AlgebraicDataType::clone
AlgebraicDataType * clone() const override
Definition: AlgebraicDataType.h:70
Node.h
souffle::ast::AlgebraicDataType::getBranches
std::vector< BranchDeclaration * > getBranches() const
Definition: AlgebraicDataType.h:62
souffle::ast::analysis::Type::name
QualifiedName name
Definition: TypeSystem.h:92
souffle::ast::analysis::Type::Type
Type(const Type &other)=delete
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
QualifiedName.h
StreamUtil.h
souffle::ast
Definition: Aggregator.h:35
souffle::toPtrVector
std::vector< T * > toPtrVector(const std::vector< std::unique_ptr< T >> &v)
A utility function enabling the creation of a vector of pointers.
Definition: ContainerUtil.h:146
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
souffle::ast::AlgebraicDataType
Combination of types using sums and products.
Definition: AlgebraicDataType.h:55
BranchDeclaration.h