Skip to content

Commit efdc449

Browse files
committed
Initial example for inductive natural numbers
1 parent c6964f9 commit efdc449

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

code/test/unit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ example04
2828
example04-smart-ptr
2929
example04-smart-ptr-bind-ptr
3030
example05
31+
example06-smart-ptr
3132
exp
3233
exp2
3334
expr
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//
2+
// Mach7: Pattern Matching Library for C++
3+
//
4+
// Copyright 2011-2013, Texas A&M University.
5+
// Copyright 2014-2015, Yuriy Solodkyy.
6+
// All rights reserved.
7+
//
8+
// Redistribution and use in source and binary forms, with or without
9+
// modification, are permitted provided that the following conditions are met:
10+
//
11+
// * Redistributions of source code must retain the above copyright
12+
// notice, this list of conditions and the following disclaimer.
13+
//
14+
// * Redistributions in binary form must reproduce the above copyright
15+
// notice, this list of conditions and the following disclaimer in the
16+
// documentation and/or other materials provided with the distribution.
17+
//
18+
// * Neither the names of Mach7 project nor the names of its contributors
19+
// may be used to endorse or promote products derived from this software
20+
// without specific prior written permission.
21+
//
22+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25+
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY
26+
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29+
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
33+
///
34+
/// \file
35+
///
36+
/// This file is a part of Mach7 library test suite.
37+
///
38+
/// \author Yuriy Solodkyy <yuriy.solodkyy@gmail.com>
39+
///
40+
/// \see https://parasol.tamu.edu/mach7/
41+
/// \see https://github.com/solodon4/Mach7
42+
/// \see https://github.com/solodon4/SELL
43+
///
44+
45+
#include <iostream>
46+
#include <mach7/type_switchN-patterns.hpp> // Support for N-ary Match statement on patterns
47+
#include <mach7/patterns/address.hpp> // Address and dereference combinators
48+
#include <mach7/patterns/bindings.hpp> // Mach7 support for bindings on arbitrary UDT
49+
#include <mach7/patterns/constructor.hpp> // Support for constructor patterns
50+
#include <mach7/patterns/primitive.hpp> // Wildcard, variable and value patterns
51+
#include <xtl/adapters/std/memory.hpp> // XTL subtyping adapters for standard smart pointers
52+
53+
using namespace mch;
54+
55+
//------------------------------------------------------------------------------
56+
57+
struct Nat
58+
{
59+
virtual ~Nat() {}
60+
};
61+
62+
typedef std::unique_ptr<Nat> NatPtr;
63+
64+
struct O : Nat
65+
{
66+
};
67+
68+
struct S : Nat
69+
{
70+
S(Nat* n) : e(n) {}
71+
NatPtr e;
72+
};
73+
74+
//------------------------------------------------------------------------------
75+
76+
namespace mch ///< Mach7 library namespace
77+
{
78+
template <> struct bindings<O> { };
79+
template <> struct bindings<S> { Members(S::e); };
80+
} // of namespace mch
81+
82+
//------------------------------------------------------------------------------
83+
84+
int evl(const Nat& n)
85+
{
86+
var<const Nat&> e;
87+
88+
Match(n)
89+
{
90+
Case(C<O>() ) return 0;
91+
Case(C<S>(&e)) return evl(e) + 1;
92+
}
93+
EndMatch
94+
95+
XTL_UNREACHABLE; // To avoid warning that control may reach end of a non-void function
96+
}
97+
98+
//------------------------------------------------------------------------------
99+
100+
int main()
101+
{
102+
NatPtr a(new S(new S(new O)));
103+
NatPtr b(new S(new O));
104+
105+
std::cout << evl(*a) << std::endl;
106+
std::cout << evl(*b) << std::endl;
107+
}
108+
109+
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)