Using polymorphic functions in definitions












1














Following my question here, I have several functions with different types of arguments which I defined the Inductive type formula on them. Is there anyway to use Inductive formula in compute_formula. I am doing this to make proving easier by decreasing the number of constructors that I have to handle in proofs. Thank you.



Fixpoint add (n:type1) (m:type2): type3 :=
match n with
(*body for add*)
end.

Fixpoint mul (n:type1) (m:type4): type5 :=
match n with
(*body for mul*)
end.

Inductive formula : Type :=
| Formula {A B}: type1-> A -> (type1->A->B) -> formula.

(* How should I write this *)
Definition compute_formula {A B} (f: formula) (extraArg:A) : B :=
match f with
|Formula {A B} part1 part2 part3=>
if (A isof type2 && B isof type3) then add part1 part2+extraArg
if (A isof type4 && B isof type5) then mul part1 part2+extraArg

end.









share|improve this question





























    1














    Following my question here, I have several functions with different types of arguments which I defined the Inductive type formula on them. Is there anyway to use Inductive formula in compute_formula. I am doing this to make proving easier by decreasing the number of constructors that I have to handle in proofs. Thank you.



    Fixpoint add (n:type1) (m:type2): type3 :=
    match n with
    (*body for add*)
    end.

    Fixpoint mul (n:type1) (m:type4): type5 :=
    match n with
    (*body for mul*)
    end.

    Inductive formula : Type :=
    | Formula {A B}: type1-> A -> (type1->A->B) -> formula.

    (* How should I write this *)
    Definition compute_formula {A B} (f: formula) (extraArg:A) : B :=
    match f with
    |Formula {A B} part1 part2 part3=>
    if (A isof type2 && B isof type3) then add part1 part2+extraArg
    if (A isof type4 && B isof type5) then mul part1 part2+extraArg

    end.









    share|improve this question



























      1












      1








      1







      Following my question here, I have several functions with different types of arguments which I defined the Inductive type formula on them. Is there anyway to use Inductive formula in compute_formula. I am doing this to make proving easier by decreasing the number of constructors that I have to handle in proofs. Thank you.



      Fixpoint add (n:type1) (m:type2): type3 :=
      match n with
      (*body for add*)
      end.

      Fixpoint mul (n:type1) (m:type4): type5 :=
      match n with
      (*body for mul*)
      end.

      Inductive formula : Type :=
      | Formula {A B}: type1-> A -> (type1->A->B) -> formula.

      (* How should I write this *)
      Definition compute_formula {A B} (f: formula) (extraArg:A) : B :=
      match f with
      |Formula {A B} part1 part2 part3=>
      if (A isof type2 && B isof type3) then add part1 part2+extraArg
      if (A isof type4 && B isof type5) then mul part1 part2+extraArg

      end.









      share|improve this question















      Following my question here, I have several functions with different types of arguments which I defined the Inductive type formula on them. Is there anyway to use Inductive formula in compute_formula. I am doing this to make proving easier by decreasing the number of constructors that I have to handle in proofs. Thank you.



      Fixpoint add (n:type1) (m:type2): type3 :=
      match n with
      (*body for add*)
      end.

      Fixpoint mul (n:type1) (m:type4): type5 :=
      match n with
      (*body for mul*)
      end.

      Inductive formula : Type :=
      | Formula {A B}: type1-> A -> (type1->A->B) -> formula.

      (* How should I write this *)
      Definition compute_formula {A B} (f: formula) (extraArg:A) : B :=
      match f with
      |Formula {A B} part1 part2 part3=>
      if (A isof type2 && B isof type3) then add part1 part2+extraArg
      if (A isof type4 && B isof type5) then mul part1 part2+extraArg

      end.






      coq






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 20:12

























      asked Nov 22 at 19:52









      Tom And.

      305




      305
























          1 Answer
          1






          active

          oldest

          votes


















          2














          What do you want the output type of compute_formula to be? The way the signature is written, the function would have to be able to compute an element of B no matter what B is. Since this is obviously impossible (what if B is Empty?), I'll show you a different approach.



          The idea is to use the formula to get the output type.



          Definition output_type (f: formula) :=
          match f with
          | @Formula _ B _ _ _ => B
          end.


          Then we can define compute_formula as



          Definition compute_formula (f: formula): output_type f :=
          match f with
          | @Formula _ _ t a func => func t a
          end.


          A few other things. I'm not sure what you mean with the extraArg part. If you're more specific about what that means I might be able to help you. Also, there isn't (at least outside of tactics) a way to do what you want with A isof type2.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437324%2fusing-polymorphic-functions-in-definitions%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            What do you want the output type of compute_formula to be? The way the signature is written, the function would have to be able to compute an element of B no matter what B is. Since this is obviously impossible (what if B is Empty?), I'll show you a different approach.



            The idea is to use the formula to get the output type.



            Definition output_type (f: formula) :=
            match f with
            | @Formula _ B _ _ _ => B
            end.


            Then we can define compute_formula as



            Definition compute_formula (f: formula): output_type f :=
            match f with
            | @Formula _ _ t a func => func t a
            end.


            A few other things. I'm not sure what you mean with the extraArg part. If you're more specific about what that means I might be able to help you. Also, there isn't (at least outside of tactics) a way to do what you want with A isof type2.






            share|improve this answer


























              2














              What do you want the output type of compute_formula to be? The way the signature is written, the function would have to be able to compute an element of B no matter what B is. Since this is obviously impossible (what if B is Empty?), I'll show you a different approach.



              The idea is to use the formula to get the output type.



              Definition output_type (f: formula) :=
              match f with
              | @Formula _ B _ _ _ => B
              end.


              Then we can define compute_formula as



              Definition compute_formula (f: formula): output_type f :=
              match f with
              | @Formula _ _ t a func => func t a
              end.


              A few other things. I'm not sure what you mean with the extraArg part. If you're more specific about what that means I might be able to help you. Also, there isn't (at least outside of tactics) a way to do what you want with A isof type2.






              share|improve this answer
























                2












                2








                2






                What do you want the output type of compute_formula to be? The way the signature is written, the function would have to be able to compute an element of B no matter what B is. Since this is obviously impossible (what if B is Empty?), I'll show you a different approach.



                The idea is to use the formula to get the output type.



                Definition output_type (f: formula) :=
                match f with
                | @Formula _ B _ _ _ => B
                end.


                Then we can define compute_formula as



                Definition compute_formula (f: formula): output_type f :=
                match f with
                | @Formula _ _ t a func => func t a
                end.


                A few other things. I'm not sure what you mean with the extraArg part. If you're more specific about what that means I might be able to help you. Also, there isn't (at least outside of tactics) a way to do what you want with A isof type2.






                share|improve this answer












                What do you want the output type of compute_formula to be? The way the signature is written, the function would have to be able to compute an element of B no matter what B is. Since this is obviously impossible (what if B is Empty?), I'll show you a different approach.



                The idea is to use the formula to get the output type.



                Definition output_type (f: formula) :=
                match f with
                | @Formula _ B _ _ _ => B
                end.


                Then we can define compute_formula as



                Definition compute_formula (f: formula): output_type f :=
                match f with
                | @Formula _ _ t a func => func t a
                end.


                A few other things. I'm not sure what you mean with the extraArg part. If you're more specific about what that means I might be able to help you. Also, there isn't (at least outside of tactics) a way to do what you want with A isof type2.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 at 23:42









                User

                32817




                32817






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437324%2fusing-polymorphic-functions-in-definitions%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Trompette piccolo

                    Slow SSRS Report in dynamic grouping and multiple parameters

                    Simon Yates (cyclisme)