File: template_commutative.f90

package info (click to toggle)
lfortran 0.60.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,416 kB
  • sloc: cpp: 173,406; f90: 80,491; python: 17,586; ansic: 9,610; yacc: 2,356; sh: 1,401; fortran: 895; makefile: 38; javascript: 15
file content (61 lines) | stat: -rw-r--r-- 2,092 bytes parent folder | download
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
module template_commutative_m
    implicit none
    public

    requirement magma_r(T, bin, equal)
        type, deferred :: T
        pure elemental function bin(x, y) result(bin)
            type(T), intent(in) :: x
            type(T), intent(in) :: y
            type(T) :: bin
        end function
        pure elemental function equal(x, y) result(equal)
            type(T), intent(in) :: x
            type(T), intent(in) :: y
            ! type(T) :: equal
            logical :: equal
        end function
    end requirement

    template commutative_prop(T, bin, equal)
        require :: magma_r(T, bin, equal)
      contains
        pure function commutative_p(x, y) result(prop)
            interface operator(==)
                procedure equal
            end interface
            type(T), intent(in) :: x, y
            type(logical) :: prop

            prop = bin(x,y) == bin(y,x)
        end function
    end template

    template alt_commutative_prop(bin, equal)
        require :: magma_r(integer, bin, equal)
      contains
        pure function commutative_p(x, y) result(prop)
            interface operator(==)
                procedure equal
            end interface
            type(integer), intent(in) :: x, y
            type(logical) :: prop

            prop = bin(x,y) == bin(y,x)
        end function
    end template
end module template_commutative_m


program test_template_commutative_p
  use template_commutative_m
  instantiate alt_commutative_prop(operator(+), operator(==)), only: plus_comm => commutative_p
  instantiate commutative_prop(integer, operator(+), operator(==)), only: int_plus_comm => commutative_p
  instantiate alt_commutative_prop(operator(-), operator(==)), only: minus_comm => commutative_p
  instantiate commutative_prop(integer, operator(-), operator(==)), only: int_minus_comm => commutative_p
  print *, "test commutative"
  print *, "plus_comm: ", plus_comm(3, 4)
  print *, "int_plus_comm: ", int_plus_comm(3, 4)
  print *, "minus_comm: ", minus_comm(3, 4)
  print *, "int_minus_comm: ", int_minus_comm(3, 4)
end program